|
| 1 | +import { Node, NodeViewWrapper, ReactNodeViewRenderer } from "@tiptap/react"; |
| 2 | +import { Editor, mergeAttributes } from "@tiptap/core"; |
| 3 | +import AiBlockView from "@/components/editor/intelli/ai-block-view"; |
| 4 | +import React, { useEffect, useRef } from "react"; |
| 5 | +import { AiBlockEditor } from "@/components/editor/intelli/ai-block-editor"; |
| 6 | + |
| 7 | +declare module '@tiptap/core' { |
| 8 | + interface Commands<ReturnType> { |
| 9 | + commands: { |
| 10 | + triggerInlineCompletion: () => ReturnType, |
| 11 | + completeInlineCompletion: () => ReturnType, |
| 12 | + cancelInlineCompletion: () => ReturnType, |
| 13 | + } |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +export const createInlineCompletion = () => { |
| 18 | + const extensionName = "inline-completion"; |
| 19 | + |
| 20 | + return Node.create({ |
| 21 | + name: extensionName, |
| 22 | + group: "block", |
| 23 | + defining: true, |
| 24 | + content: "text*", |
| 25 | + addOptions() { |
| 26 | + return { |
| 27 | + HTMLAttributes: { |
| 28 | + class: "inline-completion", |
| 29 | + }, |
| 30 | + } |
| 31 | + }, |
| 32 | + addKeyboardShortcuts() { |
| 33 | + return { |
| 34 | + "Mod-\\": (): boolean => { |
| 35 | + this.editor.commands.triggerInlineCompletion() |
| 36 | + return true |
| 37 | + }, |
| 38 | + "Tab": (): boolean => { |
| 39 | + this.editor.commands.completeInlineCompletion() |
| 40 | + return true |
| 41 | + }, |
| 42 | + "`": (): boolean => { |
| 43 | + this.editor.commands.completeInlineCompletion() |
| 44 | + return true |
| 45 | + }, |
| 46 | + Escape: (): boolean => { |
| 47 | + this.editor.commands.cancelInlineCompletion(); |
| 48 | + return true |
| 49 | + }, |
| 50 | + } |
| 51 | + }, |
| 52 | + // @ts-ignore |
| 53 | + addCommands() { |
| 54 | + return { |
| 55 | + triggerInlineCompletion: (options: any) => ({ commands }: { commands: any }) => { |
| 56 | + return commands.insertContent({ |
| 57 | + type: this.name, |
| 58 | + attrs: options, |
| 59 | + }) |
| 60 | + }, |
| 61 | + completeInlineCompletion: (options: any) => ({ commands }: { commands: any }) => { |
| 62 | + const pos = this.editor.view.state.selection.$anchor.pos; |
| 63 | + commands.deleteNode(this.name) |
| 64 | + commands.insertContentAt(pos, "done completion") |
| 65 | + }, |
| 66 | + cancelInlineCompletion: (options: any) => ({ commands }: { commands: any }) => { |
| 67 | + commands.deleteNode(this.name) |
| 68 | + } |
| 69 | + } |
| 70 | + }, |
| 71 | + addNodeView() { |
| 72 | + return ReactNodeViewRenderer(InlineCompletionView); |
| 73 | + }, |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +const InlineCompletionView = (props?: { editor: Editor }) => { |
| 78 | + const $container = useRef(); |
| 79 | + |
| 80 | + // handle for esc |
| 81 | + useEffect(() => { |
| 82 | + const handleKeyDown = (event: KeyboardEvent) => { |
| 83 | + if (event.key === "Escape") { |
| 84 | + props?.editor?.commands.cancelInlineCompletion(); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + document.addEventListener("keydown", handleKeyDown); |
| 89 | + |
| 90 | + return () => { |
| 91 | + document.removeEventListener("keydown", handleKeyDown); |
| 92 | + }; |
| 93 | + }, []); |
| 94 | + |
| 95 | + return ( |
| 96 | + <NodeViewWrapper ref={$container}> |
| 97 | + <p>show text for completion <span className={'inline-completion-tip'}>`</span></p> |
| 98 | + </NodeViewWrapper> |
| 99 | + ); |
| 100 | +}; |
| 101 | + |
0 commit comments