Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Cleanup

Tomas Valenta1715fb091aa4
diff --git a/editor/extensions/autocomplete/findSuggestionMatch.ts b/editor/extensions/autocomplete/findSuggestionMatch.ts
index d907f1a92..3f22f94e4 100644
--- a/editor/extensions/autocomplete/findSuggestionMatch.ts
+++ b/editor/extensions/autocomplete/findSuggestionMatch.ts
@@ -1,68 +1,56 @@
-import { Range } from '@tiptap/core'
-import { ResolvedPos } from 'prosemirror-model'
+import { escapeForRegEx, Range } from '@tiptap/core'
+import { ResolvedPos } from '@tiptap/pm/model'
export interface Trigger {
char: string
allowSpaces: boolean
+ allowedPrefixes: string[] | null
startOfLine: boolean
$position: ResolvedPos
}
export type SuggestionMatch = {
- range: Range,
- query: string,
- text: string,
+ range: Range
+ query: string
+ text: string
} | null
export function findSuggestionMatch(config: Trigger): SuggestionMatch {
const {
- char,
- allowSpaces,
- startOfLine,
- $position,
+ char, allowSpaces, allowedPrefixes, 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 escapedChar = escapeForRegEx(char)
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 text = $position.nodeBefore?.isText && $position.nodeBefore.text
+
+ if (!text) {
+ return null
+ }
- const textTo = $position.pos
- const text = $position.doc.textBetween(textFrom, textTo, '\0', '\0') + textAfterAnchorUntilWhitespace
+ const textFrom = $position.pos - text.length
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
+ // JavaScript doesn't have lookbehinds. This hacks a check that first character
+ // is a space or the start of the line
const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)
+ const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes?.join('')}\0]?$`).test(matchPrefix)
- if (!/^[\s\0]?$/.test(matchPrefix)) {
+ if (allowedPrefixes !== null && !matchPrefixIsAllowed) {
return null
}
// The absolute position of the match in the document
- const from = match.index + $position.start()
+ const from = textFrom + match.index
let to = from + match[0].length
// Edge case handling; if spaces are allowed and we're directly in between
diff --git a/editor/extensions/reference.ts b/editor/extensions/reference.ts
index 73dd68da2..5920d695e 100644
--- a/editor/extensions/reference.ts
+++ b/editor/extensions/reference.ts
@@ -122,7 +122,7 @@ export default Node.create<ReferenceOptions>({
addKeyboardShortcuts() {
return {
Backspace: () => this.editor.commands.command(({ tr, state }) => {
- let isMention = false
+ let isReference = false
const { selection } = state
const { empty, anchor } = selection
@@ -132,14 +132,14 @@ export default Node.create<ReferenceOptions>({
state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {
if (node.type.name === this.name) {
- isMention = true
+ isReference = true
tr.insertText(this.options.suggestion.char || '', pos, pos + node.nodeSize)
return false
}
})
- return isMention
+ return isReference
}),
}
},
@@ -153,25 +153,3 @@ export default Node.create<ReferenceOptions>({
]
},
})
- .extend({
- addCommands() {
- return {
- setReference: (reference) => ({ commands, state }) => {
- return commands
- .setContent([
- {
- type: this.name,
- attrs: {
- [REFERENCE_TYPE_ATTRIBUTE_NAME]: reference.type,
- [REFERENCE_VALUE_ATTRIBUTE_NAME]: reference.value,
- },
- },
- {
- type: 'text',
- text: ' ',
- },
- ])
- },
- }
- },
- })