CommentMentionHelper.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { apiv3Get } from '~/client/util/apiv3-client';
  2. export default class CommentMentionHelper {
  3. editor;
  4. pattern: RegExp;
  5. constructor(editor) {
  6. this.editor = editor;
  7. this.pattern = /@[A-Za-z0-9._-]{1,}/;
  8. }
  9. showUsernameHint = () => {
  10. const currentPos = this.editor.getCursor();
  11. const sc = this.editor.getSearchCursor(this.pattern, currentPos, { multiline: false });
  12. if (sc.findPrevious()) {
  13. const isMentioning = (currentPos.line === sc.to().line && currentPos.ch === sc.to().ch);
  14. if (!isMentioning) {
  15. return;
  16. }
  17. }
  18. else {
  19. return;
  20. }
  21. this.editor.showHint({
  22. completeSingle: false,
  23. hint: () => {
  24. const mention = this.editor.getDoc().getRange(sc.from(), sc.to());
  25. const username = mention.replace('@', '');
  26. const users = this.getUsersList(username);
  27. return {
  28. list: users,
  29. from: sc.from(),
  30. to: sc.to(),
  31. };
  32. },
  33. });
  34. }
  35. getUsersList = async(username) => {
  36. const { data } = await apiv3Get('/users/list', { username });
  37. return data.users.map(user => ({
  38. ...user,
  39. }));
  40. }
  41. }