diff --git a/components/editor/intelli/quick-extension.js b/components/editor/intelli/quick-extension.js index c57a1c5..5df01dd 100644 --- a/components/editor/intelli/quick-extension.js +++ b/components/editor/intelli/quick-extension.js @@ -1,24 +1,58 @@ -import { Extension, ReactRenderer } from '@tiptap/react' +import { Node, ReactRenderer } from '@tiptap/react' import tippy from 'tippy.js' import { Plugin, PluginKey } from '@tiptap/pm/state' import { QuickView } from './quick-view' +import { Suggestion } from '@tiptap/suggestion' + +const extensionName = 'quick-command' -const extensionName = 'ai-quick-command' export const createQuickExtension = () => { - return Extension.create({ + return Node.create({ name: extensionName, addOptions () { return { char: 'Mod-/', - pluginKey: extensionName, + pluginKey: 'quick-extension', } }, + // addKeyboardShortcuts () { + // return { + // 'Mod-/': () => { + // this.editor.commands.command(({ tr, state, dispatch }) => { + // dispatch(tr.setMeta(pluginKey, false)) + // return true + // }) + // }, + // } + // }, addProseMirrorPlugins () { - let plugin = new Plugin({ + let plugin = Suggestion({ editor: this.editor, - char: 'Mod-/', - ...this.options, + char: this.options.char, + pluginKey: new PluginKey(extensionName), + + command: ({ editor, props }) => { + const { state, dispatch } = editor.view + const { $head, $from } = state.selection + + const end = $from.pos + const from = $head?.nodeBefore?.text + ? end - + $head.nodeBefore.text.substring( + $head.nodeBefore.text.indexOf('/') + ).length + : $from.start() + + const tr = state.tr.deleteRange(from, end) + dispatch(tr) + props?.action?.(editor, props.user) + editor?.view?.focus() + }, + items: ({ query }) => { + // todo: match fo query + return [{}] + }, render: () => { let component @@ -27,6 +61,7 @@ export const createQuickExtension = () => { return { onStart: (props) => { + console.log(props) isEditable = props.editor.isEditable if (!isEditable) return diff --git a/components/editor/intelli/slash-extension.js b/components/editor/intelli/slash-extension.js index 90497b3..13b4a1e 100644 --- a/components/editor/intelli/slash-extension.js +++ b/components/editor/intelli/slash-extension.js @@ -3,6 +3,7 @@ import { Node } from '@tiptap/core' import { Suggestion } from '@tiptap/suggestion' import tippy from 'tippy.js' import SlashView from './slash-view' +import { PluginKey } from '@tiptap/pm/state' export const createSlashExtension = (name, options) => { const extensionName = `ai-insert` @@ -12,7 +13,7 @@ export const createSlashExtension = (name, options) => { addOptions () { return { char: '/', - pluginKey: 'slash-/', + pluginKey: 'slash', } }, addProseMirrorPlugins () { @@ -20,6 +21,7 @@ export const createSlashExtension = (name, options) => { Suggestion({ editor: this.editor, char: this.options.char, + pluginKey: new PluginKey(this.options.pluginKey), command: ({ editor, props }) => { const { state, dispatch } = editor.view diff --git a/components/editor/intelli/slash-view.jsx b/components/editor/intelli/slash-view.jsx index b981684..97db004 100644 --- a/components/editor/intelli/slash-view.jsx +++ b/components/editor/intelli/slash-view.jsx @@ -84,17 +84,17 @@ class SlashView extends React.Component { const { selectedIndex } = this.state return ( -