@blocknote/core 0.47.2 → 0.48.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-B6RhuQ3V.js +27 -0
- package/dist/BlockNoteExtension-B6RhuQ3V.js.map +1 -0
- package/dist/BlockNoteExtension-ab0REuob.cjs +2 -0
- package/dist/BlockNoteExtension-ab0REuob.cjs.map +1 -0
- package/dist/EventEmitter-CuPnGDYt.js +22 -0
- package/dist/EventEmitter-CuPnGDYt.js.map +1 -0
- package/dist/EventEmitter-DHdSnYCD.cjs +2 -0
- package/dist/EventEmitter-DHdSnYCD.cjs.map +1 -0
- package/dist/ShowSelection-BsHFfdMi.js +29 -0
- package/dist/ShowSelection-BsHFfdMi.js.map +1 -0
- package/dist/ShowSelection-DhXglbt8.cjs +2 -0
- package/dist/ShowSelection-DhXglbt8.cjs.map +1 -0
- package/dist/blocknote.cjs +1 -5
- package/dist/blocknote.js +7 -4216
- package/dist/blocks-BEvIOzln.cjs +11 -0
- package/dist/blocks-BEvIOzln.cjs.map +1 -0
- package/dist/blocks-DYl7UTS4.js +3313 -0
- package/dist/blocks-DYl7UTS4.js.map +1 -0
- package/dist/blocks.cjs +1 -2
- package/dist/blocks.js +2 -74
- package/dist/comments.cjs +2 -2
- package/dist/comments.cjs.map +1 -1
- package/dist/comments.js +604 -900
- package/dist/comments.js.map +1 -1
- package/dist/en-C4ykW581.cjs +2 -0
- package/dist/en-C4ykW581.cjs.map +1 -0
- package/dist/en-QsSI16s5.js +453 -0
- package/dist/en-QsSI16s5.js.map +1 -0
- package/dist/extensions-3nf9PPhA.js +1495 -0
- package/dist/extensions-3nf9PPhA.js.map +1 -0
- package/dist/extensions-D9y6evn6.cjs +2 -0
- package/dist/extensions-D9y6evn6.cjs.map +1 -0
- package/dist/extensions.cjs +1 -2
- package/dist/extensions.js +4 -36
- package/dist/locales.cjs +2 -2
- package/dist/locales.cjs.map +1 -1
- package/dist/locales.js +10049 -9111
- package/dist/locales.js.map +1 -1
- package/dist/src-BKJH7Jla.cjs +5 -0
- package/dist/src-BKJH7Jla.cjs.map +1 -0
- package/dist/src-Hjv1f5lc.js +2386 -0
- package/dist/src-Hjv1f5lc.js.map +1 -0
- package/dist/style.css +2 -1
- package/dist/webpack-stats.json +1 -1
- package/dist/yjs.cjs +2 -2
- package/dist/yjs.cjs.map +1 -1
- package/dist/yjs.js +20 -38
- package/dist/yjs.js.map +1 -1
- package/package.json +8 -8
- package/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.test.ts +15 -1
- package/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.ts +5 -0
- package/src/api/blockManipulation/commands/nestBlock/__snapshots__/nestBlock.test.ts.snap +1095 -0
- package/src/api/blockManipulation/commands/nestBlock/nestBlock.test.ts +661 -0
- package/src/api/blockManipulation/commands/nestBlock/nestBlock.ts +128 -11
- package/src/api/parsers/html/parseHTML.ts +2 -0
- package/src/api/parsers/html/util/normalizeWhitespace.ts +87 -0
- package/src/blocks/File/helpers/render/createResizableFileBlockWrapper.ts +17 -23
- package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.test.ts +346 -0
- package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.ts +143 -71
- package/src/editor/Block.css +5 -8
- package/src/editor/BlockNoteEditor.ts +38 -1
- package/src/editor/editor.css +0 -20
- package/src/editor/managers/ExtensionManager/extensions.ts +12 -1
- package/src/editor/performance.test.ts +134 -0
- package/src/extensions/Collaboration/ForkYDoc.test.ts +115 -103
- package/src/extensions/Placeholder/Placeholder.ts +1 -1
- package/src/extensions/PreviousBlockType/PreviousBlockType.test.ts +86 -0
- package/src/extensions/PreviousBlockType/PreviousBlockType.ts +33 -49
- package/src/extensions/SideMenu/SideMenu.ts +14 -8
- package/src/extensions/TableHandles/TableHandles.ts +14 -0
- package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +57 -17
- package/src/extensions/tiptap-extensions/UniqueID/UniqueID.ts +9 -8
- package/src/index.ts +1 -1
- package/src/schema/blocks/createSpec.ts +6 -2
- package/src/schema/inlineContent/types.ts +0 -1
- package/src/vite-env.d.ts +6 -0
- package/types/src/api/blockManipulation/commands/nestBlock/nestBlock.d.ts +14 -1
- package/types/src/api/blockManipulation/commands/nestBlock/nestBlock.test.d.ts +1 -0
- package/types/src/api/parsers/html/util/normalizeWhitespace.d.ts +6 -0
- package/types/src/blocks/ListItem/NumberedListItem/IndexingPlugin.test.d.ts +1 -0
- package/types/src/editor/BlockNoteEditor.d.ts +13 -0
- package/types/src/editor/performance.test.d.ts +1 -0
- package/types/src/extensions/PreviousBlockType/PreviousBlockType.test.d.ts +1 -0
- package/types/src/extensions/SideMenu/SideMenu.d.ts +6 -0
- package/types/src/extensions/TableHandles/TableHandles.d.ts +6 -0
- package/dist/BlockNoteExtension-BWw0r8Gy.cjs +0 -2
- package/dist/BlockNoteExtension-BWw0r8Gy.cjs.map +0 -1
- package/dist/BlockNoteExtension-C2X7LW-V.js +0 -25
- package/dist/BlockNoteExtension-C2X7LW-V.js.map +0 -1
- package/dist/BlockNoteSchema-CCs_V3lo.cjs +0 -2
- package/dist/BlockNoteSchema-CCs_V3lo.cjs.map +0 -1
- package/dist/BlockNoteSchema-ooiKsd5B.js +0 -270
- package/dist/BlockNoteSchema-ooiKsd5B.js.map +0 -1
- package/dist/EventEmitter-CLwfmbqG.cjs +0 -2
- package/dist/EventEmitter-CLwfmbqG.cjs.map +0 -1
- package/dist/EventEmitter-CjSwpTbz.js +0 -27
- package/dist/EventEmitter-CjSwpTbz.js.map +0 -1
- package/dist/ShowSelection-B0ch3unP.js +0 -51
- package/dist/ShowSelection-B0ch3unP.js.map +0 -1
- package/dist/ShowSelection-BxnbRvy4.cjs +0 -2
- package/dist/ShowSelection-BxnbRvy4.cjs.map +0 -1
- package/dist/TrailingNode-GzE59m_7.js +0 -2288
- package/dist/TrailingNode-GzE59m_7.js.map +0 -1
- package/dist/TrailingNode-n0WdMPUl.cjs +0 -2
- package/dist/TrailingNode-n0WdMPUl.cjs.map +0 -1
- package/dist/blockToNode-BNoNIXU7.js +0 -1140
- package/dist/blockToNode-BNoNIXU7.js.map +0 -1
- package/dist/blockToNode-CumVjgem.cjs +0 -7
- package/dist/blockToNode-CumVjgem.cjs.map +0 -1
- package/dist/blocknote.cjs.map +0 -1
- package/dist/blocknote.js.map +0 -1
- package/dist/blocks.cjs.map +0 -1
- package/dist/blocks.js.map +0 -1
- package/dist/defaultBlocks-Dg9kQWXm.cjs +0 -6
- package/dist/defaultBlocks-Dg9kQWXm.cjs.map +0 -1
- package/dist/defaultBlocks-ZzGbYgQn.js +0 -3284
- package/dist/defaultBlocks-ZzGbYgQn.js.map +0 -1
- package/dist/en-Cl87Uuyf.cjs +0 -2
- package/dist/en-Cl87Uuyf.cjs.map +0 -1
- package/dist/en-njEqD7AG.js +0 -408
- package/dist/en-njEqD7AG.js.map +0 -1
- package/dist/extensions.cjs.map +0 -1
- package/dist/extensions.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { Selection } from "prosemirror-state";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { BlockNoteEditor } from "../../../editor/BlockNoteEditor.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @vitest-environment jsdom
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const PLUGIN_KEY = "numbered-list-indexing-decorations$";
|
|
11
|
+
|
|
12
|
+
function createEditor() {
|
|
13
|
+
const editor = BlockNoteEditor.create();
|
|
14
|
+
editor.mount(document.createElement("div"));
|
|
15
|
+
return editor;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function getDecorationSet(editor: BlockNoteEditor<any, any, any>) {
|
|
19
|
+
const view = editor._tiptapEditor.view;
|
|
20
|
+
const plugin = view.state.plugins.find(
|
|
21
|
+
(p) => (p as any).key === PLUGIN_KEY,
|
|
22
|
+
);
|
|
23
|
+
if (!plugin) {
|
|
24
|
+
throw new Error("IndexingPlugin not found");
|
|
25
|
+
}
|
|
26
|
+
return plugin.getState(view.state)!.decorations;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Returns all decoration specs in document order. */
|
|
30
|
+
function getDecoSpecs(editor: BlockNoteEditor<any, any, any>) {
|
|
31
|
+
const decoSet = getDecorationSet(editor);
|
|
32
|
+
const doc = editor._tiptapEditor.view.state.doc;
|
|
33
|
+
const decos = decoSet.find(0, doc.nodeSize - 2);
|
|
34
|
+
return decos.map((d: any) => d.spec);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Returns the data-index values from decoration attrs in document order. */
|
|
38
|
+
function getDataIndices(editor: BlockNoteEditor<any, any, any>) {
|
|
39
|
+
const decoSet = getDecorationSet(editor);
|
|
40
|
+
const doc = editor._tiptapEditor.view.state.doc;
|
|
41
|
+
const decos = decoSet.find(0, doc.nodeSize - 2);
|
|
42
|
+
return decos.map((d: any) => {
|
|
43
|
+
// Decoration attrs are stored on the decoration object
|
|
44
|
+
const attrs =
|
|
45
|
+
(d as any).type?.attrs ?? (d as any).attrs ?? (d as any).type;
|
|
46
|
+
return parseInt(attrs["data-index"], 10);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function setBlocks(
|
|
51
|
+
editor: BlockNoteEditor<any, any, any>,
|
|
52
|
+
blocks: Array<{ type: string; content?: string; props?: any }>,
|
|
53
|
+
) {
|
|
54
|
+
editor.replaceBlocks(
|
|
55
|
+
editor.document,
|
|
56
|
+
blocks.map((b) => ({
|
|
57
|
+
type: b.type as any,
|
|
58
|
+
content: b.content ?? "text",
|
|
59
|
+
...(b.props ? { props: b.props } : {}),
|
|
60
|
+
})) as any,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
describe("IndexingPlugin: basic numbering", () => {
|
|
65
|
+
it("assigns sequential indices to a contiguous numbered list", () => {
|
|
66
|
+
const editor = createEditor();
|
|
67
|
+
setBlocks(editor, [
|
|
68
|
+
{ type: "numberedListItem", content: "a" },
|
|
69
|
+
{ type: "numberedListItem", content: "b" },
|
|
70
|
+
{ type: "numberedListItem", content: "c" },
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
const indices = getDataIndices(editor);
|
|
74
|
+
expect(indices).toEqual([1, 2, 3]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("resets index after a non-list block", () => {
|
|
78
|
+
const editor = createEditor();
|
|
79
|
+
setBlocks(editor, [
|
|
80
|
+
{ type: "numberedListItem", content: "a" },
|
|
81
|
+
{ type: "numberedListItem", content: "b" },
|
|
82
|
+
{ type: "paragraph", content: "break" },
|
|
83
|
+
{ type: "numberedListItem", content: "c" },
|
|
84
|
+
{ type: "numberedListItem", content: "d" },
|
|
85
|
+
]);
|
|
86
|
+
|
|
87
|
+
const indices = getDataIndices(editor);
|
|
88
|
+
expect(indices).toEqual([1, 2, 1, 2]);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("single numbered list item gets index 1", () => {
|
|
92
|
+
const editor = createEditor();
|
|
93
|
+
setBlocks(editor, [{ type: "numberedListItem", content: "only" }]);
|
|
94
|
+
|
|
95
|
+
const indices = getDataIndices(editor);
|
|
96
|
+
expect(indices).toEqual([1]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("no decorations for non-list blocks", () => {
|
|
100
|
+
const editor = createEditor();
|
|
101
|
+
setBlocks(editor, [
|
|
102
|
+
{ type: "paragraph", content: "a" },
|
|
103
|
+
{ type: "heading", content: "b", props: { level: 1 } },
|
|
104
|
+
]);
|
|
105
|
+
|
|
106
|
+
const indices = getDataIndices(editor);
|
|
107
|
+
expect(indices).toEqual([]);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe("IndexingPlugin: updates on structural changes", () => {
|
|
112
|
+
it("updates indices when a block is deleted from the middle", () => {
|
|
113
|
+
const editor = createEditor();
|
|
114
|
+
setBlocks(editor, [
|
|
115
|
+
{ type: "numberedListItem", content: "a" },
|
|
116
|
+
{ type: "numberedListItem", content: "b" },
|
|
117
|
+
{ type: "numberedListItem", content: "c" },
|
|
118
|
+
]);
|
|
119
|
+
|
|
120
|
+
// Delete the second block
|
|
121
|
+
const secondBlock = editor.document[1];
|
|
122
|
+
editor.removeBlocks([secondBlock]);
|
|
123
|
+
|
|
124
|
+
const indices = getDataIndices(editor);
|
|
125
|
+
expect(indices).toEqual([1, 2]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("updates indices when a block is inserted in the middle", () => {
|
|
129
|
+
const editor = createEditor();
|
|
130
|
+
setBlocks(editor, [
|
|
131
|
+
{ type: "numberedListItem", content: "a" },
|
|
132
|
+
{ type: "numberedListItem", content: "c" },
|
|
133
|
+
]);
|
|
134
|
+
|
|
135
|
+
// Insert a block after the first
|
|
136
|
+
const firstBlock = editor.document[0];
|
|
137
|
+
editor.insertBlocks(
|
|
138
|
+
[{ type: "numberedListItem" as any, content: "b" } as any],
|
|
139
|
+
firstBlock,
|
|
140
|
+
"after",
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const indices = getDataIndices(editor);
|
|
144
|
+
expect(indices).toEqual([1, 2, 3]);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("updates indices when first block is deleted", () => {
|
|
148
|
+
const editor = createEditor();
|
|
149
|
+
setBlocks(editor, [
|
|
150
|
+
{ type: "numberedListItem", content: "a" },
|
|
151
|
+
{ type: "numberedListItem", content: "b" },
|
|
152
|
+
{ type: "numberedListItem", content: "c" },
|
|
153
|
+
]);
|
|
154
|
+
|
|
155
|
+
editor.removeBlocks([editor.document[0]]);
|
|
156
|
+
|
|
157
|
+
const indices = getDataIndices(editor);
|
|
158
|
+
expect(indices).toEqual([1, 2]);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("updates indices with nested list when first block is deleted", () => {
|
|
162
|
+
const editor = createEditor();
|
|
163
|
+
editor.replaceBlocks(editor.document, [
|
|
164
|
+
{
|
|
165
|
+
type: "numberedListItem" as any,
|
|
166
|
+
content: "first item",
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
type: "numberedListItem" as any,
|
|
170
|
+
content: "second item",
|
|
171
|
+
children: [
|
|
172
|
+
{ type: "numberedListItem" as any, content: "nested item" },
|
|
173
|
+
{ type: "numberedListItem" as any, content: "second nested item" },
|
|
174
|
+
],
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
type: "numberedListItem" as any,
|
|
178
|
+
content: "third item",
|
|
179
|
+
},
|
|
180
|
+
] as any);
|
|
181
|
+
|
|
182
|
+
// Before deletion: top-level [1, 2, 3], nested [1, 2]
|
|
183
|
+
const indicesBefore = getDataIndices(editor);
|
|
184
|
+
expect(indicesBefore).toEqual([1, 2, 1, 2, 3]);
|
|
185
|
+
|
|
186
|
+
// Delete first item
|
|
187
|
+
editor.removeBlocks([editor.document[0]]);
|
|
188
|
+
|
|
189
|
+
// After deletion: top-level [1, 2], nested [1, 2]
|
|
190
|
+
const indicesAfter = getDataIndices(editor);
|
|
191
|
+
expect(indicesAfter).toEqual([1, 1, 2, 2]);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("updates indices when block type changes from numbered list to paragraph", () => {
|
|
195
|
+
const editor = createEditor();
|
|
196
|
+
setBlocks(editor, [
|
|
197
|
+
{ type: "numberedListItem", content: "a" },
|
|
198
|
+
{ type: "numberedListItem", content: "b" },
|
|
199
|
+
{ type: "numberedListItem", content: "c" },
|
|
200
|
+
]);
|
|
201
|
+
|
|
202
|
+
// Change second block to paragraph — splits the list
|
|
203
|
+
editor.updateBlock(editor.document[1], { type: "paragraph" });
|
|
204
|
+
|
|
205
|
+
const indices = getDataIndices(editor);
|
|
206
|
+
// First list: [1], then paragraph (no decoration), then new list: [1]
|
|
207
|
+
expect(indices).toEqual([1, 1]);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("updates indices when block type changes from paragraph to numbered list", () => {
|
|
211
|
+
const editor = createEditor();
|
|
212
|
+
setBlocks(editor, [
|
|
213
|
+
{ type: "numberedListItem", content: "a" },
|
|
214
|
+
{ type: "paragraph", content: "b" },
|
|
215
|
+
{ type: "numberedListItem", content: "c" },
|
|
216
|
+
]);
|
|
217
|
+
|
|
218
|
+
// Change paragraph to numbered list — merges the lists
|
|
219
|
+
editor.updateBlock(editor.document[1], { type: "numberedListItem" });
|
|
220
|
+
|
|
221
|
+
const indices = getDataIndices(editor);
|
|
222
|
+
expect(indices).toEqual([1, 2, 3]);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe("IndexingPlugin: typing preserves indices (early exit)", () => {
|
|
227
|
+
it("indices unchanged after typing in the first block", () => {
|
|
228
|
+
const editor = createEditor();
|
|
229
|
+
setBlocks(editor, [
|
|
230
|
+
{ type: "numberedListItem", content: "a" },
|
|
231
|
+
{ type: "numberedListItem", content: "b" },
|
|
232
|
+
{ type: "numberedListItem", content: "c" },
|
|
233
|
+
]);
|
|
234
|
+
|
|
235
|
+
const indicesBefore = getDataIndices(editor);
|
|
236
|
+
|
|
237
|
+
// Type a character in the first block
|
|
238
|
+
const view = editor._tiptapEditor.view;
|
|
239
|
+
view.dispatch(view.state.tr.insertText("x", 4));
|
|
240
|
+
|
|
241
|
+
const indicesAfter = getDataIndices(editor);
|
|
242
|
+
expect(indicesAfter).toEqual(indicesBefore);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("indices unchanged after typing in the last block", () => {
|
|
246
|
+
const editor = createEditor();
|
|
247
|
+
setBlocks(editor, [
|
|
248
|
+
{ type: "numberedListItem", content: "a" },
|
|
249
|
+
{ type: "numberedListItem", content: "b" },
|
|
250
|
+
{ type: "numberedListItem", content: "c" },
|
|
251
|
+
]);
|
|
252
|
+
|
|
253
|
+
const indicesBefore = getDataIndices(editor);
|
|
254
|
+
|
|
255
|
+
const view = editor._tiptapEditor.view;
|
|
256
|
+
const pos = view.state.doc.content.size - 4;
|
|
257
|
+
view.dispatch(view.state.tr.insertText("x", pos));
|
|
258
|
+
|
|
259
|
+
const indicesAfter = getDataIndices(editor);
|
|
260
|
+
expect(indicesAfter).toEqual(indicesBefore);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("indices unchanged after typing in a middle block", () => {
|
|
264
|
+
const editor = createEditor();
|
|
265
|
+
setBlocks(editor, [
|
|
266
|
+
{ type: "numberedListItem", content: "a" },
|
|
267
|
+
{ type: "numberedListItem", content: "b" },
|
|
268
|
+
{ type: "numberedListItem", content: "c" },
|
|
269
|
+
]);
|
|
270
|
+
|
|
271
|
+
const indicesBefore = getDataIndices(editor);
|
|
272
|
+
|
|
273
|
+
// Find position inside second block's content
|
|
274
|
+
const view = editor._tiptapEditor.view;
|
|
275
|
+
let targetPos = 0;
|
|
276
|
+
view.state.doc.descendants((node, pos) => {
|
|
277
|
+
if (
|
|
278
|
+
node.type.name === "numberedListItem" &&
|
|
279
|
+
targetPos === 0 &&
|
|
280
|
+
pos > 4
|
|
281
|
+
) {
|
|
282
|
+
targetPos = pos + 1; // inside the inline content
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
view.dispatch(view.state.tr.insertText("x", targetPos));
|
|
286
|
+
|
|
287
|
+
const indicesAfter = getDataIndices(editor);
|
|
288
|
+
expect(indicesAfter).toEqual(indicesBefore);
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
describe("IndexingPlugin: decoration specs", () => {
|
|
293
|
+
it("decorations have correct spec with index, isFirst, hasStart", () => {
|
|
294
|
+
const editor = createEditor();
|
|
295
|
+
setBlocks(editor, [
|
|
296
|
+
{ type: "numberedListItem", content: "a" },
|
|
297
|
+
{ type: "numberedListItem", content: "b" },
|
|
298
|
+
]);
|
|
299
|
+
|
|
300
|
+
const specs = getDecoSpecs(editor);
|
|
301
|
+
expect(specs).toEqual([
|
|
302
|
+
{ index: 1, isFirst: true, hasStart: false },
|
|
303
|
+
{ index: 2, isFirst: false, hasStart: false },
|
|
304
|
+
]);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("first item after a paragraph is marked as isFirst", () => {
|
|
308
|
+
const editor = createEditor();
|
|
309
|
+
setBlocks(editor, [
|
|
310
|
+
{ type: "numberedListItem", content: "a" },
|
|
311
|
+
{ type: "paragraph", content: "break" },
|
|
312
|
+
{ type: "numberedListItem", content: "b" },
|
|
313
|
+
{ type: "numberedListItem", content: "c" },
|
|
314
|
+
]);
|
|
315
|
+
|
|
316
|
+
const specs = getDecoSpecs(editor);
|
|
317
|
+
expect(specs).toEqual([
|
|
318
|
+
{ index: 1, isFirst: true, hasStart: false },
|
|
319
|
+
{ index: 1, isFirst: true, hasStart: false },
|
|
320
|
+
{ index: 2, isFirst: false, hasStart: false },
|
|
321
|
+
]);
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
describe("IndexingPlugin: selection-only transactions", () => {
|
|
326
|
+
it("does not recompute decorations on selection change", () => {
|
|
327
|
+
const editor = createEditor();
|
|
328
|
+
setBlocks(editor, [
|
|
329
|
+
{ type: "numberedListItem", content: "a" },
|
|
330
|
+
{ type: "numberedListItem", content: "b" },
|
|
331
|
+
]);
|
|
332
|
+
|
|
333
|
+
const decosBefore = getDecorationSet(editor);
|
|
334
|
+
|
|
335
|
+
// Move selection without changing content
|
|
336
|
+
const view = editor._tiptapEditor.view;
|
|
337
|
+
const tr = view.state.tr.setSelection(
|
|
338
|
+
Selection.near(view.state.doc.resolve(4)),
|
|
339
|
+
);
|
|
340
|
+
view.dispatch(tr);
|
|
341
|
+
|
|
342
|
+
const decosAfter = getDecorationSet(editor);
|
|
343
|
+
// Same DecorationSet reference — not recomputed
|
|
344
|
+
expect(decosAfter).toBe(decosBefore);
|
|
345
|
+
});
|
|
346
|
+
});
|
|
@@ -17,7 +17,10 @@ type DecoSpec = {
|
|
|
17
17
|
type Deco = Omit<Decoration, "spec"> & { spec: DecoSpec };
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
|
-
* Calculate the index for a numbered list item based on its position and previous siblings
|
|
20
|
+
* Calculate the index for a numbered list item based on its position and previous siblings.
|
|
21
|
+
* Iteratively walks backwards to find the start of the contiguous list (or a cached entry),
|
|
22
|
+
* then walks forward to populate the cache. This avoids deep recursion that would overflow
|
|
23
|
+
* the stack on large documents.
|
|
21
24
|
*/
|
|
22
25
|
function calculateListItemIndex(
|
|
23
26
|
node: Node,
|
|
@@ -25,52 +28,93 @@ function calculateListItemIndex(
|
|
|
25
28
|
tr: Transaction,
|
|
26
29
|
map: Map<Node, number>,
|
|
27
30
|
): { index: number; isFirst: boolean; hasStart: boolean } {
|
|
28
|
-
let index: number = node.firstChild!.attrs["start"] || 1;
|
|
29
|
-
let isFirst = true;
|
|
30
31
|
const hasStart = !!node.firstChild!.attrs["start"];
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
node,
|
|
35
|
-
});
|
|
36
|
-
|
|
33
|
+
// Fast path: previous sibling already in cache
|
|
34
|
+
const blockInfo = getBlockInfo({ posBeforeNode: pos, node });
|
|
37
35
|
if (!blockInfo.isBlockContainer) {
|
|
38
36
|
throw new Error("impossible");
|
|
39
37
|
}
|
|
40
|
-
|
|
41
|
-
// Check if this block is the start of a new ordered list
|
|
42
38
|
const prevBlock = tr.doc.resolve(blockInfo.bnBlock.beforePos).nodeBefore;
|
|
43
39
|
const prevBlockIndex = prevBlock ? map.get(prevBlock) : undefined;
|
|
44
|
-
|
|
45
40
|
if (prevBlockIndex !== undefined) {
|
|
46
|
-
index = prevBlockIndex + 1;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
41
|
+
const index = prevBlockIndex + 1;
|
|
42
|
+
map.set(node, index);
|
|
43
|
+
return { index, isFirst: false, hasStart };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Walk backwards iteratively to collect the chain of consecutive
|
|
47
|
+
// numbered list items until we hit a cached entry, a non-list block,
|
|
48
|
+
// or the start of the parent.
|
|
49
|
+
const chain: { node: Node; pos: number }[] = [{ node, pos }];
|
|
50
|
+
let curNode = prevBlock;
|
|
51
|
+
let curBeforePos = blockInfo.bnBlock.beforePos;
|
|
52
|
+
|
|
53
|
+
while (curNode) {
|
|
54
|
+
const cachedIndex = map.get(curNode);
|
|
55
|
+
if (cachedIndex !== undefined) {
|
|
56
|
+
// Found a cached predecessor — start counting from here
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
const curInfo = getBlockInfo({
|
|
60
|
+
posBeforeNode: curBeforePos - curNode.nodeSize,
|
|
61
|
+
node: curNode,
|
|
54
62
|
});
|
|
63
|
+
if (curInfo.blockNoteType !== "numberedListItem") {
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
chain.push({ node: curNode, pos: curBeforePos - curNode.nodeSize });
|
|
67
|
+
const nextPrev = tr.doc.resolve(curInfo.bnBlock.beforePos).nodeBefore;
|
|
68
|
+
curBeforePos = curInfo.bnBlock.beforePos;
|
|
69
|
+
curNode = nextPrev;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Walk forward (reverse of the collected chain) to assign indices
|
|
73
|
+
// The last element in chain is the furthest predecessor
|
|
74
|
+
let index: number;
|
|
75
|
+
let isFirst: boolean;
|
|
76
|
+
|
|
77
|
+
// Determine starting index from the block just before the chain
|
|
78
|
+
const lastInChain = chain[chain.length - 1];
|
|
79
|
+
const lastInfo = getBlockInfo({
|
|
80
|
+
posBeforeNode: lastInChain.pos,
|
|
81
|
+
node: lastInChain.node,
|
|
82
|
+
});
|
|
83
|
+
if (!lastInfo.isBlockContainer) {
|
|
84
|
+
throw new Error("impossible");
|
|
85
|
+
}
|
|
86
|
+
const predecessorNode = tr.doc.resolve(lastInfo.bnBlock.beforePos).nodeBefore;
|
|
87
|
+
const predecessorIndex = predecessorNode
|
|
88
|
+
? map.get(predecessorNode)
|
|
89
|
+
: undefined;
|
|
90
|
+
|
|
91
|
+
if (predecessorIndex !== undefined) {
|
|
92
|
+
index = predecessorIndex;
|
|
93
|
+
isFirst = false;
|
|
94
|
+
} else {
|
|
95
|
+
// Start of a new list
|
|
96
|
+
index = (lastInChain.node.firstChild!.attrs["start"] || 1) - 1;
|
|
97
|
+
isFirst = true;
|
|
98
|
+
}
|
|
55
99
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
prevBlock,
|
|
62
|
-
blockInfo.bnBlock.beforePos - prevBlock.nodeSize,
|
|
63
|
-
tr,
|
|
64
|
-
map,
|
|
65
|
-
);
|
|
66
|
-
index = itemIndex.index + 1;
|
|
100
|
+
// Assign indices from the end of the chain (furthest back) to the front (original node)
|
|
101
|
+
for (let i = chain.length - 1; i >= 0; i--) {
|
|
102
|
+
const entry = chain[i];
|
|
103
|
+
if (isFirst && i < chain.length - 1) {
|
|
104
|
+
// Only the very first item in the list gets isFirst
|
|
67
105
|
isFirst = false;
|
|
68
106
|
}
|
|
107
|
+
index++;
|
|
108
|
+
map.set(entry.node, index);
|
|
69
109
|
}
|
|
70
|
-
// Note: we set the map late, so that when we recurse, we can rely on the map to get the previous block's index in one lookup
|
|
71
|
-
map.set(node, index);
|
|
72
110
|
|
|
73
|
-
|
|
111
|
+
// isFirst is true only for the very first item in a new list:
|
|
112
|
+
// chain.length > 1 means we found predecessor list items, so not first.
|
|
113
|
+
return {
|
|
114
|
+
index,
|
|
115
|
+
isFirst: chain.length === 1 ? isFirst || predecessorIndex === undefined : false,
|
|
116
|
+
hasStart,
|
|
117
|
+
};
|
|
74
118
|
}
|
|
75
119
|
|
|
76
120
|
/**
|
|
@@ -87,42 +131,74 @@ function getDecorations(
|
|
|
87
131
|
tr.mapping,
|
|
88
132
|
tr.doc,
|
|
89
133
|
);
|
|
134
|
+
|
|
135
|
+
// Find the start of the first change to limit traversal scope.
|
|
136
|
+
// We only need to check from the change point forward, since earlier
|
|
137
|
+
// blocks are unaffected and their mapped decorations remain correct.
|
|
138
|
+
const range = tr.changedRange();
|
|
139
|
+
if (!range) {
|
|
140
|
+
return { decorations: nextDecorationSet };
|
|
141
|
+
}
|
|
90
142
|
const decorationsToAdd = [] as Deco[];
|
|
91
143
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
)
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const blockNode = tr.doc.nodeAt(pos + 1);
|
|
116
|
-
// Create a widget decoration to display the index
|
|
117
|
-
decorationsToAdd.push(
|
|
118
|
-
// move in by 1 to account for the block container
|
|
119
|
-
Decoration.node(pos + 1, pos + 1 + blockNode!.nodeSize, {
|
|
120
|
-
"data-index": index.toString(),
|
|
121
|
-
}),
|
|
144
|
+
// Track blockGroups where we've verified a decoration match past the
|
|
145
|
+
// changed range. Within a single blockGroup, indices are sequential —
|
|
146
|
+
// if one matches, all subsequent siblings must too. But sibling items
|
|
147
|
+
// in *other* blockGroups (e.g. nested lists) are independent.
|
|
148
|
+
const completedGroups = new Set<Node>();
|
|
149
|
+
|
|
150
|
+
tr.doc.nodesBetween(
|
|
151
|
+
range.from,
|
|
152
|
+
tr.doc.nodeSize - 2,
|
|
153
|
+
(node, pos, parent) => {
|
|
154
|
+
if (parent && completedGroups.has(parent)) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (
|
|
159
|
+
node.type.name === "blockContainer" &&
|
|
160
|
+
node.firstChild!.type.name === "numberedListItem"
|
|
161
|
+
) {
|
|
162
|
+
const { index, isFirst, hasStart } = calculateListItemIndex(
|
|
163
|
+
node,
|
|
164
|
+
pos,
|
|
165
|
+
tr,
|
|
166
|
+
map,
|
|
122
167
|
);
|
|
168
|
+
|
|
169
|
+
// Search only the numberedListItem node range, not the full
|
|
170
|
+
// blockContainer (which includes nested blockGroups whose
|
|
171
|
+
// decorations could falsely match).
|
|
172
|
+
const blockNode = tr.doc.nodeAt(pos + 1)!;
|
|
173
|
+
const existingDecorations = nextDecorationSet.find(
|
|
174
|
+
pos + 1,
|
|
175
|
+
pos + 1 + blockNode.nodeSize,
|
|
176
|
+
(deco: DecoSpec) =>
|
|
177
|
+
deco.index === index &&
|
|
178
|
+
deco.isFirst === isFirst &&
|
|
179
|
+
deco.hasStart === hasStart,
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
if (existingDecorations.length === 0) {
|
|
183
|
+
decorationsToAdd.push(
|
|
184
|
+
Decoration.node(
|
|
185
|
+
pos + 1,
|
|
186
|
+
pos + 1 + blockNode.nodeSize,
|
|
187
|
+
{ "data-index": index.toString() },
|
|
188
|
+
{ index, isFirst, hasStart },
|
|
189
|
+
) as Deco,
|
|
190
|
+
);
|
|
191
|
+
} else if (pos >= range.to && parent) {
|
|
192
|
+
// Past the changed range and decoration matches in this blockGroup:
|
|
193
|
+
// all subsequent siblings must also match. Mark group as done and
|
|
194
|
+
// skip this node's children (nested lists are unaffected too).
|
|
195
|
+
completedGroups.add(parent);
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
123
198
|
}
|
|
124
|
-
|
|
125
|
-
|
|
199
|
+
return undefined;
|
|
200
|
+
},
|
|
201
|
+
);
|
|
126
202
|
|
|
127
203
|
// Remove any decorations that exist at the same position, they will be replaced by the new decorations
|
|
128
204
|
const decorationsToRemove = decorationsToAdd.flatMap((deco) =>
|
|
@@ -153,12 +229,8 @@ export const NumberedListIndexingDecorationPlugin = () => {
|
|
|
153
229
|
});
|
|
154
230
|
},
|
|
155
231
|
apply(tr, previousPluginState) {
|
|
156
|
-
if (
|
|
157
|
-
|
|
158
|
-
!tr.selectionSet &&
|
|
159
|
-
previousPluginState.decorations
|
|
160
|
-
) {
|
|
161
|
-
// Just reuse the existing decorations, since nothing should have changed
|
|
232
|
+
if (!tr.docChanged && previousPluginState.decorations) {
|
|
233
|
+
// Selection-only changes don't affect list indices, just reuse existing decorations
|
|
162
234
|
return previousPluginState;
|
|
163
235
|
}
|
|
164
236
|
return getDecorations(tr, previousPluginState);
|
package/src/editor/Block.css
CHANGED
|
@@ -41,7 +41,6 @@ BASIC STYLES
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
.bn-inline-content {
|
|
44
|
-
width: 100%;
|
|
45
44
|
/* Ensure pre-wrap even when a parent node view wrapper (e.g. tiptap's
|
|
46
45
|
NodeViewWrapper) resets white-space to "normal". Without this, browsers
|
|
47
46
|
normalize trailing spaces to NBSP on input, which causes ProseMirror to
|
|
@@ -552,15 +551,13 @@ NESTED BLOCKS
|
|
|
552
551
|
}
|
|
553
552
|
|
|
554
553
|
/* PLACEHOLDERS*/
|
|
555
|
-
.bn-
|
|
556
|
-
/*float: left; */
|
|
557
|
-
pointer-events: none;
|
|
558
|
-
height: 0;
|
|
559
|
-
/* width: 0; */
|
|
560
|
-
position: absolute;
|
|
554
|
+
.bn-block-content:has(.ProseMirror-trailingBreak:only-child):after {
|
|
561
555
|
font-style: italic;
|
|
556
|
+
/* Removes text cursor offset. */
|
|
557
|
+
margin-inline: -2px;
|
|
558
|
+
pointer-events: none;
|
|
559
|
+
max-width: 100%;
|
|
562
560
|
}
|
|
563
|
-
/* TODO: should this be here? */
|
|
564
561
|
|
|
565
562
|
/* TEXT COLORS */
|
|
566
563
|
[data-style-type="textColor"][data-value="gray"],
|