@blocknote/core 0.52.0 → 0.53.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/blocknote.cjs +1 -1
- package/dist/blocknote.js +6 -6
- package/dist/{blocks-CiuUGa1E.js → blocks-BEJW9OM_.js} +516 -476
- package/dist/blocks-BEJW9OM_.js.map +1 -0
- package/dist/blocks-BPmJglFo.cjs +12 -0
- package/dist/blocks-BPmJglFo.cjs.map +1 -0
- package/dist/blocks.cjs +1 -1
- package/dist/blocks.js +2 -2
- package/dist/extensions-AitWbYdJ.cjs +52 -0
- package/dist/extensions-AitWbYdJ.cjs.map +1 -0
- package/dist/{extensions-CrUGDOks.js → extensions-BR5hkAio.js} +421 -391
- package/dist/extensions-BR5hkAio.js.map +1 -0
- package/dist/extensions.cjs +1 -1
- package/dist/extensions.js +3 -3
- package/dist/src-BHPuOo7W.cjs +21 -0
- package/dist/src-BHPuOo7W.cjs.map +1 -0
- package/dist/{src-DTF83Cl9.js → src-BUXut68d.js} +797 -779
- package/dist/src-BUXut68d.js.map +1 -0
- package/dist/style.css +1 -1
- package/dist/webpack-stats.json +1 -1
- package/dist/y.cjs +1 -1
- package/dist/y.js +9 -9
- package/dist/yjs.cjs +1 -1
- package/dist/yjs.js +1 -1
- package/package.json +14 -14
- package/src/api/clipboard/fromClipboard/handleFileInsertion.ts +10 -1
- package/src/api/exporters/html/util/serializeBlocksExternalHTML.ts +42 -8
- package/src/api/exporters/markdown/markdownExporter.ts +10 -1
- package/src/api/parsers/html/parseHTML.ts +43 -0
- package/src/blocks/Table/TableExtension.test.ts +102 -0
- package/src/blocks/ToggleWrapper/createToggleWrapper.ts +16 -5
- package/src/blocks/defaultBlockTypeGuards.ts +6 -0
- package/src/editor/Block.css +51 -1
- package/src/extensions/SideMenu/SideMenu.ts +20 -3
- package/src/extensions/SuggestionMenu/SuggestionMenu.test.ts +118 -0
- package/src/extensions/SuggestionMenu/SuggestionMenu.ts +40 -27
- package/src/extensions/SuggestionMenu/getDefaultSlashMenuItems.ts +13 -3
- package/src/extensions/TableHandles/TableHandles.ts +25 -12
- package/src/extensions/TrailingNode/TrailingNode.ts +98 -32
- package/src/index.ts +1 -0
- package/src/schema/blocks/createSpec.ts +16 -3
- package/src/schema/blocks/internal.test.ts +86 -0
- package/src/schema/blocks/internal.ts +64 -0
- package/src/schema/inlineContent/createSpec.ts +9 -1
- package/src/schema/nodeViewMutations.test.ts +121 -0
- package/src/schema/nodeViewMutations.ts +46 -0
- package/types/src/api/exporters/html/util/serializeBlocksExternalHTML.d.ts +13 -0
- package/types/src/blocks/defaultBlockTypeGuards.d.ts +2 -0
- package/types/src/extensions/SideMenu/SideMenu.d.ts +5 -0
- package/types/src/extensions/SuggestionMenu/SuggestionMenu.d.ts +6 -0
- package/types/src/extensions/TrailingNode/TrailingNode.d.ts +5 -4
- package/types/src/index.d.ts +1 -0
- package/types/src/schema/blocks/internal.d.ts +21 -0
- package/types/src/schema/blocks/internal.test.d.ts +1 -0
- package/types/src/schema/nodeViewMutations.d.ts +3 -0
- package/types/src/schema/nodeViewMutations.test.d.ts +1 -0
- package/dist/blocks-BqHv0xhc.cjs +0 -12
- package/dist/blocks-BqHv0xhc.cjs.map +0 -1
- package/dist/blocks-CiuUGa1E.js.map +0 -1
- package/dist/extensions-CrUGDOks.js.map +0 -1
- package/dist/extensions-Ghr9QqqR.cjs +0 -52
- package/dist/extensions-Ghr9QqqR.cjs.map +0 -1
- package/dist/src-DTF83Cl9.js.map +0 -1
- package/dist/src-OfbaVbMw.cjs +0 -21
- package/dist/src-OfbaVbMw.cjs.map +0 -1
|
@@ -150,6 +150,12 @@ type SuggestionPluginState =
|
|
|
150
150
|
| undefined;
|
|
151
151
|
|
|
152
152
|
export type SuggestionMenuOptions = {
|
|
153
|
+
/**
|
|
154
|
+
* The string that opens the suggestion menu when typed. Usually a single
|
|
155
|
+
* character (e.g. `"/"` or `"@"`), but multi-character strings such as
|
|
156
|
+
* `"img:"` are also supported. When multiple triggers could match the typed
|
|
157
|
+
* text, the longest one takes precedence.
|
|
158
|
+
*/
|
|
153
159
|
triggerCharacter: string;
|
|
154
160
|
/**
|
|
155
161
|
* Optional callback to determine whether the suggestion menu should be
|
|
@@ -332,34 +338,41 @@ export const SuggestionMenu = createExtension(({ editor }) => {
|
|
|
332
338
|
},
|
|
333
339
|
|
|
334
340
|
props: {
|
|
335
|
-
handleTextInput(view, from,
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
//
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
)
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
);
|
|
361
|
-
return true;
|
|
341
|
+
handleTextInput(view, from, _to, text) {
|
|
342
|
+
const doc = view.state.doc;
|
|
343
|
+
// Sort triggers by length (longest first) so that a more specific
|
|
344
|
+
// multi-character trigger (e.g. "img:") is preferred over a shorter
|
|
345
|
+
// one (e.g. ":") that would otherwise shadow it by matching first.
|
|
346
|
+
const triggers = [...suggestionMenus.entries()].sort(
|
|
347
|
+
([a], [b]) => b.length - a.length,
|
|
348
|
+
);
|
|
349
|
+
for (const [trigger, menuOptions] of triggers) {
|
|
350
|
+
const input =
|
|
351
|
+
doc.textBetween(
|
|
352
|
+
// If the cursor is at the very start of the document, and we are checking if
|
|
353
|
+
// an n-character trigger has been entered (e.g. "img:"), we need to get the n
|
|
354
|
+
// characters before the text cursor to compare do so. However, the text cursor
|
|
355
|
+
// may be near the start of the document and not have n characters before it.
|
|
356
|
+
Math.max(0, from - (trigger.length - text.length)),
|
|
357
|
+
from,
|
|
358
|
+
) + text;
|
|
359
|
+
if (trigger === input) {
|
|
360
|
+
// Check the per-suggestion-menu filter before activating.
|
|
361
|
+
if (
|
|
362
|
+
menuOptions.shouldOpen &&
|
|
363
|
+
!menuOptions.shouldOpen(view.state.tr)
|
|
364
|
+
) {
|
|
365
|
+
continue;
|
|
362
366
|
}
|
|
367
|
+
view.dispatch(view.state.tr.insertText(text));
|
|
368
|
+
view.dispatch(
|
|
369
|
+
view.state.tr
|
|
370
|
+
.setMeta(suggestionMenuPluginKey, {
|
|
371
|
+
triggerCharacter: input,
|
|
372
|
+
})
|
|
373
|
+
.scrollIntoView(),
|
|
374
|
+
);
|
|
375
|
+
return true;
|
|
363
376
|
}
|
|
364
377
|
}
|
|
365
378
|
return false;
|
|
@@ -14,9 +14,9 @@ import { DefaultSuggestionItem } from "./DefaultSuggestionItem.js";
|
|
|
14
14
|
import { SuggestionMenu } from "./SuggestionMenu.js";
|
|
15
15
|
|
|
16
16
|
// Sets the editor's text cursor position to the next content editable block,
|
|
17
|
-
// so either a block with inline content or a table.
|
|
18
|
-
//
|
|
19
|
-
//
|
|
17
|
+
// so either a block with inline content or a table. If no such block exists
|
|
18
|
+
// after the current one, an empty paragraph is appended to the end of the
|
|
19
|
+
// document and the cursor is moved to it.
|
|
20
20
|
function setSelectionToNextContentEditableBlock<
|
|
21
21
|
BSchema extends BlockSchema,
|
|
22
22
|
I extends InlineContentSchema,
|
|
@@ -29,6 +29,16 @@ function setSelectionToNextContentEditableBlock<
|
|
|
29
29
|
while (contentType === "none") {
|
|
30
30
|
block = editor.getTextCursorPosition().nextBlock;
|
|
31
31
|
if (block === undefined) {
|
|
32
|
+
// No content editable block exists after the current one, so we append
|
|
33
|
+
// an empty paragraph to the end of the document and move the cursor to
|
|
34
|
+
// it.
|
|
35
|
+
const lastBlock = editor.document[editor.document.length - 1];
|
|
36
|
+
const newBlock = editor.insertBlocks(
|
|
37
|
+
[{ type: "paragraph" }],
|
|
38
|
+
lastBlock,
|
|
39
|
+
"after",
|
|
40
|
+
)[0];
|
|
41
|
+
editor.setTextCursorPosition(newBlock, "end");
|
|
32
42
|
return;
|
|
33
43
|
}
|
|
34
44
|
contentType = editor.schema.blockSchema[block.type].content as
|
|
@@ -28,6 +28,7 @@ import { nodeToBlock } from "../../api/nodeConversions/nodeToBlock.js";
|
|
|
28
28
|
import { getNodeById } from "../../api/nodeUtil.js";
|
|
29
29
|
import {
|
|
30
30
|
editorHasBlockWithType,
|
|
31
|
+
isTableCellNode,
|
|
31
32
|
isTableCellSelection,
|
|
32
33
|
} from "../../blocks/defaultBlockTypeGuards.js";
|
|
33
34
|
import { DefaultBlockSchema } from "../../blocks/defaultBlocks.js";
|
|
@@ -533,10 +534,10 @@ export class TableHandlesView implements PluginView {
|
|
|
533
534
|
}
|
|
534
535
|
|
|
535
536
|
// Hide handles if the table block has been removed.
|
|
536
|
-
|
|
537
|
+
const refreshedBlock = this.editor.getBlock(this.state.block.id);
|
|
537
538
|
if (
|
|
538
|
-
!
|
|
539
|
-
|
|
539
|
+
!refreshedBlock ||
|
|
540
|
+
refreshedBlock.type !== "table" ||
|
|
540
541
|
// when collaborating, the table element might be replaced and out of date
|
|
541
542
|
// because yjs replaces the element when for example you change the color via the side menu
|
|
542
543
|
!this.tableElement?.isConnected
|
|
@@ -548,6 +549,7 @@ export class TableHandlesView implements PluginView {
|
|
|
548
549
|
|
|
549
550
|
return;
|
|
550
551
|
}
|
|
552
|
+
this.state.block = refreshedBlock as typeof this.state.block;
|
|
551
553
|
|
|
552
554
|
const { height: rowCount, width: colCount } = getDimensionsOfTable(
|
|
553
555
|
this.state.block,
|
|
@@ -1106,15 +1108,26 @@ export const TableHandlesExtension = createExtension(({ editor }) => {
|
|
|
1106
1108
|
// When the selection is a normal text selection
|
|
1107
1109
|
// Assumes we are within a tableParagraph
|
|
1108
1110
|
// And find the from and to cells by resolving the positions
|
|
1109
|
-
|
|
1110
|
-
selection.$from.pos - selection.$from.parentOffset - 1
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1111
|
+
const fromCellPos =
|
|
1112
|
+
selection.$from.pos - selection.$from.parentOffset - 1;
|
|
1113
|
+
const toCellPos = selection.$to.pos - selection.$to.parentOffset - 1;
|
|
1114
|
+
|
|
1115
|
+
// Opt-out when the selection is not pointing into cells. This happens when the selection
|
|
1116
|
+
// is at the start of the table's `blockContainer` node and therefore just before the
|
|
1117
|
+
// actual `table` node.
|
|
1118
|
+
if (fromCellPos < 0 || toCellPos < 0) {
|
|
1119
|
+
return undefined;
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
$fromCell = tr.doc.resolve(fromCellPos);
|
|
1123
|
+
$toCell = tr.doc.resolve(toCellPos);
|
|
1124
|
+
|
|
1125
|
+
// Opt-out when the selection is not actually pointing into table
|
|
1126
|
+
// cells (e.g. a gap cursor next to a nested block).
|
|
1127
|
+
if (
|
|
1128
|
+
!isTableCellNode($fromCell.parent) ||
|
|
1129
|
+
!isTableCellNode($toCell.parent)
|
|
1130
|
+
) {
|
|
1118
1131
|
return undefined;
|
|
1119
1132
|
}
|
|
1120
1133
|
}
|
|
@@ -13,15 +13,10 @@ import {
|
|
|
13
13
|
|
|
14
14
|
const PLUGIN_KEY = new PluginKey<DecorationSet>("trailingNode");
|
|
15
15
|
|
|
16
|
-
// Skip the widget when the
|
|
17
|
-
//
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const rootGroup = doc.lastChild;
|
|
24
|
-
const lastBlock = rootGroup?.lastChild;
|
|
16
|
+
// Skip the widget when the container already ends with an empty paragraph
|
|
17
|
+
// block (since the user can just type into it).
|
|
18
|
+
function containerNeedsTrailingWidget(container: PMNode): boolean {
|
|
19
|
+
const lastBlock = container.lastChild;
|
|
25
20
|
const lastContent = lastBlock?.firstChild;
|
|
26
21
|
|
|
27
22
|
return !(
|
|
@@ -31,12 +26,48 @@ function shouldShowTrailingWidget(doc: PMNode, isEditable: boolean): boolean {
|
|
|
31
26
|
);
|
|
32
27
|
}
|
|
33
28
|
|
|
29
|
+
// Returns the position at the end of each container that should render a
|
|
30
|
+
// trailing widget: the root blockGroup, and columns from the multi-column
|
|
31
|
+
// package. Nested blockGroups (a block's children) are excluded, as they have
|
|
32
|
+
// no empty space below them for a widget to occupy.
|
|
33
|
+
function getTrailingWidgetPositions(doc: PMNode): number[] {
|
|
34
|
+
// When the schema has no columns, the root blockGroup is the only possible
|
|
35
|
+
// container, so traversing the doc to find others can be skipped.
|
|
36
|
+
if (!doc.type.schema.nodes["column"]) {
|
|
37
|
+
const rootGroup = doc.lastChild;
|
|
38
|
+
return rootGroup && containerNeedsTrailingWidget(rootGroup)
|
|
39
|
+
? [doc.content.size - 1]
|
|
40
|
+
: [];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const positions: number[] = [];
|
|
44
|
+
|
|
45
|
+
doc.descendants((node, pos, parent) => {
|
|
46
|
+
if (node.isTextblock) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const isContainer =
|
|
51
|
+
node.type.name === "column" ||
|
|
52
|
+
(node.type.name === "blockGroup" && parent?.type.name === "doc");
|
|
53
|
+
|
|
54
|
+
if (isContainer && containerNeedsTrailingWidget(node)) {
|
|
55
|
+
positions.push(pos + node.nodeSize - 1);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return true;
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
return positions;
|
|
62
|
+
}
|
|
63
|
+
|
|
34
64
|
/**
|
|
35
65
|
* Renders a fake trailing block as a widget decoration after the last block of
|
|
36
|
-
* the document
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* content is
|
|
66
|
+
* the document, as well as of any other container that blocks can be appended
|
|
67
|
+
* to (e.g. columns). Clicking it inserts a real trailing block in the
|
|
68
|
+
* container and moves the selection into it. This way the trailing block is
|
|
69
|
+
* not part of the document content, so it doesn't appear when the editor is
|
|
70
|
+
* read-only or when the content is exported.
|
|
40
71
|
*/
|
|
41
72
|
export const TrailingNodeExtension = createExtension(
|
|
42
73
|
({ editor }: ExtensionOptions) => {
|
|
@@ -52,17 +83,33 @@ export const TrailingNodeExtension = createExtension(
|
|
|
52
83
|
// based on this click.
|
|
53
84
|
event.preventDefault();
|
|
54
85
|
|
|
86
|
+
const view = editor.prosemirrorView;
|
|
87
|
+
if (!view) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// The widget may have been remapped since it was created, so its
|
|
92
|
+
// container is resolved from its current DOM position instead of
|
|
93
|
+
// captured up front.
|
|
94
|
+
const container = view.state.doc.resolve(
|
|
95
|
+
view.posAtDOM(el, 0),
|
|
96
|
+
).parent;
|
|
97
|
+
const lastBlockId = container.lastChild?.attrs["id"];
|
|
98
|
+
if (!lastBlockId) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
55
102
|
editor.transact((tr) => {
|
|
56
103
|
const [insertedBlock] = editor.insertBlocks(
|
|
57
104
|
[{ type: "paragraph" }],
|
|
58
|
-
|
|
105
|
+
lastBlockId,
|
|
59
106
|
"after",
|
|
60
107
|
);
|
|
61
108
|
editor.setTextCursorPosition(insertedBlock, "start");
|
|
62
109
|
tr.scrollIntoView();
|
|
63
110
|
});
|
|
64
111
|
|
|
65
|
-
|
|
112
|
+
view.focus();
|
|
66
113
|
});
|
|
67
114
|
return el;
|
|
68
115
|
},
|
|
@@ -70,29 +117,44 @@ export const TrailingNodeExtension = createExtension(
|
|
|
70
117
|
);
|
|
71
118
|
}
|
|
72
119
|
|
|
73
|
-
// Maps the existing DecorationSet through the transaction, then
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
// reference-stable across transactions.
|
|
120
|
+
// Maps the existing DecorationSet through the transaction, then diffs it
|
|
121
|
+
// against the containers that should currently show a widget, only adding
|
|
122
|
+
// and removing where the two differ. Decorations (and their rendered DOM)
|
|
123
|
+
// stay reference-stable across transactions for unchanged containers.
|
|
77
124
|
function nextDecorationSet(
|
|
78
125
|
tr: Transaction,
|
|
79
126
|
oldSet: DecorationSet,
|
|
80
127
|
isEditable: boolean,
|
|
81
128
|
): DecorationSet {
|
|
82
129
|
const mapped = oldSet.map(tr.mapping, tr.doc);
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
130
|
+
const desiredPositions = new Set(
|
|
131
|
+
isEditable ? getTrailingWidgetPositions(tr.doc) : [],
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
const keptPositions = new Set<number>();
|
|
135
|
+
const stale: Decoration[] = [];
|
|
136
|
+
for (const decoration of mapped.find()) {
|
|
137
|
+
if (
|
|
138
|
+
desiredPositions.has(decoration.from) &&
|
|
139
|
+
!keptPositions.has(decoration.from)
|
|
140
|
+
) {
|
|
141
|
+
keptPositions.add(decoration.from);
|
|
142
|
+
} else {
|
|
143
|
+
stale.push(decoration);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const missing = [...desiredPositions].filter(
|
|
147
|
+
(pos) => !keptPositions.has(pos),
|
|
148
|
+
);
|
|
86
149
|
|
|
87
|
-
|
|
88
|
-
|
|
150
|
+
let next = mapped;
|
|
151
|
+
if (stale.length > 0) {
|
|
152
|
+
next = next.remove(stale);
|
|
89
153
|
}
|
|
90
|
-
if (
|
|
91
|
-
|
|
154
|
+
if (missing.length > 0) {
|
|
155
|
+
next = next.add(tr.doc, missing.map(createTrailingWidget));
|
|
92
156
|
}
|
|
93
|
-
return
|
|
94
|
-
createTrailingWidget(tr.doc.content.size - 1),
|
|
95
|
-
]);
|
|
157
|
+
return next;
|
|
96
158
|
}
|
|
97
159
|
|
|
98
160
|
return {
|
|
@@ -133,8 +195,9 @@ export const TrailingNodeExtension = createExtension(
|
|
|
133
195
|
props: {
|
|
134
196
|
decorations: (state) => PLUGIN_KEY.getState(state),
|
|
135
197
|
// Prevents ProseMirror from trying to move the selection into the
|
|
136
|
-
// trailing block, which causes the text
|
|
137
|
-
// before returning to its previous
|
|
198
|
+
// trailing block at the end of the document, which causes the text
|
|
199
|
+
// caret to flicker in it before returning to its previous
|
|
200
|
+
// position.
|
|
138
201
|
handleKeyDown: (view, event) => {
|
|
139
202
|
if (event.key !== "ArrowRight" && event.key !== "ArrowDown") {
|
|
140
203
|
return false;
|
|
@@ -150,8 +213,11 @@ export const TrailingNodeExtension = createExtension(
|
|
|
150
213
|
return false;
|
|
151
214
|
}
|
|
152
215
|
|
|
216
|
+
const rootGroup = view.state.doc.lastChild;
|
|
153
217
|
if (
|
|
154
|
-
!
|
|
218
|
+
!editor.isEditable ||
|
|
219
|
+
!rootGroup ||
|
|
220
|
+
!containerNeedsTrailingWidget(rootGroup)
|
|
155
221
|
) {
|
|
156
222
|
return false;
|
|
157
223
|
}
|
package/src/index.ts
CHANGED
|
@@ -37,6 +37,7 @@ export { selectedFragmentToHTML } from "./api/clipboard/toClipboard/copyExtensio
|
|
|
37
37
|
|
|
38
38
|
// Node conversions
|
|
39
39
|
export * from "./api/nodeConversions/blockToNode.js";
|
|
40
|
+
export * from "./api/nodeConversions/fragmentToBlocks.js";
|
|
40
41
|
export * from "./api/nodeConversions/nodeToBlock.js";
|
|
41
42
|
export * from "./extensions/tiptap-extensions/UniqueID/UniqueID.js";
|
|
42
43
|
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
} from "@tiptap/pm/model";
|
|
8
8
|
import { NodeView } from "@tiptap/pm/view";
|
|
9
9
|
import { mergeParagraphs } from "../../blocks/defaultBlockHelpers.js";
|
|
10
|
+
import { ignoreNonContentMutations } from "../nodeViewMutations.js";
|
|
10
11
|
import {
|
|
11
12
|
Extension,
|
|
12
13
|
ExtensionFactoryInstance,
|
|
@@ -14,7 +15,7 @@ import {
|
|
|
14
15
|
import { nonFormattingMarks } from "../markGroups.js";
|
|
15
16
|
import { PropSchema } from "../propTypes.js";
|
|
16
17
|
import {
|
|
17
|
-
|
|
18
|
+
getBlockFromNodeView,
|
|
18
19
|
propsToAttributes,
|
|
19
20
|
wrapInBlockStructure,
|
|
20
21
|
} from "./internal.js";
|
|
@@ -246,8 +247,15 @@ export function addNodeAndExtensionsToSpec<
|
|
|
246
247
|
return (props) => {
|
|
247
248
|
// Gets the BlockNote editor instance
|
|
248
249
|
const editor = this.options.editor;
|
|
249
|
-
// Gets the block
|
|
250
|
-
|
|
250
|
+
// Gets the block. Resolving this can't rely on `getPos()` alone —
|
|
251
|
+
// node views are constructed part-way through ProseMirror's
|
|
252
|
+
// reconciliation, where positions don't always line up with
|
|
253
|
+
// `view.state.doc` yet (see `getBlockFromNodeView`).
|
|
254
|
+
const block = getBlockFromNodeView(
|
|
255
|
+
props.getPos,
|
|
256
|
+
props.node,
|
|
257
|
+
props.view.state.doc,
|
|
258
|
+
);
|
|
251
259
|
// Gets the custom HTML attributes for `blockContent` nodes
|
|
252
260
|
const blockContentDOMAttributes =
|
|
253
261
|
this.options.domAttributes?.blockContent || {};
|
|
@@ -271,6 +279,11 @@ export function addNodeAndExtensionsToSpec<
|
|
|
271
279
|
applyNonSelectableBlockFix(typedNodeView, this.editor);
|
|
272
280
|
}
|
|
273
281
|
|
|
282
|
+
// Ignores DOM mutations that don't affect the block's content, so
|
|
283
|
+
// that browser extensions which rewrite the DOM (e.g. Dark Reader)
|
|
284
|
+
// can't trigger an infinite re-render loop that freezes the tab.
|
|
285
|
+
ignoreNonContentMutations(typedNodeView);
|
|
286
|
+
|
|
274
287
|
// See explanation for why `update` is not implemented for NodeViews
|
|
275
288
|
// https://github.com/TypeCellOS/BlockNote/pull/1904#discussion_r2313461464
|
|
276
289
|
// TODO: in a future version, we might want to implement updates so that
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test";
|
|
2
|
+
|
|
3
|
+
import { getNodeById } from "../../api/nodeUtil.js";
|
|
4
|
+
import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
|
|
5
|
+
import { getBlockFromNodeView } from "./internal.js";
|
|
6
|
+
|
|
7
|
+
describe("getBlockFromNodeView", () => {
|
|
8
|
+
let editor: BlockNoteEditor;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
editor = BlockNoteEditor.create({
|
|
12
|
+
trailingBlock: false,
|
|
13
|
+
initialContent: [
|
|
14
|
+
{ type: "paragraph", content: "first" },
|
|
15
|
+
{ type: "paragraph", content: "second" },
|
|
16
|
+
],
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
// Leaving the editor alive leaks a ProseMirror `DOMObserver`, whose
|
|
22
|
+
// `stop()` schedules a `flush()` 20ms later. That can outlive the test
|
|
23
|
+
// environment and then fail the run with `ReferenceError: document is not
|
|
24
|
+
// defined`, attributed to whichever test file happens to be running.
|
|
25
|
+
editor._tiptapEditor.destroy();
|
|
26
|
+
editor = undefined as any;
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("resolves from the position when it is valid", () => {
|
|
30
|
+
const doc = editor.prosemirrorState.doc;
|
|
31
|
+
const target = editor.document[1];
|
|
32
|
+
// `getBlockFromPos` resolves the *parent* of the position, so the position
|
|
33
|
+
// sits just inside the block container — which is what ProseMirror hands a
|
|
34
|
+
// node view.
|
|
35
|
+
const pos = getNodeById(target.id, doc)!.posBeforeNode + 1;
|
|
36
|
+
const block = getBlockFromNodeView(() => pos, doc.nodeAt(pos)!, doc) as any;
|
|
37
|
+
|
|
38
|
+
expect(block.id).toBe(target.id);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("builds a standalone block when the node is no longer in the document", () => {
|
|
42
|
+
// The only way `getPos()` fails at node view construction is a re-entrant
|
|
43
|
+
// dispatch superseding `view.state.doc`, which takes the node with it — so
|
|
44
|
+
// the node genuinely isn't in the document any more. See #2937.
|
|
45
|
+
const doc = editor.prosemirrorState.doc;
|
|
46
|
+
const orphan = editor.pmSchema.nodes.paragraph.create(
|
|
47
|
+
{ textAlignment: "right" },
|
|
48
|
+
editor.pmSchema.text("orphaned content"),
|
|
49
|
+
);
|
|
50
|
+
const block = getBlockFromNodeView(
|
|
51
|
+
() => doc.content.size + 1000,
|
|
52
|
+
orphan,
|
|
53
|
+
doc,
|
|
54
|
+
) as any;
|
|
55
|
+
|
|
56
|
+
// Everything readable off the node is correct...
|
|
57
|
+
expect(block.type).toBe("paragraph");
|
|
58
|
+
expect(block.props.textAlignment).toBe("right");
|
|
59
|
+
expect(block.content[0].text).toBe("orphaned content");
|
|
60
|
+
// ...but the id belongs to no block in the document, so it must not
|
|
61
|
+
// collide with a real one.
|
|
62
|
+
expect(editor.document.map((b) => b.id)).not.toContain(block.id);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("recovers from an in-range position that resolves to the wrong node", () => {
|
|
66
|
+
// Position 0 resolves to the doc rather than a block container — the shape
|
|
67
|
+
// a bounds check alone would not catch.
|
|
68
|
+
const doc = editor.prosemirrorState.doc;
|
|
69
|
+
const orphan = editor.pmSchema.nodes.paragraph.create(
|
|
70
|
+
null,
|
|
71
|
+
editor.pmSchema.text("orphaned content"),
|
|
72
|
+
);
|
|
73
|
+
expect(() => getBlockFromNodeView(() => 0, orphan, doc)).not.toThrow();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("recovers when the position is undefined", () => {
|
|
77
|
+
const doc = editor.prosemirrorState.doc;
|
|
78
|
+
const orphan = editor.pmSchema.nodes.paragraph.create(
|
|
79
|
+
null,
|
|
80
|
+
editor.pmSchema.text("orphaned content"),
|
|
81
|
+
);
|
|
82
|
+
expect(() =>
|
|
83
|
+
getBlockFromNodeView(() => undefined, orphan, doc),
|
|
84
|
+
).not.toThrow();
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -93,6 +93,70 @@ export function getBlockFromPos(getPos: () => number | undefined, doc: PMNode) {
|
|
|
93
93
|
return block;
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
/**
|
|
97
|
+
* Resolves the block a node view should render, tolerating a `getPos()` that
|
|
98
|
+
* can't be trusted.
|
|
99
|
+
*
|
|
100
|
+
* ProseMirror derives `getPos()` from its view-desc tree, but
|
|
101
|
+
* `EditorView.updateStateInner` assigns the new state *before* it reconciles
|
|
102
|
+
* that tree. Anything that runs partway through reconciliation - a re-entrant
|
|
103
|
+
* dispatch from a node view's effect, TipTap's `flushSync` while mounting a
|
|
104
|
+
* node view - therefore sees positions that no longer line up with
|
|
105
|
+
* `view.state.doc`. The position then either lands out of range or, just as
|
|
106
|
+
* bad, in range but pointing at the wrong node.
|
|
107
|
+
*
|
|
108
|
+
* Node views are constructed inside that same window, so this applies at
|
|
109
|
+
* construction just as much as on re-render. The state is always transient:
|
|
110
|
+
* ProseMirror finishes reconciling and rebuilds the node view against the
|
|
111
|
+
* current document immediately after. So we degrade instead of throwing,
|
|
112
|
+
* because a stale frame is invisible where a throw is not.
|
|
113
|
+
*
|
|
114
|
+
* See issues #2937, #2682 and #2621.
|
|
115
|
+
*/
|
|
116
|
+
export function getBlockFromNodeView(
|
|
117
|
+
getPos: () => number | undefined,
|
|
118
|
+
node: PMNode,
|
|
119
|
+
doc: PMNode,
|
|
120
|
+
) {
|
|
121
|
+
try {
|
|
122
|
+
return getBlockFromPos(getPos, doc);
|
|
123
|
+
} catch (e) {
|
|
124
|
+
// Failing here means the node is not in `doc` — a re-entrant dispatch
|
|
125
|
+
// superseded the document ProseMirror is building node views for, and the
|
|
126
|
+
// node went with it. So there is no container to read an id from, and the
|
|
127
|
+
// block has to be built from the node alone. Deliberately silent: this is
|
|
128
|
+
// expected and self-correcting, and there is nothing a consumer could do
|
|
129
|
+
// about it in the meantime.
|
|
130
|
+
//
|
|
131
|
+
// `type`, `props` and `content` are read off the node and are correct.
|
|
132
|
+
// `children` is empty and `id` is freshly generated, i.e. it belongs to no
|
|
133
|
+
// block in the document — callers must not treat it as addressable. (It
|
|
134
|
+
// can't collide with a real block: ids are uuids, and the deterministic
|
|
135
|
+
// test-mode generator shares one monotonic counter with real ids.) This is
|
|
136
|
+
// short-lived; ProseMirror rebuilds the node view against the real document
|
|
137
|
+
// right after.
|
|
138
|
+
//
|
|
139
|
+
// The alternatives are worse. Throwing is the crash this exists to prevent.
|
|
140
|
+
// Returning an empty placeholder node view can leave the block
|
|
141
|
+
// *permanently* blank, since vanilla node views don't implement `update()`
|
|
142
|
+
// and so are only rebuilt when something else changes the document.
|
|
143
|
+
//
|
|
144
|
+
// `createAndFill` rather than `create` so an unexpected node shape yields
|
|
145
|
+
// `null` instead of throwing over the top of the original failure.
|
|
146
|
+
const standalone = doc.type.schema.nodes["blockContainer"]?.createAndFill(
|
|
147
|
+
null,
|
|
148
|
+
node,
|
|
149
|
+
);
|
|
150
|
+
if (standalone) {
|
|
151
|
+
return nodeToBlock(standalone, doc);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Nothing left to render from. Surface the original failure rather than
|
|
155
|
+
// inventing a block that isn't grounded in anything.
|
|
156
|
+
throw e;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
96
160
|
// Function that wraps the `dom` element returned from 'blockConfig.render' in a
|
|
97
161
|
// `blockContent` div, which contains the block type and props as HTML
|
|
98
162
|
// attributes. If `blockConfig.render` also returns a `contentDOM`, it also adds
|
|
@@ -4,6 +4,7 @@ import { TagParseRule } from "@tiptap/pm/model";
|
|
|
4
4
|
import { inlineContentToNodes } from "../../api/nodeConversions/blockToNode.js";
|
|
5
5
|
import { nodeToCustomInlineContent } from "../../api/nodeConversions/nodeToBlock.js";
|
|
6
6
|
import type { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
|
|
7
|
+
import { ignoreNonContentMutations } from "../nodeViewMutations.js";
|
|
7
8
|
import { propsToAttributes } from "../blocks/internal.js";
|
|
8
9
|
import { Props } from "../propTypes.js";
|
|
9
10
|
import { StyleSchema } from "../styles/types.js";
|
|
@@ -208,12 +209,19 @@ export function createInlineContentSpec<
|
|
|
208
209
|
editor,
|
|
209
210
|
);
|
|
210
211
|
|
|
211
|
-
|
|
212
|
+
const nodeView = addInlineContentAttributes(
|
|
212
213
|
output,
|
|
213
214
|
inlineContentConfig.type,
|
|
214
215
|
node.attrs as Props<T["propSchema"]>,
|
|
215
216
|
inlineContentConfig.propSchema,
|
|
216
217
|
);
|
|
218
|
+
|
|
219
|
+
// Ignores DOM mutations that don't affect the inline content, so that
|
|
220
|
+
// browser extensions which rewrite the DOM (e.g. Dark Reader) can't
|
|
221
|
+
// trigger an infinite re-render loop that freezes the tab.
|
|
222
|
+
ignoreNonContentMutations(nodeView);
|
|
223
|
+
|
|
224
|
+
return nodeView;
|
|
217
225
|
};
|
|
218
226
|
},
|
|
219
227
|
});
|