Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Fix on keystroke reference matching

Tomas Valenta496e84a490f8
diff --git a/editor/extensions/autocomplete/findSuggestionMatch.ts b/editor/extensions/autocomplete/findSuggestionMatch.ts
deleted file mode 100644
index d907f1a92..000000000
--- a/editor/extensions/autocomplete/findSuggestionMatch.ts
+++ /dev/null
@@ -1,88 +0,0 @@
-import { Range } from '@tiptap/core'
-import { ResolvedPos } from 'prosemirror-model'
-
-export interface Trigger {
- char: string
- allowSpaces: boolean
- startOfLine: boolean
- $position: ResolvedPos
-}
-
-export type SuggestionMatch = {
- range: Range,
- query: string,
- text: string,
-} | null
-
-export function findSuggestionMatch(config: Trigger): SuggestionMatch {
- const {
- char,
- allowSpaces,
- startOfLine,
- $position,
- } = config
-
- const textAfterAnchor = $position.nodeAfter?.textContent || ''
- const whitespaceAfterAnchorIndex = textAfterAnchor.indexOf(' ')
- const textAfterAnchorUntilWhitespace = whitespaceAfterAnchorIndex < 0
- ? textAfterAnchor
- : textAfterAnchor.slice(0, whitespaceAfterAnchorIndex + 1)
-
- // Matching expressions used for later
- const escapedChar = char
- .split('')
- .map(c => `\\${c}`)
- .join('')
-
- const suffix = new RegExp(`\\s${escapedChar}$`)
- const prefix = startOfLine ? '^' : ''
- const regexp = allowSpaces
- ? new RegExp(`${prefix}${escapedChar}.*?(?=\\s${escapedChar}|$)`, 'gm')
- : new RegExp(`${prefix}(?:^)?${escapedChar}[^\\s${escapedChar}]*`, 'gm')
-
- const isTopLevelNode = $position.depth <= 0
- const textFrom = isTopLevelNode
- ? 0
- : $position.before()
-
- const textTo = $position.pos
- const text = $position.doc.textBetween(textFrom, textTo, '\0', '\0') + textAfterAnchorUntilWhitespace
- const match = Array.from(text.matchAll(regexp)).pop()
-
- if (!match || match.input === undefined || match.index === undefined) {
- return 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)) {
- 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
-}
diff --git a/editor/extensions/autocomplete/index.ts b/editor/extensions/autocomplete/index.ts
index 08c271ca0..b265b5eba 100644
--- a/editor/extensions/autocomplete/index.ts
+++ b/editor/extensions/autocomplete/index.ts
@@ -8,7 +8,7 @@ import tippy, {
Props,
} from 'tippy.js'
-import { destroyOnEsc } from 'editor/tippyPlugins'
+import { destroyOnBackspace, destroyOnEsc } from 'editor/tippyPlugins'
import AutocompleteListWrapper, { AutocompleteList } from 'components/Editor/RouteEditor/PromptEditor/Autocomplete/ListWrapper'
import Autocomplete from 'components/Editor/RouteEditor/PromptEditor/Autocomplete'
@@ -73,7 +73,7 @@ const autocomplete = Node.create<AutocompleteOptions>({
showOnCreate: true,
interactive: true,
hideOnClick: true,
- duration: 1,
+ delay: 160,
maxWidth: 'none',
trigger: 'manual',
offset: [1, 2],
@@ -87,7 +87,7 @@ const autocomplete = Node.create<AutocompleteOptions>({
disabled = true
reactRenderer.destroy()
},
- plugins: [destroyOnEsc],
+ plugins: [destroyOnEsc, destroyOnBackspace],
})
},
onUpdate(props) {
diff --git a/editor/extensions/autocomplete/suggestion.ts b/editor/extensions/autocomplete/suggestion.ts
index c22fa03b0..17c6eab0e 100644
--- a/editor/extensions/autocomplete/suggestion.ts
+++ b/editor/extensions/autocomplete/suggestion.ts
@@ -3,8 +3,6 @@ import { Plugin, PluginKey } from 'prosemirror-state'
import { Decoration, DecorationSet, EditorView } from 'prosemirror-view'
import type { Reference } from 'editor/referenceType'
-import { findSuggestionMatch } from './findSuggestionMatch'
-
export interface SuggestionOptions<Item = Reference> {
editor: Editor,
@@ -163,44 +161,39 @@ export function Suggestion<Item = Reference>({
next.active = false
}
- console.log('apply', {
- position: selection.$from,
- })
-
-
- // const text = selection.$from.node().text
-
- // if (text) {
- // const prefix = text.slice(0, selection.$from.textOffset)
- // const lastWord = /\\b(\w+)$\\/
-
- // const prefixWord = lastWord.exec(prefix)
- // if (prefixWord) {
- // const match = prefixWord[0]
- // // Calculate range
- // // Update state
- // }
- // }
-
-
- // Try to match against where our cursor currently is
- const match = findSuggestionMatch({
- char,
- allowSpaces,
- startOfLine: false,
- $position: selection.$from,
- })
- const decorationID = `id_${Math.floor(Math.random() * 0xFFFFFFFF)}`
-
- // If we found a match, update the current state to show it
- if (match && allow({ editor, range: match.range })) {
- next.active = true
- next.decorationID = prev.decorationID ? prev.decorationID : decorationID
- next.range = match.range
- next.query = match.query
- next.text = match.text
- } else {
- next.active = false
+ const $position = selection.$from
+
+ const isTopLevelNode = $position.depth <= 0
+ const textFrom = isTopLevelNode
+ ? 0
+ : $position.before()
+
+ const textTo = $position.pos
+ const text = $position.doc.textBetween(textFrom, textTo, '\0', '\0')
+
+ if (text) {
+ const lastWord = /(\S+)$/
+ const prefixWord = lastWord.exec(text)
+ if (prefixWord) {
+ const match = prefixWord[0]
+ const range = {
+ from: text.length - match.length + 1,
+ to: text.length + 1,
+ }
+
+ if (allow({ editor, range })) {
+ const decorationID = `id_${Math.floor(Math.random() * 0xFFFFFFFF)}`
+ next.active = true
+ next.decorationID = prev.decorationID ? prev.decorationID : decorationID
+ next.range = range
+ next.text = match
+ next.query = match
+ } else {
+ next.active = false
+ }
+ } else {
+ next.active = false
+ }
}
} else {
next.active = false
diff --git a/editor/schema.ts b/editor/schema.ts
index 534bee624..a2775c5ae 100644
--- a/editor/schema.ts
+++ b/editor/schema.ts
@@ -35,7 +35,7 @@ export const referenceItems: Reference[] = [
},
]
-const searchEngine = new Fuse(referenceItems, { keys: ['value'] })
+const searchEngine = new Fuse(referenceItems, { keys: ['value'], threshold: 0.3 })
export const extensions = [
StarterKit.configure({
diff --git a/editor/tippyPlugins/destroyOnBackspace.ts b/editor/tippyPlugins/destroyOnBackspace.ts
new file mode 100644
index 000000000..76d059c43
--- /dev/null
+++ b/editor/tippyPlugins/destroyOnBackspace.ts
@@ -0,0 +1,20 @@
+export const destroyOnBackspace = {
+ name: 'destroyOnBackspace',
+
+ defaultValue: true,
+
+ fn({ destroy }: { destroy: () => void }) {
+ function onKeyDown(event: any) {
+ if (event.key === 'Backspace') destroy()
+ }
+
+ return {
+ onShow() {
+ document.addEventListener('keydown', onKeyDown)
+ },
+ onHide() {
+ document.removeEventListener('keydown', onKeyDown)
+ },
+ }
+ },
+}
\ No newline at end of file
diff --git a/editor/tippyPlugins/index.ts b/editor/tippyPlugins/index.ts
index 696087ec9..3055c240e 100644
--- a/editor/tippyPlugins/index.ts
+++ b/editor/tippyPlugins/index.ts
@@ -1 +1,2 @@
-export * from './destroyOnEsc'
\ No newline at end of file
+export * from './destroyOnEsc'
+export * from './destroyOnBackspace'