Skip to content

Commit 25a0911

Browse files
committed
Changed custom block renderHTML implementation
1 parent e053f7b commit 25a0911

File tree

4 files changed

+46
-39
lines changed

4 files changed

+46
-39
lines changed

packages/core/src/BlockNoteEditor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ export class BlockNoteEditor<BSchema extends BlockSchema = DefaultBlockSchema> {
141141
const blockNoteExtensions = getBlockNoteExtensions<BSchema>({
142142
editor: this,
143143
uiFactories: newOptions.uiFactories || {},
144-
slashCommands: newOptions.slashCommands || (defaultSlashMenuItems as any),
144+
slashCommands:
145+
newOptions.slashCommands || (defaultSlashMenuItems() as any),
145146
blocks: newOptions.blockSchema,
146147
});
147148

packages/core/src/extensions/Blocks/api/block.ts

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@ export function createBlockSpec<
110110
];
111111
},
112112

113-
// TODO: Do we need renderHTML if we have a node view? Doesn't seem like it
114-
// makes sense but throws an error if ctrl+A and ctrl+C if it's not
115-
// implemented.
116113
renderHTML({ HTMLAttributes }) {
117114
// Create blockContent element
118115
const blockContent = document.createElement("div");
@@ -123,34 +120,45 @@ export function createBlockSpec<
123120
blockContent.setAttribute(attribute, value);
124121
}
125122

126-
// Gets BlockNote editor instance
127-
const editor = this.options.editor!;
128-
129-
// Quite hacky but don't think there's a better way to do this. Since the
130-
// contentDOM can be anywhere inside the DOM, we don't know which element
131-
// it is. Calling render() will give us the contentDOM, but we need to
132-
// provide a block as a parameter.
133-
const getDummyBlock: () => Block<BlockSchema> = () =>
134-
({
135-
id: "",
136-
type: "",
137-
props: {},
138-
content: [],
139-
children: [],
140-
} as Block<BlockSchema>);
141-
142-
// Render elements
143-
const rendered = blockConfig.render(getDummyBlock, editor);
144-
// Add elements to blockContent
145-
blockContent.appendChild(rendered.dom);
123+
// TODO: This only works for content copied within BlockNote.
124+
// Creates contentDOM element to serialize inline content into.
125+
let contentDOM: HTMLDivElement | undefined;
126+
if (blockConfig.containsInlineContent) {
127+
contentDOM = document.createElement("div");
128+
blockContent.appendChild(contentDOM);
129+
} else {
130+
contentDOM = undefined;
131+
}
132+
133+
// Alternative approach to serializing the block.
134+
// // Gets BlockNote editor instance
135+
// const editor = this.options.editor!;
136+
//
137+
// // Quite hacky but don't think there's a better way to do this. Since the
138+
// // contentDOM can be anywhere inside the DOM, we don't know which element
139+
// // it is. Calling render() will give us the contentDOM, but we need to
140+
// // provide a block as a parameter.
141+
// const getDummyBlock: () => Block<BlockSchema> = () =>
142+
// ({
143+
// id: "",
144+
// type: "",
145+
// props: {},
146+
// content: [],
147+
// children: [],
148+
// } as Block<BlockSchema>);
149+
//
150+
// // Render elements
151+
// const rendered = blockConfig.render(getDummyBlock, editor);
152+
// // Add elements to blockContent
153+
// blockContent.appendChild(rendered.dom);
154+
//
155+
// const contentDOM = blockConfig.containsInlineContent
156+
// ? rendered.contentDOM
157+
// : undefined;
146158

147159
return {
148160
dom: blockContent,
149-
// I don't understand what's going on with the typing here
150-
contentDOM:
151-
"contentDOM" in rendered
152-
? (rendered.contentDOM as HTMLDivElement)
153-
: undefined,
161+
contentDOM: contentDOM,
154162
};
155163
},
156164

packages/core/src/extensions/Blocks/api/blockTypes.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,13 @@ export type BlockConfig<
7272
// Additional attributes to help define block as a TipTap node.
7373
containsInlineContent: ContainsInlineContent;
7474
parse?: (element: HTMLElement) => Props<PSchema>;
75-
render: ContainsInlineContent extends true
76-
? (
77-
block: () => Block<BSchema>,
78-
editor: BlockNoteEditor<BSchema>
79-
) => { dom: HTMLElement; contentDOM: HTMLElement }
80-
: (
81-
block: () => Block<BSchema>,
82-
editor: BlockNoteEditor<BSchema>
83-
) => { dom: HTMLElement };
75+
render: (
76+
block: () => Block<BSchema>,
77+
editor: BlockNoteEditor<BSchema>
78+
) => {
79+
dom: HTMLElement;
80+
contentDOM: ContainsInlineContent extends true ? HTMLElement : undefined;
81+
};
8482
};
8583

8684
// Defines a single block spec, which includes the props that the block has and

packages/react/src/hooks/useBlockNote.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const useBlockNote = <BSchema extends BlockSchema = DefaultBlockSchema>(
3535
// but it would have to be on several different classes (BlockNoteEditor, BlockNoteEditorOptions, UiFactories) and
3636
// gets messy quick.
3737
let newOptions: Record<any, any> = {
38-
slashCommands: defaultReactSlashMenuItems,
38+
slashCommands: defaultReactSlashMenuItems(),
3939
...options,
4040
};
4141
if (!newOptions.uiFactories) {

0 commit comments

Comments
 (0)