Skip to content

Commit 5356e07

Browse files
committed
feat: add simple handle for inline completion sample
1 parent bd79739 commit 5356e07

3 files changed

Lines changed: 115 additions & 81 deletions

File tree

‎src/components/editor/inline/inline-completion.ts‎

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+

‎styles/global.css‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ input {
735735
}
736736

737737
.character-count {
738-
/* shoulde display in right with flex*/
738+
/* shoulde display in right with flex*/
739739
display: flex;
740740
justify-content: flex-end;
741741
align-items: center;
@@ -784,3 +784,16 @@ input {
784784
color: #fff;
785785
cursor: not-allowed;
786786
}
787+
788+
.inline-completion {
789+
background-color: #9ca3af;
790+
}
791+
792+
.inline-completion-tip {
793+
margin-left: 1em;
794+
background: #ccc;
795+
font-weight: 500;
796+
border-radius: 0.2em;
797+
line-height: 0.6em;
798+
padding: 0 0.4em 0 0.3em;
799+
}

0 commit comments

Comments
 (0)