@blocknote/core 0.53.0 → 0.54.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 (219) hide show
  1. package/dist/BlockNoteExtension-BJCLnwrE.cjs +2 -0
  2. package/dist/BlockNoteExtension-BJCLnwrE.cjs.map +1 -0
  3. package/dist/BlockNoteExtension-CbvDA0F7.js +80 -0
  4. package/dist/BlockNoteExtension-CbvDA0F7.js.map +1 -0
  5. package/dist/{UserStore-QbbPFj0z.js → UserStore-C4TzkGEn.js} +3 -3
  6. package/dist/{UserStore-DCG6lsla.cjs.map → UserStore-C4TzkGEn.js.map} +1 -1
  7. package/dist/{UserStore-DCG6lsla.cjs → UserStore-mj3fXW4D.cjs} +2 -2
  8. package/dist/UserStore-mj3fXW4D.cjs.map +1 -0
  9. package/dist/blocknote.cjs +1 -1
  10. package/dist/blocknote.js +6 -6
  11. package/dist/blocks-CTB_5Vyj.cjs +12 -0
  12. package/dist/blocks-CTB_5Vyj.cjs.map +1 -0
  13. package/dist/{blocks-BEJW9OM_.js → blocks-Dol85hpH.js} +680 -670
  14. package/dist/blocks-Dol85hpH.js.map +1 -0
  15. package/dist/blocks.cjs +1 -1
  16. package/dist/blocks.js +2 -2
  17. package/dist/comments.cjs +1 -1
  18. package/dist/comments.cjs.map +1 -1
  19. package/dist/comments.js +7 -7
  20. package/dist/comments.js.map +1 -1
  21. package/dist/en-BuzSuuTq.cjs +2 -0
  22. package/dist/en-BuzSuuTq.cjs.map +1 -0
  23. package/dist/{en-CGKXfWma.js → en-uzHzj6eH.js} +10 -1
  24. package/dist/en-uzHzj6eH.js.map +1 -0
  25. package/dist/extensions-B7Rsp_Ft.cjs +58 -0
  26. package/dist/extensions-B7Rsp_Ft.cjs.map +1 -0
  27. package/dist/{extensions-BR5hkAio.js → extensions-D6JcJCwd.js} +735 -457
  28. package/dist/extensions-D6JcJCwd.js.map +1 -0
  29. package/dist/extensions.cjs +1 -1
  30. package/dist/extensions.js +4 -4
  31. package/dist/locales.cjs +1 -1
  32. package/dist/locales.cjs.map +1 -1
  33. package/dist/locales.js +199 -1
  34. package/dist/locales.js.map +1 -1
  35. package/dist/{src-BUXut68d.js → src-Buuo5l7X.js} +924 -851
  36. package/dist/src-Buuo5l7X.js.map +1 -0
  37. package/dist/src-C54Ok9E0.cjs +23 -0
  38. package/dist/src-C54Ok9E0.cjs.map +1 -0
  39. package/dist/style.css +1 -1
  40. package/dist/webpack-stats.json +1 -1
  41. package/dist/y.cjs +1 -1
  42. package/dist/y.cjs.map +1 -1
  43. package/dist/y.js +245 -242
  44. package/dist/y.js.map +1 -1
  45. package/dist/yjs.cjs +1 -1
  46. package/dist/yjs.cjs.map +1 -1
  47. package/dist/yjs.js +79 -67
  48. package/dist/yjs.js.map +1 -1
  49. package/package.json +17 -18
  50. package/src/api/blockManipulation/commands/updateBlock/__snapshots__/updateBlock.test.ts.snap +2505 -763
  51. package/src/api/blockManipulation/commands/updateBlock/updateBlock.test.ts +42 -0
  52. package/src/api/blockManipulation/commands/updateBlock/updateBlock.ts +16 -4
  53. package/src/api/exporters/markdown/htmlToMarkdown.ts +45 -10
  54. package/src/api/getBlocksChangedByTransaction.test.ts +211 -0
  55. package/src/api/getBlocksChangedByTransaction.ts +48 -12
  56. package/src/api/getChangedRange.ts +54 -0
  57. package/src/api/nodeConversions/blockToNode.ts +5 -1
  58. package/src/api/nodeConversions/nodeToBlock.ts +3 -0
  59. package/src/api/pmUtil.ts +16 -8
  60. package/src/blocks/Code/CodeBlockOptions.ts +83 -0
  61. package/src/blocks/Code/block.test.ts +1 -1
  62. package/src/blocks/Code/block.ts +29 -281
  63. package/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts +120 -0
  64. package/src/blocks/Code/helpers/parse/parsePreCode.ts +45 -0
  65. package/src/blocks/Code/helpers/render/createCodeBlock.ts +98 -0
  66. package/src/blocks/Code/helpers/toExternalHTML/createPreCode.ts +14 -0
  67. package/src/blocks/index.ts +3 -1
  68. package/src/comments/extension.ts +2 -4
  69. package/src/editor/Block.css +273 -2
  70. package/src/editor/BlockNoteEditor.ts +25 -4
  71. package/src/editor/BlockNoteExtension.ts +1 -1
  72. package/src/editor/managers/ExtensionManager/extensions.ts +6 -0
  73. package/src/editor/performance.test.ts +45 -0
  74. package/src/exporter/ExportImage.ts +50 -0
  75. package/src/exporter/Exporter.test.ts +186 -0
  76. package/src/exporter/Exporter.ts +66 -14
  77. package/src/exporter/index.ts +1 -0
  78. package/src/exporter/mapping.ts +4 -1
  79. package/src/extensions/FormattingToolbar/FormattingToolbar.ts +10 -8
  80. package/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.ts +109 -0
  81. package/src/extensions/PreviousBlockType/PreviousBlockType.test.ts +21 -0
  82. package/src/extensions/PreviousBlockType/PreviousBlockType.ts +5 -3
  83. package/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.ts +192 -0
  84. package/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.ts +142 -0
  85. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.ts +120 -0
  86. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.ts +89 -0
  87. package/src/extensions/SyntaxHighlighting/inlineHighlight.test.ts +135 -0
  88. package/src/extensions/SyntaxHighlighting/shiki.ts +116 -0
  89. package/src/extensions/TableHandles/TableHandles.browser.test.ts +305 -0
  90. package/src/extensions/TableHandles/TableHandles.ts +82 -41
  91. package/src/extensions/Versioning/inMemoryVersioning.ts +13 -3
  92. package/src/extensions/index.ts +4 -0
  93. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.test.ts +51 -2
  94. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +22 -2
  95. package/src/fonts/inter.css +9 -9
  96. package/src/i18n/locales/ar.ts +9 -0
  97. package/src/i18n/locales/de.ts +9 -0
  98. package/src/i18n/locales/en.ts +9 -0
  99. package/src/i18n/locales/es.ts +9 -0
  100. package/src/i18n/locales/fa.ts +9 -0
  101. package/src/i18n/locales/fr.ts +9 -0
  102. package/src/i18n/locales/he.ts +9 -0
  103. package/src/i18n/locales/hr.ts +9 -0
  104. package/src/i18n/locales/is.ts +9 -0
  105. package/src/i18n/locales/it.ts +9 -0
  106. package/src/i18n/locales/ja.ts +9 -0
  107. package/src/i18n/locales/ko.ts +9 -0
  108. package/src/i18n/locales/nl.ts +9 -0
  109. package/src/i18n/locales/no.ts +9 -0
  110. package/src/i18n/locales/pl.ts +9 -0
  111. package/src/i18n/locales/pt.ts +9 -0
  112. package/src/i18n/locales/ru.ts +9 -0
  113. package/src/i18n/locales/sk.ts +9 -0
  114. package/src/i18n/locales/uk.ts +9 -0
  115. package/src/i18n/locales/uz.ts +9 -0
  116. package/src/i18n/locales/vi.ts +9 -0
  117. package/src/i18n/locales/zh-tw.ts +9 -0
  118. package/src/i18n/locales/zh.ts +9 -0
  119. package/src/index.ts +5 -1
  120. package/src/schema/blocks/createSpec.ts +5 -7
  121. package/src/schema/blocks/types.ts +53 -3
  122. package/src/schema/contentTypePropagation.test.ts +1 -1
  123. package/src/schema/inlineContent/createSpec.test.ts +161 -0
  124. package/src/schema/inlineContent/createSpec.ts +175 -17
  125. package/src/schema/inlineContent/internal.ts +21 -2
  126. package/src/schema/inlineContent/types.ts +35 -5
  127. package/src/schema/nodeViewMutations.test.ts +81 -56
  128. package/src/schema/nodeViewMutations.ts +30 -27
  129. package/src/schema/schema.test.ts +110 -0
  130. package/src/schema/schema.ts +13 -4
  131. package/src/user/UserStore.ts +1 -1
  132. package/src/util/Store.test.ts +132 -0
  133. package/src/util/Store.ts +135 -0
  134. package/src/y/extensions/AttributionExtension.test.ts +18 -9
  135. package/src/y/extensions/Versioning.test.ts +20 -7
  136. package/src/y/versioning/__test__/yhub.test.ts +92 -59
  137. package/src/y/versioning/yhub.ts +35 -29
  138. package/src/yjs/comments/RESTYjsThreadStore.test.ts +85 -0
  139. package/src/yjs/comments/RESTYjsThreadStore.ts +7 -1
  140. package/src/yjs/extensions/ForkYDoc.test.ts +37 -0
  141. package/src/yjs/extensions/ForkYDoc.ts +26 -1
  142. package/src/yjs/extensions/RelativePositionMapping.ts +8 -2
  143. package/src/yjs/extensions/Versioning.test.ts +17 -5
  144. package/types/src/api/getChangedRange.d.ts +14 -0
  145. package/types/src/api/pmUtil.d.ts +7 -7
  146. package/types/src/blocks/Code/CodeBlockOptions.d.ts +61 -0
  147. package/types/src/blocks/Code/block.d.ts +3 -53
  148. package/types/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.d.ts +35 -0
  149. package/types/src/blocks/Code/helpers/parse/parsePreCode.d.ts +8 -0
  150. package/types/src/blocks/Code/helpers/render/createCodeBlock.d.ts +18 -0
  151. package/types/src/blocks/Code/helpers/toExternalHTML/createPreCode.d.ts +5 -0
  152. package/types/src/blocks/index.d.ts +3 -1
  153. package/types/src/comments/extension.d.ts +1 -15
  154. package/types/src/editor/BlockNoteEditor.d.ts +3 -3
  155. package/types/src/editor/BlockNoteExtension.d.ts +1 -1
  156. package/types/src/exporter/ExportImage.d.ts +36 -0
  157. package/types/src/exporter/Exporter.d.ts +30 -0
  158. package/types/src/exporter/Exporter.test.d.ts +1 -0
  159. package/types/src/exporter/index.d.ts +1 -0
  160. package/types/src/exporter/mapping.d.ts +1 -1
  161. package/types/src/extensions/FilePanel/FilePanel.d.ts +1 -1
  162. package/types/src/extensions/FormattingToolbar/FormattingToolbar.d.ts +1 -1
  163. package/types/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.d.ts +5 -0
  164. package/types/src/extensions/ShowSelection/ShowSelection.d.ts +1 -5
  165. package/types/src/extensions/SideMenu/SideMenu.d.ts +1 -1
  166. package/types/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.d.ts +45 -0
  167. package/types/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.d.ts +49 -0
  168. package/types/src/extensions/SuggestionMenu/SuggestionMenu.d.ts +1 -11
  169. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.d.ts +44 -0
  170. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.d.ts +1 -0
  171. package/types/src/extensions/SyntaxHighlighting/inlineHighlight.test.d.ts +1 -0
  172. package/types/src/extensions/SyntaxHighlighting/shiki.d.ts +13 -0
  173. package/types/src/extensions/TableHandles/TableHandles.browser.test.d.ts +1 -0
  174. package/types/src/extensions/TableHandles/TableHandles.d.ts +4 -1
  175. package/types/src/extensions/Versioning/Versioning.d.ts +1 -35
  176. package/types/src/extensions/index.d.ts +4 -0
  177. package/types/src/i18n/locales/en.d.ts +9 -0
  178. package/types/src/i18n/locales/fa.d.ts +9 -0
  179. package/types/src/i18n/locales/sk.d.ts +9 -0
  180. package/types/src/index.d.ts +2 -1
  181. package/types/src/schema/blocks/types.d.ts +36 -3
  182. package/types/src/schema/inlineContent/createSpec.d.ts +35 -3
  183. package/types/src/schema/inlineContent/createSpec.test.d.ts +1 -0
  184. package/types/src/schema/inlineContent/internal.d.ts +3 -2
  185. package/types/src/schema/inlineContent/types.d.ts +22 -3
  186. package/types/src/schema/nodeViewMutations.d.ts +2 -2
  187. package/types/src/schema/schema.test.d.ts +1 -0
  188. package/types/src/user/UserStore.d.ts +1 -1
  189. package/types/src/util/Store.d.ts +45 -0
  190. package/types/src/util/Store.test.d.ts +1 -0
  191. package/types/src/y/extensions/AttributionExtension.d.ts +1 -1
  192. package/types/src/y/extensions/DiffVersioningExtension.d.ts +1 -1
  193. package/types/src/y/extensions/ForkYDoc.d.ts +1 -5
  194. package/types/src/y/extensions/index.d.ts +2 -10
  195. package/types/src/y/versioning/yhub.d.ts +3 -2
  196. package/types/src/yjs/comments/RESTYjsThreadStore.test.d.ts +1 -0
  197. package/types/src/yjs/extensions/ForkYDoc.d.ts +1 -5
  198. package/types/src/yjs/extensions/index.d.ts +1 -5
  199. package/dist/BlockNoteExtension-B_IEJHS8.js +0 -38
  200. package/dist/BlockNoteExtension-B_IEJHS8.js.map +0 -1
  201. package/dist/BlockNoteExtension-M0znTzxP.cjs +0 -2
  202. package/dist/BlockNoteExtension-M0znTzxP.cjs.map +0 -1
  203. package/dist/UserStore-QbbPFj0z.js.map +0 -1
  204. package/dist/blocks-BEJW9OM_.js.map +0 -1
  205. package/dist/blocks-BPmJglFo.cjs +0 -12
  206. package/dist/blocks-BPmJglFo.cjs.map +0 -1
  207. package/dist/en-C5Z4Ag9_.cjs +0 -2
  208. package/dist/en-C5Z4Ag9_.cjs.map +0 -1
  209. package/dist/en-CGKXfWma.js.map +0 -1
  210. package/dist/extensions-AitWbYdJ.cjs +0 -52
  211. package/dist/extensions-AitWbYdJ.cjs.map +0 -1
  212. package/dist/extensions-BR5hkAio.js.map +0 -1
  213. package/dist/src-BHPuOo7W.cjs +0 -21
  214. package/dist/src-BHPuOo7W.cjs.map +0 -1
  215. package/dist/src-BUXut68d.js.map +0 -1
  216. package/src/blocks/Code/shiki.ts +0 -73
  217. package/src/blocks/File/helpers/uploadToTmpFilesDotOrg_DEV_ONLY.ts +0 -20
  218. package/types/src/blocks/Code/shiki.d.ts +0 -4
  219. package/types/src/blocks/File/helpers/uploadToTmpFilesDotOrg_DEV_ONLY.d.ts +0 -6
