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(core): add blocknote transactions #1584

Open
wants to merge 6 commits into
base: main
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Node } from "prosemirror-model";
import { Fragment, Node, Slice } from "prosemirror-model";

import { Block, PartialBlock } from "../../../../blocks/defaultBlocks.js";
import type { BlockNoteEditor } from "../../../../editor/BlockNoteEditor";
Expand All @@ -11,6 +11,7 @@ import {
import { blockToNode } from "../../../nodeConversions/blockToNode.js";
import { nodeToBlock } from "../../../nodeConversions/nodeToBlock.js";
import { getNodeById } from "../../../nodeUtil.js";
import { ReplaceStep } from "prosemirror-transform";

export function insertBlocks<
BSchema extends BlockSchema,
Expand All @@ -32,28 +33,23 @@ export function insertBlocks<
);
}

const posInfo = getNodeById(id, editor._tiptapEditor.state.doc);
const tr = editor.transaction;
const posInfo = getNodeById(id, tr.doc);
if (!posInfo) {
throw new Error(`Block with ID ${id} not found`);
}

// TODO: we might want to use the ReplaceStep directly here instead of insert,
// because the fitting algorithm should not be necessary and might even cause unexpected behavior
if (placement === "before") {
editor.dispatch(
editor._tiptapEditor.state.tr.insert(posInfo.posBeforeNode, nodesToInsert)
);
}

let pos = posInfo.posBeforeNode;
if (placement === "after") {
editor.dispatch(
editor._tiptapEditor.state.tr.insert(
posInfo.posBeforeNode + posInfo.node.nodeSize,
nodesToInsert
)
);
pos += posInfo.node.nodeSize;
}

tr.step(
new ReplaceStep(pos, pos, new Slice(Fragment.from(nodesToInsert), 0, 0))
);

editor.dispatch(tr);

