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

fix: RangeError when un-nesting certain blocks #797

Open
wants to merge 1 commit 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
7 changes: 6 additions & 1 deletion packages/core/src/editor/BlockNoteEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,12 @@ export class BlockNoteEditor<
* Lifts the block containing the text cursor out of its parent.
*/
public unnestBlock() {
this._tiptapEditor.commands.liftListItem("blockContainer");
const { startPos } = getBlockInfoFromPos(
this._tiptapEditor.state.doc,
this._tiptapEditor.state.selection.from
)!;

this._tiptapEditor.commands.BNUnnestBlock(startPos);
}

// TODO: Fix when implementing HTML/Markdown import & export
Expand Down
87 changes: 80 additions & 7 deletions packages/core/src/pm-nodes/BlockContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ declare module "@tiptap/core" {
posInBlock: number,
block: PartialBlock<BSchema, I, S>
) => ReturnType;
BNUnnestBlock: (posInBlock: number) => ReturnType;
};
}
}
Expand Down Expand Up @@ -384,7 +385,6 @@ export const BlockContainer = Node.create<{
}

// Deletes next block and adds its text content to the nearest previous block.

if (dispatch) {
dispatch(
state.tr
Expand Down Expand Up @@ -484,6 +484,79 @@ export const BlockContainer = Node.create<{

return true;
},
BNUnnestBlock:
(posInBlock) =>
({ state, dispatch }) => {
const blockInfo = getBlockInfoFromPos(state.tr.doc, posInBlock)!;

if (blockInfo.depth > 2) {
if (dispatch) {
const parentBlockInfo = getBlockInfoFromPos(
state.tr.doc,
state.tr.doc.resolve(blockInfo.startPos).start(-2)
)!;

const blockChildren =
blockInfo.numChildBlocks > 0
? state.tr.doc
.resolve(
blockInfo.startPos + blockInfo.contentNode.nodeSize + 1
)
.node().content
: Fragment.empty;
const siblingBlocksAfter = state.tr.doc.slice(
blockInfo.endPos + 1,
parentBlockInfo.endPos - 1
).content;
// We need to move all siblings after the block into its children.
const children = blockChildren.append(siblingBlocksAfter);

// Checks if the block is the first child of its parent, as
// this means that we need to delete the entire `blockGroup`
// of the parent. Otherwise, we only need to delete the
// children after the block.
if (
parentBlockInfo.startPos +
parentBlockInfo.contentNode.nodeSize +
1 ===
blockInfo.startPos - 1
) {
state.tr.delete(
parentBlockInfo.startPos +
parentBlockInfo.contentNode.nodeSize,
parentBlockInfo.endPos
);
} else {
state.tr.delete(
blockInfo.startPos - 1,
parentBlockInfo.endPos - 1
);
}

const blockGroup = state.schema.nodes["blockGroup"].create(
null,
children
);
const block = state.schema.nodes["blockContainer"].create(
blockInfo.node.attrs,
[blockInfo.contentNode, blockGroup]
);

state.tr.insert(state.tr.selection.from - 2, block);
state.tr.setSelection(
new TextSelection(
state.tr.doc.resolve(state.tr.selection.from - block.nodeSize)
)
);

dispatch(state.tr);
}

return true;
}

return false;
},
};
},

Expand Down Expand Up @@ -522,15 +595,15 @@ export const BlockContainer = Node.create<{
// Removes a level of nesting if the block is indented if the selection is at the start of the block.
() =>
commands.command(({ state }) => {
const { startPos } = getBlockInfoFromPos(
state.doc,
const { startPos, depth } = getBlockInfoFromPos(
state.tr.doc,
state.selection.from
)!;

const selectionAtBlockStart = state.selection.from === startPos + 1;

if (selectionAtBlockStart) {
return commands.liftListItem("blockContainer");
if (selectionAtBlockStart && depth > 2) {
return commands.BNUnnestBlock(startPos);
}

return false;
Expand Down Expand Up @@ -711,7 +784,7 @@ export const BlockContainer = Node.create<{
this.editor.commands.sinkListItem("blockContainer");
return true;
},
"Shift-Tab": () => {
"Shift-Tab": ({ editor }) => {
if (
this.options.editor.formattingToolbar?.shown ||
this.options.editor.linkToolbar?.shown ||
Expand All @@ -720,7 +793,7 @@ export const BlockContainer = Node.create<{
// don't handle tabs if a toolbar is shown, so we can tab into / out of it
return false;
}
this.editor.commands.liftListItem("blockContainer");
this.editor.commands.BNUnnestBlock(editor.state.selection.from);
return true;
},
};
Expand Down
Loading