@@ -0,0 +1,305 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test";
2
+
3
+ import { BlockNoteSchema } from "../../blocks/BlockNoteSchema.js";
4
+ import { defaultBlockSpecs } from "../../blocks/defaultBlocks.js";
5
+ import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
6
+ import { createBlockSpec } from "../../schema/index.js";
7
+ import { TableHandlesExtension } from "./TableHandles.js";
8
+
9
+ // Unit tests for the mouse handling in `TableHandlesView`, which decides
10
+ // whether the row/column handles should be attached to the cell under the
11
+ // cursor. It reads the DOM (`getBoundingClientRect`, node views, hit testing),
12
+ // so it runs in the browser suite rather than as a node unit test.
13
+
14
+ // A custom block which renders a real `<table>` of its own, without having any
15
+ // table content in the document. Table cells like these are indistinguishable
16
+ // from a table block's cells when only looking at the DOM.
17
+ const fakeTableBlock = createBlockSpec(
18
+ {
19
+ type: "fakeTable",
20
+ propSchema: {},
21
+ content: "none",
22
+ },
23
+ {
24
+ render: () => {
25
+ const dom = document.createElement("div");
26
+ dom.className = "fake-table";
27
+ dom.innerHTML =
28
+ "<table><tbody><tr><td>Not a real cell</td></tr></tbody></table>";
29
+
30
+ return { dom };
31
+ },
32
+ },
33
+ );
34
+
35
+ // A custom block which embeds a second editor, itself containing a table. The
36
+ // nested editor's cells live inside the outer editor's DOM, so they reach the
37
+ // outer editor's mouse handlers, but its block IDs are unknown to the outer
38
+ // document.
39
+ const nestedEditorBlock = createBlockSpec(
40
+ {
41
+ type: "nestedEditor",
42
+ propSchema: {},
43
+ content: "none",
44
+ },
45
+ {
46
+ render: () => {
47
+ const dom = document.createElement("div");
48
+ dom.className = "nested-editor";
49
+ dom.contentEditable = "false";
50
+
51
+ const nestedEditor = BlockNoteEditor.create({
52
+ initialContent: [
53
+ {
54
+ id: "nested-table",
55
+ type: "table",
56
+ content: {
57
+ type: "tableContent",
58
+ rows: [{ cells: ["Nested cell"] }],
59
+ },
60
+ },
61
+ ],
62
+ });
63
+ nestedEditor.mount(dom, { portalTarget: document.body });
64
+
65
+ return { dom, destroy: () => nestedEditor.unmount() };
66
+ },
67
+ },
68
+ );
69
+
70
+ const schema = BlockNoteSchema.create({
71
+ blockSpecs: {
72
+ ...defaultBlockSpecs,
73
+ fakeTable: fakeTableBlock(),
74
+ nestedEditor: nestedEditorBlock(),
75
+ },
76
+ });
77
+
78
+ describe("TableHandlesView mouse handling", () => {
79
+ let editor: BlockNoteEditor<any, any, any>;
80
+ let mountPoint: HTMLElement;
81
+
82
+ beforeEach(() => {
83
+ mountPoint = document.createElement("div");
84
+ document.body.appendChild(mountPoint);
85
+
86
+ editor = BlockNoteEditor.create({
87
+ schema,
88
+ initialContent: [
89
+ {
90
+ id: "table-0",
91
+ type: "table",
92
+ content: {
93
+ type: "tableContent",
94
+ rows: [
95
+ { cells: ["Cell 1", "Cell 2", "Cell 3"] },
96
+ { cells: ["Cell 4", "Cell 5", "Cell 6"] },
97
+ ],
98
+ },
99
+ },
100
+ { id: "paragraph-0", type: "paragraph", content: "Paragraph" },
101
+ { id: "fake-table-0", type: "fakeTable" },
102
+ { id: "nested-editor-0", type: "nestedEditor" },
103
+ ],
104
+ }) as BlockNoteEditor<any, any, any>;
105
+ editor.mount(mountPoint);
106
+ });
107
+
108
+ afterEach(() => {
109
+ editor.unmount();
110
+ editor._tiptapEditor.destroy();
111
+ mountPoint.remove();
112
+ });
113
+
114
+ function tableHandlesState() {
115
+ return editor.getExtension(TableHandlesExtension)!.store.state;
116
+ }
117
+
118
+ function queryElement(selector: string) {
119
+ const element = mountPoint.querySelector<HTMLElement>(selector);
120
+ if (!element) {
121
+ throw new Error(`No element matching "${selector}"`);
122
+ }
123
+ return element;
124
+ }
125
+
126
+ // The cell in the (only) real table block, at the given row and column.
127
+ function tableCell(rowIndex: number, colIndex: number) {
128
+ const cell = queryElement(
129
+ `[data-id="table-0"] tbody tr:nth-child(${rowIndex + 1})`,
130
+ ).children[colIndex];
131
+ if (!(cell instanceof HTMLElement)) {
132
+ throw new Error(`No cell at row ${rowIndex}, column ${colIndex}`);
133
+ }
134
+ return cell;
135
+ }
136
+
137
+ function dispatchMouseEvent(
138
+ type: "mousemove" | "mousedown",
139
+ el: HTMLElement,
140
+ ) {
141
+ const rect = el.getBoundingClientRect();
142
+ el.dispatchEvent(
143
+ new MouseEvent(type, {
144
+ bubbles: true,
145
+ clientX: rect.left + rect.width / 2,
146
+ clientY: rect.top + rect.height / 2,
147
+ }),
148
+ );
149
+ }
150
+
151
+ // Moves the mouse over `el` and asserts the handler survived it. An event
152
+ // listener that throws doesn't propagate the error to `dispatchEvent`'s
153
+ // caller - the browser reports it as an `error` event on `window` instead -
154
+ // so the error has to be picked up from there to be visible to the test.
155
+ function moveMouseOver(el: HTMLElement) {
156
+ const errors: unknown[] = [];
157
+ const collectError = (event: ErrorEvent) => {
158
+ errors.push(event.error ?? event.message);
159
+ };
160
+
161
+ window.addEventListener("error", collectError);
162
+ try {
163
+ dispatchMouseEvent("mousemove", el);
164
+ } finally {
165
+ window.removeEventListener("error", collectError);
166
+ }
167
+
168
+ expect(errors).toEqual([]);
169
+ }
170
+
171
+ it("attaches the handles to the hovered cell", () => {
172
+ moveMouseOver(tableCell(1, 1));
173
+
174
+ expect(tableHandlesState()).toMatchObject({
175
+ show: true,
176
+ rowIndex: 1,
177
+ colIndex: 1,
178
+ block: { id: "table-0" },
179
+ });
180
+ });
181
+
182
+ it("only offers the add/remove buttons on the last row and column", () => {
183
+ moveMouseOver(tableCell(0, 0));
184
+
185
+ expect(tableHandlesState()).toMatchObject({
186
+ showAddOrRemoveRowsButton: false,
187
+ showAddOrRemoveColumnsButton: false,
188
+ });
189
+
190
+ moveMouseOver(tableCell(1, 2));
191
+
192
+ expect(tableHandlesState()).toMatchObject({
193
+ showAddOrRemoveRowsButton: true,
194
+ showAddOrRemoveColumnsButton: true,
195
+ });
196
+ });
197
+
198
+ it("hides the handles when the mouse leaves the table", () => {
199
+ moveMouseOver(tableCell(0, 0));
200
+ expect(tableHandlesState()?.show).toBe(true);
201
+
202
+ moveMouseOver(queryElement('[data-id="paragraph-0"]'));
203
+
204
+ expect(tableHandlesState()?.show).toBe(false);
205
+ });
206
+
207
+ it("hides the handles while text is being selected with the mouse", () => {
208
+ moveMouseOver(tableCell(0, 0));
209
+ expect(tableHandlesState()?.show).toBe(true);
210
+
211
+ dispatchMouseEvent("mousedown", tableCell(0, 0));
212
+ moveMouseOver(tableCell(0, 1));
213
+
214
+ expect(tableHandlesState()?.show).toBe(false);
215
+ });
216
+
217
+ it("keeps the handles on the same cell while frozen", () => {
218
+ moveMouseOver(tableCell(0, 0));
219
+ editor.getExtension(TableHandlesExtension)!.freezeHandles();
220
+
221
+ moveMouseOver(tableCell(1, 2));
222
+
223
+ expect(tableHandlesState()).toMatchObject({
224
+ show: true,
225
+ rowIndex: 0,
226
+ colIndex: 0,
227
+ });
228
+
229
+ editor.getExtension(TableHandlesExtension)!.unfreezeHandles();
230
+ moveMouseOver(tableCell(1, 2));
231
+
232
+ expect(tableHandlesState()).toMatchObject({
233
+ show: true,
234
+ rowIndex: 1,
235
+ colIndex: 2,
236
+ });
237
+ });
238
+
239
+ it("does not show the handles when the editor is not editable", () => {
240
+ editor.isEditable = false;
241
+
242
+ moveMouseOver(tableCell(0, 0));
243
+
244
+ expect(tableHandlesState()?.show).toBeFalsy();
245
+ });
246
+
247
+ // Regression test for https://github.com/TypeCellOS/BlockNote/issues/2964.
248
+ // The hovered block used to be treated as a table as soon as the schema had
249
+ // a table block in it, so a custom block rendering a `<table>` ended up on
250
+ // the table path and crashed on its (absent) table content.
251
+ it("ignores cells of a custom block that renders its own table", () => {
252
+ moveMouseOver(queryElement(".fake-table td"));
253
+
254
+ expect(tableHandlesState()?.show).toBeFalsy();
255
+ });
256
+
257
+ it("hides the handles when moving from a table onto a custom block's table", () => {
258
+ moveMouseOver(tableCell(0, 0));
259
+ expect(tableHandlesState()?.show).toBe(true);
260
+
261
+ moveMouseOver(queryElement(".fake-table td"));
262
+
263
+ expect(tableHandlesState()?.show).toBe(false);
264
+ });
265
+
266
+ // The other half of #2964: the hovered cell belongs to a nested editor, so
267
+ // its block ID cannot be resolved in the outer editor's document. That used
268
+ // to throw `Block with ID <id> not found` on every mouse move.
269
+ it("ignores table cells belonging to a nested editor", () => {
270
+ moveMouseOver(queryElement(".nested-editor td"));
271
+
272
+ expect(tableHandlesState()?.show).toBeFalsy();
273
+ });
274
+
275
+ it("hides the handles when moving from a table onto a nested editor's table", () => {
276
+ moveMouseOver(tableCell(0, 0));
277
+ expect(tableHandlesState()?.show).toBe(true);
278
+
279
+ moveMouseOver(queryElement(".nested-editor td"));
280
+
281
+ expect(tableHandlesState()?.show).toBe(false);
282
+ });
283
+
284
+ // The table's position is captured when the handles attach to a cell, but
285
+ // the table shifts whenever content before it changes - a collaborator or an
286
+ // extension editing while a handle menu sits open, say. Acting on the handles
287
+ // afterwards used to resolve the stale position, landing in the wrong node.
288
+ it("acts on the hovered table after content is inserted before it", () => {
289
+ moveMouseOver(tableCell(0, 0));
290
+
291
+ editor.insertBlocks(
292
+ [{ type: "paragraph", content: "Inserted" }],
293
+ "table-0",
294
+ "before",
295
+ );
296
+
297
+ editor
298
+ .getExtension(TableHandlesExtension)!
299
+ .addRowOrColumn(0, { orientation: "row", side: "below" });
300
+
301
+ expect(
302
+ mountPoint.querySelectorAll('[data-id="table-0"] tbody tr'),
303
+ ).toHaveLength(3);
304
+ });
305
+ });
@@ -1,3 +1,4 @@
1
+ import type { Node } from "prosemirror-model";
1
2
  import { EditorState, Plugin, PluginKey, PluginView } from "prosemirror-state";