// Now that the `PartialBlock`s have been converted to nodes, we can
// re-convert them into full `Block`s.
const insertedBlocks: Block<BSchema, I, S>[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function mergeBlocks(posBetweenBlocks: number) {
}

function getPosBeforeSelectedBlock() {
return getBlockInfoFromSelection(getEditor()._tiptapEditor.state).bnBlock
return getBlockInfoFromSelection(getEditor().prosemirrorState).bnBlock
.beforePos;
}

Expand Down Expand Up @@ -62,14 +62,14 @@ describe("Test mergeBlocks", () => {
getEditor().setTextCursorPosition("paragraph-0", "end");

const firstBlockEndOffset =
getEditor()._tiptapEditor.state.selection.$anchor.parentOffset;
getEditor().prosemirrorState.selection.$anchor.parentOffset;

getEditor().setTextCursorPosition("paragraph-1");

mergeBlocks(getPosBeforeSelectedBlock());

const anchorIsAtOldFirstBlockEndPos =
getEditor()._tiptapEditor.state.selection.$anchor.parentOffset ===
getEditor().prosemirrorState.selection.$anchor.parentOffset ===
firstBlockEndOffset;

expect(anchorIsAtOldFirstBlockEndPos).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,35 @@ import {
const getEditor = setupTestEnv();

function makeSelectionSpanContent(selectionType: "text" | "node" | "cell") {
const blockInfo = getBlockInfoFromSelection(getEditor()._tiptapEditor.state);
const blockInfo = getBlockInfoFromSelection(getEditor().prosemirrorState);
if (!blockInfo.isBlockContainer) {
throw new Error(
`Selection points to a ${blockInfo.blockNoteType} node, not a blockContainer node`
);
}
const { blockContent } = blockInfo;

const editor = getEditor();
const tr = editor.transaction;
if (selectionType === "cell") {
getEditor().dispatch(
getEditor()._tiptapEditor.state.tr.setSelection(
editor.dispatch(
tr.setSelection(
CellSelection.create(
getEditor()._tiptapEditor.state.doc,
getEditor()
._tiptapEditor.state.doc.resolve(blockContent.beforePos + 3)
.before(),
getEditor()
._tiptapEditor.state.doc.resolve(blockContent.afterPos - 3)
.before()
tr.doc,
tr.doc.resolve(blockContent.beforePos + 3).before(),
tr.doc.resolve(blockContent.afterPos - 3).before()
)
)
);
} else if (selectionType === "node") {
getEditor().dispatch(
getEditor()._tiptapEditor.state.tr.setSelection(
NodeSelection.create(
getEditor()._tiptapEditor.state.doc,
blockContent.beforePos
)
)
editor.dispatch(
tr.setSelection(NodeSelection.create(tr.doc, blockContent.beforePos))
);
} else {
getEditor().dispatch(
getEditor()._tiptapEditor.state.tr.setSelection(
editor.dispatch(
tr.setSelection(
TextSelection.create(
getEditor()._tiptapEditor.state.doc,
tr.doc,
blockContent.beforePos + 1,
blockContent.afterPos - 1
)
Expand All @@ -64,13 +57,11 @@ describe("Test moveSelectedBlockAndSelection", () => {

moveSelectedBlocksAndSelection(getEditor(), "paragraph-0", "before");

const selection = getEditor()._tiptapEditor.state.selection;
const selection = getEditor().transaction.selection;
getEditor().setTextCursorPosition("paragraph-1");
makeSelectionSpanContent("text");

expect(
selection.eq(getEditor()._tiptapEditor.state.selection)
).toBeTruthy();
expect(selection.eq(getEditor().transaction.selection)).toBeTruthy();
});

it("Node selection", () => {
Expand All @@ -79,13 +70,11 @@ describe("Test moveSelectedBlockAndSelection", () => {

moveSelectedBlocksAndSelection(getEditor(), "paragraph-0", "before");

const selection = getEditor()._tiptapEditor.state.selection;
const selection = getEditor().transaction.selection;
getEditor().setTextCursorPosition("image-0");
makeSelectionSpanContent("node");

expect(
selection.eq(getEditor()._tiptapEditor.state.selection)
).toBeTruthy();
expect(selection.eq(getEditor().transaction.selection)).toBeTruthy();
});

it("Cell selection", () => {
Expand All @@ -94,39 +83,33 @@ describe("Test moveSelectedBlockAndSelection", () => {

moveSelectedBlocksAndSelection(getEditor(), "paragraph-0", "before");

const selection = getEditor()._tiptapEditor.state.selection;
const selection = getEditor().transaction.selection;
getEditor().setTextCursorPosition("table-0");
makeSelectionSpanContent("cell");

expect(
selection.eq(getEditor()._tiptapEditor.state.selection)
).toBeTruthy();
expect(selection.eq(getEditor().transaction.selection)).toBeTruthy();
});

it("Multiple block selection", () => {
getEditor().setSelection("paragraph-1", "paragraph-2");

moveSelectedBlocksAndSelection(getEditor(), "paragraph-0", "before");

const selection = getEditor()._tiptapEditor.state.selection;
const selection = getEditor().transaction.selection;
getEditor().setSelection("paragraph-1", "paragraph-2");

expect(
selection.eq(getEditor()._tiptapEditor.state.selection)
).toBeTruthy();
expect(selection.eq(getEditor().transaction.selection)).toBeTruthy();
});

it("Multiple block selection with table", () => {
getEditor().setSelection("paragraph-6", "table-0");

moveSelectedBlocksAndSelection(getEditor(), "paragraph-0", "before");

const selection = getEditor()._tiptapEditor.state.selection;
const selection = getEditor().transaction.selection;
getEditor().setSelection("paragraph-6", "table-0");

expect(
selection.eq(getEditor()._tiptapEditor.state.selection)
).toBeTruthy();
expect(selection.eq(getEditor().transaction.selection)).toBeTruthy();
});
});

Expand Down
Loading
Loading