@blocknote/core 0.47.3 → 0.48.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.
- 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 -4256
- 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/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/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
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
import { Fragment, NodeType, Slice } from "prosemirror-model";
|
|
1
|
+
import { Fragment, NodeRange, NodeType, Slice } from "prosemirror-model";
|
|
2
2
|
import { Transaction } from "prosemirror-state";
|
|
3
|
-
import { ReplaceAroundStep } from "prosemirror-transform";
|
|
3
|
+
import { canJoin, liftTarget, ReplaceAroundStep } from "prosemirror-transform";
|
|
4
4
|
|
|
5
5
|
import { BlockNoteEditor } from "../../../../editor/BlockNoteEditor.js";
|
|
6
6
|
import { getBlockInfoFromTransaction } from "../../../getBlockInfoFromPos.js";
|
|
7
7
|
|
|
8
|
-
// TODO: Unit tests
|
|
9
8
|
/**
|
|
10
|
-
*
|
|
9
|
+
* Modified version of prosemirror-schema-list's sinkItem.
|
|
10
|
+
* https://github.com/ProseMirror/prosemirror-schema-list/blob/master/src/schema-list.ts
|
|
11
11
|
*
|
|
12
|
-
*
|
|
12
|
+
* Changes from the original:
|
|
13
|
+
* 1. Range predicate checks node.type instead of firstChild.type
|
|
14
|
+
* 2. nestedBefore checks groupType instead of parent.type
|
|
15
|
+
* 3. Slice creates groupType instead of parent.type
|
|
16
|
+
* 4. Operates on Transaction directly instead of state+dispatch
|
|
13
17
|
*/
|
|
14
|
-
function
|
|
18
|
+
function sinkItem(
|
|
15
19
|
tr: Transaction,
|
|
16
20
|
itemType: NodeType,
|
|
17
21
|
groupType: NodeType,
|
|
@@ -21,7 +25,7 @@ function sinkListItem(
|
|
|
21
25
|
$to,
|
|
22
26
|
(node) =>
|
|
23
27
|
node.childCount > 0 &&
|
|
24
|
-
(node.type.name === "blockGroup" || node.type.name === "column"), // change
|
|
28
|
+
(node.type.name === "blockGroup" || node.type.name === "column"), // change 1
|
|
25
29
|
);
|
|
26
30
|
if (!range) {
|
|
27
31
|
return false;
|
|
@@ -36,11 +40,11 @@ function sinkListItem(
|
|
|
36
40
|
return false;
|
|
37
41
|
}
|
|
38
42
|
const nestedBefore =
|
|
39
|
-
nodeBefore.lastChild && nodeBefore.lastChild.type === groupType; // change
|
|
43
|
+
nodeBefore.lastChild && nodeBefore.lastChild.type === groupType; // change 2
|
|
40
44
|
const inner = Fragment.from(nestedBefore ? itemType.create() : null);
|
|
41
45
|
const slice = new Slice(
|
|
42
46
|
Fragment.from(
|
|
43
|
-
itemType.create(null, Fragment.from(groupType.create(null, inner))), // change
|
|
47
|
+
itemType.create(null, Fragment.from(groupType.create(null, inner))), // change 3
|
|
44
48
|
),
|
|
45
49
|
nestedBefore ? 3 : 1,
|
|
46
50
|
0,
|
|
@@ -66,7 +70,7 @@ function sinkListItem(
|
|
|
66
70
|
|
|
67
71
|
export function nestBlock(editor: BlockNoteEditor<any, any, any>) {
|
|
68
72
|
return editor.transact((tr) => {
|
|
69
|
-
return
|
|
73
|
+
return sinkItem(
|
|
70
74
|
tr,
|
|
71
75
|
editor.pmSchema.nodes["blockContainer"],
|
|
72
76
|
editor.pmSchema.nodes["blockGroup"],
|
|
@@ -74,8 +78,121 @@ export function nestBlock(editor: BlockNoteEditor<any, any, any>) {
|
|
|
74
78
|
});
|
|
75
79
|
}
|
|
76
80
|
|
|
81
|
+
/**
|
|
82
|
+
* Modified version of prosemirror-schema-list's liftToOuterList.
|
|
83
|
+
* https://github.com/ProseMirror/prosemirror-schema-list/blob/master/src/schema-list.ts
|
|
84
|
+
*
|
|
85
|
+
* Changes from the original:
|
|
86
|
+
* 1. Operates on Transaction directly instead of state+dispatch (TipTap compat)
|
|
87
|
+
* 2. When the lifted block already has children (a groupType child), uses deeper
|
|
88
|
+
* openStart/offset so siblings merge into the existing group instead of
|
|
89
|
+
* creating a second one (which would violate blockContainer's schema)
|
|
90
|
+
* 3. Uses groupType.create() instead of range.parent.copy() (same as sinkItem)
|
|
91
|
+
*/
|
|
92
|
+
function liftToOuterList(
|
|
93
|
+
tr: Transaction,
|
|
94
|
+
itemType: NodeType,
|
|
95
|
+
groupType: NodeType, // change 3
|
|
96
|
+
range: NodeRange,
|
|
97
|
+
) {
|
|
98
|
+
const end = range.end;
|
|
99
|
+
const endOfList = range.$to.end(range.depth);
|
|
100
|
+
|
|
101
|
+
if (end < endOfList) {
|
|
102
|
+
// There are siblings after the lifted items, which must become
|
|
103
|
+
// children of the last item
|
|
104
|
+
const blockBeingLifted = range.parent.child(range.endIndex - 1);
|
|
105
|
+
const nestedAfter =
|
|
106
|
+
blockBeingLifted.lastChild &&
|
|
107
|
+
blockBeingLifted.lastChild.type === groupType; // change 2
|
|
108
|
+
|
|
109
|
+
tr.step(
|
|
110
|
+
new ReplaceAroundStep(
|
|
111
|
+
end - (nestedAfter ? 2 : 1), // change 2: go deeper when merging into existing children
|
|
112
|
+
endOfList,
|
|
113
|
+
end,
|
|
114
|
+
endOfList,
|
|
115
|
+
new Slice(
|
|
116
|
+
Fragment.from(
|
|
117
|
+
itemType.create(null, groupType.create()), // change 3
|
|
118
|
+
),
|
|
119
|
+
nestedAfter ? 2 : 1, // change 2: open deeper when merging into existing children
|
|
120
|
+
0,
|
|
121
|
+
),
|
|
122
|
+
nestedAfter ? 0 : 1, // change 2: Slice.insertAt offsets by openStart, so 0+2=2 lands inside existing bg
|
|
123
|
+
true,
|
|
124
|
+
),
|
|
125
|
+
);
|
|
126
|
+
range = new NodeRange(
|
|
127
|
+
tr.doc.resolve(range.$from.pos),
|
|
128
|
+
tr.doc.resolve(endOfList),
|
|
129
|
+
range.depth,
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const target = liftTarget(range);
|
|
134
|
+
if (target == null) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
tr.lift(range, target);
|
|
139
|
+
|
|
140
|
+
const $after = tr.doc.resolve(tr.mapping.map(end, -1) - 1);
|
|
141
|
+
if (
|
|
142
|
+
canJoin(tr.doc, $after.pos) &&
|
|
143
|
+
$after.nodeBefore!.type === $after.nodeAfter!.type
|
|
144
|
+
) {
|
|
145
|
+
tr.join($after.pos);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
tr.scrollIntoView();
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Modified version of prosemirror-schema-list's liftListItem.
|
|
154
|
+
* https://github.com/ProseMirror/prosemirror-schema-list/blob/master/src/schema-list.ts
|
|
155
|
+
*
|
|
156
|
+
* Changes from the original:
|
|
157
|
+
* 1. Range predicate checks node.type instead of firstChild.type (same as sinkItem)
|
|
158
|
+
* 2. Passes groupType to liftToOuterList
|
|
159
|
+
* 3. Operates on Transaction directly instead of state+dispatch
|
|
160
|
+
* 4. Skips liftOutOfList (root-level blocks can't be unnested in BlockNote)
|
|
161
|
+
*/
|
|
162
|
+
export function liftItem(
|
|
163
|
+
tr: Transaction,
|
|
164
|
+
itemType: NodeType,
|
|
165
|
+
groupType: NodeType, // change 2
|
|
166
|
+
) {
|
|
167
|
+
const { $from, $to } = tr.selection;
|
|
168
|
+
const range = $from.blockRange(
|
|
169
|
+
$to,
|
|
170
|
+
(node) =>
|
|
171
|
+
node.childCount > 0 &&
|
|
172
|
+
(node.type.name === "blockGroup" || node.type.name === "column"), // change 1
|
|
173
|
+
);
|
|
174
|
+
if (!range) {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if ($from.node(range.depth - 1).type === itemType) {
|
|
179
|
+
// Inside a parent node
|
|
180
|
+
return liftToOuterList(tr, itemType, groupType, range); // change 2
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// This is the "liftOutOfList" path — lifting out of a list entirely.
|
|
184
|
+
// Not applicable to BlockNote (root-level blocks can't be unnested). // change 4
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
|
|
77
188
|
export function unnestBlock(editor: BlockNoteEditor<any, any, any>) {
|
|
78
|
-
editor.
|
|
189
|
+
return editor.transact((tr) =>
|
|
190
|
+
liftItem(
|
|
191
|
+
tr,
|
|
192
|
+
editor.pmSchema.nodes["blockContainer"],
|
|
193
|
+
editor.pmSchema.nodes["blockGroup"],
|
|
194
|
+
),
|
|
195
|
+
);
|
|
79
196
|
}
|
|
80
197
|
|
|
81
198
|
export function canNestBlock(editor: BlockNoteEditor<any, any, any>) {
|
|
@@ -47,9 +47,13 @@ export const createResizableFileBlockWrapper = (
|
|
|
47
47
|
const leftResizeHandle = document.createElement("div");
|
|
48
48
|
leftResizeHandle.className = "bn-resize-handle";
|
|
49
49
|
leftResizeHandle.style.left = "4px";
|
|
50
|
+
leftResizeHandle.style.display = "none";
|
|
51
|
+
resizeHandlesContainerElement.appendChild(leftResizeHandle);
|
|
50
52
|
const rightResizeHandle = document.createElement("div");
|
|
51
53
|
rightResizeHandle.className = "bn-resize-handle";
|
|
52
54
|
rightResizeHandle.style.right = "4px";
|
|
55
|
+
rightResizeHandle.style.display = "none";
|
|
56
|
+
resizeHandlesContainerElement.appendChild(rightResizeHandle);
|
|
53
57
|
|
|
54
58
|
// This element ensures `mousemove` and `mouseup` events are captured while
|
|
55
59
|
// resizing when the cursor is over the wrapper content. This is because
|
|
@@ -75,13 +79,9 @@ export const createResizableFileBlockWrapper = (
|
|
|
75
79
|
// offset from when the resize began, and which resize handle is being used.
|
|
76
80
|
const windowMouseMoveHandler = (event: MouseEvent | TouchEvent) => {
|
|
77
81
|
if (!resizeParams) {
|
|
78
|
-
if (
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
resizeHandlesContainerElement.contains(rightResizeHandle)
|
|
82
|
-
) {
|
|
83
|
-
resizeHandlesContainerElement.removeChild(leftResizeHandle);
|
|
84
|
-
resizeHandlesContainerElement.removeChild(rightResizeHandle);
|
|
82
|
+
if (!editor.isEditable) {
|
|
83
|
+
leftResizeHandle.style.display = "none";
|
|
84
|
+
rightResizeHandle.style.display = "none";
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
return;
|
|
@@ -128,14 +128,12 @@ export const createResizableFileBlockWrapper = (
|
|
|
128
128
|
const windowMouseUpHandler = (event: MouseEvent | TouchEvent) => {
|
|
129
129
|
// Hides the drag handles if the cursor is no longer over the element.
|
|
130
130
|
if (
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
resizeHandlesContainerElement.contains(leftResizeHandle) &&
|
|
135
|
-
resizeHandlesContainerElement.contains(rightResizeHandle)
|
|
131
|
+
!event.target ||
|
|
132
|
+
!wrapper.contains(event.target as Node) ||
|
|
133
|
+
!editor.isEditable
|
|
136
134
|
) {
|
|
137
|
-
|
|
138
|
-
|
|
135
|
+
leftResizeHandle.style.display = "none";
|
|
136
|
+
rightResizeHandle.style.display = "none";
|
|
139
137
|
}
|
|
140
138
|
|
|
141
139
|
if (!resizeParams) {
|
|
@@ -158,8 +156,8 @@ export const createResizableFileBlockWrapper = (
|
|
|
158
156
|
// Shows the resize handles when hovering over the wrapper with the cursor.
|
|
159
157
|
const wrapperMouseEnterHandler = () => {
|
|
160
158
|
if (editor.isEditable) {
|
|
161
|
-
|
|
162
|
-
|
|
159
|
+
leftResizeHandle.style.display = "";
|
|
160
|
+
rightResizeHandle.style.display = "";
|
|
163
161
|
}
|
|
164
162
|
};
|
|
165
163
|
// Hides the resize handles when the cursor leaves the wrapper, unless the
|
|
@@ -176,13 +174,9 @@ export const createResizableFileBlockWrapper = (
|
|
|
176
174
|
return;
|
|
177
175
|
}
|
|
178
176
|
|
|
179
|
-
if (
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
resizeHandlesContainerElement.contains(rightResizeHandle)
|
|
183
|
-
) {
|
|
184
|
-
resizeHandlesContainerElement.removeChild(leftResizeHandle);
|
|
185
|
-
resizeHandlesContainerElement.removeChild(rightResizeHandle);
|
|
177
|
+
if (editor.isEditable) {
|
|
178
|
+
leftResizeHandle.style.display = "none";
|
|
179
|
+
rightResizeHandle.style.display = "none";
|
|
186
180
|
}
|
|
187
181
|
};
|
|
188
182
|
|
|
@@ -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
|
+
});
|