Skip to content
Merged
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
@@ -0,0 +1,9 @@
<div class="bn-block-group" data-node-type="blockGroup">
<div class="bn-block-outer" data-node-type="blockOuter" data-id="1">
<div class="bn-block" data-node-type="blockContainer" data-id="1">
<div class="bn-block-content" data-content-type="simpleCustomParagraph">
<p class="bn-inline-content simple-custom-paragraph">Simple Custom Paragraph</p>
</div>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p class="simple-custom-paragraph">Simple Custom Paragraph</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Simple Custom Paragraph
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"attrs": {
"id": "1",
},
"content": [
{
"attrs": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"content": [
{
"text": "Simple Custom Paragraph",
"type": "text",
},
],
"type": "simpleCustomParagraph",
},
],
"type": "blockContainer",
},
]
12 changes: 12 additions & 0 deletions tests/src/unit/core/formatConversion/export/exportTestInstances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,18 @@ export const exportTestInstancesBlockNoteHTML: TestInstance<
},
executeTest: testExportBlockNoteHTML,
},
{
testCase: {
name: "custom-blocks/simpleCustomParagraph",
content: [
{
type: "simpleCustomParagraph",
content: "Simple Custom Paragraph",
},
],
},
executeTest: testExportBlockNoteHTML,
},
];

export const exportTestInstancesHTML: TestInstance<
Expand Down
1 change: 1 addition & 0 deletions tests/src/unit/core/schema/__snapshots__/blocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
"extensions": undefined,
"implementation": {
"node": null,
"parse": [Function],
"render": [Function],
"toExternalHTML": [Function],
},
Expand Down
67 changes: 44 additions & 23 deletions tests/src/unit/core/testSchema.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import {
BlockNoteSchema,
addNodeAndExtensionsToSpec,
createBlockConfig,
createBlockSpec,
createImageBlockConfig,
createImageBlockSpec,
createInlineContentSpec,
createPageBreakBlockSpec,
createStyleSpec,
defaultProps,
parseDefaultProps,
} from "@blocknote/core";

// BLOCKS ----------------------------------------------------------------------

// This is a modified version of the default image block that does not implement
// a `toExternalHTML` function. It's used to test if the custom serializer by
// default serializes custom blocks using their `render` function.
const SimpleImage = addNodeAndExtensionsToSpec(
{
type: "simpleImage",
propSchema: createImageBlockConfig({}).propSchema,
content: "none",
},
const SimpleImage = createBlockSpec(
createBlockConfig(
() =>
({
type: "simpleImage",
propSchema: createImageBlockConfig({}).propSchema,
content: "none",
}) as const,
),
{
render(block, editor) {
return createImageBlockSpec().implementation.render.call(
Expand All @@ -31,13 +36,27 @@ const SimpleImage = addNodeAndExtensionsToSpec(
},
);

const CustomParagraph = addNodeAndExtensionsToSpec(
{
type: "customParagraph",
propSchema: defaultProps,
content: "inline",
},
const CustomParagraph = createBlockSpec(
createBlockConfig(
() =>
({
type: "customParagraph",
propSchema: defaultProps,
content: "inline",
}) as const,
),
{
parse: (e) => {
if (e.tagName !== "P") {
return undefined;
}

if (e.classList.contains("custom-paragraph")) {
return parseDefaultProps(e);
}

return undefined;
},
render: () => {
const paragraph = document.createElement("p");
paragraph.className = "custom-paragraph";
Expand All @@ -50,7 +69,6 @@ const CustomParagraph = addNodeAndExtensionsToSpec(
toExternalHTML: () => {
const paragraph = document.createElement("p");
paragraph.className = "custom-paragraph";
paragraph.innerHTML = "Hello World";

return {
dom: paragraph,
Expand All @@ -59,12 +77,15 @@ const CustomParagraph = addNodeAndExtensionsToSpec(
},
);

const SimpleCustomParagraph = addNodeAndExtensionsToSpec(
{
type: "simpleCustomParagraph",
propSchema: defaultProps,
content: "inline",
},
const SimpleCustomParagraph = createBlockSpec(
createBlockConfig(
() =>
({
type: "simpleCustomParagraph",
propSchema: defaultProps,
content: "inline",
}) as const,
),
{
render: () => {
const paragraph = document.createElement("p");
Expand Down Expand Up @@ -198,9 +219,9 @@ const FontSize = createStyleSpec(
export const testSchema = BlockNoteSchema.create().extend({
blockSpecs: {
pageBreak: createPageBreakBlockSpec(),
customParagraph: CustomParagraph,
simpleCustomParagraph: SimpleCustomParagraph,
simpleImage: SimpleImage,
customParagraph: CustomParagraph(),
simpleCustomParagraph: SimpleCustomParagraph(),
simpleImage: SimpleImage(),
},
inlineContentSpecs: {
mention: Mention,
Expand Down
Loading