Skip to content

Commit

Permalink
improve prevent deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
lamtuanamc committed Nov 27, 2024
1 parent 8ebf69a commit b0854f9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,18 @@ const mergeBlocks = (
// TODO: test merging between a columnList and paragraph, between two columnLists, and v.v.
let startPos = prevBlockInfo.blockContent.afterPos - 1;
let endPos = nextBlockInfo.blockContent.beforePos + 1;

// Check if the previous block is an image
if (prevBlockInfo.blockNoteType === "image") {
// If the next block contains text, merging will remove the image (previous block) and retain the text
const isPrevFileBlock =
prevBlockInfo.blockNoteType === "image" ||
prevBlockInfo.blockNoteType === "file" ||
prevBlockInfo.blockNoteType === "audio";

// Check if the previous is a file block
if (isPrevFileBlock) {
// If the next block contains text, merging will remove the file (previous block) and retain the text
if (nextBlockInfo.blockContent.node.textContent) {
endPos = nextBlockInfo.blockContent.beforePos;
} else {
// If the next block has no text, merging will remove the text block and retain the image
// If the next block has no text, merging will remove the text block and retain the file
startPos = prevBlockInfo.blockContent.afterPos;
}
}
Expand Down Expand Up @@ -169,9 +173,12 @@ export const mergeBlocksCommand =
prevBlockInfo
);

const prevBlockIsImage = prevBlockInfo.blockNoteType === "image";
const isPrevFileBlock =
prevBlockInfo.blockNoteType === "image" ||
prevBlockInfo.blockNoteType === "file" ||
prevBlockInfo.blockNoteType === "audio";

if (!canMerge(bottomNestedBlockInfo, nextBlockInfo) && !prevBlockIsImage) {
if (!canMerge(bottomNestedBlockInfo, nextBlockInfo) && !isPrevFileBlock) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,12 @@ export const KeyboardShortcutsExtension = Extension.create<{
"inline*" &&
bottomBlock.blockContent.node.childCount === 0);

const isImageBlock = bottomBlock.blockNoteType === "image";
const isFileBlock =
bottomBlock.blockNoteType === "image" ||
bottomBlock.blockNoteType === "file" ||
bottomBlock.blockNoteType === "audio";

if (prevBlockNotTableAndNoContent && !isImageBlock) {
if (prevBlockNotTableAndNoContent && !isFileBlock) {
return chain()
.cut(
{
Expand Down

0 comments on commit b0854f9

Please sign in to comment.