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
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ function mergeBlocks(posBetweenBlocks: number) {
}

function getPosAfterSelectedBlock() {
const { startPos } = getBlockInfoFromPos(
return getBlockInfoFromPos(
getEditor()._tiptapEditor.state.doc,
getEditor()._tiptapEditor.state.selection.from
);
return getEditor()._tiptapEditor.state.doc.resolve(startPos).after();
).blockContainer.afterPos;
}

describe("Test mergeBlocks", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ export const mergeBlocksCommand =
return false;
}

const nextBlockInfo = getBlockInfoFromPos(state.doc, posBetweenBlocks + 1);

const { node, contentNode, startPos, endPos, depth } = nextBlockInfo!;
const nextBlockInfo = getBlockInfoFromPos(state.doc, posBetweenBlocks);

// Removes a level of nesting all children of the next block by 1 level, if it contains both content and block
// group nodes.
if (node.childCount === 2) {
if (nextBlockInfo.blockGroup) {
const childBlocksStart = state.doc.resolve(
startPos + contentNode.nodeSize + 1
nextBlockInfo.blockGroup.beforePos + 1
);
const childBlocksEnd = state.doc.resolve(
nextBlockInfo.blockGroup.afterPos - 1
);
const childBlocksEnd = state.doc.resolve(endPos - 1);
const childBlocksRange = childBlocksStart.blockRange(childBlocksEnd);

// Moves the block group node inside the block into the block group node that the current block is in.
if (dispatch) {
state.tr.lift(childBlocksRange!, depth - 1);
state.tr.lift(childBlocksRange!, nextBlockInfo.depth);
}
}

let prevBlockEndPos = posBetweenBlocks - 1;
let prevBlockInfo = getBlockInfoFromPos(state.doc, prevBlockEndPos);

// Finds the nearest previous block, regardless of nesting level.
while (prevBlockInfo!.numChildBlocks > 0) {
while (prevBlockInfo.blockGroup) {
prevBlockEndPos--;
prevBlockInfo = getBlockInfoFromPos(state.doc, prevBlockEndPos);
if (prevBlockInfo === undefined) {
Expand All @@ -59,11 +59,14 @@ export const mergeBlocksCommand =
if (dispatch) {
dispatch(
state.tr
.deleteRange(startPos, startPos + contentNode.nodeSize)
.deleteRange(
nextBlockInfo.blockContent.beforePos,
nextBlockInfo.blockContent.afterPos
)
.replace(
prevBlockEndPos - 1,
startPos,
new Slice(contentNode.content, 0, 0)
nextBlockInfo.blockContent.beforePos,
new Slice(nextBlockInfo.blockContent.node.content, 0, 0)
)
// .scrollIntoView()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { setupTestEnv } from "../../setupTestEnv.js";
const getEditor = setupTestEnv();

function makeSelectionSpanContent(selectionType: "text" | "node" | "cell") {
const { startPos, contentNode } = getBlockInfoFromPos(
const { blockContent } = getBlockInfoFromPos(
getEditor()._tiptapEditor.state.doc,
getEditor()._tiptapEditor.state.selection.from
);
Expand All @@ -24,19 +24,18 @@ function makeSelectionSpanContent(selectionType: "text" | "node" | "cell") {
CellSelection.create(
getEditor()._tiptapEditor.state.doc,
getEditor()
._tiptapEditor.state.doc.resolve(startPos + 3)
._tiptapEditor.state.doc.resolve(blockContent.beforePos + 3)
.before(),
getEditor()
._tiptapEditor.state.doc.resolve(
startPos + contentNode.nodeSize - 3
)
._tiptapEditor.state.doc.resolve(blockContent.afterPos - 3)
.before()
)
)
);
} else if (selectionType === "node") {
const resolvedContentStartPos =
getEditor()._tiptapEditor.state.doc.resolve(startPos);
const resolvedContentStartPos = getEditor()._tiptapEditor.state.doc.resolve(
blockContent.beforePos
);

getEditor()._tiptapEditor.view.dispatch(
getEditor()._tiptapEditor.state.tr.setSelection(
Expand All @@ -51,10 +50,11 @@ function makeSelectionSpanContent(selectionType: "text" | "node" | "cell") {
)
);
} else {
const resolvedContentStartPos =
getEditor()._tiptapEditor.state.doc.resolve(startPos);
const resolvedContentStartPos = getEditor()._tiptapEditor.state.doc.resolve(
blockContent.beforePos
);
const resolvedContentEndPos = getEditor()._tiptapEditor.state.doc.resolve(
startPos + contentNode.nodeSize
blockContent.afterPos
);

getEditor()._tiptapEditor.view.dispatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ type BlockSelectionData = (
function getBlockSelectionData(
editor: BlockNoteEditor<any, any, any>
): BlockSelectionData {
const { id, startPos } = getBlockInfoFromPos(
const { blockContainer } = getBlockInfoFromPos(
editor._tiptapEditor.state.doc,
editor._tiptapEditor.state.selection.from
);

const selectionData = {
blockId: id,
blockPos: startPos - 1,
blockId: blockContainer.node.attrs.id,
blockPos: blockContainer.beforePos,
};

if (editor._tiptapEditor.state.selection instanceof CellSelection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ export const splitBlockCommand =
return false;
}

const { contentNode, contentType, startPos, endPos, depth } = blockInfo;
const { depth, blockContainer, blockContent } = blockInfo;

const originalBlockContent = state.doc.cut(startPos + 1, posInBlock);
const newBlockContent = state.doc.cut(posInBlock, endPos - 1);
const originalBlockContent = state.doc.cut(
blockContainer.beforePos + 2,
matthewlipski marked this conversation as resolved.
Show resolved Hide resolved
posInBlock
);
const newBlockContent = state.doc.cut(
posInBlock,
blockContainer.afterPos - 2
matthewlipski marked this conversation as resolved.
Show resolved Hide resolved
);

const newBlock = state.schema.nodes["blockContainer"].createAndFill()!;

const newBlockInsertionPos = endPos + 1;
const newBlockInsertionPos = blockContainer.afterPos;
const newBlockContentPos = newBlockInsertionPos + 2;

if (dispatch) {
Expand All @@ -38,7 +44,7 @@ export const splitBlockCommand =
newBlockContentPos,
newBlockContentPos + 1,
newBlockContent.content.size > 0
? new Slice(Fragment.from(newBlockContent), depth + 2, depth + 2)
? new Slice(Fragment.from(newBlockContent), depth + 3, depth + 3)
matthewlipski marked this conversation as resolved.
Show resolved Hide resolved
: undefined
);

Expand All @@ -48,8 +54,8 @@ export const splitBlockCommand =
state.tr.setBlockType(
newBlockContentPos,
newBlockContentPos,
state.schema.node(contentType).type,
keepProps ? contentNode.attrs : undefined
blockContent.node.type,
keepProps ? blockContent.node.attrs : undefined
);
}

Expand All @@ -61,10 +67,10 @@ export const splitBlockCommand =
// Replaces the content of the original block's content node. Doesn't replace the whole content node so its
// type doesn't change.
state.tr.replace(
startPos + 1,
endPos - 1,
blockContainer.beforePos + 2,
blockContainer.afterPos - 2,
originalBlockContent.content.size > 0
? new Slice(Fragment.from(originalBlockContent), depth + 2, depth + 2)
? new Slice(Fragment.from(originalBlockContent), depth + 3, depth + 3)
: undefined
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const updateBlockCommand =
return false;
}

const { startPos, endPos, node, contentNode } = blockInfo;
const { blockContainer, blockContent, blockGroup } = blockInfo;

if (dispatch) {
// Adds blockGroup node with child blocks if necessary.
Expand All @@ -56,23 +56,23 @@ export const updateBlockCommand =
}

// Checks if a blockGroup node already exists.
if (node.childCount === 2) {
if (blockGroup) {
// Replaces all child nodes in the existing blockGroup with the ones created earlier.
state.tr.replace(
startPos + contentNode.nodeSize + 1,
endPos - 1,
blockGroup.beforePos + 1,
blockGroup.afterPos - 1,
new Slice(Fragment.from(childNodes), 0, 0)
);
} else {
// Inserts a new blockGroup containing the child nodes created earlier.
state.tr.insert(
startPos + contentNode.nodeSize,
blockContent.afterPos,
state.schema.nodes["blockGroup"].create({}, childNodes)
);
}
}

const oldType = contentNode.type.name;
const oldType = blockContent.node.type.name;
const newType = block.type || oldType;

// The code below determines the new content of the block.
Expand Down Expand Up @@ -134,10 +134,10 @@ export const updateBlockCommand =
if (content === "keep") {
// use setNodeMarkup to only update the type and attributes
state.tr.setNodeMarkup(
startPos,
blockContent.beforePos,
block.type === undefined ? undefined : state.schema.nodes[block.type],
{
...contentNode.attrs,
...blockContent.node.attrs,
...block.props,
}
);
Expand All @@ -147,11 +147,11 @@ export const updateBlockCommand =
// sets it to the next block.
state.tr
.replaceWith(
startPos,
startPos + contentNode.nodeSize,
blockContent.beforePos,
blockContent.afterPos,
state.schema.nodes[newType].create(
{
...contentNode.attrs,
...blockContent.node.attrs,
...block.props,
},
content
Expand All @@ -162,20 +162,22 @@ export const updateBlockCommand =
// we want to set the selection to the start of it.
.setSelection(
state.schema.nodes[newType].spec.content === ""
? new NodeSelection(state.tr.doc.resolve(startPos))
? new NodeSelection(state.tr.doc.resolve(blockContent.beforePos))
: state.schema.nodes[newType].spec.content === "inline*"
? new TextSelection(state.tr.doc.resolve(startPos))
? new TextSelection(state.tr.doc.resolve(blockContent.beforePos))
: // Need to offset the position as we have to get through the
// `tableRow` and `tableCell` nodes to get to the
// `tableParagraph` node we want to set the selection in.
new TextSelection(state.tr.doc.resolve(startPos + 4))
new TextSelection(
state.tr.doc.resolve(blockContent.beforePos + 4)
)
);
}

// Adds all provided props as attributes to the parent blockContainer node too, and also preserves existing
// attributes.
state.tr.setNodeMarkup(startPos - 1, undefined, {
...node.attrs,
state.tr.setNodeMarkup(blockContainer.beforePos, undefined, {
...blockContainer.node.attrs,
...block.props,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export async function handleFileInsertion<

insertedBlockId = editor.insertBlocks(
[fileBlock],
blockInfo.id,
blockInfo.blockContainer.node.attrs.id,
"after"
)[0].id;
} else {
Expand Down
Loading
Loading