|
|
@@ -1,5 +1,9 @@
|
|
|
+import { debounce } from 'throttle-debounce';
|
|
|
+
|
|
|
import { apiv3Get } from '~/client/util/apiv3-client';
|
|
|
|
|
|
+const USERNAME_PATTERN = new RegExp(/@[A-Za-z0-9._-]{1,}/);
|
|
|
+
|
|
|
export default class CommentMentionHelper {
|
|
|
|
|
|
editor;
|
|
|
@@ -9,10 +13,11 @@ export default class CommentMentionHelper {
|
|
|
|
|
|
constructor(editor) {
|
|
|
this.editor = editor;
|
|
|
- this.pattern = /@[A-Za-z0-9._-]{1,}/;
|
|
|
+ this.pattern = USERNAME_PATTERN;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- showUsernameHint = () => {
|
|
|
+ getUsernamHint = () => {
|
|
|
const currentPos = this.editor.getCursor();
|
|
|
const sc = this.editor.getSearchCursor(this.pattern, currentPos, { multiline: false });
|
|
|
if (sc.findPrevious()) {
|
|
|
@@ -30,23 +35,26 @@ export default class CommentMentionHelper {
|
|
|
hint: async() => {
|
|
|
const mention = this.editor.getDoc().getRange(sc.from(), sc.to());
|
|
|
const username = mention.replace('@', '');
|
|
|
- const users = await this.getUsersList(username);
|
|
|
- return {
|
|
|
- list: users,
|
|
|
- from: sc.from(),
|
|
|
- to: sc.to(),
|
|
|
- };
|
|
|
+ if (username.length > 0) {
|
|
|
+ const users = await this.getUsersList(username);
|
|
|
+ return {
|
|
|
+ list: users,
|
|
|
+ from: sc.from(),
|
|
|
+ to: sc.to(),
|
|
|
+ };
|
|
|
+ }
|
|
|
},
|
|
|
});
|
|
|
}
|
|
|
|
|
|
getUsersList = async(username) => {
|
|
|
- const { data } = await apiv3Get('/users/list');
|
|
|
+ const { data } = await apiv3Get('/users/list', { username });
|
|
|
return data.users.map(user => ({
|
|
|
text: `@${user.username} `,
|
|
|
displayText: user.username,
|
|
|
- }))
|
|
|
- .filter(user => user.displayText.includes(username));
|
|
|
+ }));
|
|
|
}
|
|
|
|
|
|
+showUsernameHint= debounce(800, () => this.getUsernamHint());
|
|
|
+
|
|
|
}
|