Skip to content

Commit ce11166

Browse files
authored
feat: add debounce handling for submit comments (#152)
为提交评论添加防抖处理。 /kind feature ```release-note 为提交评论添加防抖处理。 ```
1 parent d34f01e commit ce11166

File tree

3 files changed

+1245
-1000
lines changed

3 files changed

+1245
-1000
lines changed

packages/comment-widget/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@
3939
"dayjs": "^1.11.10",
4040
"emoji-mart": "^5.5.2",
4141
"javascript-time-ago": "^2.5.9",
42-
"lit": "^3.1.2"
42+
"lit": "^3.1.2",
43+
"lodash-es": "^4.17.21"
4344
},
4445
"devDependencies": {
46+
"@types/lodash-es": "^4.17.12",
4547
"lit-analyzer": "^2.0.3"
4648
}
4749
}

packages/comment-widget/src/base-form.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { consume } from '@lit/context';
33
import { css, html, LitElement } from 'lit';
44
import { property, state } from 'lit/decorators.js';
55
import { createRef, Ref, ref } from 'lit/directives/ref.js';
6+
import { debounce } from 'lodash-es';
67
import {
78
allowAnonymousCommentsContext,
89
baseUrlContext,
@@ -254,6 +255,13 @@ export class BaseForm extends LitElement {
254255
`;
255256
}
256257

258+
private debouncedSubmit = debounce((data: Record<string, unknown>) => {
259+
const event = new CustomEvent('submit', {
260+
detail: data,
261+
});
262+
this.dispatchEvent(event);
263+
}, 300);
264+
257265
onSubmit(e: Event) {
258266
e.preventDefault();
259267
const form = e.target as HTMLFormElement;
@@ -270,10 +278,7 @@ export class BaseForm extends LitElement {
270278
})
271279
);
272280

273-
const event = new CustomEvent('submit', {
274-
detail: data,
275-
});
276-
this.dispatchEvent(event);
281+
this.debouncedSubmit(data);
277282
}
278283

279284
resetForm() {

0 commit comments

Comments
 (0)