Delete Content Operation

문서 모델에서 범위의 콘텐츠를 삭제합니다.

개요

Delete content operation은 특정 경로와 길이에서 문서에서 콘텐츠를 제거합니다. 효율적인 undo를 위해 항상 삭제된 콘텐츠를 저장해야 합니다.

인터페이스

interface DeleteContentOperation extends Operation {
  type: 'deleteContent';
  path: Path;
  length: number; // 삭제할 문자/노드 수
  deletedContent?: any; // undo를 위해 삭제된 콘텐츠 저장
}

사용법

// 선택된 텍스트 삭제
function deleteSelection(editor: Editor, selection: Selection) {
  const operation: DeleteContentOperation = {
    type: 'deleteContent',
    path: selection.start,
    length: selection.length,
    deletedContent: editor.getContent(selection) // undo를 위해 저장
  };
  
  editor.applyOperation(operation);
}

// Transaction에서
function deleteInTransaction(editor: Editor, selection: Selection) {
  const tx = editor.beginTransaction();
  tx.add({
    type: 'deleteContent',
    path: selection.start,
    length: selection.length,
    deletedContent: editor.getContent(selection)
  });
  tx.commit();
}

리버스 Operation

function getInverse(operation: DeleteContentOperation): InsertTextOperation {
  return {
    type: 'insertText',
    path: operation.path,
    text: operation.deletedContent
  };
}