Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Simplify editor initialization

Tomas Valenta773ed5e90dde
diff --git a/editor/extensions/keymap.ts b/editor/extensions/keymap.ts
new file mode 100644
index 000000000..c7ce14011
--- /dev/null
+++ b/editor/extensions/keymap.ts
@@ -0,0 +1,12 @@
+import { Node } from '@tiptap/core'
+
+export default Node.create({
+ addKeyboardShortcuts() {
+ return {
+ // Override default browser Ctrl/Cmd+S shortcut.
+ 'Mod-s': function () {
+ return true
+ },
+ }
+ }
+})
diff --git a/editor/extensions/reference.ts b/editor/extensions/reference.ts
index 3a8479931..68ee95ffe 100644
--- a/editor/extensions/reference.ts
+++ b/editor/extensions/reference.ts
@@ -119,28 +119,28 @@ export default Node.create<ReferenceOptions>({
})
},
- addKeyboardShortcuts() {
- return {
- Backspace: () => this.editor.commands.command(({ tr, state }) => {
- let isReference = false
- const { selection } = state
- const { empty, anchor } = selection
-
- if (!empty) {
- return false
- }
-
- state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {
- if (node.type.name === this.name) {
- isReference = true
- tr.insertText(this.options.suggestion.char || '', pos, pos + node.nodeSize)
-
- return false
- }
- })
-
- return isReference
- }),
- }
- },
+ // addKeyboardShortcuts() {
+ // return {
+ // Backspace: () => this.editor.commands.command(({ tr, state }) => {
+ // let isReference = false
+ // const { selection } = state
+ // const { empty, anchor } = selection
+
+ // if (!empty) {
+ // return false
+ // }
+
+ // state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {
+ // if (node.type.name === this.name) {
+ // isReference = true
+ // tr.insertText(this.options.suggestion.char || '', pos, pos + node.nodeSize)
+
+ // return false
+ // }
+ // })
+
+ // return isReference
+ // }),
+ // }
+ // },
})
diff --git a/editor/schema.ts b/editor/schema.ts
index c866447a6..534bee624 100644
--- a/editor/schema.ts
+++ b/editor/schema.ts
@@ -12,6 +12,7 @@ import ReferenceExtension, {
REFERENCE_VALUE_ATTRIBUTE_NAME,
} from 'editor/extensions/reference'
import AutocompleteExtension from 'editor/extensions/autocomplete'
+import CustomKeymap from 'editor/extensions/keymap'
export enum ReferenceType {
NPMPackage = 'NPM_PACKAGE',
@@ -59,12 +60,13 @@ export const extensions = [
OrderedList,
ListItem,
BulletList,
+ ReferenceExtension,
AutocompleteExtension.configure({
suggestion: {
items: (query) => searchEngine.search(query).map(q => q.item),
},
}),
- ReferenceExtension,
+ CustomKeymap,
]
const schema = getSchema(extensions)
diff --git a/hooks/useDocEditor.ts b/hooks/useDocEditor.ts
index 969201cde..7fb219368 100644
--- a/hooks/useDocEditor.ts
+++ b/hooks/useDocEditor.ts
@@ -1,10 +1,6 @@
-import {
- useEffect,
- useState,
-} from 'react'
-import { Editor } from '@tiptap/react'
+import { useEditor } from '@tiptap/react'
import Placeholder from '@tiptap/extension-placeholder'
-import { keymap } from 'prosemirror-keymap'
+
import { extensions } from 'editor/schema'
function useDocEditor({
@@ -16,57 +12,36 @@ function useDocEditor({
onContentChange: (content: string) => void,
placeholder?: string
}) {
- const [editor, setEditor] = useState<Editor | null>(null)
-
- useEffect(function initialize() {
- const instance = new Editor({
- content: initialContent,
- parseOptions: {
- preserveWhitespace: 'full',
+ const editor = useEditor({
+ content: initialContent,
+ // parseOptions: {
+ // preserveWhitespace: 'full',
+ // },
+ // injectCSS: false,
+ editorProps: {
+ attributes: {
+ 'data-gramm': 'false',
+ 'spellcheck': 'false',
},
- injectCSS: false,
- editorProps: {
- attributes: {
- 'data-gramm': 'false',
- 'spellcheck': 'false',
- },
- },
- extensions: [
- ...extensions,
- Placeholder.configure({
- placeholder: ({ editor }) => {
- if (!editor.getText()) {
- return placeholder || ''
- }
- return ''
+ },
+ extensions: [
+ ...extensions,
+ Placeholder.configure({
+ placeholder: ({ editor }) => {
+ if (!editor.getText()) {
+ return placeholder || ''
}
- }),
- ],
- })
-
- // Override default browser Ctrl/Cmd+S shortcut.
- instance.registerPlugin(keymap({
- 'Mod-s': function () {
- return true
- },
- }))
-
- instance.on('transaction', t => {
- if (t.transaction.docChanged) {
- onContentChange(t.editor.getHTML())
+ return ''
+ }
+ }),
+ ],
+
+ onUpdate({ transaction, editor }) {
+ if (transaction.docChanged) {
+ onContentChange(editor.getHTML())
}
- })
-
- setEditor(instance)
-
- return () => {
- instance.destroy()
- setEditor(null)
- }
- }, [
- onContentChange,
- // We don't want the initialContent in the dependencies because that would reload the editor when we type.
- ])
+ },
+ }, [onContentChange])
return editor
}