diff --git a/src/components/editor/editor-pane/autocompletion/code-block.ts b/src/components/editor/editor-pane/autocompletion/code-block.ts index b93caa282..9c66d4318 100644 --- a/src/components/editor/editor-pane/autocompletion/code-block.ts +++ b/src/components/editor/editor-pane/autocompletion/code-block.ts @@ -1,14 +1,13 @@ import { Editor, Hint, Hints, Pos } from 'codemirror' import { findWordAtCursor, Hinter, search } from './index' -const allowedChars = /[`\w-_+]/ const wordRegExp = /^```((\w|-|_|\+)*)$/ let allSupportedLanguages: string[] = [] const codeBlockHint = (editor: Editor): Promise< Hints| null > => { return import(/* webpackChunkName: "highlight.js" */ 'highlight.js').then(hljs => new Promise((resolve) => { - const searchTerm = findWordAtCursor(editor, allowedChars) + const searchTerm = findWordAtCursor(editor) const searchResult = wordRegExp.exec(searchTerm.text) if (searchResult === null) { resolve(null) @@ -36,7 +35,6 @@ const codeBlockHint = (editor: Editor): Promise< Hints| null > => { } export const CodeBlockHinter: Hinter = { - allowedChars, wordRegExp, hint: codeBlockHint } diff --git a/src/components/editor/editor-pane/autocompletion/collapsable-block.ts b/src/components/editor/editor-pane/autocompletion/collapsable-block.ts index a2c45b4d1..5091daa38 100644 --- a/src/components/editor/editor-pane/autocompletion/collapsable-block.ts +++ b/src/components/editor/editor-pane/autocompletion/collapsable-block.ts @@ -1,12 +1,11 @@ import { Editor, Hint, Hints, Pos } from 'codemirror' import { findWordAtCursor, Hinter } from './index' -const allowedChars = /[<\w>]/ const wordRegExp = /^( => { return new Promise((resolve) => { - const searchTerm = findWordAtCursor(editor, allowedChars) + const searchTerm = findWordAtCursor(editor) const searchResult = wordRegExp.exec(searchTerm.text) if (searchResult === null) { resolve(null) @@ -29,7 +28,6 @@ const collapsableBlockHint = (editor: Editor): Promise< Hints| null > => { } export const CollapsableBlockHinter: Hinter = { - allowedChars, wordRegExp, hint: collapsableBlockHint } diff --git a/src/components/editor/editor-pane/autocompletion/container.ts b/src/components/editor/editor-pane/autocompletion/container.ts index 79107060e..a1ccb75d9 100644 --- a/src/components/editor/editor-pane/autocompletion/container.ts +++ b/src/components/editor/editor-pane/autocompletion/container.ts @@ -1,13 +1,12 @@ import { Editor, Hint, Hints, Pos } from 'codemirror' import { findWordAtCursor, Hinter } from './index' -const allowedChars = /[:\w-_+]/ const wordRegExp = /^:::((\w|-|_|\+)*)$/ const allSupportedConatiner = ['success', 'info', 'warning', 'danger'] const containerHint = (editor: Editor): Promise< Hints| null > => { return new Promise((resolve) => { - const searchTerm = findWordAtCursor(editor, allowedChars) + const searchTerm = findWordAtCursor(editor) const searchResult = wordRegExp.exec(searchTerm.text) if (searchResult === null) { resolve(null) @@ -31,7 +30,6 @@ const containerHint = (editor: Editor): Promise< Hints| null > => { } export const ContainerHinter: Hinter = { - allowedChars, wordRegExp, hint: containerHint } diff --git a/src/components/editor/editor-pane/autocompletion/emoji.ts b/src/components/editor/editor-pane/autocompletion/emoji.ts index 749769011..41fcfc5b3 100644 --- a/src/components/editor/editor-pane/autocompletion/emoji.ts +++ b/src/components/editor/editor-pane/autocompletion/emoji.ts @@ -5,13 +5,12 @@ import { customEmojis } from '../tool-bar/emoji-picker/emoji-picker' import { getEmojiIcon, getEmojiShortCode } from '../tool-bar/utils/emojiUtils' import { findWordAtCursor, Hinter } from './index' -const allowedCharsInEmojiCodeRegex = /[:\w-_+]/ const emojiIndex = new NimbleEmojiIndex(data as unknown as Data) const emojiWordRegex = /^:([\w-_+]*)$/ const generateEmojiHints = (editor: Editor): Promise< Hints| null > => { return new Promise((resolve) => { - const searchTerm = findWordAtCursor(editor, allowedCharsInEmojiCodeRegex) + const searchTerm = findWordAtCursor(editor) const searchResult = emojiWordRegex.exec(searchTerm.text) if (searchResult === null) { resolve(null) @@ -50,7 +49,6 @@ const generateEmojiHints = (editor: Editor): Promise< Hints| null > => { } export const EmojiHinter: Hinter = { - allowedChars: allowedCharsInEmojiCodeRegex, wordRegExp: emojiWordRegex, hint: generateEmojiHints } diff --git a/src/components/editor/editor-pane/autocompletion/header.ts b/src/components/editor/editor-pane/autocompletion/header.ts index cc11361c6..a07f636fa 100644 --- a/src/components/editor/editor-pane/autocompletion/header.ts +++ b/src/components/editor/editor-pane/autocompletion/header.ts @@ -1,14 +1,13 @@ import { Editor, Hint, Hints, Pos } from 'codemirror' import { findWordAtCursor, Hinter, search } from './index' -const allowedChars = /#/ const wordRegExp = /^(\s{0,3})(#{1,6})$/ const allSupportedHeaders = ['# h1', '## h2', '### h3', '#### h4', '##### h5', '###### h6', '###### tags: `example`'] const allSupportedHeadersTextToInsert = ['# ', '## ', '### ', '#### ', '##### ', '###### ', '###### tags: `example`'] const headerHint = (editor: Editor): Promise< Hints| null > => { return new Promise((resolve) => { - const searchTerm = findWordAtCursor(editor, allowedChars) + const searchTerm = findWordAtCursor(editor) const searchResult = wordRegExp.exec(searchTerm.text) if (searchResult === null) { resolve(null) @@ -25,8 +24,8 @@ const headerHint = (editor: Editor): Promise< Hints| null > => { resolve(null) } else { resolve({ - list: suggestions.map((suggestion, index): Hint => ({ - text: allSupportedHeadersTextToInsert[index], + list: suggestions.map((suggestion): Hint => ({ + text: allSupportedHeadersTextToInsert[allSupportedHeaders.indexOf(suggestion)], displayText: suggestion })), from: Pos(cursor.line, searchTerm.start), @@ -37,7 +36,6 @@ const headerHint = (editor: Editor): Promise< Hints| null > => { } export const HeaderHinter: Hinter = { - allowedChars, wordRegExp, hint: headerHint } diff --git a/src/components/editor/editor-pane/autocompletion/image.ts b/src/components/editor/editor-pane/autocompletion/image.ts index c2932b7ac..6a469b686 100644 --- a/src/components/editor/editor-pane/autocompletion/image.ts +++ b/src/components/editor/editor-pane/autocompletion/image.ts @@ -1,7 +1,6 @@ import { Editor, Hint, Hints, Pos } from 'codemirror' import { findWordAtCursor, Hinter } from './index' -const allowedChars = /[![\]\w]/ const wordRegExp = /^(!(\[.*])?)$/ const allSupportedImages = [ '![image alt](https:// "title")', @@ -11,7 +10,7 @@ const allSupportedImages = [ const imageHint = (editor: Editor): Promise< Hints| null > => { return new Promise((resolve) => { - const searchTerm = findWordAtCursor(editor, allowedChars) + const searchTerm = findWordAtCursor(editor) const searchResult = wordRegExp.exec(searchTerm.text) if (searchResult === null) { resolve(null) @@ -34,7 +33,6 @@ const imageHint = (editor: Editor): Promise< Hints| null > => { } export const ImageHinter: Hinter = { - allowedChars, wordRegExp, hint: imageHint } diff --git a/src/components/editor/editor-pane/autocompletion/index.ts b/src/components/editor/editor-pane/autocompletion/index.ts index 609d0e340..1f7350f87 100644 --- a/src/components/editor/editor-pane/autocompletion/index.ts +++ b/src/components/editor/editor-pane/autocompletion/index.ts @@ -15,12 +15,13 @@ interface findWordAtCursorResponse { } export interface Hinter { - allowedChars: RegExp, wordRegExp: RegExp, hint: (editor: Editor) => Promise< Hints| null > } -export const findWordAtCursor = (editor: Editor, allowedChars: RegExp): findWordAtCursorResponse => { +const allowedChars = /[^\s]/ + +export const findWordAtCursor = (editor: Editor): findWordAtCursorResponse => { const cursor = editor.getCursor() const line = editor.getLine(cursor.line) let start = cursor.ch diff --git a/src/components/editor/editor-pane/autocompletion/link-and-extra-tag.ts b/src/components/editor/editor-pane/autocompletion/link-and-extra-tag.ts index 951a9a3b9..e516fb2bd 100644 --- a/src/components/editor/editor-pane/autocompletion/link-and-extra-tag.ts +++ b/src/components/editor/editor-pane/autocompletion/link-and-extra-tag.ts @@ -3,7 +3,6 @@ import moment from 'moment' import { getUser } from '../../../../redux/user/methods' import { findWordAtCursor, Hinter } from './index' -const allowedChars = /[[\]\w]/ const wordRegExp = /^(\[(.*])?)$/ const allSupportedLinks = [ '[link text](https:// "title")', @@ -22,7 +21,7 @@ const allSupportedLinks = [ const linkAndExtraTagHint = (editor: Editor): Promise< Hints| null > => { return new Promise((resolve) => { - const searchTerm = findWordAtCursor(editor, allowedChars) + const searchTerm = findWordAtCursor(editor) const searchResult = wordRegExp.exec(searchTerm.text) if (searchResult === null) { resolve(null) @@ -63,7 +62,6 @@ const linkAndExtraTagHint = (editor: Editor): Promise< Hints| null > => { } export const LinkAndExtraTagHinter: Hinter = { - allowedChars, wordRegExp, hint: linkAndExtraTagHint } diff --git a/src/components/editor/editor-pane/autocompletion/pdf.ts b/src/components/editor/editor-pane/autocompletion/pdf.ts index edc680837..04dfc980b 100644 --- a/src/components/editor/editor-pane/autocompletion/pdf.ts +++ b/src/components/editor/editor-pane/autocompletion/pdf.ts @@ -1,12 +1,11 @@ import { Editor, Hint, Hints, Pos } from 'codemirror' import { findWordAtCursor, Hinter } from './index' -const allowedChars = /[{%]/ const wordRegExp = /^({[%}]?)$/ const pdfHint = (editor: Editor): Promise< Hints| null > => { return new Promise((resolve) => { - const searchTerm = findWordAtCursor(editor, allowedChars) + const searchTerm = findWordAtCursor(editor) const searchResult = wordRegExp.exec(searchTerm.text) if (searchResult === null) { resolve(null) @@ -29,7 +28,6 @@ const pdfHint = (editor: Editor): Promise< Hints| null > => { } export const PDFHinter: Hinter = { - allowedChars, wordRegExp, hint: pdfHint } diff --git a/src/components/editor/editor-pane/editor-pane.tsx b/src/components/editor/editor-pane/editor-pane.tsx index bec478c5e..0aaea8121 100644 --- a/src/components/editor/editor-pane/editor-pane.tsx +++ b/src/components/editor/editor-pane/editor-pane.tsx @@ -41,7 +41,7 @@ export interface EditorPaneProps { const onChange = (editor: Editor) => { for (const hinter of allHinters) { - const searchTerm = findWordAtCursor(editor, hinter.allowedChars) + const searchTerm = findWordAtCursor(editor) if (hinter.wordRegExp.test(searchTerm.text)) { editor.showHint({ hint: hinter.hint,