개요
Apply format operation은 콘텐츠를 변경하지 않고 텍스트 스타일(굵게, 기울임, 밑줄 등)을 수정합니다.
인터페이스
interface ApplyFormatOperation extends Operation {
type: 'applyFormat';
path: Path;
length: number; // 서식 적용할 범위
format: string; // 서식 이름 (예: 'bold', 'italic')
value: any; // 서식 값
}사용법
// 굵게 서식 적용
function applyBold(editor: Editor, selection: Selection) {
const operation: ApplyFormatOperation = {
type: 'applyFormat',
path: selection.start,
length: selection.length,
format: 'bold',
value: true
};
editor.applyOperation(operation);
}
// Transaction에서 여러 서식 적용
function applyFormats(editor: Editor, selection: Selection, formats: string[]) {
const tx = editor.beginTransaction();
for (const format of formats) {
tx.add({
type: 'applyFormat',
path: selection.start,
length: selection.length,
format: format,
value: true
});
}
tx.commit(); // 단일 undo entry
}리버스 Operation
function getInverse(operation: ApplyFormatOperation): RemoveFormatOperation {
return {
type: 'removeFormat',
path: operation.path,
length: operation.length,
format: operation.format
};
}