@@ -35,39 +35,57 @@ export const serializeNodeInner = <BSchema extends BlockSchema>(
35
35
36
36
// Handles converting `blockContainer` nodes to HTML.
37
37
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
+
38
48
// Converts `blockContent` node using the custom `blockSpec`'s
39
49
// `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
+ }
52
76
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
+ ) ;
58
80
}
59
81
60
- blockContent . contentDOM . appendChild (
61
- serializer . serializeFragment ( node . firstChild ! . content , options )
62
- ) ;
82
+ contentDOM . appendChild ( blockContent . dom ) ;
63
83
}
64
84
65
- contentDOM . appendChild ( blockContent . dom ) ;
66
-
67
85
// Converts `blockGroup` node to HTML using its `renderHTML` method.
68
- if ( node . childCount === 2 ) {
86
+ if ( blockGroupNode !== undefined ) {
69
87
serializer . serializeFragment (
70
- Fragment . from ( node . content . lastChild ) ,
88
+ Fragment . from ( blockGroupNode ) ,
71
89
options ,
72
90
contentDOM
73
91
) ;
0 commit comments