@blocknote/core 0.19.0 → 0.19.1

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.
Files changed (97) hide show
  1. package/README.md +2 -0
  2. package/dist/blocknote.js +2549 -2497
  3. package/dist/blocknote.js.map +1 -1
  4. package/dist/blocknote.umd.cjs +6 -6
  5. package/dist/blocknote.umd.cjs.map +1 -1
  6. package/dist/src/api/blockManipulation/commands/moveBlock/moveBlock.test.js +5 -1
  7. package/dist/src/api/blockManipulation/commands/moveBlock/moveBlock.test.js.map +1 -1
  8. package/dist/src/api/blockManipulation/commands/removeBlocks/removeBlocks.js +2 -40
  9. package/dist/src/api/blockManipulation/commands/removeBlocks/removeBlocks.js.map +1 -1
  10. package/dist/src/api/blockManipulation/commands/removeBlocks/removeBlocks.test.js +4 -0
  11. package/dist/src/api/blockManipulation/commands/removeBlocks/removeBlocks.test.js.map +1 -1
  12. package/dist/src/api/blockManipulation/commands/replaceBlocks/replaceBlocks.js +51 -9
  13. package/dist/src/api/blockManipulation/commands/replaceBlocks/replaceBlocks.js.map +1 -1
  14. package/dist/src/api/blockManipulation/commands/splitBlock/splitBlock.js +2 -2
  15. package/dist/src/api/blockManipulation/commands/splitBlock/splitBlock.js.map +1 -1
  16. package/dist/src/api/clipboard/fromClipboard/acceptedMIMETypes.js +1 -1
  17. package/dist/src/api/clipboard/fromClipboard/acceptedMIMETypes.js.map +1 -1
  18. package/dist/src/api/clipboard/fromClipboard/handleFileInsertion.js +4 -2
  19. package/dist/src/api/clipboard/fromClipboard/handleFileInsertion.js.map +1 -1
  20. package/dist/src/api/getBlockInfoFromPos.js +19 -25
  21. package/dist/src/api/getBlockInfoFromPos.js.map +1 -1
  22. package/dist/src/blocks/HeadingBlockContent/HeadingBlockContent.js +8 -4
  23. package/dist/src/blocks/HeadingBlockContent/HeadingBlockContent.js.map +1 -1
  24. package/dist/src/blocks/ListItemBlockContent/BulletListItemBlockContent/BulletListItemBlockContent.js +5 -3
  25. package/dist/src/blocks/ListItemBlockContent/BulletListItemBlockContent/BulletListItemBlockContent.js.map +1 -1
  26. package/dist/src/blocks/ListItemBlockContent/CheckListItemBlockContent/CheckListItemBlockContent.js +12 -6
  27. package/dist/src/blocks/ListItemBlockContent/CheckListItemBlockContent/CheckListItemBlockContent.js.map +1 -1
  28. package/dist/src/blocks/ListItemBlockContent/ListItemKeyboardShortcuts.js +5 -1
  29. package/dist/src/blocks/ListItemBlockContent/ListItemKeyboardShortcuts.js.map +1 -1
  30. package/dist/src/blocks/ListItemBlockContent/NumberedListItemBlockContent/NumberedListItemBlockContent.js +4 -2
  31. package/dist/src/blocks/ListItemBlockContent/NumberedListItemBlockContent/NumberedListItemBlockContent.js.map +1 -1
  32. package/dist/src/blocks/ParagraphBlockContent/ParagraphBlockContent.js +2 -1
  33. package/dist/src/blocks/ParagraphBlockContent/ParagraphBlockContent.js.map +1 -1
  34. package/dist/src/blocks/TableBlockContent/TableBlockContent.js +0 -1
  35. package/dist/src/blocks/TableBlockContent/TableBlockContent.js.map +1 -1
  36. package/dist/src/editor/BlockNoteEditor.js +39 -43
  37. package/dist/src/editor/BlockNoteEditor.js.map +1 -1
  38. package/dist/src/editor/BlockNoteEditor.test.js +2 -2
  39. package/dist/src/editor/BlockNoteEditor.test.js.map +1 -1
  40. package/dist/src/editor/BlockNoteExtensions.js +52 -6
  41. package/dist/src/editor/BlockNoteExtensions.js.map +1 -1
  42. package/dist/src/extensions/KeyboardShortcuts/KeyboardShortcutsExtension.js +36 -6
  43. package/dist/src/extensions/KeyboardShortcuts/KeyboardShortcutsExtension.js.map +1 -1
  44. package/dist/src/extensions/NodeSelectionKeyboard/NodeSelectionKeyboardPlugin.js +35 -32
  45. package/dist/src/extensions/NodeSelectionKeyboard/NodeSelectionKeyboardPlugin.js.map +1 -1
  46. package/dist/src/extensions/Placeholder/PlaceholderPlugin.js +74 -71
  47. package/dist/src/extensions/Placeholder/PlaceholderPlugin.js.map +1 -1
  48. package/dist/src/extensions/PreviousBlockType/PreviousBlockTypePlugin.js +153 -149
  49. package/dist/src/extensions/PreviousBlockType/PreviousBlockTypePlugin.js.map +1 -1
  50. package/dist/tsconfig.tsbuildinfo +1 -1
  51. package/dist/webpack-stats.json +1 -1
  52. package/package.json +2 -2
  53. package/src/api/blockManipulation/commands/insertBlocks/__snapshots__/insertBlocks.test.ts.snap +0 -6
  54. package/src/api/blockManipulation/commands/mergeBlocks/__snapshots__/mergeBlocks.test.ts.snap +0 -5
  55. package/src/api/blockManipulation/commands/moveBlock/__snapshots__/moveBlock.test.ts.snap +0 -8
  56. package/src/api/blockManipulation/commands/moveBlock/moveBlock.test.ts +7 -3
  57. package/src/api/blockManipulation/commands/removeBlocks/__snapshots__/removeBlocks.test.ts.snap +439 -2
  58. package/src/api/blockManipulation/commands/removeBlocks/removeBlocks.test.ts +6 -0
  59. package/src/api/blockManipulation/commands/removeBlocks/removeBlocks.ts +2 -82
  60. package/src/api/blockManipulation/commands/replaceBlocks/__snapshots__/replaceBlocks.test.ts.snap +0 -8
  61. package/src/api/blockManipulation/commands/replaceBlocks/replaceBlocks.ts +96 -20
  62. package/src/api/blockManipulation/commands/splitBlock/__snapshots__/splitBlock.test.ts.snap +0 -6
  63. package/src/api/blockManipulation/commands/splitBlock/splitBlock.ts +2 -5
  64. package/src/api/blockManipulation/commands/updateBlock/__snapshots__/updateBlock.test.ts.snap +0 -490
  65. package/src/api/clipboard/fromClipboard/acceptedMIMETypes.ts +1 -1
  66. package/src/api/clipboard/fromClipboard/handleFileInsertion.ts +6 -5
  67. package/src/api/getBlockInfoFromPos.ts +20 -30
  68. package/src/api/parsers/html/__snapshots__/parse-notion-html.json +1 -2
  69. package/src/blocks/HeadingBlockContent/HeadingBlockContent.ts +16 -4
  70. package/src/blocks/ListItemBlockContent/BulletListItemBlockContent/BulletListItemBlockContent.ts +9 -3
  71. package/src/blocks/ListItemBlockContent/CheckListItemBlockContent/CheckListItemBlockContent.ts +22 -6
  72. package/src/blocks/ListItemBlockContent/ListItemKeyboardShortcuts.ts +5 -3
  73. package/src/blocks/ListItemBlockContent/NumberedListItemBlockContent/NumberedListItemBlockContent.ts +8 -2
  74. package/src/blocks/ParagraphBlockContent/ParagraphBlockContent.ts +4 -1
  75. package/src/blocks/TableBlockContent/TableBlockContent.ts +0 -1
  76. package/src/editor/BlockNoteEditor.test.ts +2 -5
  77. package/src/editor/BlockNoteEditor.ts +71 -42
  78. package/src/editor/BlockNoteExtensions.ts +90 -14
  79. package/src/extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +36 -9
  80. package/src/extensions/NodeSelectionKeyboard/NodeSelectionKeyboardPlugin.ts +45 -42
  81. package/src/extensions/Placeholder/PlaceholderPlugin.ts +94 -90
  82. package/src/extensions/PreviousBlockType/PreviousBlockTypePlugin.ts +173 -169
  83. package/types/src/api/blockManipulation/commands/removeBlocks/removeBlocks.d.ts +0 -3
  84. package/types/src/api/blockManipulation/commands/replaceBlocks/replaceBlocks.d.ts +4 -0
  85. package/types/src/api/blockManipulation/setupTestEnv.d.ts +0 -6
  86. package/types/src/api/clipboard/fromClipboard/acceptedMIMETypes.d.ts +1 -1
  87. package/types/src/api/getBlockInfoFromPos.d.ts +9 -34
  88. package/types/src/api/testUtil/cases/customBlocks.d.ts +0 -6
  89. package/types/src/api/testUtil/cases/customInlineContent.d.ts +0 -6
  90. package/types/src/api/testUtil/cases/customStyles.d.ts +0 -6
  91. package/types/src/blocks/TableBlockContent/TableBlockContent.d.ts +0 -9
  92. package/types/src/blocks/defaultBlocks.d.ts +0 -12
  93. package/types/src/editor/BlockNoteEditor.d.ts +19 -2
  94. package/types/src/editor/BlockNoteExtensions.d.ts +14 -7
  95. package/types/src/extensions/NodeSelectionKeyboard/NodeSelectionKeyboardPlugin.d.ts +4 -1
  96. package/types/src/extensions/Placeholder/PlaceholderPlugin.d.ts +4 -1
  97. package/types/src/extensions/PreviousBlockType/PreviousBlockTypePlugin.d.ts +4 -5
