Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
tableContentToNodes,
} from "../../../nodeConversions/blockToNode.js";
import { nodeToCustomInlineContent } from "../../../nodeConversions/nodeToBlock.js";
import { partialBlockToFullBlock } from "../../../partialBlockToFullBlock.js";

function addAttributesAndRemoveClasses(element: HTMLElement) {
// Removes all BlockNote specific class names.
Expand Down Expand Up @@ -175,20 +176,12 @@ function serializeBlock<
const doc = options?.document ?? document;
const BC_NODE = editor.pmSchema.nodes["blockContainer"];

// set default props in case we were passed a partial block
const props = block.props || {};
for (const [name, spec] of Object.entries(
editor.schema.blockSchema[block.type as any].propSchema,
)) {
if (!(name in props) && spec.default !== undefined) {
(props as any)[name] = spec.default;
}
}
const fullBlock = partialBlockToFullBlock(block, editor);

const bc = BC_NODE.spec?.toDOM?.(
BC_NODE.create({
id: block.id,
...props,
id: fullBlock.id,
...fullBlock.props,
}),
) as {
dom: HTMLElement;
Expand All @@ -199,19 +192,14 @@ function serializeBlock<
// we should change toExternalHTML so that this is not necessary
const attrs = Array.from(bc.dom.attributes);

const blockImplementation = editor.blockImplementations[block.type as any]
const blockImplementation = editor.blockImplementations[fullBlock.type]
.implementation as BlockImplementation;
const ret =
blockImplementation.toExternalHTML?.call(
{},
{ ...block, props } as any,
fullBlock as any,
editor as any,
) ||
blockImplementation.render.call(
{},
{ ...block, props } as any,
editor as any,
);
) || blockImplementation.render.call({}, fullBlock as any, editor as any);

const elementFragment = doc.createDocumentFragment();

Expand Down Expand Up @@ -241,10 +229,10 @@ function serializeBlock<
elementFragment.append(ret.dom);
}

if (ret.contentDOM && block.content) {
if (ret.contentDOM && fullBlock.content) {
const ic = serializeInlineContentExternalHTML(
editor,
block.content as any, // TODO
fullBlock.content as any, // TODO
serializer,
options,
);
Expand All @@ -253,9 +241,9 @@ function serializeBlock<
}

let listType = undefined;
if (orderedListItemBlockTypes.has(block.type!)) {
if (orderedListItemBlockTypes.has(fullBlock.type!)) {
listType = "OL";
} else if (unorderedListItemBlockTypes.has(block.type!)) {
} else if (unorderedListItemBlockTypes.has(fullBlock.type!)) {
listType = "UL";
}

Expand All @@ -265,11 +253,11 @@ function serializeBlock<

if (
listType === "OL" &&
"start" in props &&
props.start &&
props?.start !== 1
"start" in fullBlock.props &&
fullBlock.props.start &&
fullBlock.props?.start !== 1
) {
list.setAttribute("start", props.start + "");
list.setAttribute("start", fullBlock.props.start + "");
}
fragment.append(list);
}
Expand All @@ -278,12 +266,12 @@ function serializeBlock<
fragment.append(elementFragment);
}

if (block.children && block.children.length > 0) {
if (fullBlock.children && fullBlock.children.length > 0) {
const childFragment = doc.createDocumentFragment();
serializeBlocksToFragment(
childFragment,
editor,
block.children,
fullBlock.children,
serializer,
orderedListItemBlockTypes,
unorderedListItemBlockTypes,
Expand All @@ -302,7 +290,7 @@ function serializeBlock<
}
}

if (editor.pmSchema.nodes[block.type as any].isInGroup("blockContent")) {
if (editor.pmSchema.nodes[fullBlock.type].isInGroup("blockContent")) {
// default "blockContainer" style blocks are flattened (no "nested block" support) for externalHTML, so append the child fragment to the outer fragment
fragment.append(childFragment);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import {
inlineContentToNodes,
tableContentToNodes,
} from "../../../nodeConversions/blockToNode.js";

import { nodeToCustomInlineContent } from "../../../nodeConversions/nodeToBlock.js";
import { partialBlockToFullBlock } from "../../../partialBlockToFullBlock.js";

export function serializeInlineContentInternalHTML<
BSchema extends BlockSchema,
I extends InlineContentSchema,
Expand Down Expand Up @@ -138,45 +139,36 @@ function serializeBlock<
) {
const BC_NODE = editor.pmSchema.nodes["blockContainer"];

// set default props in case we were passed a partial block
const props = block.props || {};
for (const [name, spec] of Object.entries(
editor.schema.blockSchema[block.type as any].propSchema,
)) {
if (!(name in props) && spec.default !== undefined) {
(props as any)[name] = spec.default;
}
}
const children = block.children || [];
const fullBlock = partialBlockToFullBlock(block, editor);

const impl = editor.blockImplementations[block.type as any].implementation;
const impl = editor.blockImplementations[fullBlock.type].implementation;
const ret = impl.render.call(
{
renderType: "dom",
props: undefined,
},
{ ...block, props, children } as any,
fullBlock,
editor as any,
);

if (ret.contentDOM && block.content) {
if (ret.contentDOM && fullBlock.content) {
const ic = serializeInlineContentInternalHTML(
editor,
block.content as any, // TODO
fullBlock.content as any, // TODO
serializer,
block.type,
fullBlock.type,
options,
);
ret.contentDOM.appendChild(ic);
}

const pmType = editor.pmSchema.nodes[block.type as any];
const pmType = editor.pmSchema.nodes[fullBlock.type as any];

if (pmType.isInGroup("bnBlock")) {
if (block.children && block.children.length > 0) {
if (fullBlock.children && fullBlock.children.length > 0) {
const fragment = serializeBlocks(
editor,
block.children,
fullBlock.children,
serializer,
options,
);
Expand All @@ -189,8 +181,8 @@ function serializeBlock<
// wrap the block in a blockContainer
const bc = BC_NODE.spec?.toDOM?.(
BC_NODE.create({
id: block.id,
...props,
id: fullBlock.id,
...fullBlock.props,
}),
) as {
dom: HTMLElement;
Expand All @@ -199,9 +191,14 @@ function serializeBlock<

bc.contentDOM?.appendChild(ret.dom);

if (block.children && block.children.length > 0) {
if (fullBlock.children && fullBlock.children.length > 0) {
bc.contentDOM?.appendChild(
serializeBlocksInternalHTML(editor, block.children, serializer, options),
serializeBlocksInternalHTML(
editor,
fullBlock.children,
serializer,
options,
),
);
}
return bc.dom;
Expand Down
Loading
Loading