Skip to content
Draft
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 @@ -207,6 +207,9 @@ function serializeBlock<
{},
{ ...block, props } as any,
editor as any,
{
nestingLevel,
},
) ||
blockImplementation.render.call(
{},
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/schema/blocks/createSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export function addNodeAndExtensionsToSpec<
},
// TODO: this should not have wrapInBlockStructure and generally be a lot simpler
// post-processing in externalHTMLExporter should not be necessary
toExternalHTML: (block, editor) => {
toExternalHTML: (block, editor, context) => {
const blockContentDOMAttributes =
node.options.domAttributes?.blockContent || {};

Expand All @@ -251,6 +251,7 @@ export function addNodeAndExtensionsToSpec<
{ blockContentDOMAttributes },
block as any,
editor as any,
context,
) ??
blockImplementation.render.call(
{ blockContentDOMAttributes, renderType: "dom", props: undefined },
Expand Down Expand Up @@ -393,11 +394,12 @@ export function createBlockSpec<
...blockImplementation,
// TODO: this should not have wrapInBlockStructure and generally be a lot simpler
// post-processing in externalHTMLExporter should not be necessary
toExternalHTML(block, editor) {
toExternalHTML(block, editor, context) {
const output = blockImplementation.toExternalHTML?.call(
{ blockContentDOMAttributes: this.blockContentDOMAttributes },
block as any,
editor as any,
context,
);

if (output === undefined) {
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/schema/blocks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ export type LooseBlockSpec<
toExternalHTML?: (
block: any,
editor: BlockNoteEditor<any>,
context: {
nestingLevel: number;
},
) =>
| {
dom: HTMLElement | DocumentFragment;
Expand Down Expand Up @@ -193,6 +196,9 @@ export type BlockSpecs = {
toExternalHTML?: (
block: any,
editor: BlockNoteEditor<any>,
context: {
nestingLevel: number;
},
) =>
| {
dom: HTMLElement | DocumentFragment;
Expand Down Expand Up @@ -463,6 +469,9 @@ export type BlockImplementation<
editor: BlockNoteEditor<
Record<TName, BlockConfig<TName, TProps, TContent>>
>,
context: {
nestingLevel: number;
},
) =>
| {
dom: HTMLElement | DocumentFragment;
Expand Down
13 changes: 10 additions & 3 deletions packages/react/src/schema/ReactBlockSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import {
BlockImplementation,
BlockNoDefaults,
BlockNoteEditor,
Extension,
BlockSpec,
camelToDataKebab,
CustomBlockImplementation,
Extension,
getBlockFromPos,
mergeCSSClasses,
Props,
Expand Down Expand Up @@ -51,7 +51,13 @@ export type ReactCustomBlockImplementation<
"render" | "toExternalHTML"
> & {
render: FC<ReactCustomBlockRenderProps<TName, TProps, TContent>>;
toExternalHTML?: FC<ReactCustomBlockRenderProps<TName, TProps, TContent>>;
toExternalHTML?: FC<
ReactCustomBlockRenderProps<TName, TProps, TContent> & {
context: {
nestingLevel: number;
};
}
>;
};

export type ReactCustomBlockSpec<
Expand Down Expand Up @@ -224,7 +230,7 @@ export function createReactBlockSpec<
config: blockConfig,
implementation: {
...blockImplementation,
toExternalHTML(block, editor) {
toExternalHTML(block, editor, context) {
const BlockContent =
blockImplementation.toExternalHTML || blockImplementation.render;
const output = renderToDOMSpec((refCB) => {
Expand All @@ -247,6 +253,7 @@ export function createReactBlockSpec<
);
}
}}
context={context}
/>
</BlockContentWrapper>
);
Expand Down
Loading