소스 검색

Merge pull request #7546 from weseek/fix/gw7938-username-search-in-comment

fix: search username in comment not working
Mudana-Grune 3 년 전
부모
커밋
dfb576cd1e

+ 1 - 0
packages/app/src/components/PageEditor/CodeMirrorEditor.jsx

@@ -40,6 +40,7 @@ import styles from './CodeMirrorEditor.module.scss';
 window.JSHINT = JSHINT;
 window.JSHINT = JSHINT;
 window.kuromojin = { dicPath: '/static/dict' };
 window.kuromojin = { dicPath: '/static/dict' };
 
 
+require('codemirror/addon/hint/show-hint.css'); // Import from CodeMirrorEditor.module.scss not working
 require('codemirror/addon/display/placeholder');
 require('codemirror/addon/display/placeholder');
 require('codemirror/addon/edit/matchbrackets');
 require('codemirror/addon/edit/matchbrackets');
 require('codemirror/addon/edit/matchtags');
 require('codemirror/addon/edit/matchtags');

+ 0 - 1
packages/app/src/components/PageEditor/CodeMirrorEditor.module.scss

@@ -4,7 +4,6 @@
   @import '~codemirror/lib/codemirror';
   @import '~codemirror/lib/codemirror';
 
 
   // addons
   // addons
-  @import '~codemirror/addon/hint/show-hint';
   @import '~codemirror/addon/fold/foldgutter';
   @import '~codemirror/addon/fold/foldgutter';
   @import '~codemirror/addon/lint/lint';
   @import '~codemirror/addon/lint/lint';
 
 

+ 15 - 12
packages/app/src/components/PageEditor/CommentMentionHelper.ts

@@ -1,20 +1,23 @@
-import i18n from 'i18next';
+import { Editor } from 'codemirror';
+import { i18n } from 'next-i18next';
 import { debounce } from 'throttle-debounce';
 import { debounce } from 'throttle-debounce';
 
 
 import { apiv3Get } from '~/client/util/apiv3-client';
 import { apiv3Get } from '~/client/util/apiv3-client';
 
 
-export default class CommentMentionHelper {
-
-  editor;
 
 
-  pattern: RegExp;
+type UsersListForHints = {
+  text: string
+  displayText: string
+}
+export default class CommentMentionHelper {
 
 
+  editor: Editor;
 
 
-  constructor(editor) {
+  constructor(editor: Editor) {
     this.editor = editor;
     this.editor = editor;
   }
   }
 
 
-  getUsernamHint = () => {
+  getUsenameHint = (): void => {
     // Get word that contains `@` character at the begining
     // Get word that contains `@` character at the begining
     const currentPos = this.editor.getCursor();
     const currentPos = this.editor.getCursor();
     const wordStart = this.editor.findWordAt(currentPos).anchor.ch - 1;
     const wordStart = this.editor.findWordAt(currentPos).anchor.ch - 1;
@@ -32,14 +35,14 @@ export default class CommentMentionHelper {
     }
     }
 
 
     // Get username after `@` character and search username
     // Get username after `@` character and search username
-    const mention = searchMention.substr(1);
+    const mention = searchMention.slice(1);
     this.editor.showHint({
     this.editor.showHint({
       completeSingle: false,
       completeSingle: false,
       hint: async() => {
       hint: async() => {
         if (mention.length > 0) {
         if (mention.length > 0) {
           const users = await this.getUsersList(mention);
           const users = await this.getUsersList(mention);
           return {
           return {
-            list: users.length > 0 ? users : [{ text: '', displayText: i18n.t('page_comment.no_user_found') }],
+            list: users.length > 0 ? users : [{ text: '', displayText: i18n?.t('page_comment.no_user_found') }],
             from: searchFrom,
             from: searchFrom,
             to: searchTo,
             to: searchTo,
           };
           };
@@ -48,15 +51,15 @@ export default class CommentMentionHelper {
     });
     });
   };
   };
 
 
-  getUsersList = async(q: string) => {
+  getUsersList = async(q: string): Promise<UsersListForHints[]> => {
     const limit = 20;
     const limit = 20;
     const { data } = await apiv3Get('/users/usernames', { q, limit });
     const { data } = await apiv3Get('/users/usernames', { q, limit });
-    return data.activeUser.usernames.map(username => ({
+    return data.activeUser.usernames.map((username: string) => ({
       text: `@${username} `,
       text: `@${username} `,
       displayText: username,
       displayText: username,
     }));
     }));
   };
   };
 
 
-  showUsernameHint = debounce(800, () => this.getUsernamHint());
+  showUsernameHint = debounce(800, () => this.getUsenameHint());
 
 
 }
 }