Skip to content

Commit

Permalink
Fixes for blocks without inline content
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewlipski committed Aug 22, 2023
1 parent 0b16095 commit 31be20f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
48 changes: 36 additions & 12 deletions packages/core/src/extensions/Blocks/nodes/BlockContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,42 @@ export const BlockContainer = Node.create<{
);
}

// Changes the blockContent node type and adds the provided props as attributes. Also preserves all existing
// attributes that are compatible with the new type.
state.tr.setNodeMarkup(
startPos,
block.type === undefined
? undefined
: state.schema.nodes[block.type],
{
...contentNode.attrs,
...block.props,
}
);
// Since some block types contain inline content and others don't,
// we either need to call setNodeMarkup to just update type &
// attributes, or replaceWith to replace the whole blockContent.
const oldType = contentNode.type.name;
const newType = block.type || oldType;

const oldContentType = state.schema.nodes[oldType].spec.content;
const newContentType = state.schema.nodes[newType].spec.content;

if (oldContentType === "inline*" && newContentType === "") {
// Replaces the blockContent node with one of the new type and
// adds the provided props as attributes. Also preserves all
// existing attributes that are compatible with the new type.
state.tr.replaceWith(
startPos,
endPos,
state.schema.nodes[newType].create({
...contentNode.attrs,
...block.props,
})
);
} else {
// Changes the blockContent node type and adds the provided props
// as attributes. Also preserves all existing attributes that are
// compatible with the new type.
state.tr.setNodeMarkup(
startPos,
block.type === undefined
? undefined
: state.schema.nodes[block.type],
{
...contentNode.attrs,
...block.props,
}
);
}

// Adds all provided props as attributes to the parent blockContainer node too, and also preserves existing
// attributes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,19 @@ export const TrailingNode = Extension.create<TrailingNodeOptions>({
if (!lastNode || lastNode.type.name !== "blockContainer") {
throw new Error("Expected blockContainer");
}
return lastNode.nodeSize > 4; // empty <block><content/></block> is length 4

const lastContentNode = lastNode.firstChild;

if (!lastContentNode) {
throw new Error("Expected blockContent");
}

return (
lastNode.nodeSize > 4 ||
lastContentNode.type.spec.content !== "inline*"
);
// empty <block><content/></block> is length 4
// blocks with no inline content are always empty
},
},
}),
Expand Down

0 comments on commit 31be20f

Please sign in to comment.