@blocknote/core 0.52.1 → 0.54.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/BlockNoteExtension-BJCLnwrE.cjs +2 -0
- package/dist/BlockNoteExtension-BJCLnwrE.cjs.map +1 -0
- package/dist/BlockNoteExtension-CbvDA0F7.js +80 -0
- package/dist/BlockNoteExtension-CbvDA0F7.js.map +1 -0
- package/dist/{UserStore-QbbPFj0z.js → UserStore-C4TzkGEn.js} +3 -3
- package/dist/{UserStore-DCG6lsla.cjs.map → UserStore-C4TzkGEn.js.map} +1 -1
- package/dist/{UserStore-DCG6lsla.cjs → UserStore-mj3fXW4D.cjs} +2 -2
- package/dist/UserStore-mj3fXW4D.cjs.map +1 -0
- package/dist/blocknote.cjs +1 -1
- package/dist/blocknote.js +6 -6
- package/dist/{blocks-CiuUGa1E.js → blocks-CzQLehlc.js} +695 -652
- package/dist/blocks-CzQLehlc.js.map +1 -0
- package/dist/blocks-CzkqclGj.cjs +12 -0
- package/dist/blocks-CzkqclGj.cjs.map +1 -0
- package/dist/blocks.cjs +1 -1
- package/dist/blocks.js +2 -2
- package/dist/comments.cjs +1 -1
- package/dist/comments.cjs.map +1 -1
- package/dist/comments.js +7 -7
- package/dist/comments.js.map +1 -1
- package/dist/en-BuzSuuTq.cjs +2 -0
- package/dist/en-BuzSuuTq.cjs.map +1 -0
- package/dist/{en-CGKXfWma.js → en-uzHzj6eH.js} +10 -1
- package/dist/en-uzHzj6eH.js.map +1 -0
- package/dist/extensions-CSB9ZvuL.cjs +58 -0
- package/dist/extensions-CSB9ZvuL.cjs.map +1 -0
- package/dist/{extensions-CrUGDOks.js → extensions-DGuhtOgB.js} +717 -452
- package/dist/extensions-DGuhtOgB.js.map +1 -0
- package/dist/extensions.cjs +1 -1
- package/dist/extensions.js +4 -4
- package/dist/locales.cjs +1 -1
- package/dist/locales.cjs.map +1 -1
- package/dist/locales.js +199 -1
- package/dist/locales.js.map +1 -1
- package/dist/{src-DTF83Cl9.js → src-BkfZVfaO.js} +921 -835
- package/dist/src-BkfZVfaO.js.map +1 -0
- package/dist/src-DM3syMdf.cjs +23 -0
- package/dist/src-DM3syMdf.cjs.map +1 -0
- package/dist/style.css +1 -1
- package/dist/webpack-stats.json +1 -1
- package/dist/y.cjs +1 -1
- package/dist/y.cjs.map +1 -1
- package/dist/y.js +65 -62
- package/dist/y.js.map +1 -1
- package/dist/yjs.cjs +1 -1
- package/dist/yjs.cjs.map +1 -1
- package/dist/yjs.js +88 -76
- package/dist/yjs.js.map +1 -1
- package/package.json +29 -30
- package/src/api/blockManipulation/commands/updateBlock/__snapshots__/updateBlock.test.ts.snap +2505 -763
- package/src/api/blockManipulation/commands/updateBlock/updateBlock.test.ts +42 -0
- package/src/api/blockManipulation/commands/updateBlock/updateBlock.ts +16 -4
- package/src/api/clipboard/fromClipboard/handleFileInsertion.ts +10 -1
- package/src/api/exporters/html/util/serializeBlocksExternalHTML.ts +42 -8
- package/src/api/exporters/markdown/htmlToMarkdown.ts +45 -10
- package/src/api/exporters/markdown/markdownExporter.ts +10 -1
- package/src/api/nodeConversions/blockToNode.ts +5 -1
- package/src/api/nodeConversions/nodeToBlock.ts +3 -0
- package/src/api/parsers/html/parseHTML.ts +43 -0
- package/src/api/pmUtil.ts +16 -8
- package/src/blocks/Code/CodeBlockOptions.ts +83 -0
- package/src/blocks/Code/block.test.ts +1 -1
- package/src/blocks/Code/block.ts +29 -281
- package/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts +120 -0
- package/src/blocks/Code/helpers/parse/parsePreCode.ts +45 -0
- package/src/blocks/Code/helpers/render/createCodeBlock.ts +98 -0
- package/src/blocks/Code/helpers/toExternalHTML/createPreCode.ts +14 -0
- package/src/blocks/Table/TableExtension.test.ts +102 -0
- package/src/blocks/ToggleWrapper/createToggleWrapper.ts +16 -5
- package/src/blocks/defaultBlockTypeGuards.ts +6 -0
- package/src/blocks/index.ts +3 -0
- package/src/comments/extension.ts +2 -4
- package/src/editor/Block.css +324 -3
- package/src/editor/BlockNoteEditor.ts +25 -4
- package/src/editor/BlockNoteExtension.ts +1 -1
- package/src/editor/managers/ExtensionManager/extensions.ts +6 -0
- package/src/exporter/ExportImage.ts +50 -0
- package/src/exporter/Exporter.test.ts +48 -0
- package/src/exporter/Exporter.ts +46 -12
- package/src/exporter/index.ts +1 -0
- package/src/exporter/mapping.ts +4 -1
- package/src/extensions/FormattingToolbar/FormattingToolbar.ts +10 -8
- package/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.ts +109 -0
- package/src/extensions/SideMenu/SideMenu.ts +20 -3
- package/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.ts +192 -0
- package/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.ts +142 -0
- package/src/extensions/SuggestionMenu/SuggestionMenu.test.ts +118 -0
- package/src/extensions/SuggestionMenu/SuggestionMenu.ts +40 -27
- package/src/extensions/SuggestionMenu/getDefaultSlashMenuItems.ts +13 -3
- package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.ts +120 -0
- package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.ts +89 -0
- package/src/extensions/SyntaxHighlighting/inlineHighlight.test.ts +135 -0
- package/src/extensions/SyntaxHighlighting/shiki.ts +116 -0
- package/src/extensions/TableHandles/TableHandles.ts +25 -12
- package/src/extensions/TrailingNode/TrailingNode.ts +98 -32
- package/src/extensions/Versioning/inMemoryVersioning.ts +13 -3
- package/src/extensions/index.ts +4 -0
- package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.test.ts +51 -2
- package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +22 -2
- package/src/i18n/locales/ar.ts +9 -0
- package/src/i18n/locales/de.ts +9 -0
- package/src/i18n/locales/en.ts +9 -0
- package/src/i18n/locales/es.ts +9 -0
- package/src/i18n/locales/fa.ts +9 -0
- package/src/i18n/locales/fr.ts +9 -0
- package/src/i18n/locales/he.ts +9 -0
- package/src/i18n/locales/hr.ts +9 -0
- package/src/i18n/locales/is.ts +9 -0
- package/src/i18n/locales/it.ts +9 -0
- package/src/i18n/locales/ja.ts +9 -0
- package/src/i18n/locales/ko.ts +9 -0
- package/src/i18n/locales/nl.ts +9 -0
- package/src/i18n/locales/no.ts +9 -0
- package/src/i18n/locales/pl.ts +9 -0
- package/src/i18n/locales/pt.ts +9 -0
- package/src/i18n/locales/ru.ts +9 -0
- package/src/i18n/locales/sk.ts +9 -0
- package/src/i18n/locales/uk.ts +9 -0
- package/src/i18n/locales/uz.ts +9 -0
- package/src/i18n/locales/vi.ts +9 -0
- package/src/i18n/locales/zh-tw.ts +9 -0
- package/src/i18n/locales/zh.ts +9 -0
- package/src/index.ts +6 -1
- package/src/schema/blocks/createSpec.ts +17 -5
- package/src/schema/blocks/internal.test.ts +86 -0
- package/src/schema/blocks/internal.ts +64 -0
- package/src/schema/blocks/types.ts +53 -3
- package/src/schema/contentTypePropagation.test.ts +1 -1
- package/src/schema/inlineContent/createSpec.test.ts +161 -0
- package/src/schema/inlineContent/createSpec.ts +180 -13
- package/src/schema/inlineContent/internal.ts +21 -2
- package/src/schema/inlineContent/types.ts +35 -5
- package/src/schema/nodeViewMutations.test.ts +121 -0
- package/src/schema/nodeViewMutations.ts +46 -0
- package/src/user/UserStore.ts +1 -1
- package/src/util/Store.test.ts +132 -0
- package/src/util/Store.ts +135 -0
- package/src/y/extensions/AttributionExtension.test.ts +10 -0
- package/src/y/extensions/Versioning.test.ts +20 -7
- package/src/yjs/comments/RESTYjsThreadStore.test.ts +85 -0
- package/src/yjs/comments/RESTYjsThreadStore.ts +7 -1
- package/src/yjs/extensions/ForkYDoc.test.ts +37 -0
- package/src/yjs/extensions/ForkYDoc.ts +26 -1
- package/src/yjs/extensions/RelativePositionMapping.ts +8 -2
- package/src/yjs/extensions/Versioning.test.ts +17 -5
- package/types/src/api/exporters/html/util/serializeBlocksExternalHTML.d.ts +13 -0
- package/types/src/api/pmUtil.d.ts +7 -7
- package/types/src/blocks/Code/CodeBlockOptions.d.ts +61 -0
- package/types/src/blocks/Code/block.d.ts +3 -53
- package/types/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.d.ts +35 -0
- package/types/src/blocks/Code/helpers/parse/parsePreCode.d.ts +8 -0
- package/types/src/blocks/Code/helpers/render/createCodeBlock.d.ts +18 -0
- package/types/src/blocks/Code/helpers/toExternalHTML/createPreCode.d.ts +5 -0
- package/types/src/blocks/defaultBlockTypeGuards.d.ts +2 -0
- package/types/src/blocks/index.d.ts +3 -0
- package/types/src/comments/extension.d.ts +1 -15
- package/types/src/editor/BlockNoteEditor.d.ts +3 -3
- package/types/src/editor/BlockNoteExtension.d.ts +1 -1
- package/types/src/exporter/ExportImage.d.ts +36 -0
- package/types/src/exporter/Exporter.d.ts +21 -0
- package/types/src/exporter/Exporter.test.d.ts +1 -0
- package/types/src/exporter/index.d.ts +1 -0
- package/types/src/exporter/mapping.d.ts +1 -1
- package/types/src/extensions/FilePanel/FilePanel.d.ts +1 -1
- package/types/src/extensions/FormattingToolbar/FormattingToolbar.d.ts +1 -1
- package/types/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.d.ts +5 -0
- package/types/src/extensions/ShowSelection/ShowSelection.d.ts +1 -5
- package/types/src/extensions/SideMenu/SideMenu.d.ts +6 -1
- package/types/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.d.ts +45 -0
- package/types/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.d.ts +49 -0
- package/types/src/extensions/SuggestionMenu/SuggestionMenu.d.ts +7 -11
- package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.d.ts +44 -0
- package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.d.ts +1 -0
- package/types/src/extensions/SyntaxHighlighting/inlineHighlight.test.d.ts +1 -0
- package/types/src/extensions/SyntaxHighlighting/shiki.d.ts +13 -0
- package/types/src/extensions/TableHandles/TableHandles.d.ts +1 -1
- package/types/src/extensions/TrailingNode/TrailingNode.d.ts +5 -4
- package/types/src/extensions/Versioning/Versioning.d.ts +1 -35
- package/types/src/extensions/index.d.ts +4 -0
- package/types/src/i18n/locales/en.d.ts +9 -0
- package/types/src/i18n/locales/fa.d.ts +9 -0
- package/types/src/i18n/locales/sk.d.ts +9 -0
- package/types/src/index.d.ts +3 -1
- package/types/src/schema/blocks/internal.d.ts +21 -0
- package/types/src/schema/blocks/internal.test.d.ts +1 -0
- package/types/src/schema/blocks/types.d.ts +36 -3
- package/types/src/schema/inlineContent/createSpec.d.ts +35 -3
- package/types/src/schema/inlineContent/createSpec.test.d.ts +1 -0
- package/types/src/schema/inlineContent/internal.d.ts +3 -2
- package/types/src/schema/inlineContent/types.d.ts +22 -3
- package/types/src/schema/nodeViewMutations.d.ts +3 -0
- package/types/src/schema/nodeViewMutations.test.d.ts +1 -0
- package/types/src/user/UserStore.d.ts +1 -1
- package/types/src/util/Store.d.ts +45 -0
- package/types/src/util/Store.test.d.ts +1 -0
- package/types/src/y/extensions/AttributionExtension.d.ts +1 -1
- package/types/src/y/extensions/DiffVersioningExtension.d.ts +1 -1
- package/types/src/y/extensions/ForkYDoc.d.ts +1 -5
- package/types/src/y/extensions/index.d.ts +2 -10
- package/types/src/yjs/comments/RESTYjsThreadStore.test.d.ts +1 -0
- package/types/src/yjs/extensions/ForkYDoc.d.ts +1 -5
- package/types/src/yjs/extensions/index.d.ts +1 -5
- package/dist/BlockNoteExtension-B_IEJHS8.js +0 -38
- package/dist/BlockNoteExtension-B_IEJHS8.js.map +0 -1
- package/dist/BlockNoteExtension-M0znTzxP.cjs +0 -2
- package/dist/BlockNoteExtension-M0znTzxP.cjs.map +0 -1
- package/dist/UserStore-QbbPFj0z.js.map +0 -1
- package/dist/blocks-BqHv0xhc.cjs +0 -12
- package/dist/blocks-BqHv0xhc.cjs.map +0 -1
- package/dist/blocks-CiuUGa1E.js.map +0 -1
- package/dist/en-C5Z4Ag9_.cjs +0 -2
- package/dist/en-C5Z4Ag9_.cjs.map +0 -1
- package/dist/en-CGKXfWma.js.map +0 -1
- package/dist/extensions-CrUGDOks.js.map +0 -1
- package/dist/extensions-Ghr9QqqR.cjs +0 -52
- package/dist/extensions-Ghr9QqqR.cjs.map +0 -1
- package/dist/src-DTF83Cl9.js.map +0 -1
- package/dist/src-OfbaVbMw.cjs +0 -21
- package/dist/src-OfbaVbMw.cjs.map +0 -1
- package/src/blocks/Code/shiki.ts +0 -73
- package/types/src/blocks/Code/shiki.d.ts +0 -4
|
@@ -140,6 +140,124 @@ describe("SuggestionMenu", () => {
|
|
|
140
140
|
editor._tiptapEditor.destroy();
|
|
141
141
|
});
|
|
142
142
|
|
|
143
|
+
it("should open a suggestion menu with a multi-character trigger", () => {
|
|
144
|
+
const editor = createEditor();
|
|
145
|
+
const sm = editor.getExtension(SuggestionMenu)!;
|
|
146
|
+
|
|
147
|
+
sm.addSuggestionMenu({ triggerCharacter: "img:" });
|
|
148
|
+
|
|
149
|
+
editor.replaceBlocks(editor.document, [
|
|
150
|
+
{
|
|
151
|
+
id: "paragraph-0",
|
|
152
|
+
type: "paragraph",
|
|
153
|
+
content: "img",
|
|
154
|
+
},
|
|
155
|
+
]);
|
|
156
|
+
|
|
157
|
+
editor.setTextCursorPosition("paragraph-0", "end");
|
|
158
|
+
|
|
159
|
+
expect(getSuggestionPluginState(editor)).toBeUndefined();
|
|
160
|
+
|
|
161
|
+
// Typing the final ":" completes the "img:" trigger.
|
|
162
|
+
const handled = simulateTextInput(editor, ":");
|
|
163
|
+
|
|
164
|
+
expect(handled).toBe(true);
|
|
165
|
+
|
|
166
|
+
const pluginState = getSuggestionPluginState(editor);
|
|
167
|
+
expect(pluginState).toBeDefined();
|
|
168
|
+
expect(pluginState.triggerCharacter).toBe("img:");
|
|
169
|
+
|
|
170
|
+
editor._tiptapEditor.destroy();
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("should match a multi-character trigger that is preceded by other text", () => {
|
|
174
|
+
const editor = createEditor();
|
|
175
|
+
const sm = editor.getExtension(SuggestionMenu)!;
|
|
176
|
+
|
|
177
|
+
sm.addSuggestionMenu({ triggerCharacter: "img:" });
|
|
178
|
+
|
|
179
|
+
editor.replaceBlocks(editor.document, [
|
|
180
|
+
{
|
|
181
|
+
id: "paragraph-0",
|
|
182
|
+
type: "paragraph",
|
|
183
|
+
content: "hello img",
|
|
184
|
+
},
|
|
185
|
+
]);
|
|
186
|
+
|
|
187
|
+
editor.setTextCursorPosition("paragraph-0", "end");
|
|
188
|
+
|
|
189
|
+
expect(getSuggestionPluginState(editor)).toBeUndefined();
|
|
190
|
+
|
|
191
|
+
const handled = simulateTextInput(editor, ":");
|
|
192
|
+
|
|
193
|
+
expect(handled).toBe(true);
|
|
194
|
+
|
|
195
|
+
const pluginState = getSuggestionPluginState(editor);
|
|
196
|
+
expect(pluginState).toBeDefined();
|
|
197
|
+
expect(pluginState.triggerCharacter).toBe("img:");
|
|
198
|
+
|
|
199
|
+
editor._tiptapEditor.destroy();
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("should prefer a longer trigger over a shorter one that would shadow it", () => {
|
|
203
|
+
const editor = createEditor();
|
|
204
|
+
const sm = editor.getExtension(SuggestionMenu)!;
|
|
205
|
+
|
|
206
|
+
// Register the shorter ":" trigger first so it would win in insertion
|
|
207
|
+
// order, then the longer "img:" trigger that ends in the same character.
|
|
208
|
+
sm.addSuggestionMenu({ triggerCharacter: ":" });
|
|
209
|
+
sm.addSuggestionMenu({ triggerCharacter: "img:" });
|
|
210
|
+
|
|
211
|
+
editor.replaceBlocks(editor.document, [
|
|
212
|
+
{
|
|
213
|
+
id: "paragraph-0",
|
|
214
|
+
type: "paragraph",
|
|
215
|
+
content: "img",
|
|
216
|
+
},
|
|
217
|
+
]);
|
|
218
|
+
|
|
219
|
+
editor.setTextCursorPosition("paragraph-0", "end");
|
|
220
|
+
|
|
221
|
+
const handled = simulateTextInput(editor, ":");
|
|
222
|
+
|
|
223
|
+
expect(handled).toBe(true);
|
|
224
|
+
|
|
225
|
+
const pluginState = getSuggestionPluginState(editor);
|
|
226
|
+
expect(pluginState).toBeDefined();
|
|
227
|
+
expect(pluginState.triggerCharacter).toBe("img:");
|
|
228
|
+
|
|
229
|
+
editor._tiptapEditor.destroy();
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("should fall back to the shorter trigger when the longer one does not match", () => {
|
|
233
|
+
const editor = createEditor();
|
|
234
|
+
const sm = editor.getExtension(SuggestionMenu)!;
|
|
235
|
+
|
|
236
|
+
sm.addSuggestionMenu({ triggerCharacter: ":" });
|
|
237
|
+
sm.addSuggestionMenu({ triggerCharacter: "img:" });
|
|
238
|
+
|
|
239
|
+
editor.replaceBlocks(editor.document, [
|
|
240
|
+
{
|
|
241
|
+
id: "paragraph-0",
|
|
242
|
+
type: "paragraph",
|
|
243
|
+
content: "hello",
|
|
244
|
+
},
|
|
245
|
+
]);
|
|
246
|
+
|
|
247
|
+
editor.setTextCursorPosition("paragraph-0", "end");
|
|
248
|
+
|
|
249
|
+
// "hello" + ":" doesn't complete "img:", so the bare ":" trigger wins.
|
|
250
|
+
const handled = simulateTextInput(editor, ":");
|
|
251
|
+
|
|
252
|
+
expect(handled).toBe(true);
|
|
253
|
+
|
|
254
|
+
const pluginState = getSuggestionPluginState(editor);
|
|
255
|
+
expect(pluginState).toBeDefined();
|
|
256
|
+
expect(pluginState.triggerCharacter).toBe(":");
|
|
257
|
+
|
|
258
|
+
editor._tiptapEditor.destroy();
|
|
259
|
+
});
|
|
260
|
+
|
|
143
261
|
it("should still allow suggestion menus without shouldTrigger in table content", () => {
|
|
144
262
|
const editor = createEditor();
|
|
145
263
|
const sm = editor.getExtension(SuggestionMenu)!;
|
|
@@ -150,6 +150,12 @@ type SuggestionPluginState =
|
|
|
150
150
|
| undefined;
|
|
151
151
|
|
|
152
152
|
export type SuggestionMenuOptions = {
|
|
153
|
+
/**
|
|
154
|
+
* The string that opens the suggestion menu when typed. Usually a single
|
|
155
|
+
* character (e.g. `"/"` or `"@"`), but multi-character strings such as
|
|
156
|
+
* `"img:"` are also supported. When multiple triggers could match the typed
|
|
157
|
+
* text, the longest one takes precedence.
|
|
158
|
+
*/
|
|
153
159
|
triggerCharacter: string;
|
|
154
160
|
/**
|
|
155
161
|
* Optional callback to determine whether the suggestion menu should be
|
|
@@ -332,34 +338,41 @@ export const SuggestionMenu = createExtension(({ editor }) => {
|
|
|
332
338
|
},
|
|
333
339
|
|
|
334
340
|
props: {
|
|
335
|
-
handleTextInput(view, from,
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
//
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
)
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
);
|
|
361
|
-
return true;
|
|
341
|
+
handleTextInput(view, from, _to, text) {
|
|
342
|
+
const doc = view.state.doc;
|
|
343
|
+
// Sort triggers by length (longest first) so that a more specific
|
|
344
|
+
// multi-character trigger (e.g. "img:") is preferred over a shorter
|
|
345
|
+
// one (e.g. ":") that would otherwise shadow it by matching first.
|
|
346
|
+
const triggers = [...suggestionMenus.entries()].sort(
|
|
347
|
+
([a], [b]) => b.length - a.length,
|
|
348
|
+
);
|
|
349
|
+
for (const [trigger, menuOptions] of triggers) {
|
|
350
|
+
const input =
|
|
351
|
+
doc.textBetween(
|
|
352
|
+
// If the cursor is at the very start of the document, and we are checking if
|
|
353
|
+
// an n-character trigger has been entered (e.g. "img:"), we need to get the n
|
|
354
|
+
// characters before the text cursor to compare do so. However, the text cursor
|
|
355
|
+
// may be near the start of the document and not have n characters before it.
|
|
356
|
+
Math.max(0, from - (trigger.length - text.length)),
|
|
357
|
+
from,
|
|
358
|
+
) + text;
|
|
359
|
+
if (trigger === input) {
|
|
360
|
+
// Check the per-suggestion-menu filter before activating.
|
|
361
|
+
if (
|
|
362
|
+
menuOptions.shouldOpen &&
|
|
363
|
+
!menuOptions.shouldOpen(view.state.tr)
|
|
364
|
+
) {
|
|
365
|
+
continue;
|
|
362
366
|
}
|
|
367
|
+
view.dispatch(view.state.tr.insertText(text));
|
|
368
|
+
view.dispatch(
|
|
369
|
+
view.state.tr
|
|
370
|
+
.setMeta(suggestionMenuPluginKey, {
|
|
371
|
+
triggerCharacter: input,
|
|
372
|
+
})
|
|
373
|
+
.scrollIntoView(),
|
|
374
|
+
);
|
|
375
|
+
return true;
|
|
363
376
|
}
|
|
364
377
|
}
|
|
365
378
|
return false;
|
|
@@ -14,9 +14,9 @@ import { DefaultSuggestionItem } from "./DefaultSuggestionItem.js";
|
|
|
14
14
|
import { SuggestionMenu } from "./SuggestionMenu.js";
|
|
15
15
|
|
|
16
16
|
// Sets the editor's text cursor position to the next content editable block,
|
|
17
|
-
// so either a block with inline content or a table.
|
|
18
|
-
//
|
|
19
|
-
//
|
|
17
|
+
// so either a block with inline content or a table. If no such block exists
|
|
18
|
+
// after the current one, an empty paragraph is appended to the end of the
|
|
19
|
+
// document and the cursor is moved to it.
|
|
20
20
|
function setSelectionToNextContentEditableBlock<
|
|
21
21
|
BSchema extends BlockSchema,
|
|
22
22
|
I extends InlineContentSchema,
|
|
@@ -29,6 +29,16 @@ function setSelectionToNextContentEditableBlock<
|
|
|
29
29
|
while (contentType === "none") {
|
|
30
30
|
block = editor.getTextCursorPosition().nextBlock;
|
|
31
31
|
if (block === undefined) {
|
|
32
|
+
// No content editable block exists after the current one, so we append
|
|
33
|
+
// an empty paragraph to the end of the document and move the cursor to
|
|
34
|
+
// it.
|
|
35
|
+
const lastBlock = editor.document[editor.document.length - 1];
|
|
36
|
+
const newBlock = editor.insertBlocks(
|
|
37
|
+
[{ type: "paragraph" }],
|
|
38
|
+
lastBlock,
|
|
39
|
+
"after",
|
|
40
|
+
)[0];
|
|
41
|
+
editor.setTextCursorPosition(newBlock, "end");
|
|
32
42
|
return;
|
|
33
43
|
}
|
|
34
44
|
contentType = editor.schema.blockSchema[block.type].content as
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { describe, expect, it } from "vite-plus/test";
|
|
2
|
+
import {
|
|
3
|
+
collectHighlightNodeTypes,
|
|
4
|
+
SyntaxHighlightingExtension,
|
|
5
|
+
} from "./SyntaxHighlighting.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @vitest-environment jsdom
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
describe("SyntaxHighlightingExtension", () => {
|
|
12
|
+
// The extension only reads `editor.schema.blockSpecs` and
|
|
13
|
+
// `inlineContentSpecs`, so a minimal stub is enough.
|
|
14
|
+
const fakeEditor = () =>
|
|
15
|
+
({
|
|
16
|
+
schema: {
|
|
17
|
+
blockSpecs: {
|
|
18
|
+
paragraph: { config: { type: "paragraph", content: "inline" } },
|
|
19
|
+
codeBlock: { config: { type: "codeBlock", content: "plain" } },
|
|
20
|
+
image: { config: { type: "image", content: "none" } },
|
|
21
|
+
},
|
|
22
|
+
inlineContentSpecs: {},
|
|
23
|
+
},
|
|
24
|
+
}) as any;
|
|
25
|
+
|
|
26
|
+
const pluginsFor = (options: any) =>
|
|
27
|
+
SyntaxHighlightingExtension(options)({ editor: fakeEditor() })
|
|
28
|
+
.prosemirrorPlugins;
|
|
29
|
+
|
|
30
|
+
// Whether highlighting is enabled at all is decided by the user (they choose
|
|
31
|
+
// to add this extension to the editor's `extensions`), so the extension
|
|
32
|
+
// itself always installs the plugin once created.
|
|
33
|
+
it("installs a highlight plugin when a highlighter is configured", () => {
|
|
34
|
+
const plugins = pluginsFor({ createHighlighter: async () => ({}) as any });
|
|
35
|
+
|
|
36
|
+
expect(plugins).toHaveLength(1);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("installs the plugin even without a highlighter (it no-ops at parse time)", () => {
|
|
40
|
+
expect(pluginsFor({})).toHaveLength(1);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe("collectHighlightNodeTypes", () => {
|
|
45
|
+
const highlight = () => "latex";
|
|
46
|
+
|
|
47
|
+
it("includes blocks with `content: plain` and a `meta.highlight`", () => {
|
|
48
|
+
const types = collectHighlightNodeTypes({
|
|
49
|
+
blockSpecs: {
|
|
50
|
+
// Highlightable: plain content + a highlight callback.
|
|
51
|
+
mathBlock: {
|
|
52
|
+
config: { type: "mathBlock", content: "plain" },
|
|
53
|
+
implementation: { meta: { highlight } },
|
|
54
|
+
},
|
|
55
|
+
// Not highlightable: no highlight callback.
|
|
56
|
+
paragraph: {
|
|
57
|
+
config: { type: "paragraph", content: "inline" },
|
|
58
|
+
implementation: { meta: {} },
|
|
59
|
+
},
|
|
60
|
+
// Not highlightable: `content: none` holds no editable text.
|
|
61
|
+
image: {
|
|
62
|
+
config: { type: "image", content: "none" },
|
|
63
|
+
implementation: { meta: { highlight } },
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
inlineContentSpecs: {},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
expect(types).toEqual(["mathBlock"]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("includes inline content with `content: plain` and a `meta.highlight`", () => {
|
|
73
|
+
const types = collectHighlightNodeTypes({
|
|
74
|
+
blockSpecs: {},
|
|
75
|
+
inlineContentSpecs: {
|
|
76
|
+
// Highlightable: plain (editable plain text) + a highlight callback.
|
|
77
|
+
math: {
|
|
78
|
+
config: { type: "math", content: "plain" },
|
|
79
|
+
implementation: { meta: { highlight } },
|
|
80
|
+
},
|
|
81
|
+
// Not highlightable: no highlight callback.
|
|
82
|
+
mention: {
|
|
83
|
+
config: { type: "mention", content: "plain" },
|
|
84
|
+
implementation: { meta: {} },
|
|
85
|
+
},
|
|
86
|
+
// Not highlightable: `content: none` holds no editable text.
|
|
87
|
+
tag: {
|
|
88
|
+
config: { type: "tag", content: "none" },
|
|
89
|
+
implementation: { meta: { highlight } },
|
|
90
|
+
},
|
|
91
|
+
// Built-in `text`/`link` specs have string configs, not objects.
|
|
92
|
+
text: { config: "text", implementation: undefined },
|
|
93
|
+
link: { config: "link", implementation: undefined },
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
expect(types).toEqual(["math"]);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("collects both block and inline-content highlight types together", () => {
|
|
101
|
+
const types = collectHighlightNodeTypes({
|
|
102
|
+
blockSpecs: {
|
|
103
|
+
mathBlock: {
|
|
104
|
+
config: { type: "mathBlock", content: "plain" },
|
|
105
|
+
implementation: { meta: { highlight } },
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
inlineContentSpecs: {
|
|
109
|
+
math: {
|
|
110
|
+
config: { type: "math", content: "plain" },
|
|
111
|
+
implementation: { meta: { highlight } },
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
expect(types).toContain("mathBlock");
|
|
117
|
+
expect(types).toContain("math");
|
|
118
|
+
expect(types).toHaveLength(2);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { HighlighterGeneric } from "@shikijs/types";
|
|
2
|
+
import {
|
|
3
|
+
createExtension,
|
|
4
|
+
ExtensionOptions,
|
|
5
|
+
} from "../../editor/BlockNoteExtension.js";
|
|
6
|
+
import { lazyShikiPlugin } from "./shiki.js";
|
|
7
|
+
import {
|
|
8
|
+
CustomInlineContentConfig,
|
|
9
|
+
InlineContentSpec,
|
|
10
|
+
LooseBlockSpec,
|
|
11
|
+
} from "../../schema/index.js";
|
|
12
|
+
|
|
13
|
+
export type SyntaxHighlightingOptions = {
|
|
14
|
+
/**
|
|
15
|
+
* Creates the Shiki highlighter used for syntax highlighting. Can be
|
|
16
|
+
* asynchronous, so the highlighter is only loaded once it's first needed.
|
|
17
|
+
*
|
|
18
|
+
* When omitted, content renders without syntax highlighting.
|
|
19
|
+
*/
|
|
20
|
+
createHighlighter: () => Promise<HighlighterGeneric<any, any>>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Collects the node type names that should be syntax-highlighted from a schema's
|
|
25
|
+
* block and inline-content specs.
|
|
26
|
+
*
|
|
27
|
+
* A spec is a candidate when it has a `meta.highlight` callback (which decides
|
|
28
|
+
* the language) AND the node actually holds editable text. Block and
|
|
29
|
+
* inline-content specs use different `content` value spaces, so "editable text"
|
|
30
|
+
* means `content === "plain"` for both blocks (code/math blocks) and inline
|
|
31
|
+
* content (inline math) - both hold plain text - hence the two are filtered
|
|
32
|
+
* separately.
|
|
33
|
+
*
|
|
34
|
+
* Inline content (e.g. inline math) is highlighted too: `prosemirror-highlight`
|
|
35
|
+
* collects nodes by `node.inlineContent` since v0.15.3
|
|
36
|
+
* (https://github.com/ocavue/prosemirror-highlight/pull/137), so inline nodes
|
|
37
|
+
* holding inline content are visited alongside text blocks.
|
|
38
|
+
*/
|
|
39
|
+
export function collectHighlightNodeTypes(schema: {
|
|
40
|
+
blockSpecs: Record<string, unknown>;
|
|
41
|
+
inlineContentSpecs: Record<string, unknown>;
|
|
42
|
+
}): string[] {
|
|
43
|
+
const blockNodeTypes = Object.values(schema.blockSpecs)
|
|
44
|
+
.filter(
|
|
45
|
+
(blockSpec): blockSpec is LooseBlockSpec =>
|
|
46
|
+
typeof (blockSpec as LooseBlockSpec)?.config === "object" &&
|
|
47
|
+
(blockSpec as LooseBlockSpec).config.content === "plain" &&
|
|
48
|
+
!!(blockSpec as LooseBlockSpec).implementation?.meta?.highlight,
|
|
49
|
+
)
|
|
50
|
+
.map((blockSpec) => blockSpec.config.type);
|
|
51
|
+
|
|
52
|
+
const inlineContentNodeTypes = Object.values(schema.inlineContentSpecs)
|
|
53
|
+
.filter(
|
|
54
|
+
(
|
|
55
|
+
inlineContentSpec,
|
|
56
|
+
): inlineContentSpec is InlineContentSpec<CustomInlineContentConfig> =>
|
|
57
|
+
typeof (
|
|
58
|
+
inlineContentSpec as InlineContentSpec<CustomInlineContentConfig>
|
|
59
|
+
)?.config === "object" &&
|
|
60
|
+
(inlineContentSpec as InlineContentSpec<CustomInlineContentConfig>)
|
|
61
|
+
.config.content === "plain" &&
|
|
62
|
+
!!(inlineContentSpec as InlineContentSpec<CustomInlineContentConfig>)
|
|
63
|
+
.implementation?.meta?.highlight,
|
|
64
|
+
)
|
|
65
|
+
.map((inlineContentSpec) => inlineContentSpec.config.type);
|
|
66
|
+
|
|
67
|
+
return [...blockNodeTypes, ...inlineContentNodeTypes];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A single editor-wide extension that syntax-highlights block and inline-content
|
|
72
|
+
* content. Which nodes get highlighted (and as which language) is decided by
|
|
73
|
+
* each spec's `meta.highlight` callback, so individual specs declare their own
|
|
74
|
+
* language rather than the extension configuring them.
|
|
75
|
+
*
|
|
76
|
+
* Highlighting is opt-in: the user adds this extension to the editor's
|
|
77
|
+
* `extensions` (configured with a `createHighlighter`) to enable it. When it's
|
|
78
|
+
* absent, content renders as plain text.
|
|
79
|
+
*/
|
|
80
|
+
export const SyntaxHighlightingExtension = createExtension(
|
|
81
|
+
({ editor, options }: ExtensionOptions<SyntaxHighlightingOptions>) => {
|
|
82
|
+
const nodeTypes = collectHighlightNodeTypes(editor.schema);
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
key: "syntaxHighlighting",
|
|
86
|
+
prosemirrorPlugins: [lazyShikiPlugin(options, nodeTypes, editor.schema)],
|
|
87
|
+
};
|
|
88
|
+
},
|
|
89
|
+
);
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { Schema } from "prosemirror-model";
|
|
2
|
+
import { EditorState, PluginKey } from "prosemirror-state";
|
|
3
|
+
import { Decoration } from "prosemirror-view";
|
|
4
|
+
import { createHighlightPlugin } from "prosemirror-highlight";
|
|
5
|
+
import { describe, expect, it } from "vite-plus/test";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @vitest-environment jsdom
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// Regression coverage for inline syntax highlighting. `prosemirror-highlight`
|
|
12
|
+
// used to collect only text-block nodes, so inline nodes (e.g. inline math)
|
|
13
|
+
// were never highlighted. Since v0.15.3 it collects nodes by
|
|
14
|
+
// `node.inlineContent` (https://github.com/ocavue/prosemirror-highlight/pull/137),
|
|
15
|
+
// so inline content with a `meta.highlight` is highlighted too. This suite
|
|
16
|
+
// guards that behavior. It drives the plugin directly against a hand-built
|
|
17
|
+
// ProseMirror doc, so it needs neither Shiki nor a browser.
|
|
18
|
+
describe("inline syntax highlighting", () => {
|
|
19
|
+
// A minimal schema with an *inline* node type that holds inline content
|
|
20
|
+
// (`content: "text*"`), mirroring how inline content like inline math is
|
|
21
|
+
// structured, plus an *atom* inline node that holds no content.
|
|
22
|
+
const schema = new Schema({
|
|
23
|
+
nodes: {
|
|
24
|
+
text: { group: "inline" },
|
|
25
|
+
inlineCode: {
|
|
26
|
+
group: "inline",
|
|
27
|
+
inline: true,
|
|
28
|
+
content: "text*",
|
|
29
|
+
toDOM: () => ["span", 0],
|
|
30
|
+
},
|
|
31
|
+
// An atom inline node (no inline content) - like a mention. It should
|
|
32
|
+
// never be collected for highlighting even if named in `nodeTypes`, since
|
|
33
|
+
// it holds no editable text. This is why the library keys off
|
|
34
|
+
// `node.inlineContent` rather than merely `node.isInline`.
|
|
35
|
+
mention: {
|
|
36
|
+
group: "inline",
|
|
37
|
+
inline: true,
|
|
38
|
+
atom: true,
|
|
39
|
+
toDOM: () => ["span", "@x"],
|
|
40
|
+
},
|
|
41
|
+
paragraph: {
|
|
42
|
+
group: "block",
|
|
43
|
+
content: "inline*",
|
|
44
|
+
toDOM: () => ["p", 0],
|
|
45
|
+
},
|
|
46
|
+
doc: { content: "block+" },
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const docWithInline = schema.node("doc", null, [
|
|
51
|
+
schema.node("paragraph", null, [
|
|
52
|
+
schema.text("before "),
|
|
53
|
+
schema.node("inlineCode", null, [schema.text("const x = 1")]),
|
|
54
|
+
schema.text(" "),
|
|
55
|
+
schema.node("mention"),
|
|
56
|
+
schema.text(" after"),
|
|
57
|
+
]),
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
it("calls the parser with the inline node's content and decorates it", () => {
|
|
61
|
+
const seen: { content: string; language?: string }[] = [];
|
|
62
|
+
|
|
63
|
+
const plugin = createHighlightPlugin({
|
|
64
|
+
// Synchronous stub parser mirroring how the real Shiki parser emits token
|
|
65
|
+
// decorations: `Decoration.inline` over positions *inside* the node,
|
|
66
|
+
// starting at `pos + 1` (the node's content starts after its opening
|
|
67
|
+
// boundary). Node-spanning decorations aren't valid over an inline node,
|
|
68
|
+
// which is fine - the token decorations are what actually color the text.
|
|
69
|
+
parser: ({ content, language, pos }) => {
|
|
70
|
+
seen.push({ content, language });
|
|
71
|
+
return [
|
|
72
|
+
Decoration.inline(pos + 1, pos + 1 + content.length, {
|
|
73
|
+
class: "hl",
|
|
74
|
+
}),
|
|
75
|
+
];
|
|
76
|
+
},
|
|
77
|
+
nodeTypes: ["inlineCode"],
|
|
78
|
+
languageExtractor: () => "javascript",
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const state = EditorState.create({
|
|
82
|
+
schema,
|
|
83
|
+
doc: docWithInline,
|
|
84
|
+
plugins: [plugin],
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const key = (plugin as any).spec.key as PluginKey;
|
|
88
|
+
const pluginState = key.getState(state);
|
|
89
|
+
|
|
90
|
+
// The inline node's text reached the parser - proving inline nodes are
|
|
91
|
+
// collected (with the pre-0.15.3 library, `seen` would be empty).
|
|
92
|
+
expect(seen).toHaveLength(1);
|
|
93
|
+
expect(seen[0].content).toBe("const x = 1");
|
|
94
|
+
expect(seen[0].language).toBe("javascript");
|
|
95
|
+
|
|
96
|
+
// And a decoration was produced for it.
|
|
97
|
+
expect(pluginState.decorations).toBeDefined();
|
|
98
|
+
expect(pluginState.decorations.find().length).toBeGreaterThan(0);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("still leaves non-matching inline nodes untouched", () => {
|
|
102
|
+
const seen: string[] = [];
|
|
103
|
+
const plugin = createHighlightPlugin({
|
|
104
|
+
parser: ({ content }) => {
|
|
105
|
+
seen.push(content);
|
|
106
|
+
return [];
|
|
107
|
+
},
|
|
108
|
+
nodeTypes: ["somethingElse"],
|
|
109
|
+
languageExtractor: () => "javascript",
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
EditorState.create({ schema, doc: docWithInline, plugins: [plugin] });
|
|
113
|
+
|
|
114
|
+
expect(seen).toHaveLength(0);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("excludes atom inline nodes even when named in `nodeTypes`", () => {
|
|
118
|
+
const seen: string[] = [];
|
|
119
|
+
const plugin = createHighlightPlugin({
|
|
120
|
+
parser: ({ content }) => {
|
|
121
|
+
seen.push(content);
|
|
122
|
+
return [];
|
|
123
|
+
},
|
|
124
|
+
// `mention` is an atom inline node with no inline content, so it holds no
|
|
125
|
+
// text to highlight. The library keys off `node.inlineContent`, not
|
|
126
|
+
// `node.isInline`, so it's correctly skipped.
|
|
127
|
+
nodeTypes: ["mention"],
|
|
128
|
+
languageExtractor: () => "javascript",
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
EditorState.create({ schema, doc: docWithInline, plugins: [plugin] });
|
|
132
|
+
|
|
133
|
+
expect(seen).toHaveLength(0);
|
|
134
|
+
});
|
|
135
|
+
});
|