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

feat: change event allows getting a list of the changes made #1585

Open
wants to merge 3 commits into
base: feat/blocknote-transactions
Choose a base branch
from
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
361 changes: 361 additions & 0 deletions packages/core/src/api/nodeUtil.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,361 @@
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";

import { setupTestEnv } from "./blockManipulation/setupTestEnv.js";
import { getBlocksChangedByTransaction } from "./nodeUtil.js";
import { Transaction } from "prosemirror-state";
import { BlockNoteEditor } from "../editor/BlockNoteEditor.js";
import { Step } from "prosemirror-transform";

const getEditor = setupTestEnv();

describe("Test getBlocksChangedByTransaction", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add test-cases for child blocks (add / update / delete child blocks). We can debate whether this should trigger a change on the parent or not, but at least it should be tested / documented imo

let transaction: Transaction;
let editor: BlockNoteEditor;
let originalDispatch: typeof editor.dispatch;

beforeEach(() => {
transaction = undefined as unknown as Transaction;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not easier to test the onchange event directly instead of patching like this?

editor = getEditor();
originalDispatch = editor.dispatch;
const mockDispatch = vi.fn((tr) => {
editor._tiptapEditor.dispatch(tr);
if (transaction) {
tr.steps.forEach((step: Step) => {
transaction.step(step);
});
} else {
transaction = tr;
}
});
editor.dispatch = mockDispatch;
});

afterEach(() => {
editor.dispatch = originalDispatch;
});

it("should return the correct blocks changed by a transaction", () => {
const transaction = editor.transaction;
const blocksChanged = getBlocksChangedByTransaction(transaction, editor);
expect(blocksChanged).toEqual([]);
});

it("should return blocks inserted by a transaction", () => {
editor.insertBlocks([{ type: "paragraph" }], "paragraph-0", "after");

const blocksChanged = getBlocksChangedByTransaction(transaction!, editor);

expect(blocksChanged).toEqual([
{
block: {
children: [],
content: [],
id: "0",
props: {
backgroundColor: "default",
textAlignment: "left",
textColor: "default",
},
type: "paragraph",
},
prevBlock: undefined,
source: { type: "local" },
type: "insert",
},
]);
});

it("should return blocks deleted by a transaction", () => {
editor.removeBlocks(["paragraph-0"]);

const blocksChanged = getBlocksChangedByTransaction(transaction!, editor);

expect(blocksChanged).toEqual([
{
block: {
children: [],
id: "paragraph-0",
props: {
backgroundColor: "default",
textAlignment: "left",
textColor: "default",
},
type: "paragraph",
content: [
{
styles: {},
text: "Paragraph 0",
type: "text",
},
],
},
prevBlock: undefined,
source: { type: "local" },
type: "delete",
},
]);
});

it("should return blocks updated by a transaction", () => {
editor.updateBlock("paragraph-0", {
props: {
backgroundColor: "red",
},
});

const blocksChanged = getBlocksChangedByTransaction(transaction!, editor);

expect(blocksChanged).toEqual([
{
block: {
children: [],
id: "paragraph-0",
props: {
backgroundColor: "red",
textAlignment: "left",
textColor: "default",
},
type: "paragraph",
content: [
{
styles: {},
text: "Paragraph 0",
type: "text",
},
],
},
prevBlock: {
children: [],
id: "paragraph-0",
props: {
backgroundColor: "default",
textAlignment: "left",
textColor: "default",
},
type: "paragraph",
content: [
{
styles: {},
text: "Paragraph 0",
type: "text",
},
],
},
source: { type: "local" },
type: "update",
},
]);
});

it("should only return a single block, if multiple updates change a single block in a transaction", () => {
editor.updateBlock("paragraph-0", {
props: {
backgroundColor: "red",
},
});
editor.updateBlock("paragraph-0", {
props: {
backgroundColor: "blue",
},
});

const blocksChanged = getBlocksChangedByTransaction(transaction!, editor);

expect(blocksChanged).toEqual([
{
block: {
children: [],
id: "paragraph-0",
props: {
backgroundColor: "blue",
textAlignment: "left",
textColor: "default",
},
type: "paragraph",
content: [
{
styles: {},
text: "Paragraph 0",
type: "text",
},
],
},
prevBlock: {
children: [],
id: "paragraph-0",
props: {
backgroundColor: "default",
textAlignment: "left",
textColor: "default",
},
type: "paragraph",
content: [
{
styles: {},
text: "Paragraph 0",
type: "text",
},
],
},
source: { type: "local" },
type: "update",
},
]);
});

it("should return multiple blocks, if multiple updates change multiple blocks in a transaction", () => {
editor.updateBlock("paragraph-0", {
props: {
backgroundColor: "red",
},
});
editor.updateBlock("paragraph-1", {
props: {
backgroundColor: "blue",
},
});

const blocksChanged = getBlocksChangedByTransaction(transaction!, editor);

expect(blocksChanged).toEqual([
{
block: {
children: [],
id: "paragraph-0",
props: {
backgroundColor: "red",
textAlignment: "left",
textColor: "default",
},
type: "paragraph",
content: [
{
styles: {},
text: "Paragraph 0",
type: "text",
},
],
},
prevBlock: {
children: [],
id: "paragraph-0",
props: {
backgroundColor: "default",
textAlignment: "left",
textColor: "default",
},
type: "paragraph",
content: [
{
styles: {},
text: "Paragraph 0",
type: "text",
},
],
},
source: { type: "local" },
type: "update",
},
{
block: {
children: [],
id: "paragraph-1",
props: {
backgroundColor: "blue",
textAlignment: "left",
textColor: "default",
},
type: "paragraph",
content: [
{
styles: {},
text: "Paragraph 1",
type: "text",
},
],
},
prevBlock: {
children: [],
id: "paragraph-1",
props: {
backgroundColor: "default",
textAlignment: "left",
textColor: "default",
},
type: "paragraph",
content: [
{
styles: {},
text: "Paragraph 1",
type: "text",
},
],
},
source: { type: "local" },
type: "update",
},
]);
});

it("should return multiple blocks, if multiple inserts add new blocks in a transaction", () => {
editor.insertBlocks(
[{ type: "paragraph", content: "ABC" }],
"paragraph-0",
"after"
);
editor.insertBlocks(
[{ type: "paragraph", content: "DEF" }],
"paragraph-1",
"after"
);

const blocksChanged = getBlocksChangedByTransaction(transaction!, editor);

expect(blocksChanged).toEqual([
{
block: {
children: [],
content: [
{
styles: {},
text: "ABC",
type: "text",
},
],
id: "0",
props: {
backgroundColor: "default",
textAlignment: "left",
textColor: "default",
},
type: "paragraph",
},
prevBlock: undefined,
source: { type: "local" },
type: "insert",
},
{
block: {
children: [],
content: [
{
styles: {},
text: "DEF",
type: "text",
},
],
id: "1",
props: {
backgroundColor: "default",
textAlignment: "left",
textColor: "default",
},
type: "paragraph",
},
prevBlock: undefined,
source: { type: "local" },
type: "insert",
},
]);
});
});
Loading
Loading