forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockNoteExtensions.ts
More file actions
283 lines (254 loc) · 9.55 KB
/
Copy pathBlockNoteExtensions.ts
File metadata and controls
283 lines (254 loc) · 9.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import { AnyExtension, Extension, extensions } from "@tiptap/core";
import type { BlockNoteEditor, BlockNoteExtension } from "./BlockNoteEditor.js";
import Collaboration from "@tiptap/extension-collaboration";
import CollaborationCursor from "@tiptap/extension-collaboration-cursor";
import { Gapcursor } from "@tiptap/extension-gapcursor";
import { HardBreak } from "@tiptap/extension-hard-break";
import { History } from "@tiptap/extension-history";
import { Link } from "@tiptap/extension-link";
import { Text } from "@tiptap/extension-text";
import { Plugin } from "prosemirror-state";
import * as Y from "yjs";
import { createDropFileExtension } from "../api/clipboard/fromClipboard/fileDropExtension.js";
import { createPasteFromClipboardExtension } from "../api/clipboard/fromClipboard/pasteExtension.js";
import { createCopyToClipboardExtension } from "../api/clipboard/toClipboard/copyExtension.js";
import { BackgroundColorExtension } from "../extensions/BackgroundColor/BackgroundColorExtension.js";
import { FilePanelProsemirrorPlugin } from "../extensions/FilePanel/FilePanelPlugin.js";
import { FormattingToolbarProsemirrorPlugin } from "../extensions/FormattingToolbar/FormattingToolbarPlugin.js";
import { KeyboardShortcutsExtension } from "../extensions/KeyboardShortcuts/KeyboardShortcutsExtension.js";
import { LinkToolbarProsemirrorPlugin } from "../extensions/LinkToolbar/LinkToolbarPlugin.js";
import {
DEFAULT_LINK_PROTOCOL,
VALID_LINK_PROTOCOLS,
} from "../extensions/LinkToolbar/protocols.js";
import { NodeSelectionKeyboardPlugin } from "../extensions/NodeSelectionKeyboard/NodeSelectionKeyboardPlugin.js";
import { PlaceholderPlugin } from "../extensions/Placeholder/PlaceholderPlugin.js";
import { PreviousBlockTypePlugin } from "../extensions/PreviousBlockType/PreviousBlockTypePlugin.js";
import { SideMenuProsemirrorPlugin } from "../extensions/SideMenu/SideMenuPlugin.js";
import { SuggestionMenuProseMirrorPlugin } from "../extensions/SuggestionMenu/SuggestionPlugin.js";
import { TableHandlesProsemirrorPlugin } from "../extensions/TableHandles/TableHandlesPlugin.js";
import { TextAlignmentExtension } from "../extensions/TextAlignment/TextAlignmentExtension.js";
import { TextColorExtension } from "../extensions/TextColor/TextColorExtension.js";
import { TrailingNode } from "../extensions/TrailingNode/TrailingNodeExtension.js";
import UniqueID from "../extensions/UniqueID/UniqueID.js";
import { BlockContainer, BlockGroup, Doc } from "../pm-nodes/index.js";
import {
BlockNoteDOMAttributes,
BlockSchema,
BlockSpecs,
InlineContentSchema,
InlineContentSpecs,
StyleSchema,
StyleSpecs,
} from "../schema/index.js";
type ExtensionOptions<
BSchema extends BlockSchema,
I extends InlineContentSchema,
S extends StyleSchema
> = {
editor: BlockNoteEditor<BSchema, I, S>;
domAttributes: Partial<BlockNoteDOMAttributes>;
blockSpecs: BlockSpecs;
inlineContentSpecs: InlineContentSpecs;
styleSpecs: StyleSpecs;
trailingBlock: boolean | undefined;
collaboration?: {
fragment: Y.XmlFragment;
user: {
name: string;
color: string;
[key: string]: string;
};
provider: any;
renderCursor?: (user: any) => HTMLElement;
};
disableExtensions: string[] | undefined;
setIdAttribute?: boolean;
animations: boolean;
tableHandles: boolean;
dropCursor: (opts: any) => Plugin;
placeholders: Record<string | "default", string>;
tabBehavior?: "prefer-navigate-ui" | "prefer-indent";
};
/**
* Get all the Tiptap extensions BlockNote is configured with by default
*/
export const getBlockNoteExtensions = <
BSchema extends BlockSchema,
I extends InlineContentSchema,
S extends StyleSchema
>(
opts: ExtensionOptions<BSchema, I, S>
) => {
const ret: Record<string, BlockNoteExtension> = {};
const tiptapExtensions = getTipTapExtensions(opts);
for (const ext of tiptapExtensions) {
ret[ext.name] = ext;
}
// Note: this is pretty hardcoded and will break when user provides plugins with same keys.
// Define name on plugins instead and not make this a map?
ret["formattingToolbar"] = new FormattingToolbarProsemirrorPlugin(
opts.editor
);
ret["linkToolbar"] = new LinkToolbarProsemirrorPlugin(opts.editor);
ret["sideMenu"] = new SideMenuProsemirrorPlugin(opts.editor);
ret["suggestionMenus"] = new SuggestionMenuProseMirrorPlugin(opts.editor);
ret["filePanel"] = new FilePanelProsemirrorPlugin(opts.editor as any);
ret["placeholder"] = new PlaceholderPlugin(opts.editor, opts.placeholders);
if (opts.animations ?? true) {
ret["animations"] = new PreviousBlockTypePlugin();
}
if (opts.tableHandles) {
ret["tableHandles"] = new TableHandlesProsemirrorPlugin(opts.editor as any);
}
ret["dropCursor"] = {
plugin: opts.dropCursor({
width: 5,
color: "#ddeeff",
editor: opts.editor,
}),
};
ret["nodeSelectionKeyboard"] = new NodeSelectionKeyboardPlugin();
const disableExtensions: string[] = opts.disableExtensions || [];
for (const ext of disableExtensions) {
delete ret[ext];
}
return ret;
};
/**
* Get all the Tiptap extensions BlockNote is configured with by default
*/
const getTipTapExtensions = <
BSchema extends BlockSchema,
I extends InlineContentSchema,
S extends StyleSchema
>(
opts: ExtensionOptions<BSchema, I, S>
) => {
const tiptapExtensions: AnyExtension[] = [
extensions.ClipboardTextSerializer,
extensions.Commands,
extensions.Editable,
extensions.FocusEvents,
extensions.Tabindex,
// DevTools,
Gapcursor,
// DropCursor,
UniqueID.configure({
// everything from bnBlock group (nodes that represent a BlockNote block should have an id)
types: ["blockContainer", "columnList", "column"],
setIdAttribute: opts.setIdAttribute,
}),
HardBreak.extend({ priority: 10 }),
// Comments,
// basics:
Text,
// marks:
Link.extend({
inclusive: false,
}).configure({
defaultProtocol: DEFAULT_LINK_PROTOCOL,
protocols: VALID_LINK_PROTOCOLS,
}),
...Object.values(opts.styleSpecs).map((styleSpec) => {
return styleSpec.implementation.mark;
}),
TextColorExtension,
BackgroundColorExtension,
TextAlignmentExtension,
// make sure escape blurs editor, so that we can tab to other elements in the host page (accessibility)
Extension.create({
name: "OverrideEscape",
addKeyboardShortcuts() {
return {
Escape: () => {
if (opts.editor.suggestionMenus.shown) {
// escape is handled by suggestionmenu
return false;
}
return this.editor.commands.blur();
},
};
},
}),
// nodes
Doc,
BlockContainer.configure({
editor: opts.editor,
domAttributes: opts.domAttributes,
}),
KeyboardShortcutsExtension.configure({
editor: opts.editor,
tabBehavior: opts.tabBehavior,
}),
BlockGroup.configure({
domAttributes: opts.domAttributes,
}),
...Object.values(opts.inlineContentSpecs)
.filter((a) => a.config !== "link" && a.config !== "text")
.map((inlineContentSpec) => {
return inlineContentSpec.implementation!.node.configure({
editor: opts.editor as any,
});
}),
...Object.values(opts.blockSpecs).flatMap((blockSpec) => {
return [
// dependent nodes (e.g.: tablecell / row)
...(blockSpec.implementation.requiredExtensions || []).map((ext) =>
ext.configure({
editor: opts.editor,
domAttributes: opts.domAttributes,
})
),
// the actual node itself
blockSpec.implementation.node.configure({
editor: opts.editor,
domAttributes: opts.domAttributes,
}),
];
}),
createCopyToClipboardExtension(opts.editor),
createPasteFromClipboardExtension(opts.editor),
createDropFileExtension(opts.editor),
// This needs to be at the bottom of this list, because Key events (such as enter, when selecting a /command),
// should be handled before Enter handlers in other components like splitListItem
...(opts.trailingBlock === undefined || opts.trailingBlock
? [TrailingNode]
: []),
];
if (opts.collaboration) {
tiptapExtensions.push(
Collaboration.configure({
fragment: opts.collaboration.fragment,
})
);
if (opts.collaboration.provider?.awareness) {
const defaultRender = (user: { color: string; name: string }) => {
const cursor = document.createElement("span");
cursor.classList.add("collaboration-cursor__caret");
cursor.setAttribute("style", `border-color: ${user.color}`);
const label = document.createElement("span");
label.classList.add("collaboration-cursor__label");
label.setAttribute("style", `background-color: ${user.color}`);
label.insertBefore(document.createTextNode(user.name), null);
const nonbreakingSpace1 = document.createTextNode("\u2060");
const nonbreakingSpace2 = document.createTextNode("\u2060");
cursor.insertBefore(nonbreakingSpace1, null);
cursor.insertBefore(label, null);
cursor.insertBefore(nonbreakingSpace2, null);
return cursor;
};
tiptapExtensions.push(
CollaborationCursor.configure({
user: opts.collaboration.user,
render: opts.collaboration.renderCursor || defaultRender,
provider: opts.collaboration.provider,
})
);
}
} else {
// disable history extension when collaboration is enabled as Yjs takes care of undo / redo
tiptapExtensions.push(History);
}
return tiptapExtensions;
};