@blocknote/core 0.50.0 → 0.51.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/blocknote.cjs +1 -1
- package/dist/blocknote.js +4 -4
- package/dist/{blocks-UU7EM-QM.js → blocks-D0lMxErU.js} +397 -368
- package/dist/blocks-D0lMxErU.js.map +1 -0
- package/dist/blocks-KyMOxPT3.cjs +11 -0
- package/dist/blocks-KyMOxPT3.cjs.map +1 -0
- package/dist/blocks.cjs +1 -1
- package/dist/blocks.js +2 -2
- package/dist/{extensions-CkLT--Nc.js → extensions-CQFfm5f5.js} +619 -353
- package/dist/extensions-CQFfm5f5.js.map +1 -0
- package/dist/extensions-Cuw3zOdC.cjs +52 -0
- package/dist/extensions-Cuw3zOdC.cjs.map +1 -0
- package/dist/extensions.cjs +1 -1
- package/dist/extensions.js +3 -3
- package/dist/locales.cjs +1 -1
- package/dist/locales.cjs.map +1 -1
- package/dist/locales.js +1 -1
- package/dist/locales.js.map +1 -1
- package/dist/{src-SVFpMAWE.js → src-DK_8q1j5.js} +1070 -564
- package/dist/src-DK_8q1j5.js.map +1 -0
- package/dist/src-_83jmA7A.cjs +21 -0
- package/dist/src-_83jmA7A.cjs.map +1 -0
- package/dist/style.css +1 -1
- package/dist/webpack-stats.json +1 -1
- package/dist/yjs.cjs +1 -1
- package/dist/yjs.js +8 -8
- package/package.json +1 -13
- package/src/api/blockManipulation/commands/insertBlocks/__snapshots__/insertBlocks.test.ts.snap +7 -7
- package/src/api/blockManipulation/commands/insertBlocks/insertBlocks.ts +5 -3
- package/src/api/blockManipulation/commands/mergeBlocks/__snapshots__/mergeBlocks.test.ts.snap +5 -5
- package/src/api/blockManipulation/commands/moveBlocks/__snapshots__/moveBlocks.test.ts.snap +2550 -291
- package/src/api/blockManipulation/commands/moveBlocks/moveBlocks.test.ts +83 -2
- package/src/api/blockManipulation/commands/moveBlocks/moveBlocks.ts +103 -42
- package/src/api/blockManipulation/commands/nestBlock/__snapshots__/nestBlock.test.ts.snap +0 -143
- package/src/api/blockManipulation/commands/replaceBlocks/__snapshots__/replaceBlocks.test.ts.snap +12 -12
- package/src/api/blockManipulation/commands/replaceBlocks/replaceBlocks.ts +5 -3
- package/src/api/blockManipulation/commands/splitBlock/__snapshots__/splitBlock.test.ts.snap +6 -6
- package/src/api/blockManipulation/commands/updateBlock/__snapshots__/updateBlock.test.ts.snap +22 -22
- package/src/api/blockManipulation/commands/updateBlock/updateBlock.ts +12 -8
- package/src/api/blockManipulation/setupTestEnv.ts +1 -1
- package/src/api/clipboard/fromClipboard/handleVSCodePaste.ts +2 -6
- package/src/api/clipboard/fromClipboard/pasteExtension.ts +7 -2
- package/src/api/exporters/markdown/htmlToMarkdown.ts +813 -0
- package/src/api/exporters/markdown/markdownExporter.ts +2 -21
- package/src/api/nodeConversions/blockToNode.ts +3 -2
- package/src/api/parsers/html/util/__snapshots__/nestedLists.test.ts.snap +122 -107
- package/src/api/parsers/html/util/nestedLists.test.ts +1 -12
- package/src/api/parsers/markdown/markdownToHtml.ts +1194 -0
- package/src/api/parsers/markdown/parseMarkdown.ts +2 -101
- package/src/blocks/Audio/block.ts +1 -4
- package/src/blocks/Code/block.test.ts +258 -0
- package/src/blocks/File/block.ts +1 -4
- package/src/blocks/File/helpers/render/createFileBlockWrapper.ts +7 -2
- package/src/blocks/Image/block.ts +9 -6
- package/src/blocks/ListItem/BulletListItem/block.ts +7 -4
- package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.ts +3 -4
- package/src/blocks/Video/block.ts +4 -5
- package/src/blocks/Video/parseVideoElement.ts +2 -1
- package/src/editor/Block.css +8 -0
- package/src/editor/BlockNoteEditor.test.ts +10 -4
- package/src/editor/BlockNoteEditor.ts +30 -13
- package/src/editor/managers/BlockManager.ts +10 -6
- package/src/editor/managers/ExtensionManager/index.ts +80 -25
- package/src/editor/performance.test.ts +2 -2
- package/src/editor/transformPasted.test.ts +295 -0
- package/src/editor/transformPasted.ts +77 -0
- package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap-editor-forked.json +1 -12
- package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap-editor.json +0 -11
- package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap-forked.html +1 -1
- package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap.html +1 -1
- package/src/extensions/SideMenu/dragging.ts +7 -10
- package/src/extensions/SuggestionMenu/getDefaultSlashMenuItems.ts +21 -0
- package/src/extensions/TrailingNode/TrailingNode.ts +124 -65
- package/src/i18n/locales/fr.ts +1 -1
- package/types/src/api/blockManipulation/commands/moveBlocks/moveBlocks.d.ts +13 -2
- package/types/src/api/clipboard/fromClipboard/handleVSCodePaste.d.ts +1 -1
- package/types/src/api/exporters/markdown/htmlToMarkdown.d.ts +11 -0
- package/types/src/api/parsers/markdown/markdownToHtml.d.ts +5 -0
- package/types/src/blocks/Code/block.test.d.ts +1 -0
- package/types/src/blocks/Video/block.d.ts +2 -0
- package/types/src/blocks/Video/parseVideoElement.d.ts +1 -0
- package/types/src/editor/BlockNoteEditor.d.ts +20 -6
- package/types/src/editor/managers/BlockManager.d.ts +8 -4
- package/types/src/editor/transformPasted.test.d.ts +1 -0
- package/types/src/extensions/TrailingNode/TrailingNode.d.ts +8 -3
- package/dist/blocks-BFnTypT-.cjs +0 -11
- package/dist/blocks-BFnTypT-.cjs.map +0 -1
- package/dist/blocks-UU7EM-QM.js.map +0 -1
- package/dist/extensions-CYsFi5Is.cjs +0 -2
- package/dist/extensions-CYsFi5Is.cjs.map +0 -1
- package/dist/extensions-CkLT--Nc.js.map +0 -1
- package/dist/src-DsJ6yzs9.cjs +0 -5
- package/dist/src-DsJ6yzs9.cjs.map +0 -1
- package/dist/src-SVFpMAWE.js.map +0 -1
- package/src/api/exporters/markdown/util/addSpacesToCheckboxesRehypePlugin.ts +0 -42
- package/src/api/exporters/markdown/util/convertVideoToMarkdownRehypePlugin.ts +0 -19
- package/src/api/exporters/markdown/util/removeUnderlinesRehypePlugin.ts +0 -39
- package/types/src/api/exporters/markdown/util/addSpacesToCheckboxesRehypePlugin.d.ts +0 -7
- package/types/src/api/exporters/markdown/util/convertVideoToMarkdownRehypePlugin.d.ts +0 -2
- package/types/src/api/exporters/markdown/util/removeUnderlinesRehypePlugin.d.ts +0 -6
|
@@ -234,18 +234,22 @@ export class BlockManager<
|
|
|
234
234
|
/**
|
|
235
235
|
* Moves the selected blocks up. If the previous block has children, moves
|
|
236
236
|
* them to the end of its children. If there is no previous block, but the
|
|
237
|
-
* current blocks share a common parent, moves them out of & before it.
|
|
237
|
+
* current blocks share a common parent, moves them out of & before it. If a
|
|
238
|
+
* `blockIdentifier` is provided, that block is moved instead of the
|
|
239
|
+
* selection, and the selection is left unchanged.
|
|
238
240
|
*/
|
|
239
|
-
public moveBlocksUp() {
|
|
240
|
-
return moveBlocksUp(this.editor);
|
|
241
|
+
public moveBlocksUp(blockIdentifier?: BlockIdentifier) {
|
|
242
|
+
return moveBlocksUp(this.editor, blockIdentifier);
|
|
241
243
|
}
|
|
242
244
|
|
|
243
245
|
/**
|
|
244
246
|
* Moves the selected blocks down. If the next block has children, moves
|
|
245
247
|
* them to the start of its children. If there is no next block, but the
|
|
246
|
-
* current blocks share a common parent, moves them out of & after it.
|
|
248
|
+
* current blocks share a common parent, moves them out of & after it. If a
|
|
249
|
+
* `blockIdentifier` is provided, that block is moved instead of the
|
|
250
|
+
* selection, and the selection is left unchanged.
|
|
247
251
|
*/
|
|
248
|
-
public moveBlocksDown() {
|
|
249
|
-
return moveBlocksDown(this.editor);
|
|
252
|
+
public moveBlocksDown(blockIdentifier?: BlockIdentifier) {
|
|
253
|
+
return moveBlocksDown(this.editor, blockIdentifier);
|
|
250
254
|
}
|
|
251
255
|
}
|
|
@@ -7,8 +7,9 @@ import {
|
|
|
7
7
|
Extension as TiptapExtension,
|
|
8
8
|
} from "@tiptap/core";
|
|
9
9
|
import { keymap } from "@tiptap/pm/keymap";
|
|
10
|
-
import { Plugin } from "prosemirror-state";
|
|
10
|
+
import { Plugin, TextSelection } from "prosemirror-state";
|
|
11
11
|
import { updateBlockTr } from "../../../api/blockManipulation/commands/updateBlock/updateBlock.js";
|
|
12
|
+
import { setTextCursorPosition } from "../../../api/blockManipulation/selections/textCursorPosition.js";
|
|
12
13
|
import { getBlockInfoFromTransaction } from "../../../api/getBlockInfoFromPos.js";
|
|
13
14
|
import { sortByDependencies } from "../../../util/topo-sort.js";
|
|
14
15
|
import type {
|
|
@@ -369,7 +370,49 @@ export class ExtensionManager {
|
|
|
369
370
|
// Append in reverse priority order
|
|
370
371
|
rules.push(...inputRulesByPriority.get(priority)!);
|
|
371
372
|
});
|
|
372
|
-
|
|
373
|
+
const inputRules = inputRulesPlugin({ rules });
|
|
374
|
+
// Sidecar plugin: triggers the same input rules on Enter by
|
|
375
|
+
// delegating to the inputRules plugin's handleTextInput with a
|
|
376
|
+
// synthetic "\n" insertion. The handlewithcare regex `\s$` already
|
|
377
|
+
// matches `\n`, so any rule that fires on space fires on Enter too.
|
|
378
|
+
// We call its handleTextInput directly (rather than via
|
|
379
|
+
// view.someProp) so other plugins don't observe the synthetic input,
|
|
380
|
+
// and so the rule's undo metadata is keyed to the same plugin
|
|
381
|
+
// instance that Tiptap's `commands.undoInputRule` reads from.
|
|
382
|
+
const inputRulesEnter = new Plugin({
|
|
383
|
+
props: {
|
|
384
|
+
handleKeyDown(view, event) {
|
|
385
|
+
if (event.key !== "Enter") {
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
// Only trigger on plain Enter — modifier combos like
|
|
389
|
+
// Shift/Cmd/Ctrl/Alt+Enter are reserved for other handlers
|
|
390
|
+
// (e.g. soft-break, submit) and should fall through.
|
|
391
|
+
if (
|
|
392
|
+
event.shiftKey ||
|
|
393
|
+
event.ctrlKey ||
|
|
394
|
+
event.metaKey ||
|
|
395
|
+
event.altKey
|
|
396
|
+
) {
|
|
397
|
+
return false;
|
|
398
|
+
}
|
|
399
|
+
const { $cursor } = view.state.selection as TextSelection;
|
|
400
|
+
if (!$cursor) {
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
return !!inputRules.props.handleTextInput?.call(
|
|
404
|
+
inputRules,
|
|
405
|
+
view,
|
|
406
|
+
$cursor.pos,
|
|
407
|
+
$cursor.pos,
|
|
408
|
+
"\n",
|
|
409
|
+
() =>
|
|
410
|
+
view.state.tr.insertText("\n", $cursor.pos, $cursor.pos),
|
|
411
|
+
);
|
|
412
|
+
},
|
|
413
|
+
},
|
|
414
|
+
});
|
|
415
|
+
return [inputRules, inputRulesEnter];
|
|
373
416
|
},
|
|
374
417
|
}),
|
|
375
418
|
);
|
|
@@ -408,30 +451,42 @@ export class ExtensionManager {
|
|
|
408
451
|
if (extension.inputRules?.length) {
|
|
409
452
|
inputRules.push(
|
|
410
453
|
...extension.inputRules.map((inputRule) => {
|
|
411
|
-
return new InputRule(
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
454
|
+
return new InputRule(
|
|
455
|
+
inputRule.find,
|
|
456
|
+
(state, match, start, end) => {
|
|
457
|
+
const replaceWith = inputRule.replace({
|
|
458
|
+
match,
|
|
459
|
+
range: { from: start, to: end },
|
|
460
|
+
editor: this.editor,
|
|
461
|
+
});
|
|
462
|
+
if (replaceWith) {
|
|
463
|
+
const tr = state.tr;
|
|
464
|
+
const blockInfo = getBlockInfoFromTransaction(tr);
|
|
465
|
+
|
|
466
|
+
if (
|
|
467
|
+
!blockInfo.isBlockContainer ||
|
|
468
|
+
this.editor.schema.blockSchema[blockInfo.blockNoteType]
|
|
469
|
+
?.content !== "inline"
|
|
470
|
+
) {
|
|
471
|
+
return null;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
tr.deleteRange(start, end);
|
|
475
|
+
updateBlockTr(tr, blockInfo.bnBlock.beforePos, replaceWith);
|
|
476
|
+
// updateBlockTr's replaceWith path leaves the selection after
|
|
477
|
+
// the new block when the content is replaced wholesale (e.g.
|
|
478
|
+
// when the rule returns content: []). Move the cursor back
|
|
479
|
+
// inside the new block so the user can keep typing.
|
|
480
|
+
const blockId = blockInfo.bnBlock.node.attrs.id;
|
|
481
|
+
if (blockId) {
|
|
482
|
+
setTextCursorPosition(tr, blockId, "start");
|
|
483
|
+
}
|
|
484
|
+
return tr;
|
|
425
485
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
updateBlockTr(tr, blockInfo.bnBlock.beforePos, replaceWith);
|
|
431
|
-
return tr;
|
|
432
|
-
}
|
|
433
|
-
return null;
|
|
434
|
-
});
|
|
486
|
+
return null;
|
|
487
|
+
},
|
|
488
|
+
{ undoable: true },
|
|
489
|
+
);
|
|
435
490
|
}),
|
|
436
491
|
);
|
|
437
492
|
}
|
|
@@ -114,11 +114,11 @@ describe("Performance: transaction processing scales sub-linearly (#2595)", () =
|
|
|
114
114
|
|
|
115
115
|
const smallAvg = measureAvgInsertTime(
|
|
116
116
|
smallEditor,
|
|
117
|
-
smallEditor._tiptapEditor.view.state.doc.content.size -
|
|
117
|
+
smallEditor._tiptapEditor.view.state.doc.content.size - 2,
|
|
118
118
|
);
|
|
119
119
|
const largeAvg = measureAvgInsertTime(
|
|
120
120
|
largeEditor,
|
|
121
|
-
largeEditor._tiptapEditor.view.state.doc.content.size -
|
|
121
|
+
largeEditor._tiptapEditor.view.state.doc.content.size - 2,
|
|
122
122
|
);
|
|
123
123
|
const ratio = largeAvg / smallAvg;
|
|
124
124
|
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { TextSelection } from "@tiptap/pm/state";
|
|
2
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { BlockNoteEditor } from "./BlockNoteEditor.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @vitest-environment jsdom
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const editorsToCleanup: BlockNoteEditor<any, any, any>[] = [];
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
for (const editor of editorsToCleanup) {
|
|
14
|
+
editor.unmount();
|
|
15
|
+
}
|
|
16
|
+
editorsToCleanup.length = 0;
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
function mountEditor(editor: BlockNoteEditor<any, any, any>) {
|
|
20
|
+
editorsToCleanup.push(editor);
|
|
21
|
+
editor.mount(document.createElement("div"));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function selectStartOfFirstBlock(editor: BlockNoteEditor) {
|
|
25
|
+
editor.transact((tr) => {
|
|
26
|
+
let pos: number | undefined;
|
|
27
|
+
tr.doc.descendants((node, nodePos) => {
|
|
28
|
+
if (node.type.spec.group === "blockContent") {
|
|
29
|
+
pos = nodePos + 1;
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
return pos === undefined;
|
|
33
|
+
});
|
|
34
|
+
tr.setSelection(TextSelection.create(tr.doc, pos!));
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe("paste into empty inline-content block", () => {
|
|
39
|
+
it.each(["bulletListItem", "numberedListItem", "checkListItem"] as const)(
|
|
40
|
+
"pastes a paragraph into an empty %s without replacing it",
|
|
41
|
+
(type) => {
|
|
42
|
+
const editor = BlockNoteEditor.create({
|
|
43
|
+
initialContent: [{ type, content: "" }],
|
|
44
|
+
});
|
|
45
|
+
mountEditor(editor);
|
|
46
|
+
selectStartOfFirstBlock(editor);
|
|
47
|
+
|
|
48
|
+
editor.pasteHTML(`<p>Pasted</p>`);
|
|
49
|
+
|
|
50
|
+
const blocks = editor.document;
|
|
51
|
+
expect(blocks[0].type).toBe(type);
|
|
52
|
+
expect(blocks[0].content).toEqual([
|
|
53
|
+
{ type: "text", text: "Pasted", styles: {} },
|
|
54
|
+
]);
|
|
55
|
+
},
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
it("inserts paragraph content into an empty list item without dropping marks", () => {
|
|
59
|
+
const editor = BlockNoteEditor.create({
|
|
60
|
+
initialContent: [{ type: "bulletListItem", content: "" }],
|
|
61
|
+
});
|
|
62
|
+
mountEditor(editor);
|
|
63
|
+
selectStartOfFirstBlock(editor);
|
|
64
|
+
|
|
65
|
+
editor.pasteHTML(`<p>Hello <strong>world</strong></p>`);
|
|
66
|
+
|
|
67
|
+
const blocks = editor.document;
|
|
68
|
+
expect(blocks[0].type).toBe("bulletListItem");
|
|
69
|
+
expect(blocks[0].content).toEqual([
|
|
70
|
+
{ type: "text", text: "Hello ", styles: {} },
|
|
71
|
+
{ type: "text", text: "world", styles: { bold: true } },
|
|
72
|
+
]);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("merges leading paragraph into empty list item and inserts rest as siblings", () => {
|
|
76
|
+
const editor = BlockNoteEditor.create({
|
|
77
|
+
initialContent: [{ type: "bulletListItem", content: "" }],
|
|
78
|
+
});
|
|
79
|
+
mountEditor(editor);
|
|
80
|
+
selectStartOfFirstBlock(editor);
|
|
81
|
+
|
|
82
|
+
editor.pasteHTML(`<p>First</p><p>Second</p>`);
|
|
83
|
+
|
|
84
|
+
const blocks = editor.document;
|
|
85
|
+
expect(blocks[0].type).toBe("bulletListItem");
|
|
86
|
+
expect(blocks[0].content).toEqual([
|
|
87
|
+
{ type: "text", text: "First", styles: {} },
|
|
88
|
+
]);
|
|
89
|
+
expect(blocks[1].type).toBe("paragraph");
|
|
90
|
+
expect(blocks[1].content).toEqual([
|
|
91
|
+
{ type: "text", text: "Second", styles: {} },
|
|
92
|
+
]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("replaces an empty list item with a heading when pasting a heading", () => {
|
|
96
|
+
const editor = BlockNoteEditor.create({
|
|
97
|
+
initialContent: [{ type: "bulletListItem", content: "" }],
|
|
98
|
+
});
|
|
99
|
+
mountEditor(editor);
|
|
100
|
+
selectStartOfFirstBlock(editor);
|
|
101
|
+
|
|
102
|
+
editor.pasteHTML(`<h1>Heading</h1>`);
|
|
103
|
+
|
|
104
|
+
// The empty list item is replaced by the heading rather than absorbing
|
|
105
|
+
// its inline content. Headings carry semantic meaning the user explicitly
|
|
106
|
+
// chose, so we keep them as-is.
|
|
107
|
+
const blocks = editor.document;
|
|
108
|
+
expect(blocks[0].type).toBe("heading");
|
|
109
|
+
expect(blocks[0].content).toEqual([
|
|
110
|
+
{ type: "text", text: "Heading", styles: {} },
|
|
111
|
+
]);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("keeps the list item but discards the heading wrapper when a paragraph follows the heading", () => {
|
|
115
|
+
const editor = BlockNoteEditor.create({
|
|
116
|
+
initialContent: [{ type: "bulletListItem", content: "" }],
|
|
117
|
+
});
|
|
118
|
+
mountEditor(editor);
|
|
119
|
+
selectStartOfFirstBlock(editor);
|
|
120
|
+
|
|
121
|
+
// Heading first means the leading block is not a paragraph, so the
|
|
122
|
+
// unwrap/retype rule doesn't apply: the empty list item gets replaced
|
|
123
|
+
// by the pasted heading and the trailing paragraph follows as a sibling.
|
|
124
|
+
editor.pasteHTML(`<h1>Heading</h1><p>Body</p>`);
|
|
125
|
+
|
|
126
|
+
const blocks = editor.document;
|
|
127
|
+
expect(blocks[0].type).toBe("heading");
|
|
128
|
+
expect(blocks[0].content).toEqual([
|
|
129
|
+
{ type: "text", text: "Heading", styles: {} },
|
|
130
|
+
]);
|
|
131
|
+
expect(blocks[1].type).toBe("paragraph");
|
|
132
|
+
expect(blocks[1].content).toEqual([
|
|
133
|
+
{ type: "text", text: "Body", styles: {} },
|
|
134
|
+
]);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("still replaces the empty list item when pasting another list item", () => {
|
|
138
|
+
const editor = BlockNoteEditor.create({
|
|
139
|
+
initialContent: [{ type: "bulletListItem", content: "" }],
|
|
140
|
+
});
|
|
141
|
+
mountEditor(editor);
|
|
142
|
+
selectStartOfFirstBlock(editor);
|
|
143
|
+
|
|
144
|
+
editor.pasteHTML(`<ul><li>Pasted item</li></ul>`);
|
|
145
|
+
|
|
146
|
+
const blocks = editor.document;
|
|
147
|
+
expect(blocks[0].type).toBe("bulletListItem");
|
|
148
|
+
// The empty list item should be replaced (not have inline content
|
|
149
|
+
// appended in-place), which matches the existing behavior.
|
|
150
|
+
expect(blocks[0].content).toEqual([
|
|
151
|
+
{ type: "text", text: "Pasted item", styles: {} },
|
|
152
|
+
]);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("pastes a paragraph into a non-empty list item without replacing it", () => {
|
|
156
|
+
const editor = BlockNoteEditor.create({
|
|
157
|
+
initialContent: [{ type: "bulletListItem", content: "abc" }],
|
|
158
|
+
});
|
|
159
|
+
mountEditor(editor);
|
|
160
|
+
editor.setTextCursorPosition(editor.document[0].id, "end");
|
|
161
|
+
|
|
162
|
+
editor.pasteHTML(`<p>hello</p>`);
|
|
163
|
+
|
|
164
|
+
const blocks = editor.document;
|
|
165
|
+
expect(blocks[0].type).toBe("bulletListItem");
|
|
166
|
+
expect(blocks[0].content).toEqual([
|
|
167
|
+
{ type: "text", text: "abchello", styles: {} },
|
|
168
|
+
]);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("pastes bare <li>a</li><li>b</li> into an empty list item as two list items", () => {
|
|
172
|
+
const editor = BlockNoteEditor.create({
|
|
173
|
+
initialContent: [{ type: "bulletListItem", content: "" }],
|
|
174
|
+
});
|
|
175
|
+
mountEditor(editor);
|
|
176
|
+
selectStartOfFirstBlock(editor);
|
|
177
|
+
|
|
178
|
+
editor.pasteHTML(`<li>a</li><li>b</li>`);
|
|
179
|
+
|
|
180
|
+
const blocks = editor.document;
|
|
181
|
+
expect(blocks[0].type).toBe("bulletListItem");
|
|
182
|
+
expect(blocks[0].content).toEqual([
|
|
183
|
+
{ type: "text", text: "a", styles: {} },
|
|
184
|
+
]);
|
|
185
|
+
expect(blocks[1].type).toBe("bulletListItem");
|
|
186
|
+
expect(blocks[1].content).toEqual([
|
|
187
|
+
{ type: "text", text: "b", styles: {} },
|
|
188
|
+
]);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("pastes bare <li>a</li><li>b</li> into a non-empty list item, splicing the first into the cursor", () => {
|
|
192
|
+
const editor = BlockNoteEditor.create({
|
|
193
|
+
initialContent: [{ type: "bulletListItem", content: "X" }],
|
|
194
|
+
});
|
|
195
|
+
mountEditor(editor);
|
|
196
|
+
editor.setTextCursorPosition(editor.document[0].id, "end");
|
|
197
|
+
|
|
198
|
+
editor.pasteHTML(`<li>a</li><li>b</li>`);
|
|
199
|
+
|
|
200
|
+
const blocks = editor.document;
|
|
201
|
+
expect(blocks[0].type).toBe("bulletListItem");
|
|
202
|
+
expect(blocks[0].content).toEqual([
|
|
203
|
+
{ type: "text", text: "Xa", styles: {} },
|
|
204
|
+
]);
|
|
205
|
+
expect(blocks[1].type).toBe("bulletListItem");
|
|
206
|
+
expect(blocks[1].content).toEqual([
|
|
207
|
+
{ type: "text", text: "b", styles: {} },
|
|
208
|
+
]);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("pastes two list items into an empty list item as two siblings", () => {
|
|
212
|
+
const editor = BlockNoteEditor.create({
|
|
213
|
+
initialContent: [{ type: "bulletListItem", content: "" }],
|
|
214
|
+
});
|
|
215
|
+
mountEditor(editor);
|
|
216
|
+
selectStartOfFirstBlock(editor);
|
|
217
|
+
|
|
218
|
+
editor.pasteHTML(`<ul><li>a</li><li>b</li></ul>`);
|
|
219
|
+
|
|
220
|
+
// The empty list item is replaced by the two pasted list items.
|
|
221
|
+
const blocks = editor.document;
|
|
222
|
+
expect(blocks[0].type).toBe("bulletListItem");
|
|
223
|
+
expect(blocks[0].content).toEqual([
|
|
224
|
+
{ type: "text", text: "a", styles: {} },
|
|
225
|
+
]);
|
|
226
|
+
expect(blocks[1].type).toBe("bulletListItem");
|
|
227
|
+
expect(blocks[1].content).toEqual([
|
|
228
|
+
{ type: "text", text: "b", styles: {} },
|
|
229
|
+
]);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("pastes two list items into a non-empty list item, splicing the first into the cursor", () => {
|
|
233
|
+
const editor = BlockNoteEditor.create({
|
|
234
|
+
initialContent: [{ type: "bulletListItem", content: "X" }],
|
|
235
|
+
});
|
|
236
|
+
mountEditor(editor);
|
|
237
|
+
editor.setTextCursorPosition(editor.document[0].id, "end");
|
|
238
|
+
|
|
239
|
+
editor.pasteHTML(`<ul><li>a</li><li>b</li></ul>`);
|
|
240
|
+
|
|
241
|
+
// The first list item's inline content is spliced at the cursor (the
|
|
242
|
+
// existing list item becomes "Xa") and the second list item becomes a
|
|
243
|
+
// new sibling.
|
|
244
|
+
const blocks = editor.document;
|
|
245
|
+
expect(blocks[0].type).toBe("bulletListItem");
|
|
246
|
+
expect(blocks[0].content).toEqual([
|
|
247
|
+
{ type: "text", text: "Xa", styles: {} },
|
|
248
|
+
]);
|
|
249
|
+
expect(blocks[1].type).toBe("bulletListItem");
|
|
250
|
+
expect(blocks[1].content).toEqual([
|
|
251
|
+
{ type: "text", text: "b", styles: {} },
|
|
252
|
+
]);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("preserves nested list structure when pasting a nested list into an empty list item", () => {
|
|
256
|
+
const editor = BlockNoteEditor.create({
|
|
257
|
+
initialContent: [{ type: "bulletListItem", content: "" }],
|
|
258
|
+
});
|
|
259
|
+
mountEditor(editor);
|
|
260
|
+
selectStartOfFirstBlock(editor);
|
|
261
|
+
|
|
262
|
+
// Pasting a list with nested children: the leading block is a list item,
|
|
263
|
+
// so the unwrap/retype rule does not apply and the existing slice-level
|
|
264
|
+
// list-nesting fix in `transformPasted` keeps the nested structure
|
|
265
|
+
// intact.
|
|
266
|
+
editor.pasteHTML(`<ul><li>Outer<ul><li>Inner</li></ul></li></ul>`);
|
|
267
|
+
|
|
268
|
+
const blocks = editor.document;
|
|
269
|
+
expect(blocks[0].type).toBe("bulletListItem");
|
|
270
|
+
expect(blocks[0].content).toEqual([
|
|
271
|
+
{ type: "text", text: "Outer", styles: {} },
|
|
272
|
+
]);
|
|
273
|
+
expect(blocks[0].children).toHaveLength(1);
|
|
274
|
+
expect(blocks[0].children[0].type).toBe("bulletListItem");
|
|
275
|
+
expect(blocks[0].children[0].content).toEqual([
|
|
276
|
+
{ type: "text", text: "Inner", styles: {} },
|
|
277
|
+
]);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it("preserves the existing paragraph behavior when pasting into a non-empty paragraph", () => {
|
|
281
|
+
const editor = BlockNoteEditor.create({
|
|
282
|
+
initialContent: [{ type: "paragraph", content: "Existing " }],
|
|
283
|
+
});
|
|
284
|
+
mountEditor(editor);
|
|
285
|
+
editor.setTextCursorPosition(editor.document[0].id, "end");
|
|
286
|
+
|
|
287
|
+
editor.pasteHTML(`<p>added</p>`);
|
|
288
|
+
|
|
289
|
+
const blocks = editor.document;
|
|
290
|
+
expect(blocks[0].type).toBe("paragraph");
|
|
291
|
+
expect(blocks[0].content).toEqual([
|
|
292
|
+
{ type: "text", text: "Existing added", styles: {} },
|
|
293
|
+
]);
|
|
294
|
+
});
|
|
295
|
+
});
|
|
@@ -113,6 +113,11 @@ export function transformPasted(slice: Slice, view: EditorView) {
|
|
|
113
113
|
let f = Fragment.from(slice.content);
|
|
114
114
|
f = wrapTableRows(f, view.state.schema);
|
|
115
115
|
|
|
116
|
+
const retyped = retypeLeadingParagraphForEmptyTarget(f, view, slice);
|
|
117
|
+
if (retyped) {
|
|
118
|
+
return retyped;
|
|
119
|
+
}
|
|
120
|
+
|
|
116
121
|
if (isInTableCell(view)) {
|
|
117
122
|
let hasTableContent = false;
|
|
118
123
|
f.descendants((node) => {
|
|
@@ -173,6 +178,78 @@ export function transformPasted(slice: Slice, view: EditorView) {
|
|
|
173
178
|
return new Slice(f, slice.openStart, slice.openEnd);
|
|
174
179
|
}
|
|
175
180
|
|
|
181
|
+
/**
|
|
182
|
+
* Pasting plain text into an empty inline-content block (e.g. an empty
|
|
183
|
+
* bullet list item) would normally replace that block with a paragraph:
|
|
184
|
+
* BlockNote's serializer always wraps content in
|
|
185
|
+
* `blockGroup > blockContainer > <block>`, producing a closed slice that
|
|
186
|
+
* ProseMirror inserts as a new block rather than splicing inline.
|
|
187
|
+
*
|
|
188
|
+
* To preserve the empty block's type, retype the leading paragraph in the
|
|
189
|
+
* slice to match the target block. Subsequent blocks in the slice are left
|
|
190
|
+
* alone and end up as siblings.
|
|
191
|
+
*
|
|
192
|
+
* Scoped to: empty, non-paragraph, inline-content target + paragraph leading
|
|
193
|
+
* the slice. A non-empty target already gives ProseMirror a valid inline
|
|
194
|
+
* insertion point so it splices correctly on its own; non-paragraph leading
|
|
195
|
+
* blocks (heading, list item, …) carry semantic meaning the user picked, so
|
|
196
|
+
* we keep the existing replace behavior.
|
|
197
|
+
*/
|
|
198
|
+
function retypeLeadingParagraphForEmptyTarget(
|
|
199
|
+
fragment: Fragment,
|
|
200
|
+
view: EditorView,
|
|
201
|
+
slice: Slice,
|
|
202
|
+
): Slice | null {
|
|
203
|
+
if (isInTableCell(view)) {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// `transformPasted` is also called for drop events, where the slice will be
|
|
208
|
+
// inserted at the drop position rather than the current selection. In that
|
|
209
|
+
// case the selection-derived target is wrong, so bail out and let the
|
|
210
|
+
// default behavior handle drops.
|
|
211
|
+
if (view.dragging) {
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const blockInfo = getBlockInfoFromSelection(view.state);
|
|
216
|
+
const target = blockInfo.isBlockContainer
|
|
217
|
+
? blockInfo.blockContent.node
|
|
218
|
+
: null;
|
|
219
|
+
if (
|
|
220
|
+
!target ||
|
|
221
|
+
target.type.name === "paragraph" ||
|
|
222
|
+
target.type.spec.content !== "inline*" ||
|
|
223
|
+
target.childCount > 0
|
|
224
|
+
) {
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const blockGroup = fragment.firstChild;
|
|
229
|
+
const blockContainer = blockGroup?.firstChild;
|
|
230
|
+
const leading = blockContainer?.firstChild;
|
|
231
|
+
if (
|
|
232
|
+
blockGroup?.type.name !== "blockGroup" ||
|
|
233
|
+
blockContainer?.type.name !== "blockContainer" ||
|
|
234
|
+
leading?.type.name !== "paragraph"
|
|
235
|
+
) {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const retyped = target.type.create(target.attrs, leading.content);
|
|
240
|
+
const newBlockContainer = blockContainer.copy(
|
|
241
|
+
blockContainer.content.replaceChild(0, retyped),
|
|
242
|
+
);
|
|
243
|
+
const newBlockGroup = blockGroup.copy(
|
|
244
|
+
blockGroup.content.replaceChild(0, newBlockContainer),
|
|
245
|
+
);
|
|
246
|
+
return new Slice(
|
|
247
|
+
fragment.replaceChild(0, newBlockGroup),
|
|
248
|
+
slice.openStart,
|
|
249
|
+
slice.openEnd,
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
176
253
|
/**
|
|
177
254
|
* Used in `transformPasted` to check if the fix there should be applied, i.e.
|
|
178
255
|
* if the pasted fragment should be wrapped in a `blockContainer` node. This
|
|
@@ -8,18 +8,7 @@
|
|
|
8
8
|
"type": "text",
|
|
9
9
|
},
|
|
10
10
|
],
|
|
11
|
-
"id": "
|
|
12
|
-
"props": {
|
|
13
|
-
"backgroundColor": "default",
|
|
14
|
-
"textAlignment": "left",
|
|
15
|
-
"textColor": "default",
|
|
16
|
-
},
|
|
17
|
-
"type": "paragraph",
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
"children": [],
|
|
21
|
-
"content": [],
|
|
22
|
-
"id": "3",
|
|
11
|
+
"id": "1",
|
|
23
12
|
"props": {
|
|
24
13
|
"backgroundColor": "default",
|
|
25
14
|
"textAlignment": "left",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
<blockgroup><blockcontainer id="
|
|
1
|
+
<blockgroup><blockcontainer id="1"><paragraph backgroundColor="default" textAlignment="left" textColor="default">Hello World</paragraph></blockcontainer></blockgroup>
|
|
@@ -1 +1 @@
|
|
|
1
|
-
<blockgroup><blockcontainer id="0"><paragraph backgroundColor="default" textAlignment="left" textColor="default">Hello</paragraph></blockcontainer
|
|
1
|
+
<blockgroup><blockcontainer id="0"><paragraph backgroundColor="default" textAlignment="left" textColor="default">Hello</paragraph></blockcontainer></blockgroup>
|
|
@@ -101,16 +101,13 @@ function setDragImage(view: EditorView, from: number, to = from) {
|
|
|
101
101
|
|
|
102
102
|
// Browsers may have CORS policies which prevents iframes from being
|
|
103
103
|
// manipulated, so better to stay on the safe side and remove them from the
|
|
104
|
-
// drag preview. The drag preview doesn't work with
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
parent.removeChild(iframe);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
104
|
+
// drag preview. The drag preview doesn't work with embedded documents
|
|
105
|
+
// (iframe/embed/object) anyway, and including an <embed> (e.g. a PDF)
|
|
106
|
+
// can prevent the drag from initiating at all.
|
|
107
|
+
const embeddedDocs = dragImageElement.querySelectorAll(
|
|
108
|
+
"iframe, embed, object",
|
|
109
|
+
);
|
|
110
|
+
embeddedDocs.forEach((el) => el.parentElement?.removeChild(el));
|
|
114
111
|
|
|
115
112
|
// TODO: This is hacky, need a better way of assigning classes to the editor so that they can also be applied to the
|
|
116
113
|
// drag preview.
|