Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add default size options #109

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ build {
}

halo {
version = "2.13.0"
version = "2.14.0"
}
2 changes: 1 addition & 1 deletion packages/comment-widget/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"dependencies": {
"@emoji-mart/data": "^1.1.2",
"@halo-dev/api-client": "^2.13.0",
"@halo-dev/api-client": "^2.14.0",
"@lit/context": "^1.1.0",
"dayjs": "^1.11.10",
"emoji-mart": "^5.5.2",
Expand Down
12 changes: 6 additions & 6 deletions packages/comment-widget/src/comment-replies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommentVo, ReplyVo, ReplyVoList } from '@halo-dev/api-client';
import { LitElement, css, html } from 'lit';
import { css, html, LitElement } from 'lit';
import { property, state } from 'lit/decorators.js';
import { repeat } from 'lit/directives/repeat.js';
import { consume } from '@lit/context';
Expand Down Expand Up @@ -50,15 +50,15 @@ export class CommentReplies extends LitElement {
toastManager: ToastManager | undefined;

override render() {
return html`<div class="replies__wrapper">
return html` <div class="replies__wrapper">
${this.replies.length
? html`
<div class="replies__list">
${repeat(
this.replies,
(item) => item.metadata.name,
(item) =>
html`<reply-item
html` <reply-item
.comment=${this.comment}
.reply="${item}"
.replies=${this.replies}
Expand All @@ -70,9 +70,9 @@ export class CommentReplies extends LitElement {
</div>
`
: ''}
${this.loading ? html`<loading-block></loading-block>` : ''}
${this.loading ? html` <loading-block></loading-block>` : ''}
${this.hasNext && !this.loading
? html`<div class="replies__next-wrapper">
? html` <div class="replies__next-wrapper">
<button @click=${this.fetchNext}>加载更多</button>
</div>`
: ''}
Expand Down Expand Up @@ -123,7 +123,7 @@ export class CommentReplies extends LitElement {

async fetchNext() {
this.page++;
this.fetchReplies({ append: true });
await this.fetchReplies({ append: true });
}

override connectedCallback(): void {
Expand Down
27 changes: 16 additions & 11 deletions packages/comment-widget/src/comment-widget.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LitElement, css, html } from 'lit';
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';
Expand Down Expand Up @@ -44,13 +44,19 @@ export class CommentWidget extends LitElement {
@property({ type: String })
name = '';

@property({ type: Number, attribute: 'size' })
size: number = 20;

@provide({ context: replySizeContext })
@property({ type: Number, attribute: 'reply-size' })
replySize: number = 10;

@provide({ context: withRepliesContext })
@property({ type: Boolean, attribute: 'with-replies' })
withReplies = false;

@provide({ context: replySizeContext })
@property({ type: Number, attribute: 'reply-size' })
replySize = 10;
@property({ type: Number, attribute: 'with-reply-size' })
withReplySize = 10;

@provide({ context: emojiDataUrlContext })
@property({ type: String, attribute: 'emoji-data-url' })
Expand Down Expand Up @@ -93,12 +99,12 @@ export class CommentWidget extends LitElement {
}

override render() {
return html`<div class="comment-widget">
return html` <div class="comment-widget">
<comment-form
@reload="${() => this.fetchComments({ page: 1, scrollIntoView: true })}"
></comment-form>
${this.loading
? html`<loading-block></loading-block>`
? html` <loading-block></loading-block>`
: html`
<div class="comment-widget__wrapper">
<div class="comment-widget__stats">
Expand All @@ -109,7 +115,7 @@ export class CommentWidget extends LitElement {
${repeat(
this.comments.items,
(item) => item.metadata.name,
(item) => html`<comment-item .comment=${item}></comment-item>`
(item) => html` <comment-item .comment=${item}></comment-item>`
)}
</div>
</div>
Expand Down Expand Up @@ -163,10 +169,10 @@ export class CommentWidget extends LitElement {
`kind=${this.kind}`,
`name=${this.name}`,
`page=${this.comments.page}`,
`size=${this.comments.size}`,
`size=${this.size}`,
`version=${this.version}`,
`withReplies=${this.withReplies}`,
`replySize=${this.replySize}`,
`replySize=${this.withReplySize}`,
];

const response = await fetch(
Expand All @@ -177,8 +183,7 @@ export class CommentWidget extends LitElement {
throw new Error('评论列表加载失败,请稍后重试');
}

const data = await response.json();
this.comments = data;
this.comments = await response.json();
} catch (error) {
if (error instanceof Error) {
this.toastManager?.error(error.message);
Expand Down
2 changes: 1 addition & 1 deletion packages/comment-widget/src/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export const kindContext = createContext<string>(Symbol('kind'));
export const groupContext = createContext<string>(Symbol('group'));
export const nameContext = createContext<string>(Symbol('name'));
export const versionContext = createContext<string>(Symbol('version'));
export const withRepliesContext = createContext<boolean>(Symbol('withReplies'));
export const replySizeContext = createContext<number>(Symbol('replySize'));
export const withRepliesContext = createContext<boolean>(Symbol('withReplies'));

export const allowAnonymousCommentsContext = createContext<boolean>(
Symbol('allowAnonymousComments')
Expand Down
8 changes: 6 additions & 2 deletions packages/widget/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ interface Props {
group: string;
kind: string;
name: string;
withReplies?: boolean;
size?: number;
replySize?: number;
withReplies?: boolean;
withReplySize?: number;
}

export function init(el: string, props: Props) {
Expand All @@ -26,8 +28,10 @@ export function init(el: string, props: Props) {
commentWidget.group = props.group;
commentWidget.version = 'v1alpha1';
commentWidget.name = props.name;
commentWidget.withReplies = props.withReplies || false;
commentWidget.size = props.size || 20;
commentWidget.replySize = props.replySize || 10;
commentWidget.withReplies = props.withReplies || false;
commentWidget.withReplySize = props.withReplySize || 10;
commentWidget.emojiDataUrl =
'/plugins/PluginCommentWidget/assets/static/emoji/native.json';

Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions src/main/java/run/halo/comment/widget/DefaultCommentWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ private String commentHtml(IAttribute groupAttribute, IAttribute kindAttribute,
var basicConfig = settingFetcher.fetch(BasicConfig.GROUP, BasicConfig.class)
.orElse(new BasicConfig());
// placeholderHelper only support string, so we need to convert boolean to string
properties.setProperty("withReplies", String.valueOf(basicConfig.isWithReplies()));
properties.setProperty("size", String.valueOf(basicConfig.getSize()));
properties.setProperty("replySize", String.valueOf(basicConfig.getReplySize()));
properties.setProperty("withReplies", String.valueOf(basicConfig.isWithReplies()));
properties.setProperty("withReplySize", String.valueOf(basicConfig.getWithReplySize()));

return PROPERTY_PLACEHOLDER_HELPER.replacePlaceholders("""
<div id="${domId}"></div>
Expand All @@ -79,8 +81,10 @@ private String commentHtml(IAttribute groupAttribute, IAttribute kindAttribute,
group: "${group}",
kind: "${kind}",
name: "${name}",
size: ${size},
replySize: ${replySize},
withReplies: ${withReplies},
replySize: ${replySize}
withReplySize: ${withReplySize}
}
);
</script>
Expand All @@ -90,8 +94,10 @@ private String commentHtml(IAttribute groupAttribute, IAttribute kindAttribute,
@Data
static class BasicConfig {
public static final String GROUP = "basic";
private boolean withReplies;
private int size;
private int replySize;
private boolean withReplies;
private int withReplySize;
}

private String domIdFrom(String group, String kind, String name) {
Expand Down
21 changes: 16 additions & 5 deletions src/main/resources/extensions/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,27 @@ spec:
- group: basic
label: 基本设置
formSchema:
- $formkit: number
label: 默认加载评论条数
name: size
validation: required
value: 20
- $formkit: number
label: 默认加载回复条数
name: replySize
validation: required
value: 10
- $formkit: checkbox
label: 同时加载评论的回复
name: withReplies
id: withReplies
key: withReplies
value: false
- $formkit: number
label: 默认加载回复条数
name: replySize
id: replySize
key: replySize
label: 同时加载回复的条数
if: "$get(withReplies).value === true"
name: withReplySize
id: withReplySize
key: withReplySize
validation: required
value: 20
value: 5
Loading