2
3
  import {
3
4
  CellSelection,
@@ -27,7 +28,7 @@ import {
27
28
  import { nodeToBlock } from "../../api/nodeConversions/nodeToBlock.js";
28
29
  import { getNodeById } from "../../api/nodeUtil.js";
29
30
  import {
30
- editorHasBlockWithType,
31
+ blockHasType,
31
32
  isTableCellNode,
32
33
  isTableCellSelection,
33
34
  } from "../../blocks/defaultBlockTypeGuards.js";
@@ -188,6 +189,17 @@ export class TableHandlesView implements PluginView {
188
189
  this.mouseState = "down";
189
190
  };
190
191
 
192
+ // Hides the handles if they're currently shown. Used whenever the mouse moves
193
+ // somewhere that shouldn't have table handles attached to it.
194
+ hideHandles = () => {
195
+ if (this.state?.show) {
196
+ this.state.show = false;
197
+ this.state.showAddOrRemoveRowsButton = false;
198
+ this.state.showAddOrRemoveColumnsButton = false;
199
+ this.emitUpdate();
200
+ }
201
+ };
202
+
191
203
  mouseUpHandler = (event: MouseEvent) => {
192
204
  this.mouseState = "up";
193
205
  this.mouseMoveHandler(event);
@@ -219,22 +231,12 @@ export class TableHandlesView implements PluginView {
219
231
  // hide draghandles when selecting text as they could be in the way of the user
220
232
  this.mouseState = "selecting";
221
233
 
222
- if (this.state?.show) {
223
- this.state.show = false;
224
- this.state.showAddOrRemoveRowsButton = false;
225
- this.state.showAddOrRemoveColumnsButton = false;
226
- this.emitUpdate();
227
- }
234
+ this.hideHandles();
228
235
  return;
229
236
  }
230
237
 
231
238
  if (!target || !this.editor.isEditable) {
232
- if (this.state?.show) {
233
- this.state.show = false;
234
- this.state.showAddOrRemoveRowsButton = false;
235
- this.state.showAddOrRemoveColumnsButton = false;
236
- this.emitUpdate();
237
- }
239
+ this.hideHandles();
238
240
  return;
239
241
  }
240
242
 
@@ -248,38 +250,40 @@ export class TableHandlesView implements PluginView {
248
250
  if (!blockEl) {
249
251
  return;
250
252
  }
251
- this.tableElement = blockEl.node;
252
-
253
- let tableBlock:
254
- | BlockFromConfigNoChildren<DefaultBlockSchema["table"], any, any>
255
- | undefined;
256
253
 
257
254
  const { pmNodeInfo, doc } = this.editor.transact((tr) => ({
258
255
  pmNodeInfo: getNodeById(blockEl.id, tr.doc),
259
256
  doc: tr.doc,
260
257
  }));
258
+
259
+ // The hovered cell may belong to a document other than this editor's, as a
260
+ // custom block can embed a nested editor which itself contains a table. The
261
+ // nested editor's DOM is inside this view's DOM, so its cells still reach
262
+ // this handler, but its block IDs are unknown here.
261
263
  if (!pmNodeInfo) {
262
- throw new Error(`Block with ID ${blockEl.id} not found`);
264
+ this.hideHandles();
265
+ return;
263
266
  }
264
267
 
265
- const block = nodeToBlock(
266
- pmNodeInfo.node,
267
- doc,
268
- ) as unknown as BlockFromConfigNoChildren<
268
+ const block = nodeToBlock(pmNodeInfo.node, doc);
269
+
270
+ // `domCellAround` matches any `TD`/`TH` in the DOM, so the hovered block may
271
+ // just as well be a custom block that renders a table of its own. Checking
272
+ // the block itself (rather than only whether the schema has a table block)
273
+ // keeps the handles off blocks that have no table content to work with.
274
+ if (!blockHasType(block, this.editor, "table")) {
275
+ this.hideHandles();
276
+ return;
277
+ }
278
+
279
+ const tableBlock = block as unknown as BlockFromConfigNoChildren<
269
280
  DefaultBlockSchema["table"],
270
281
  any,
271
282
  any
272
283
  >;
273
284
 
274
- if (editorHasBlockWithType(this.editor, "table")) {
275
- this.tablePos = pmNodeInfo.posBeforeNode + 1;
276
- tableBlock = block;
277
- }
278
-
279
- if (!tableBlock) {
280
- return;
281
- }
282
-
285
+ this.tablePos = pmNodeInfo.posBeforeNode + 1;
286
+ this.tableElement = blockEl.node;
283
287
  this.tableId = blockEl.id;
284
288
  const widgetContainer = target.domNode
285
289
  .closest(".tableWrapper")
@@ -527,6 +531,22 @@ export class TableHandlesView implements PluginView {
527
531
 
528
532
  return true;
529
533
  };
534
+
535
+ // Resolves the position just inside the hovered table, within `doc`. The
536
+ // position is looked up by ID on each use rather than read from a cached one,
537
+ // as anything changing before the table (a collaborator inserting a block
538
+ // above it, say) shifts it: a stale position resolves into the wrong node, or
539
+ // past the end of the document entirely.
540
+ getTablePos(doc: Node): number | undefined {
541
+ if (this.tableId === undefined) {
542
+ return undefined;
543
+ }
544
+
545
+ const pmNodeInfo = getNodeById(this.tableId, doc);
546
+
547
+ return pmNodeInfo && pmNodeInfo.posBeforeNode + 1;
548
+ }
549
+
530
550
  // Updates drag handles when the table is modified or removed.
531
551
  update() {
532
552
  if (!this.state || !this.state.show) {
@@ -544,6 +564,7 @@ export class TableHandlesView implements PluginView {
544
564
  ) {
545
565
  this.state = undefined;
546
566
  this.tableId = undefined;
567
+ this.tablePos = undefined;
547
568
  this.tableElement = undefined;
548
569
  this.emitUpdate();
549
570
 
@@ -551,6 +572,9 @@ export class TableHandlesView implements PluginView {
551
572
  }
552
573
  this.state.block = refreshedBlock as typeof this.state.block;
553
574
 
575
+ // The table may have shifted in this update, so re-resolve its position.
576
+ this.tablePos = this.getTablePos(this.pmView.state.doc);
577
+
554
578
  const { height: rowCount, width: colCount } = getDimensionsOfTable(
555
579
  this.state.block,
556
580
  );
@@ -647,12 +671,21 @@ export const TableHandlesExtension = createExtension(({ editor }) => {
647
671
  if (
648
672
  view === undefined ||
649
673
  view.state === undefined ||
650
- view.state.draggingState === undefined ||
651
- view.tablePos === undefined
674
+ view.state.draggingState === undefined
652
675
  ) {
653
676
  return;
654
677
  }
655
678
 
679
+ // Resolved against the state being rendered rather than read from
680
+ // `view.tablePos`, as ProseMirror computes decorations for a
681
+ // transaction before it calls the plugin view's `update` - so the
682
+ // cached position is still the pre-transaction one here.
683
+ const tablePos = view.getTablePos(state.doc);
684
+
685
+ if (tablePos === undefined) {
686
+ return;
687
+ }
688
+
656
689
  const newIndex =
657
690
  view.state.draggingState.draggedCellOrientation === "row"
658
691
  ? view.state.rowIndex
@@ -683,7 +716,7 @@ export const TableHandlesExtension = createExtension(({ editor }) => {
683
716
  }
684
717
 
685
718
  // Gets the table to show the drop cursor in.
686
- const tableResolvedPos = state.doc.resolve(view.tablePos + 1);
719
+ const tableResolvedPos = state.doc.resolve(tablePos + 1);
687
720
 
688
721
  if (view.state.draggingState.draggedCellOrientation === "row") {
689
722
  const cellsInRow = getCellsAtRowHandle(
@@ -916,11 +949,8 @@ export const TableHandlesExtension = createExtension(({ editor }) => {
916
949
  * interfering with open submenus.
917
950
  */
918
951
  hideHandlesIfNotFrozen() {
919
- if (!view!.menuFrozen && view!.state?.show) {
920
- view!.state.show = false;
921
- view!.state.showAddOrRemoveRowsButton = false;
922
- view!.state.showAddOrRemoveColumnsButton = false;
923
- view!.emitUpdate();
952
+ if (!view!.menuFrozen) {
953
+ view!.hideHandles();
924
954
  }
925
955
  },
926
956
 
@@ -954,7 +984,18 @@ export const TableHandlesExtension = createExtension(({ editor }) => {
954
984
  throw new Error("Table handles view not initialized");
955
985
  }
956
986
 
957
- const tableResolvedPos = state.doc.resolve(view.tablePos! + 1);
987
+ // Resolved against `state`, which may be several transactions ahead of
988
+ // the mouse move that attached the handles (a menu can stay open across
989
+ // edits from elsewhere, e.g. a collaborator).
990
+ const tablePos = view.getTablePos(state.doc);
991
+
992
+ if (tablePos === undefined) {
993
+ throw new Error(
994
+ "Attempted to select table cells, but the hovered table is no longer in the document.",
995
+ );
996
+ }
997
+
998
+ const tableResolvedPos = state.doc.resolve(tablePos + 1);
958
999
  const startRowResolvedPos = state.doc.resolve(
959
1000
  tableResolvedPos.posAtIndex(relativeStartCell.row) + 1,
960
1001
  );
@@ -136,13 +136,23 @@ export function createInMemoryVersioningEndpoints(): VersioningEndpoints<
136
136
  const contents = new Map<string, Block<any, any, any>[]>();
137
137
  let nextId = 1;
138
138
 
139
+ // `Date.now()` only has millisecond resolution, so two snapshots created in
140
+ // the same tick would share a timestamp and `sortSnapshotsNewestFirst` (which
141
+ // has nothing else to order on) could list them oldest-first. Hand out
142
+ // strictly increasing timestamps so creation order is always preserved.
143
+ let lastTimestamp = 0;
144
+ function nextTimestamp() {
145
+ lastTimestamp = Math.max(Date.now(), lastTimestamp + 1);
146
+ return lastTimestamp;
147
+ }
148
+
139
149
  return {
140
150
  async list() {
141
151
  return sortSnapshotsNewestFirst([...snapshots]);
142
152
  },
143
153
 
144
154
  async create(currentDoc, options) {
145
- const now = Date.now();
155
+ const now = nextTimestamp();
146
156
  const id = String(nextId++);
147
157
  const snapshot: VersionSnapshot = {
148
158
  id,
@@ -166,7 +176,7 @@ export function createInMemoryVersioningEndpoints(): VersioningEndpoints<
166
176
 
167
177
  // Create a "Restored from …" snapshot of the current state before
168
178
  // restoring, so the user can undo the restore.
169
- const now = Date.now();
179
+ const now = nextTimestamp();
170
180
  const backupId = String(nextId++);
171
181
  const backup: VersionSnapshot = {
172
182
  id: backupId,
@@ -196,7 +206,7 @@ export function createInMemoryVersioningEndpoints(): VersioningEndpoints<
196
206
  throw new Error(`Snapshot ${String(snapshot.id)} not found`);
197
207
  }
198
208
  stored.name = name;
199
- stored.updatedAt = Date.now();
209
+ stored.updatedAt = nextTimestamp();
200
210
  },
201
211
 
202
212
  async remove(snapshot) {
@@ -3,6 +3,7 @@ export * from "./DropCursor/DropCursor.js";
3
3
  export * from "./FilePanel/FilePanel.js";
4
4
  export * from "./FormattingToolbar/FormattingToolbar.js";
5
5
  export * from "./History/History.js";
6
+ export * from "./InlineContentBoundaryEdit/InlineContentBoundaryEdit.js";
6
7
  export * from "./LinkToolbar/LinkToolbar.js";
7
8
  export * from "./LinkToolbar/protocols.js";
8
9
  export * from "./NodeSelectionKeyboard/NodeSelectionKeyboard.js";
@@ -11,7 +12,10 @@ export * from "./PositionMapping/PositionMapping.js";
11
12
  export * from "./PreviousBlockType/PreviousBlockType.js";
12
13
  export * from "./ShowSelection/ShowSelection.js";
13
14
  export * from "./SideMenu/SideMenu.js";
15
+ export * from "./SourceBlockWithPreview/SourceBlockWithPreview.js";
16
+ export * from "./SourceInlineContentWithPreview/SourceInlineContentWithPreview.js";
14
17
  export * from "./SuggestionMenu/DefaultGridSuggestionItem.js";
18
+ export * from "./SyntaxHighlighting/SyntaxHighlighting.js";
15
19
  export * from "./SuggestionMenu/DefaultSuggestionItem.js";
16
20
  export * from "./SuggestionMenu/getDefaultEmojiPickerItems.js";
17
21
  export * from "./SuggestionMenu/getDefaultSlashMenuItems.js";
@@ -18,15 +18,17 @@ import { createBlockSpec } from "../../../schema/index.js";
18
18
  const createHardBreakTestBlockSpec = <
19
19
  const T extends string,
20
20
  const S extends "shift+enter" | "enter" | "none",
21
+ const C extends "inline" | "plain",
21
22
  >(
22
23
  type: T,
23
24
  hardBreakShortcut: S,
25
+ content: C = "inline" as C,
24
26
  ) =>
25
27
  createBlockSpec(
26
28
  {
27
29
  type,
28
30
  propSchema: {},
29
- content: "inline",
31
+ content,
30
32
  },
31
33
  {
32
34
  meta: {
@@ -47,11 +49,22 @@ const schema = BlockNoteSchema.create({
47
49
  ...defaultBlockSpecs,
48
50
  hardBreakEnter: createHardBreakTestBlockSpec("hardBreakEnter", "enter"),
49
51
  hardBreakNone: createHardBreakTestBlockSpec("hardBreakNone", "none"),
52
+ // "plain" content (`text*`) can't hold a `hardBreak` node, so these blocks
53
+ // insert a literal newline character instead - e.g. code/math/diagram source.
54
+ hardBreakEnterPlain: createHardBreakTestBlockSpec(
55
+ "hardBreakEnterPlain",
56
+ "enter",
57
+ "plain",
58
+ ),
50
59
  },
51
60
  });
52
61
 
53
62
  function createEditor(
54
- blockType: "paragraph" | "hardBreakEnter" | "hardBreakNone",
63
+ blockType:
64
+ | "paragraph"
65
+ | "hardBreakEnter"
66
+ | "hardBreakNone"
67
+ | "hardBreakEnterPlain",
55
68
  ) {
56
69
  const editor = BlockNoteEditor.create({
57
70
  schema,
@@ -87,6 +100,16 @@ function countHardBreaks(editor: BlockNoteEditor<any, any, any>) {
87
100
  return count;
88
101
  }
89
102
 
103
+ function getTextContent(editor: BlockNoteEditor<any, any, any>) {
104
+ let text = "";
105
+ editor._tiptapEditor.state.doc.descendants((node) => {
106
+ if (node.isText) {
107
+ text += node.text;
108
+ }
109
+ });
110
+ return text;
111
+ }
112
+
90
113
  describe("KeyboardShortcutsExtension hardBreakShortcut", () => {
91
114
  it("inserts a hard break on Shift-Enter by default", () => {
92
115
  const editor = createEditor("paragraph");
@@ -152,4 +175,30 @@ describe("KeyboardShortcutsExtension hardBreakShortcut", () => {
152
175
 
153
176
  editor._tiptapEditor.destroy();
154
177
  });
178
+
179
+ it('inserts a newline character on Enter when content is "plain"', () => {
180
+ const editor = createEditor("hardBreakEnterPlain");
181
+
182
+ pressKeys(editor, "Enter");
183
+
184
+ // A "plain" block can't hold a `hardBreak` node, so no node is inserted and
185
+ // the block is not split - a literal newline is added to its text instead.
186
+ expect(countHardBreaks(editor)).toBe(0);
187
+ expect(editor.document.length).toBe(1);
188
+ expect(getTextContent(editor)).toBe("Hello world\n");
189
+
190
+ editor._tiptapEditor.destroy();
191
+ });
192
+
193
+ it('inserts a newline character on Shift-Enter when content is "plain"', () => {
194
+ const editor = createEditor("hardBreakEnterPlain");
195
+
196
+ pressKeys(editor, "Shift-Enter");
197
+
198
+ expect(countHardBreaks(editor)).toBe(0);
199
+ expect(editor.document.length).toBe(1);
200
+ expect(getTextContent(editor)).toBe("Hello world\n");
201
+
202
+ editor._tiptapEditor.destroy();
203
+ });
155
204
  });