Composite Operations

복잡한 변환을 위해 여러 operation을 결합합니다.

개요

Composite operation은 붙여넣기, 단락 분할 또는 여러 범위 서식 적용과 같은 복잡한 변환을 위해 transaction에서 여러 operation을 결합합니다.

붙여넣기 Operation

function pasteContent(editor: Editor, position: Path, content: string) {
  const tx = editor.beginTransaction();
  const selection = editor.getSelection();
  
  // 선택 영역이 있으면 삭제
  if (!selection.isCollapsed) {
    tx.add({
      type: 'deleteContent',
      path: selection.start,
      length: selection.length
    });
  }
  
  // 붙여넣은 콘텐츠 삽입
  tx.add({
    type: 'insertText',
    path: position,
    text: content
  });
  
  tx.commit(); // 전체 붙여넣기에 대한 단일 undo entry
}

여러 범위 서식 적용

function formatMultipleRanges(editor: Editor, ranges: Range[], format: string) {
  const tx = editor.beginTransaction();
  
  for (const range of ranges) {
    tx.add({
      type: 'applyFormat',
      path: range.start,
      length: range.length,
      format: format,
      value: true
    });
  }
  
  tx.commit(); // 모든 서식에 대한 단일 undo
}

단락 분할

function splitParagraph(editor: Editor, position: Path) {
  const tx = editor.beginTransaction();
  const paragraph = editor.getNodeAtPath(position);
  
  const beforeText = paragraph.text.slice(0, position[1]);
  const afterText = paragraph.text.slice(position[1]);
  
  // 현재 단락 업데이트
  tx.add({
    type: 'replace',
    path: position,
    length: paragraph.text.length,
    content: beforeText
  });
  
  // 새 단락 삽입
  tx.add({
    type: 'insertNode',
    path: [position[0] + 1],
    node: {
      type: 'paragraph',
      children: [{ type: 'text', text: afterText }]
    }
  });
  
  tx.commit();
}