Skip to content

Commit

Permalink
Refine UI
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Wang <[email protected]>
  • Loading branch information
ruibaby committed Jun 18, 2024
1 parent 8a53b85 commit 2bcf0e2
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 28 deletions.
41 changes: 39 additions & 2 deletions packages/comment-widget/src/base-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import { createRef, Ref, ref } from 'lit/directives/ref.js';
import {
allowAnonymousCommentsContext,
baseUrlContext,
captchaEnabledContext,
currentUserContext,
groupContext,
kindContext,
nameContext,
toastContext,
} from './context';
import './emoji-button';
import './icons/icon-loading';
import { ToastManager } from './lit-toast';
import baseStyles from './styles/base';
import varStyles from './styles/var';

Expand All @@ -29,6 +32,10 @@ export class BaseForm extends LitElement {
@state()
allowAnonymousComments = false;

@consume({ context: captchaEnabledContext, subscribe: true })
@state()
captchaEnabled = false;

@consume({ context: groupContext })
@state()
group = '';
Expand All @@ -42,11 +49,16 @@ export class BaseForm extends LitElement {
name = '';

@property({ type: String })
@state()
captcha = '';

@property({ type: Boolean })
submitting = false;

@consume({ context: toastContext, subscribe: true })
@state()
toastManager: ToastManager | undefined;

textareaRef: Ref<HTMLTextAreaElement> = createRef<HTMLTextAreaElement>();

get customAccount() {
Expand All @@ -61,6 +73,25 @@ export class BaseForm extends LitElement {
return `/console/login?redirect_uri=${encodeURIComponent(window.location.href + parentDomId)}`;
}

get showCaptcha() {
return this.captchaEnabled && !this.currentUser;
}

async handleFetchCaptcha() {
if (!this.showCaptcha) {
return;
}

const response = await fetch(`/apis/api.commentwidget.halo.run/v1alpha1/captcha/-/generate`);

if (!response.ok) {
this.toastManager?.error('获取验证码失败');
return;
}

this.captcha = await response.text();
}

handleOpenLoginPage() {
window.location.href = this.loginUrl;
}
Expand Down Expand Up @@ -127,6 +158,7 @@ export class BaseForm extends LitElement {
override connectedCallback(): void {
super.connectedCallback();
this.addEventListener('keydown', this.onKeydown);
this.handleFetchCaptcha();
}

override disconnectedCallback(): void {
Expand Down Expand Up @@ -185,11 +217,16 @@ export class BaseForm extends LitElement {
</button> `
: ''}
<div class="form__actions">
${this.captcha
${this.showCaptcha
? html`
<div class="form__action--captcha">
<input name="captchaCode" type="text" placeholder="请输入验证码" />
<img src="${this.captcha}" alt="captcha" width="100%" />
<img
@click=${this.handleFetchCaptcha}
src="${this.captcha}"
alt="captcha"
width="100%"
/>
</div>
`
: ''}
Expand Down
2 changes: 1 addition & 1 deletion packages/comment-widget/src/comment-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class CommentForm extends LitElement {
return;
}

this.captcha = '';
this.baseFormRef.value?.handleFetchCaptcha();

if (!response.ok) {
throw new Error('评论失败,请稍后重试');
Expand Down
47 changes: 26 additions & 21 deletions packages/comment-widget/src/comment-widget.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
import { css, html, LitElement } from 'lit';
import { property, state } from 'lit/decorators.js';
import { CommentVoList, User } from '@halo-dev/api-client';
import { repeat } from 'lit/directives/repeat.js';
import baseStyles from './styles/base';
import { provide } from '@lit/context';
import { LitElement, css, html } from 'lit';
import { property, state } from 'lit/decorators.js';
import { repeat } from 'lit/directives/repeat.js';
import {
AllUserPolicy,
AnonymousUserPolicy,
AvatarPolicyEnum,
NoAvatarUserPolicy,
setPolicyInstance,
} from './avatar/avatar-policy';
import { setAvatarProvider } from './avatar/providers';
import './comment-form';
import './comment-item';
import './comment-pagination';
import {
allowAnonymousCommentsContext,
avatarPolicyContext,
avatarProviderContext,
avatarProviderMirrorContext,
baseUrlContext,
captchaEnabledContext,
currentUserContext,
emojiDataUrlContext,
groupContext,
kindContext,
nameContext,
replySizeContext,
toastContext,
useAvatarProviderContext,
versionContext,
withRepliesContext,
allowAnonymousCommentsContext,
useAvatarProviderContext,
avatarPolicyContext,
avatarProviderContext,
avatarProviderMirrorContext,
} from './context';
import './comment-form';
import './comment-item';
import './comment-pagination';
import varStyles from './styles/var';
import { ToastManager } from './lit-toast';
import {
AnonymousUserPolicy,
AllUserPolicy,
NoAvatarUserPolicy,
AvatarPolicyEnum,
setPolicyInstance,
} from './avatar/avatar-policy';
import { setAvatarProvider } from './avatar/providers';
import baseStyles from './styles/base';
import varStyles from './styles/var';

export class CommentWidget extends LitElement {
@provide({ context: baseUrlContext })
Expand Down Expand Up @@ -98,6 +99,10 @@ export class CommentWidget extends LitElement {
@state()
allowAnonymousComments = false;

@provide({ context: captchaEnabledContext })
@property({ type: Boolean, attribute: 'enable-captcha' })
captchaEnabled = false;

@provide({ context: toastContext })
@state()
toastManager: ToastManager | undefined;
Expand Down
2 changes: 2 additions & 0 deletions packages/comment-widget/src/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export const allowAnonymousCommentsContext = createContext<boolean>(
Symbol('allowAnonymousComments')
);

export const captchaEnabledContext = createContext<boolean>(Symbol('captchaEnabledContext'));

export const currentUserContext = createContext<User | undefined>(Symbol('currentUser'));

export const emojiDataUrlContext = createContext<string>(Symbol('emojiDataUrl'));
Expand Down
2 changes: 1 addition & 1 deletion packages/comment-widget/src/reply-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class ReplyForm extends LitElement {
return;
}

this.captcha = '';
this.baseFormRef.value?.handleFetchCaptcha();

if (!response.ok) {
throw new Error('评论失败,请稍后重试');
Expand Down
2 changes: 2 additions & 0 deletions packages/widget/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface Props {
avatarProvider?: string;
avatarProviderMirror?: string;
avatarPolicy?: string;
captchaEnabled: boolean;
}

export function init(el: string, props: Props) {
Expand Down Expand Up @@ -42,6 +43,7 @@ export function init(el: string, props: Props) {
commentWidget.avatarProvider = props.avatarProvider || '';
commentWidget.avatarProviderMirror = props.avatarProviderMirror || '';
commentWidget.avatarPolicy = props.avatarPolicy || '';
commentWidget.captchaEnabled = props.captchaEnabled || false;

const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private String commentHtml(IAttribute groupAttribute, IAttribute kindAttribute,
.map(SettingConfigGetter.CaptchaConfig::anonymousCommentCaptcha)
.blockOptional()
.orElse(false);
properties.setProperty("enableCaptcha", String.valueOf(captcha));
properties.setProperty("captchaEnabled", String.valueOf(captcha));

// placeholderHelper only support string, so we need to convert boolean to string
return PROPERTY_PLACEHOLDER_HELPER.replacePlaceholders("""
Expand All @@ -104,7 +104,7 @@ private String commentHtml(IAttribute groupAttribute, IAttribute kindAttribute,
avatarProvider: "${avatarProvider}",
avatarProviderMirror: "${avatarProviderMirror}",
avatarPolicy: "${avatarPolicy}",
enableCaptcha: ${enableCaptcha},
captchaEnabled: ${captchaEnabled},
}
);
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/extensions/role-templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ metadata:
rbac.authorization.halo.run/module: "Comment Widget"
rbac.authorization.halo.run/display-name: "Comment Widget Public APIs"
rules:
- apiGroups: [ "api.commentwidget.halo.run/v1alpha1" ]
- apiGroups: [ "api.commentwidget.halo.run" ]
resources: [ "captcha/generate" ]
verbs: [ "get" ]

0 comments on commit 2bcf0e2

Please sign in to comment.