Toggle Format Operation

텍스트 범위에 대한 서식을 켜기/끄기 토글합니다.

개요

Toggle format은 현재 상태에 따라 서식을 적용하거나 제거하는 복합 operation입니다.

사용법

// Toggle format은 복합 operation
function toggleFormat(editor: Editor, selection: Selection, format: string) {
  const hasFormat = editor.hasFormat(selection, format);
  
  if (hasFormat) {
    // 서식 제거
    editor.applyOperation({
      type: 'removeFormat',
      path: selection.start,
      length: selection.length,
      format: format
    });
  } else {
    // 서식 적용
    editor.applyOperation({
      type: 'applyFormat',
      path: selection.start,
      length: selection.length,
      format: format,
      value: true
    });
  }
}

// Transaction에서
function toggleFormatInTransaction(editor: Editor, selection: Selection, format: string) {
  const tx = editor.beginTransaction();
  const hasFormat = editor.hasFormat(selection, format);
  
  if (hasFormat) {
    tx.add({
      type: 'removeFormat',
      path: selection.start,
      length: selection.length,
      format: format
    });
  } else {
    tx.add({
      type: 'applyFormat',
      path: selection.start,
      length: selection.length,
      format: format,
      value: true
    });
  }
  
  tx.commit();
}