Elffuss BraidHub

kikocis/continue public

d04f16ee25ad · 4.9 KiBRawPermalink
1import { mergeAttributes, Node } from "@tiptap/core";
2import { PluginKey } from "@tiptap/pm/state";
3import Suggestion from "@tiptap/suggestion";
4import { MentionOptions } from "./types";
5
6export const Mention = Node.create<MentionOptions>({
7 name: "mention",
8
9 addOptions() {
10 return {
11 HTMLAttributes: {
12 class: "mention",
13 },
14
15 renderHTML({ options, node }) {
16 return [
17 "span",
18 this.HTMLAttributes,
19 `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`,
20 ];
21 },
22 suggestion: {
23 char: "@",
24 pluginKey: new PluginKey(this.name),
25 command: ({ editor, range, props }) => {
26 // increase range.to by one when the next node is of type "text"
27 // and starts with a space character
28 const nodeAfter = editor.view.state.selection.$to.nodeAfter;
29 const overrideSpace = nodeAfter?.text?.startsWith(" ");
30
31 if (overrideSpace) {
32 range.to += 1;
33 }
34
35 editor
36 .chain()
37 .focus()
38 .insertContentAt(range, [
39 {
40 type: this.name,
41 attrs: props,
42 },
43 {
44 type: "text",
45 text: " ",
46 },
47 ])
48 .run();
49
50 window.getSelection()?.collapseToEnd();
51 },
52 allow: ({ state, range }) => {
53 const $from = state.doc.resolve(range.from);
54 const type = state.schema.nodes[this.name];
55 const allow = !!$from.parent.type.contentMatch.matchType(type);
56
57 // Check if there's a space after the "@"
58 const textFrom = range.from;
59 const textTo = state.selection.$to.pos;
60 const text = state.doc.textBetween(textFrom, textTo);
61 const hasSpace = text.includes(" ");
62
63 return allow && !hasSpace;
64 },
65 },
66 };
67 },
68
69 group: "inline",
70
71 inline: true,
72
73 selectable: false,
74
75 atom: true,
76
77 addAttributes() {
78 return {
79 id: {
80 default: null,
81 parseHTML: (element) => element.getAttribute("data-id"),
82 renderHTML: (attributes) => {
83 if (!attributes.id) {
84 return {};
85 }
86
87 return {
88 "data-id": attributes.id,
89 };
90 },
91 },
92
93 label: {
94 default: null,
95 parseHTML: (element) => element.getAttribute("data-label"),
96 renderHTML: (attributes) => {
97 if (!attributes.label) {
98 return {};
99 }
100
101 return {
102 "data-label": attributes.label,
103 };
104 },
105 },
106
107 renderInlineAs: {
108 default: null,
109 parseHTML: (element) => element.getAttribute("data-renderInlineAs"),
110 renderHTML: (attributes) => {
111 if (typeof attributes.renderInlineAs !== "string") {
112 return {};
113 }
114
115 return {
116 "data-renderInlineAs": attributes.renderInlineAs,
117 };
118 },
119 },
120
121 query: {
122 default: null,
123 parseHTML: (element) => element.getAttribute("data-query"),
124 renderHTML: (attributes) => {
125 if (!attributes.query) {
126 return {};
127 }
128
129 return {
130 "data-query": attributes.query,
131 };
132 },
133 },
134
135 itemType: {
136 default: null,
137 parseHTML: (element) => element.getAttribute("data-itemType"),
138 renderHTML: (attributes) => {
139 if (!attributes.itemType) {
140 return {};
141 }
142
143 return {
144 "data-itemType": attributes.itemType,
145 };
146 },
147 },
148 };
149 },
150
151 parseHTML() {
152 return [
153 {
154 tag: `span[data-type="${this.name}"]`,
155 },
156 ];
157 },
158
159 renderHTML({ node, HTMLAttributes }) {
160 const html = this.options.renderHTML({
161 options: this.options,
162 node,
163 });
164
165 if (typeof html === "string") {
166 return [
167 "span",
168 mergeAttributes(
169 { "data-type": this.name },
170 this.options.HTMLAttributes,
171 HTMLAttributes,
172 ),
173 html,
174 ];
175 }
176 return html;
177 },
178
179 addKeyboardShortcuts() {
180 return {
181 Backspace: () =>
182 this.editor.commands.command(({ tr, state }) => {
183 let isMention = false;
184 const { selection } = state;
185 const { empty, anchor } = selection;
186
187 if (!empty) {
188 return false;
189 }
190
191 state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {
192 if (node.type.name === this.name) {
193 isMention = true;
194 tr.insertText(
195 this.options.suggestion.char || "",
196 pos,
197 pos + node.nodeSize,
198 );
199
200 return false;
201 }
202 });
203
204 return isMention;
205 }),
206 };
207 },
208
209 addProseMirrorPlugins() {
210 return [
211 Suggestion({
212 editor: this.editor,
213 ...this.options.suggestion,
214 }),
215 ];
216 },
217});