Skip to content
Draft
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
4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@
"zod": "^4.3.5",
"@y/protocols": "^1.0.6-rc.1",
"@y/websocket": "^4.0.0-3",
"@y/y": "^14.0.0-rc.16",
"@y/y": "^14.0.0-rc.17",
"@y/prosemirror": "^2.0.0-2",
"@floating-ui/react": "^0.27.18",
"lib0": "1.0.0-rc.13",
"lib0": "1.0.0-rc.14",
"y-websocket": "^2.1.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions examples/07-collaboration/10-versioning/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
"react-dom": "^19.2.3",
"@y/protocols": "^1.0.6-rc.1",
"@y/websocket": "^4.0.0-3",
"@y/y": "^14.0.0-rc.16",
"@y/y": "^14.0.0-rc.17",
"react-icons": "5.6.0",
"@floating-ui/react": "^0.27.18",
"lib0": "1.0.0-rc.13"
"lib0": "1.0.0-rc.14"
},
"devDependencies": {
"@types/react": "^19.2.3",
Expand Down
2 changes: 1 addition & 1 deletion examples/07-collaboration/11-yhub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"react": "^19.2.3",
"react-dom": "^19.2.3",
"@y/protocols": "^1.0.6-rc.1",
"@y/y": "^14.0.0-rc.16",
"@y/y": "^14.0.0-rc.17",
"@y/prosemirror": "^2.0.0-2",
"@y/websocket": "^4.0.0-rc.2"
},
Expand Down
4 changes: 2 additions & 2 deletions examples/07-collaboration/13-versioning-yjs14/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"react-dom": "^19.2.3",
"@y/protocols": "^1.0.6-rc.1",
"@y/websocket": "^4.0.0-3",
"@y/y": "^14.0.0-rc.16",
"lib0": "1.0.0-rc.13"
"@y/y": "^14.0.0-rc.17",
"lib0": "1.0.0-rc.14"
},
"devDependencies": {
"@types/react": "^19.2.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
"@tiptap/pm": "^3.13.0",
"emoji-mart": "^5.6.0",
"fast-deep-equal": "^3.1.3",
"lib0": "1.0.0-rc.13",
"lib0": "1.0.0-rc.14",
"prosemirror-highlight": "^0.15.1",
"prosemirror-model": "^1.25.4",
"prosemirror-state": "^1.4.4",
Expand All @@ -137,7 +137,7 @@
"y-prosemirror": "^1.3.7",
"y-protocols": "^1.0.6",
"yjs": "^13.6.27",
"@y/y": "^14.0.0-rc.16",
"@y/y": "^14.0.0-rc.17",
"@y/prosemirror": "^2.0.0-2",
"@y/protocols": "^1.0.6-rc.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function insertBlocks<
// Now that the `PartialBlock`s have been converted to nodes, we can
// re-convert them into full `Block`s.
const insertedBlocks = nodesToInsert.map((node) =>
nodeToBlock(node, pmSchema),
nodeToBlock(node, tr.doc),
) as Block<BSchema, I, S>[];

return insertedBlocks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";

import { getBlockInfoFromTransaction } from "../../../getBlockInfoFromPos.js";
import { getBlockInfoFromSelection } from "../../../getBlockInfoFromPos.js";
import { setupTestEnv } from "../../setupTestEnv.js";
import { getParentBlockInfo, mergeBlocksCommand } from "./mergeBlocks.js";

Expand All @@ -14,7 +14,7 @@ function mergeBlocks(posBetweenBlocks: number) {

function getPosBeforeSelectedBlock() {
return getEditor().transact(
(tr) => getBlockInfoFromTransaction(tr).bnBlock.beforePos,
(tr) => getBlockInfoFromSelection(tr).bnBlock.beforePos,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { CellSelection } from "prosemirror-tables";
import { describe, expect, it } from "vitest";

import {
getBlockInfoFromTransaction,
getNearestBlockPos,
getBlockInfoAt,
getBlockInfoFromSelection,
getNodeId,
} from "../../../getBlockInfoFromPos.js";
import { setupTestEnv } from "../../setupTestEnv.js";
import {
Expand All @@ -16,9 +17,7 @@ import {
const getEditor = setupTestEnv();

function makeSelectionSpanContent(selectionType: "text" | "node" | "cell") {
const blockInfo = getEditor().transact((tr) =>
getBlockInfoFromTransaction(tr),
);
const blockInfo = getEditor().transact((tr) => getBlockInfoFromSelection(tr));
if (!blockInfo.isBlockContainer) {
throw new Error(
`Selection points to a ${blockInfo.blockNoteType} node, not a blockContainer node`,
Expand Down Expand Up @@ -222,13 +221,10 @@ describe("Test moveBlocksUp", () => {

moveBlocksUp(getEditor(), "paragraph-2");

const { anchor, head } = getEditor().transact((tr) => tr.selection);
const anchorBlockId = getEditor().transact(
(tr) => getNearestBlockPos(tr.doc, anchor).node.attrs.id,
);
const headBlockId = getEditor().transact(
(tr) => getNearestBlockPos(tr.doc, head).node.attrs.id,
);
const { anchorBlockId, headBlockId } = getEditor().transact((tr) => ({
anchorBlockId: getNodeId(getBlockInfoAt(tr, tr.selection.anchor).bnBlock.node, tr.doc),
headBlockId: getNodeId(getBlockInfoAt(tr, tr.selection.head).bnBlock.node, tr.doc),
}));
expect(anchorBlockId).toBe("paragraph-1");
expect(headBlockId).toBe("paragraph-1");
});
Expand Down Expand Up @@ -343,13 +339,10 @@ describe("Test moveBlocksDown", () => {

moveBlocksDown(getEditor(), "paragraph-0");

const { anchor, head } = getEditor().transact((tr) => tr.selection);
const anchorBlockId = getEditor().transact(
(tr) => getNearestBlockPos(tr.doc, anchor).node.attrs.id,
);
const headBlockId = getEditor().transact(
(tr) => getNearestBlockPos(tr.doc, head).node.attrs.id,
);
const { anchorBlockId, headBlockId } = getEditor().transact((tr) => ({
anchorBlockId: getNodeId(getBlockInfoAt(tr, tr.selection.anchor).bnBlock.node, tr.doc),
headBlockId: getNodeId(getBlockInfoAt(tr, tr.selection.head).bnBlock.node, tr.doc),
}));
expect(anchorBlockId).toBe("paragraph-1");
expect(headBlockId).toBe("paragraph-1");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CellSelection } from "prosemirror-tables";
import { Block } from "../../../../blocks/defaultBlocks.js";
import type { BlockNoteEditor } from "../../../../editor/BlockNoteEditor";
import { BlockIdentifier } from "../../../../schema/index.js";
import { getNearestBlockPos } from "../../../getBlockInfoFromPos.js";
import { getBlockInfoAt, getNodeId } from "../../../getBlockInfoFromPos.js";
import { getNodeById } from "../../../nodeUtil.js";

type BlockSelectionData = (
Expand Down Expand Up @@ -44,31 +44,34 @@ function getBlockSelectionData(
editor: BlockNoteEditor<any, any, any>,
): BlockSelectionData {
return editor.transact((tr) => {
const anchorBlockPosInfo = getNearestBlockPos(tr.doc, tr.selection.anchor);
const anchorBlockPosInfo = getBlockInfoAt(tr, tr.selection.anchor);

const anchorBlockId = getNodeId(anchorBlockPosInfo.bnBlock.node, tr.doc);

if (tr.selection instanceof CellSelection) {
return {
type: "cell" as const,
anchorBlockId: anchorBlockPosInfo.node.attrs.id,
anchorBlockId,
anchorCellOffset:
tr.selection.$anchorCell.pos - anchorBlockPosInfo.posBeforeNode,
tr.selection.$anchorCell.pos - anchorBlockPosInfo.bnBlock.beforePos,
headCellOffset:
tr.selection.$headCell.pos - anchorBlockPosInfo.posBeforeNode,
tr.selection.$headCell.pos - anchorBlockPosInfo.bnBlock.beforePos,
};
} else if (tr.selection instanceof NodeSelection) {
return {
type: "node" as const,
anchorBlockId: anchorBlockPosInfo.node.attrs.id,
anchorBlockId,
};
} else {
const headBlockPosInfo = getNearestBlockPos(tr.doc, tr.selection.head);
const headBlockPosInfo = getBlockInfoAt(tr, tr.selection.head);

return {
type: "text" as const,
anchorBlockId: anchorBlockPosInfo.node.attrs.id,
headBlockId: headBlockPosInfo.node.attrs.id,
anchorOffset: tr.selection.anchor - anchorBlockPosInfo.posBeforeNode,
headOffset: tr.selection.head - headBlockPosInfo.posBeforeNode,
anchorBlockId,
headBlockId: getNodeId(headBlockPosInfo.bnBlock.node, tr.doc),
anchorOffset:
tr.selection.anchor - anchorBlockPosInfo.bnBlock.beforePos,
headOffset: tr.selection.head - headBlockPosInfo.bnBlock.beforePos,
};
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Transaction } from "prosemirror-state";
import { canJoin, liftTarget, ReplaceAroundStep } from "prosemirror-transform";

import { BlockNoteEditor } from "../../../../editor/BlockNoteEditor.js";
import { getBlockInfoFromTransaction } from "../../../getBlockInfoFromPos.js";
import { getBlockInfoFromSelection } from "../../../getBlockInfoFromPos.js";

/**
* Modified version of prosemirror-schema-list's sinkItem.
Expand All @@ -15,11 +15,7 @@ import { getBlockInfoFromTransaction } from "../../../getBlockInfoFromPos.js";
* 3. Slice creates groupType instead of parent.type
* 4. Operates on Transaction directly instead of state+dispatch
*/
function sinkItem(
tr: Transaction,
itemType: NodeType,
groupType: NodeType,
) {
function sinkItem(tr: Transaction, itemType: NodeType, groupType: NodeType) {
const { $from, $to } = tr.selection;
const range = $from.blockRange(
$to,
Expand Down Expand Up @@ -197,15 +193,15 @@ export function unnestBlock(editor: BlockNoteEditor<any, any, any>) {

export function canNestBlock(editor: BlockNoteEditor<any, any, any>) {
return editor.transact((tr) => {
const { bnBlock: blockContainer } = getBlockInfoFromTransaction(tr);
const { bnBlock: blockContainer } = getBlockInfoFromSelection(tr);

return tr.doc.resolve(blockContainer.beforePos).nodeBefore !== null;
});
}

export function canUnnestBlock(editor: BlockNoteEditor<any, any, any>) {
return editor.transact((tr) => {
const { bnBlock: blockContainer } = getBlockInfoFromTransaction(tr);
const { bnBlock: blockContainer } = getBlockInfoFromSelection(tr);

return tr.doc.resolve(blockContainer.beforePos).depth > 1;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type Node } from "prosemirror-model";
import { type Transaction } from "prosemirror-state";
import type { Block, PartialBlock } from "../../../../blocks/defaultBlocks.js";
import { getNodeId } from "../../../getBlockInfoFromPos.js";
import type {
BlockIdentifier,
BlockSchema,
Expand Down Expand Up @@ -54,18 +55,21 @@ export function removeAndInsertBlocks<
}

// Keeps traversing nodes if block with target ID has not been found.
if (
!node.type.isInGroup("bnBlock") ||
!idsOfBlocksToRemove.has(node.attrs.id)
) {
if (!node.type.isInGroup("bnBlock")) {
return true;
}

const nodeId = getNodeId(node, tr.doc);

if (!idsOfBlocksToRemove.has(nodeId)) {
return true;
}

// Saves the block that is being deleted.
removedBlocks.push(nodeToBlock(node, pmSchema));
idsOfBlocksToRemove.delete(node.attrs.id);
removedBlocks.push(nodeToBlock(node, tr.doc));
idsOfBlocksToRemove.delete(nodeId);

if (blocksToInsert.length > 0 && node.attrs.id === idOfFirstBlock) {
if (blocksToInsert.length > 0 && nodeId === idOfFirstBlock) {
const oldDocSize = tr.doc.nodeSize;
tr.insert(pos, nodesToInsert);
const newDocSize = tr.doc.nodeSize;
Expand Down Expand Up @@ -116,7 +120,7 @@ export function removeAndInsertBlocks<

// Converts the nodes created from `blocksToInsert` into full `Block`s.
const insertedBlocks = nodesToInsert.map((node) =>
nodeToBlock(node, pmSchema),
nodeToBlock(node, tr.doc),
) as Block<BSchema, I, S>[];

return { insertedBlocks, removedBlocks };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { describe, expect, it } from "vitest";

import {
getBlockInfo,
getBlockInfoFromTransaction,
getBlockInfoFromSelection,
getNodeId,
} from "../../../getBlockInfoFromPos.js";
import { getNodeById } from "../../../nodeUtil.js";
import { setupTestEnv } from "../../setupTestEnv.js";
Expand Down Expand Up @@ -137,12 +138,12 @@ describe("Test splitBlocks", () => {

splitBlock(getEditor().transact((tr) => tr.selection.anchor));

const bnBlock = getEditor().transact(
(tr) => getBlockInfoFromTransaction(tr).bnBlock,
const blockId = getEditor().transact(
(tr) => getNodeId(getBlockInfoFromSelection(tr).bnBlock.node, tr.doc),
);

const anchorIsAtStartOfNewBlock =
bnBlock.node.attrs.id === "0" &&
blockId === "0" &&
getEditor().transact((tr) => tr.selection.$anchor.parentOffset) === 0;

expect(anchorIsAtStartOfNewBlock).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export function updateBlockTr<
// currently, we calculate the new node and replace the entire node with the desired new node.
// for this, we do a nodeToBlock on the existing block to get the children.
// it would be cleaner to use a ReplaceAroundStep, but this is a bit simpler and it's quite an edge case
const existingBlock = nodeToBlock(blockInfo.bnBlock.node, pmSchema);
const existingBlock = nodeToBlock(blockInfo.bnBlock.node, tr.doc);
const replacementNode = blockToNode(
{
children: existingBlock.children, // if no children are passed in, use existing children
Expand Down Expand Up @@ -340,8 +340,7 @@ export function updateBlock<
.resolve(posInfo.posBeforeNode + 1) // TODO: clean?
.node();

const pmSchema = getPmSchema(tr);
return nodeToBlock(blockContainerNode, pmSchema);
return nodeToBlock(blockContainerNode, tr.doc);
}

type CellAnchor = { row: number; col: number; offset: number };
Expand Down
Loading
Loading