Skip to content
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
Expand Up @@ -10,22 +10,24 @@ import {
* Returns the block info from the parent block
* or undefined if we're at the root
*/
export const getParentBlockInfo = (doc: Node, beforePos: number) => {
export const getParentBlockInfo = (doc: Node, beforePos: number): BlockInfo | undefined => {
const $pos = doc.resolve(beforePos);
const depth = $pos.depth - 1;
const parentBeforePos = $pos.before(depth);
const parentNode = doc.resolve(parentBeforePos).nodeAfter;

if ($pos.depth <= 1) {
if (!parentNode) {
return undefined;
}

// get start pos of parent
const parentBeforePos = $pos.posAtIndex(
$pos.index($pos.depth - 1),
$pos.depth - 1,
);
if (!parentNode.type.spec.group?.includes("bnBlock")) {
return getParentBlockInfo(doc, parentBeforePos);
}

const parentBlockInfo = getBlockInfoFromResolvedPos(
doc.resolve(parentBeforePos),
);

return parentBlockInfo;
};

Expand All @@ -50,6 +52,27 @@ export const getPrevBlockInfo = (doc: Node, beforePos: number) => {
return prevBlockInfo;
};

/**
* Returns the block info from the sibling block after (below) the given block,
* or undefined if the given block is the last sibling.
*/
export const getNextBlockInfo = (doc: Node, beforePos: number) => {
const $pos = doc.resolve(beforePos);

const indexInParent = $pos.index();

if (indexInParent === $pos.node().childCount - 1) {
return undefined;
}

const nextBlockBeforePos = $pos.posAtIndex(indexInParent + 1);

const nextBlockInfo = getBlockInfoFromResolvedPos(
doc.resolve(nextBlockBeforePos),
);
return nextBlockInfo;
};

/**
* If a block has children like this:
* A
Expand Down
Loading
Loading