Skip to content

Commit 755a574

Browse files
committed
Fixed error when copying only nested blocks
1 parent 786b3d6 commit 755a574

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

packages/core/src/api/serialization/html/sharedHTMLConversion.ts

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,57 @@ export const serializeNodeInner = <BSchema extends BlockSchema>(
3535

3636
// Handles converting `blockContainer` nodes to HTML.
3737
if (node.type.name === "blockContainer") {
38+
const blockContentNode =
39+
node.childCount > 0 &&
40+
node.firstChild!.type.spec.group === "blockContent"
41+
? node.firstChild!
42+
: undefined;
43+
const blockGroupNode =
44+
node.childCount > 0 && node.lastChild!.type.spec.group === "blockGroup"
45+
? node.lastChild!
46+
: undefined;
47+
3848
// Converts `blockContent` node using the custom `blockSpec`'s
3949
// `toExternalHTML` or `toInternalHTML` function.
40-
const blockSpec =
41-
editor.schema[node.firstChild!.type.name as keyof BSchema];
42-
const toHTML = toExternalHTML
43-
? blockSpec.toExternalHTML
44-
: blockSpec.toInternalHTML;
45-
const blockContent = toHTML(
46-
nodeToBlock(node, editor.schema, editor.blockCache) as SpecificBlock<
47-
BlockSchema,
48-
keyof BlockSchema
49-
>,
50-
editor as BlockNoteEditor<BlockSchema>
51-
);
50+
// Note: While `blockContainer` nodes should always contain a
51+
// `blockContent` node according to the schema, PM Fragments don't always
52+
// conform to the schema. This is unintuitive but important as it occurs
53+
// when copying only nested blocks.
54+
if (blockContentNode !== undefined) {
55+
const blockSpec =
56+
editor.schema[blockContentNode.type.name as keyof BSchema];
57+
const toHTML = toExternalHTML
58+
? blockSpec.toExternalHTML
59+
: blockSpec.toInternalHTML;
60+
const blockContent = toHTML(
61+
nodeToBlock(node, editor.schema, editor.blockCache) as SpecificBlock<
62+
BlockSchema,
63+
keyof BlockSchema
64+
>,
65+
editor as BlockNoteEditor<BlockSchema>
66+
);
67+
68+
// Converts inline nodes in the `blockContent` node's content to HTML
69+
// using their `renderHTML` methods.
70+
if (blockContent.contentDOM !== undefined) {
71+
if (node.isLeaf) {
72+
throw new RangeError(
73+
"Content hole not allowed in a leaf node spec"
74+
);
75+
}
5276

53-
// Converts inline nodes in the `blockContent` node's content to HTML
54-
// using their `renderHTML` methods.
55-
if (blockContent.contentDOM) {
56-
if (node.isLeaf) {
57-
throw new RangeError("Content hole not allowed in a leaf node spec");
77+
blockContent.contentDOM.appendChild(
78+
serializer.serializeFragment(blockContentNode.content, options)
79+
);
5880
}
5981

60-
blockContent.contentDOM.appendChild(
61-
serializer.serializeFragment(node.firstChild!.content, options)
62-
);
82+
contentDOM.appendChild(blockContent.dom);
6383
}
6484

65-
contentDOM.appendChild(blockContent.dom);
66-
6785
// Converts `blockGroup` node to HTML using its `renderHTML` method.
68-
if (node.childCount === 2) {
86+
if (blockGroupNode !== undefined) {
6987
serializer.serializeFragment(
70-
Fragment.from(node.content.lastChild),
88+
Fragment.from(blockGroupNode),
7189
options,
7290
contentDOM
7391
);

0 commit comments

Comments
 (0)