Skip to content

Commit

Permalink
feat: 하위요소 삭제 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
pipisebastian committed Nov 26, 2024
1 parent 911f41e commit 83f4718
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions client/src/features/editor/hooks/useBlockOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,28 @@ export const useBlockOptionSelect = ({
};

const handleDeleteSelect = (blockId: BlockId) => {
const currentIndex = editorCRDT.LinkedList.spread().findIndex((block) =>
block.id.equals(blockId),
);
sendBlockDeleteOperation(editorCRDT.localDelete(currentIndex, undefined, pageId));
const blocks = editorCRDT.LinkedList.spread(); // spread() 한 번만 호출
const currentIndex = blocks.findIndex((block) => block.id.equals(blockId));

const currentBlock = blocks[currentIndex];
if (!currentBlock) return;

const deleteIndices = [];
const currentIndent = currentBlock.indent;

// 현재 블록과 자식 블록들의 인덱스를 한 번에 수집
for (let i = currentIndex; i < blocks.length; i++) {
if (i === currentIndex || blocks[i].indent > currentIndent) {
deleteIndices.push(i);
} else if (blocks[i].indent <= currentIndent) {
break; // 더 이상 자식 블록이 없으면 종료
}
}

// 인덱스 역순으로 삭제
for (let i = deleteIndices.length - 1; i >= 0; i--) {
sendBlockDeleteOperation(editorCRDT.localDelete(deleteIndices[i], undefined, pageId));
}

// 삭제할 블록이 현재 활성화된 블록인 경우
if (editorCRDT.currentBlock?.id.equals(blockId)) {
Expand Down

0 comments on commit 83f4718

Please sign in to comment.