Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple latex block #818

Closed
wants to merge 14 commits into from
Prev Previous commit
Next Next commit
add test for BNUpdateBlock with line break
  • Loading branch information
jkcs committed Jun 6, 2024
commit 23d1c5b2dae1e736a2c56e1423e00374c1d24b69
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,5 @@ exports[`Inserting Blocks with Different Placements > Insert nested inside exist
},
]
`;

exports[`Update Custom Block content > update Custom Block with line break 1`] = `"<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=\\"customBlock\\"><div class=\\"bn-inline-content custom-block\\" data-editable=\\"\\">Updated Custom Block with <br>line <br>break</div></div><div class=\\"bn-block-group\\" data-node-type=\\"blockGroup\\"><div class=\\"bn-block-outer\\" data-node-type=\\"blockOuter\\" data-id=\\"2\\"><div class=\\"bn-block\\" data-node-type=\\"blockContainer\\" data-id=\\"2\\"><div class=\\"bn-block-content\\" data-content-type=\\"customBlock\\"><div class=\\"bn-inline-content custom-block\\" data-editable=\\"\\">Custom Block</div></div></div></div></div></div></div><div class=\\"bn-block-outer\\" data-node-type=\\"blockOuter\\" data-id=\\"0\\"><div class=\\"bn-block\\" data-node-type=\\"blockContainer\\" data-id=\\"0\\"><div class=\\"bn-block-content\\" data-content-type=\\"paragraph\\"><p class=\\"bn-inline-content\\"></p></div></div></div></div>"`;
74 changes: 71 additions & 3 deletions packages/core/src/api/blockManipulation/blockManipulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,43 @@ import { afterEach, beforeEach, describe, expect, it } from "vitest";
import {
Block,
DefaultBlockSchema,
defaultBlockSpecs,
DefaultInlineContentSchema,
DefaultStyleSchema,
PartialBlock,
} from "../../blocks/defaultBlocks";
import { BlockNoteEditor } from "../../editor/BlockNoteEditor";
import { createBlockSpec } from "../../schema";
import { BlockNoteSchema } from "../../editor/BlockNoteSchema";
import { createInternalHTMLSerializer } from "../exporters/html/internalHTMLSerializer";

const CustomBlock = createBlockSpec(
{
type: "customBlock",
propSchema: {},
content: "inline",
},
{
render: () => {
const dom = document.createElement("div");
dom.className = "custom-block";

return {
dom: dom,
contentDOM: dom,
};
},
}
);

const schema = BlockNoteSchema.create({
blockSpecs: {
...defaultBlockSpecs,
customBlock: CustomBlock
},
});

let editor: BlockNoteEditor;
let editor: BlockNoteEditor<typeof schema.blockSchema>;
const div = document.createElement("div");

function waitForEditor() {
Expand All @@ -34,16 +64,25 @@ let multipleBlocks: PartialBlock<
DefaultStyleSchema
>[];

let customBlock: PartialBlock<
typeof schema.blockSchema,
DefaultInlineContentSchema,
DefaultStyleSchema
>;

let insert: (
placement: "before" | "nested" | "after"
) => Block<
DefaultBlockSchema,
typeof schema.blockSchema,
DefaultInlineContentSchema,
DefaultStyleSchema
>[];

beforeEach(() => {
editor = BlockNoteEditor.create();
editor = BlockNoteEditor.create<typeof schema.blockSchema>({
schema: schema
});

editor.mount(div);

singleBlock = {
Expand Down Expand Up @@ -86,6 +125,11 @@ beforeEach(() => {
},
];

customBlock = {
type: 'customBlock',
content: 'Custom Block',
}

insert = (placement) => {
const existingBlock = editor.document[0];
editor.insertBlocks(multipleBlocks, existingBlock, placement);
Expand Down Expand Up @@ -236,3 +280,27 @@ describe("Insert, Update, & Delete Blocks", () => {
expect(editor.document).toMatchSnapshot();
});
});

describe("Update Custom Block content", () => {
it("update Custom Block with line break", async () => {
await waitForEditor();

const existingBlock = editor.document[0];
editor.insertBlocks([customBlock], existingBlock);

const newBlock = editor.document[0];
editor.updateBlock(newBlock, {
type: "customBlock",
content: "Updated Custom Block with \nline \nbreak",
children: [customBlock],
});

const serializer = createInternalHTMLSerializer(
editor._tiptapEditor.schema,
editor
);
const internalHTML = serializer.serializeBlocks(editor.document);

expect(internalHTML).toMatchSnapshot();
});
});
Loading