Set Node Type Operation

콘텐츠와 구조를 보존하면서 노드 타입을 변경합니다.

개요

Set node type operation은 노드의 타입(예: 단락을 제목으로, 제목을 단락으로)을 변경하면서 콘텐츠와 자식을 보존합니다.

인터페이스

interface SetNodeTypeOperation extends Operation {
  type: 'setNodeType';
  path: Path;
  nodeType: string; // 새 노드 타입
  previousType?: string; // undo를 위해 이전 타입 저장
  attributes?: Record<string, any>; // 선택적 새 속성
  previousAttributes?: Record<string, any>; // 이전 속성 저장
}

사용법

// 단락을 제목으로 변환
function paragraphToHeading(editor: Editor, paragraphPath: Path, level: number) {
  const node = editor.getNodeAtPath(paragraphPath);
  
  const operation: SetNodeTypeOperation = {
    type: 'setNodeType',
    path: paragraphPath,
    nodeType: 'heading',
    previousType: node.type,
    attributes: { level },
    previousAttributes: node.attributes
  };
  
  editor.applyOperation(operation);
}

// 제목을 단락으로 변환
function headingToParagraph(editor: Editor, headingPath: Path) {
  const node = editor.getNodeAtPath(headingPath);
  
  const operation: SetNodeTypeOperation = {
    type: 'setNodeType',
    path: headingPath,
    nodeType: 'paragraph',
    previousType: node.type,
    previousAttributes: node.attributes
  };
  
  editor.applyOperation(operation);
}

// 제목 레벨 변경
function changeHeadingLevel(editor: Editor, headingPath: Path, newLevel: number) {
  const node = editor.getNodeAtPath(headingPath);
  
  const operation: SetNodeTypeOperation = {
    type: 'setNodeType',
    path: headingPath,
    nodeType: 'heading', // 같은 타입
    previousType: node.type,
    attributes: { level: newLevel },
    previousAttributes: { level: node.attributes.level }
  };
  
  editor.applyOperation(operation);
}

리버스 Operation

리버스 Operation은 이전 노드 타입을 복원합니다:

function getInverse(operation: SetNodeTypeOperation): SetNodeTypeOperation {
  return {
    type: 'setNodeType',
    path: operation.path,
    nodeType: operation.previousType!,
    previousType: operation.nodeType,
    attributes: operation.previousAttributes,
    previousAttributes: operation.attributes
  };
}