@@ -1,5 +1,3 @@
1
- import { Node } from "prosemirror-model";
2
-
3
1
  import { Block, PartialBlock } from "../../../../blocks/defaultBlocks.js";
4
2
  import type { BlockNoteEditor } from "../../../../editor/BlockNoteEditor";
5
3
  import {
@@ -8,11 +6,11 @@ import {
8
6
  InlineContentSchema,
9
7
  StyleSchema,
10
8
  } from "../../../../schema/index.js";
9
+ import { Node } from "prosemirror-model";
11
10
  import { blockToNode } from "../../../nodeConversions/blockToNode.js";
12
11
  import { nodeToBlock } from "../../../nodeConversions/nodeToBlock.js";
13
- import { removeBlocksWithCallback } from "../removeBlocks/removeBlocks.js";
14
12
 
15
- export function replaceBlocks<
13
+ export function removeAndInsertBlocks<
16
14
  BSchema extends BlockSchema,
17
15
  I extends InlineContentSchema,
18
16
  S extends StyleSchema
@@ -24,6 +22,11 @@ export function replaceBlocks<
24
22
  insertedBlocks: Block<BSchema, I, S>[];
25
23
  removedBlocks: Block<BSchema, I, S>[];
26
24
  } {
25
+ const ttEditor = editor._tiptapEditor;
26
+ let tr = ttEditor.state.tr;
27
+
28
+ // Converts the `PartialBlock`s to ProseMirror nodes to insert them into the
29
+ // document.
27
30
  const nodesToInsert: Node[] = [];
28
31
  for (const block of blocksToInsert) {
29
32
  nodesToInsert.push(
@@ -31,28 +34,86 @@ export function replaceBlocks<
31
34
  );
32
35
  }
33
36
 
37
+ const idsOfBlocksToRemove = new Set<string>(
38
+ blocksToRemove.map((block) =>
39
+ typeof block === "string" ? block : block.id
40
+ )
41
+ );
42
+ const removedBlocks: Block<BSchema, I, S>[] = [];
43
+
34
44
  const idOfFirstBlock =
35
45
  typeof blocksToRemove[0] === "string"
36
46
  ? blocksToRemove[0]
37
47
  : blocksToRemove[0].id;
38
- const removedBlocks = removeBlocksWithCallback(
39
- editor,
40
- blocksToRemove,
41
- (node, pos, tr, removedSize) => {
42
- if (node.attrs.id === idOfFirstBlock) {
43
- const oldDocSize = tr.doc.nodeSize;
44
- tr.insert(pos, nodesToInsert);
45
- const newDocSize = tr.doc.nodeSize;
46
-
47
- return removedSize + oldDocSize - newDocSize;
48
- }
49
-
50
- return removedSize;
48
+ let removedSize = 0;
49
+
50
+ ttEditor.state.doc.descendants((node, pos) => {
51
+ // Skips traversing nodes after all target blocks have been removed.
52
+ if (idsOfBlocksToRemove.size === 0) {
53
+ return false;
54
+ }
55
+
56
+ // Keeps traversing nodes if block with target ID has not been found.
57
+ if (
58
+ !node.type.isInGroup("bnBlock") ||
59
+ !idsOfBlocksToRemove.has(node.attrs.id)
60
+ ) {
61
+ return true;
62
+ }
63
+
64
+ // Saves the block that is being deleted.
65
+ removedBlocks.push(
66
+ nodeToBlock(
67
+ node,
68
+ editor.schema.blockSchema,
69
+ editor.schema.inlineContentSchema,
70
+ editor.schema.styleSchema,
71
+ editor.blockCache
72
+ )
73
+ );
74
+ idsOfBlocksToRemove.delete(node.attrs.id);
75
+
76
+ if (blocksToInsert.length > 0 && node.attrs.id === idOfFirstBlock) {
77
+ const oldDocSize = tr.doc.nodeSize;
78
+ tr = tr.insert(pos, nodesToInsert);
79
+ const newDocSize = tr.doc.nodeSize;
80
+
81
+ removedSize += oldDocSize - newDocSize;
51
82
  }
52
- );
53
83
 
54
- // Now that the `PartialBlock`s have been converted to nodes, we can
55
- // re-convert them into full `Block`s.
84
+ const oldDocSize = tr.doc.nodeSize;
85
+ // Checks if the block is the only child of its parent. In this case, we
86
+ // need to delete the parent `blockGroup` node instead of just the
87
+ // `blockContainer`.
88
+ const $pos = tr.doc.resolve(pos - removedSize);
89
+ if (
90
+ $pos.node().type.name === "blockGroup" &&
91
+ $pos.node($pos.depth - 1).type.name !== "doc" &&
92
+ $pos.node().childCount === 1
93
+ ) {
94
+ tr = tr.delete($pos.before(), $pos.after());
95
+ } else {
96
+ tr = tr.delete(pos - removedSize, pos - removedSize + node.nodeSize);
97
+ }
98
+ const newDocSize = tr.doc.nodeSize;
99
+ removedSize += oldDocSize - newDocSize;
100
+
101
+ return false;
102
+ });
103
+
104
+ // Throws an error if now all blocks could be found.
105
+ if (idsOfBlocksToRemove.size > 0) {
106
+ const notFoundIds = [...idsOfBlocksToRemove].join("\n");
107
+
108
+ throw Error(
109
+ "Blocks with the following IDs could not be found in the editor: " +
110
+ notFoundIds
111
+ );
112
+ }
113
+
114
+ editor.dispatch(tr);
115
+
116
+ // Converts the nodes created from `blocksToInsert` into full `Block`s.
56
117
  const insertedBlocks: Block<BSchema, I, S>[] = [];
57
118
  for (const node of nodesToInsert) {
58
119
  insertedBlocks.push(
@@ -68,3 +129,18 @@ export function replaceBlocks<
68
129
 
69
130
  return { insertedBlocks, removedBlocks };
70
131
  }
132
+
133
+ export function replaceBlocks<
134
+ BSchema extends BlockSchema,
135
+ I extends InlineContentSchema,
136
+ S extends StyleSchema
137
+ >(
138
+ editor: BlockNoteEditor<BSchema, I, S>,
139
+ blocksToRemove: BlockIdentifier[],
140
+ blocksToInsert: PartialBlock<BSchema, I, S>[]
141
+ ): {
142
+ insertedBlocks: Block<BSchema, I, S>[];
143
+ removedBlocks: Block<BSchema, I, S>[];
144
+ } {
145
+ return removeAndInsertBlocks(editor, blocksToRemove, blocksToInsert);
146
+ }
@@ -361,7 +361,6 @@ exports[`Test splitBlocks > Basic 1`] = `
361
361
  },
362
362
  "id": "table-0",
363
363
  "props": {
364
- "backgroundColor": "default",
365
364
  "textColor": "default",
366
365
  },
367
366
  "type": "table",
@@ -854,7 +853,6 @@ exports[`Test splitBlocks > Block has children 1`] = `
854
853
  },
855
854
  "id": "table-0",
856
855
  "props": {
857
- "backgroundColor": "default",
858
856
  "textColor": "default",
859
857
  },
860
858
  "type": "table",
@@ -1347,7 +1345,6 @@ exports[`Test splitBlocks > Don't keep props 1`] = `
1347
1345
  },
1348
1346
  "id": "table-0",
1349
1347
  "props": {
1350
- "backgroundColor": "default",
1351
1348
  "textColor": "default",
1352
1349
  },
1353
1350
  "type": "table",
@@ -1840,7 +1837,6 @@ exports[`Test splitBlocks > Don't keep type 1`] = `
1840
1837
  },
1841
1838
  "id": "table-0",
1842
1839
  "props": {
1843
- "backgroundColor": "default",
1844
1840
  "textColor": "default",
1845
1841
  },
1846
1842
  "type": "table",
@@ -2327,7 +2323,6 @@ exports[`Test splitBlocks > End of content 1`] = `
2327
2323
  },
2328
2324
  "id": "table-0",
2329
2325
  "props": {
2330
- "backgroundColor": "default",
2331
2326
  "textColor": "default",
2332
2327
  },
2333
2328
  "type": "table",
@@ -2821,7 +2816,6 @@ exports[`Test splitBlocks > Keep type 1`] = `
2821
2816
  },
2822
2817
  "id": "table-0",
2823
2818
  "props": {
2824
- "backgroundColor": "default",
2825
2819
  "textColor": "default",
2826
2820
  },
2827
2821
  "type": "table",
@@ -2,7 +2,7 @@ import { EditorState } from "prosemirror-state";
2
2
 
3
3
  import {
4
4
  getBlockInfo,
5
- getNearestBlockContainerPos,
5
+ getNearestBlockPos,
6
6
  } from "../../../getBlockInfoFromPos.js";
7
7
 
8
8
  export const splitBlockCommand = (
@@ -17,10 +17,7 @@ export const splitBlockCommand = (
17
17
  state: EditorState;
18
18
  dispatch: ((args?: any) => any) | undefined;
19
19
  }) => {
20
- const nearestBlockContainerPos = getNearestBlockContainerPos(
21
- state.doc,
22
- posInBlock
23
- );
20
+ const nearestBlockContainerPos = getNearestBlockPos(state.doc, posInBlock);
24
21
 
25
22
  const info = getBlockInfo(nearestBlockContainerPos);
26
23