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

Refactor: clean blockcontainer #1137

Merged
merged 38 commits into from
Oct 17, 2024
Merged
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
51f4216
extract updateBlockCommand
YousefED Oct 8, 2024
18ff213
Extracted remaining commands
matthewlipski Oct 8, 2024
de6f1ec
extract keyboard shortcuts
YousefED Oct 8, 2024
c3cea79
move directory
YousefED Oct 8, 2024
717301e
remove createblockcommand
YousefED Oct 8, 2024
9c32c92
Added merge/split tests
matthewlipski Oct 8, 2024
3fe50d5
Updated snapshots
matthewlipski Oct 9, 2024
6a0fda3
Added update block tests and unified test setup
matthewlipski Oct 9, 2024
df6dccc
Added test cases for reverting props
matthewlipski Oct 9, 2024
d4f206d
Added additional test cases for changing content type
matthewlipski Oct 9, 2024
5ac23d9
remove "nested" insert option
YousefED Oct 9, 2024
fc0010b
Split remaining commands & cleaned up
matthewlipski Oct 9, 2024
e392637
Added `getNearestBlockContainerPos`
matthewlipski Oct 10, 2024
5dd4afd
Refactored `getBlockInfoFromPos`
matthewlipski Oct 10, 2024
58016a9
Rewrote `splitBlockCommand`
matthewlipski Oct 12, 2024
6bcd81d
Added text cursor position tests
matthewlipski Oct 12, 2024
ab18dd4
Fixed lint issue
matthewlipski Oct 12, 2024
968b6fc
fix lint
YousefED Oct 13, 2024
89a86fb
Fixed `splitBlock` selection
matthewlipski Oct 14, 2024
adb0907
Merge remote-tracking branch 'origin/refactor/clean-blockcontainer' i…
matthewlipski Oct 14, 2024
ba11dd2
Small fix
matthewlipski Oct 14, 2024
dd572c3
Added unit tests to check selection setting
matthewlipski Oct 14, 2024
74a0cda
simplify splitblocks
YousefED Oct 14, 2024
690ec6a
Merge branch 'refactor/clean-blockcontainer' of github.com:TypeCellOS…
YousefED Oct 14, 2024
f57b0eb
Fixed selection in `splitBlock` tests
matthewlipski Oct 14, 2024
1236476
wip: deprecate getBlockInfoFromPos
YousefED Oct 14, 2024
7b17ded
finish cleanup
YousefED Oct 14, 2024
bf4635e
Fixed `mergeBlocks` edge cases
matthewlipski Oct 14, 2024
3f73033
fix build
YousefED Oct 15, 2024
0a50308
Merge branch 'refactor/clean-blockcontainer' of github.com:TypeCellOS…
YousefED Oct 15, 2024
9ae58f6
clean nodeconversions
YousefED Oct 15, 2024
80c8b46
Implemented PR feedback
matthewlipski Oct 15, 2024
1414e14
Finished review and remaining changes
matthewlipski Oct 15, 2024
f627732
Fixed bug in `insertOrUpdateBlock`
matthewlipski Oct 15, 2024
3d09350
Removed log
matthewlipski Oct 15, 2024
3d0e80e
Tiny changes
matthewlipski Oct 16, 2024
cb1e705
Merge pull request #1151 from TypeCellOS/refactor/clean-blockcontaine…
matthewlipski Oct 16, 2024
ab902c9
Fixed merge/delete behaviour on Backspace
matthewlipski Oct 17, 2024
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
101 changes: 51 additions & 50 deletions packages/core/src/api/getBlockInfoFromPos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,70 +36,71 @@ export function getBlockInfo(blockContainer: Node): BlockInfoWithoutPositions {
}

