From 0d7ce1c9d6b0d535acd131a51543c073b62cd613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 20 Jan 2021 16:36:29 +0100 Subject: [PATCH 1/3] improve mention filter --- docs/src/demos/Nodes/Mention/index.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/demos/Nodes/Mention/index.vue b/docs/src/demos/Nodes/Mention/index.vue index 16bbf954..ad3b4dda 100644 --- a/docs/src/demos/Nodes/Mention/index.vue +++ b/docs/src/demos/Nodes/Mention/index.vue @@ -36,7 +36,7 @@ export default { }, suggestionOptions: { items: query => { - return ['Hans', 'Philipp', 'Kris'].filter(item => item.startsWith(query)) + return ['Hans', 'Philipp', 'Kris'].filter(item => item.toLowerCase().startsWith(query.toLowerCase())) }, render: () => { let component From 4004ec0ad5db3d12118c7544dabd4012975e551c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 20 Jan 2021 16:39:42 +0100 Subject: [PATCH 2/3] refactoring --- packages/core/src/utilities/isClass.ts | 7 +++++++ packages/core/src/utilities/isObject.ts | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 packages/core/src/utilities/isClass.ts diff --git a/packages/core/src/utilities/isClass.ts b/packages/core/src/utilities/isClass.ts new file mode 100644 index 00000000..a35ba2e7 --- /dev/null +++ b/packages/core/src/utilities/isClass.ts @@ -0,0 +1,7 @@ +export default function isClass(item: any): boolean { + if (item.constructor?.toString().substring(0, 5) !== 'class') { + return false + } + + return true +} diff --git a/packages/core/src/utilities/isObject.ts b/packages/core/src/utilities/isObject.ts index 5321f363..84e8d66d 100644 --- a/packages/core/src/utilities/isObject.ts +++ b/packages/core/src/utilities/isObject.ts @@ -1,3 +1,5 @@ +import isClass from './isClass' + export default function isObject(item: any): boolean { if (!item) { return false @@ -11,7 +13,7 @@ export default function isObject(item: any): boolean { return false } - if (item.constructor?.toString().substring(0, 5) === 'class') { + if (isClass(item)) { return false } From 11d365f524e668d78060c24cd2aef03f02236ac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 20 Jan 2021 16:52:56 +0100 Subject: [PATCH 3/3] refactoring --- .../suggestion/src/findSuggestionMatch.ts | 74 +++++++++---------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/packages/suggestion/src/findSuggestionMatch.ts b/packages/suggestion/src/findSuggestionMatch.ts index e548a700..88b843c5 100644 --- a/packages/suggestion/src/findSuggestionMatch.ts +++ b/packages/suggestion/src/findSuggestionMatch.ts @@ -35,49 +35,45 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch { ? new RegExp(`${prefix}${escapedChar}.*?(?=\\s${escapedChar}|$)`, 'gm') : new RegExp(`${prefix}(?:^)?${escapedChar}[^\\s${escapedChar}]*`, 'gm') - // Lookup the boundaries of the current node const textFrom = $position.before() - - // Only look up to the cursor, old behavior: textTo = $position.end() const textTo = $position.pos - const text = $position.doc.textBetween(textFrom, textTo, '\0', '\0') + const match = regexp.exec(text) - let match = regexp.exec(text) - let position = null - - while (match !== null) { - // JavaScript doesn't have lookbehinds; this hacks a check that first character is " " - // or the line beginning - const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index) - - if (/^[\s\0]?$/.test(matchPrefix)) { - // The absolute position of the match in the document - const from = match.index + $position.start() - let to = from + match[0].length - - // Edge case handling; if spaces are allowed and we're directly in between - // two triggers - if (allowSpaces && suffix.test(text.slice(to - 1, to + 1))) { - match[0] += ' ' - to += 1 - } - - // If the $position is located within the matched substring, return that range - if (from < $position.pos && to >= $position.pos) { - position = { - range: { - from, - to, - }, - query: match[0].slice(char.length), - text: match[0], - } - } - } - - match = regexp.exec(text) + if (!match) { + return null } - return position + // JavaScript doesn't have lookbehinds; this hacks a check that first character is " " + // or the line beginning + const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index) + + if (!/^[\s\0]?$/.test(matchPrefix)) { + return null + } + + // The absolute position of the match in the document + const from = match.index + $position.start() + let to = from + match[0].length + + // Edge case handling; if spaces are allowed and we're directly in between + // two triggers + if (allowSpaces && suffix.test(text.slice(to - 1, to + 1))) { + match[0] += ' ' + to += 1 + } + + // If the $position is located within the matched substring, return that range + if (from < $position.pos && to >= $position.pos) { + return { + range: { + from, + to, + }, + query: match[0].slice(char.length), + text: match[0], + } + } + + return null }