Merge Nodes Operation

두 개의 인접한 노드를 단일 노드로 병합합니다.

개요

Merge nodes operation은 두 개의 인접한 노드를 하나로 결합합니다. 사용자가 단락 시작 부분에서 Backspace를 눌러 이전 단락과 병합할 때 일반적으로 사용됩니다.

인터페이스

interface MergeNodesOperation extends Operation {
  type: 'mergeNodes';
  path: Path; // 첫 번째 노드 경로
  targetPath: Path; // 두 번째 노드 경로 (일반적으로 다음 형제)
  position?: number; // 병합할 첫 번째 노드 내 위치
}

사용법

// 현재 단락을 이전 단락과 병합
function mergeWithPrevious(editor: Editor, currentPath: Path) {
  const previousPath = [currentPath[0] - 1];
  
  const operation: MergeNodesOperation = {
    type: 'mergeNodes',
    path: previousPath,
    targetPath: currentPath
  };
  
  editor.applyOperation(operation);
}

// 특정 위치에서 병합
function mergeAtPosition(editor: Editor, firstPath: Path, secondPath: Path, position: number) {
  const operation: MergeNodesOperation = {
    type: 'mergeNodes',
    path: firstPath,
    targetPath: secondPath,
    position: position
  };
  
  editor.applyOperation(operation);
}

리버스 Operation

Merge nodes의 리버스 Operation은 split node입니다:

function getInverse(operation: MergeNodesOperation): SplitNodeOperation {
  return {
    type: 'splitNode',
    path: operation.path,
    position: operation.position || editor.getNodeAtPath(operation.path).text.length
  };
}