/**
* Retrieves information regarding the nearest blockContainer node in a
* ProseMirror doc, relative to a position.
* Retrieves the position just before the nearest blockContainer node in a
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TODO Y: before merging review all function docs

* ProseMirror doc, relative to a position. If the position is within a
* blockContainer node or its descendants, the position just before it is
* returned. If the position is not within a blockContainer node or its
* descendants, the position just before the next closest blockContainer node
* is returned. If the position is beyond the last blockContainer, the position
* just before the last blockContainer is returned.
* @param doc The ProseMirror doc.
* @param pos An integer position.
* @returns A BlockInfo object for the nearest blockContainer node.
* @returns The position just before the nearest blockContainer node.
*/
export function getBlockInfoFromPos(doc: Node, pos: number): BlockInfo {
// If the position is outside the outer block group, we need to move it to the
// nearest block. This happens when the collaboration plugin is active, where
// the selection is placed at the very end of the doc.
const outerBlockGroupStartPos = 1;
const outerBlockGroupEndPos = doc.nodeSize - 2;
if (pos <= outerBlockGroupStartPos) {
pos = outerBlockGroupStartPos + 1;

while (
doc.resolve(pos).parent.type.name !== "blockContainer" &&
pos < outerBlockGroupEndPos
) {
pos++;
}
} else if (pos >= outerBlockGroupEndPos) {
pos = outerBlockGroupEndPos - 1;
export function getNearestBlockContainerPos(doc: Node, pos: number) {
const $pos = doc.resolve(pos);

while (
doc.resolve(pos).parent.type.name !== "blockContainer" &&
pos > outerBlockGroupStartPos
) {
pos--;
// Checks the node containing the position and its ancestors until a
// blockContainer node is found and returned.
let depth = $pos.depth;
let node = $pos.node(depth);
while (depth > 0) {
if (node.type.name === "blockContainer") {
return $pos.before(depth);
}
}

// This gets triggered when a node selection on a block is active, i.e. when
// you drag and drop a block.
if (doc.resolve(pos).parent.type.name === "blockGroup") {
pos++;
depth--;
node = $pos.node(depth);
}

const $pos = doc.resolve(pos);

const maxDepth = $pos.depth;
let node = $pos.node(maxDepth);
let depth = maxDepth;

// eslint-disable-next-line no-constant-condition
while (true) {
if (depth < 0) {
throw new Error(
"Could not find blockContainer node. This can only happen if the underlying BlockNote schema has been edited."
);
}

// If the position doesn't lie within a blockContainer node, we instead find
// the position of the next closest one. If the position is beyond the last
// blockContainer, we return the position of the last blockContainer. While
// running `doc.descendants` is expensive, this case should be very rarely
// triggered as almost every position will be within a blockContainer,
// according to the schema.
const allBlockContainerPositions: number[] = [];
doc.descendants((node, pos) => {
if (node.type.name === "blockContainer") {
break;
allBlockContainerPositions.push(pos);
}
});

depth -= 1;
node = $pos.node(depth);
}
return (
matthewlipski marked this conversation as resolved.
Show resolved Hide resolved
allBlockContainerPositions.find((position) => position >= pos) ||
allBlockContainerPositions[allBlockContainerPositions.length - 1]
);
}

/**
* Retrieves information regarding the nearest blockContainer node in a
* ProseMirror doc, relative to a position.
* @param doc The ProseMirror doc.
* @param pos An integer position.
* @returns A BlockInfo object for the nearest blockContainer node.
*/
export function getBlockInfoFromPos(doc: Node, pos: number): BlockInfo {
const $pos = doc.resolve(getNearestBlockContainerPos(doc, pos));
const node = $pos.nodeAfter!;
matthewlipski marked this conversation as resolved.
Show resolved Hide resolved

const { id, contentNode, contentType, numChildBlocks } = getBlockInfo(node);

const startPos = $pos.start(depth);
const endPos = $pos.end(depth);
const posInsideBlockContainer = $pos.pos + 1;
const $posInsideBlockContainer = doc.resolve(posInsideBlockContainer);
const depth = $posInsideBlockContainer.depth;

const startPos = $posInsideBlockContainer.start(depth);
const endPos = $posInsideBlockContainer.end(depth);

return {
id,
Expand Down
Loading