개요
Replace operation은 단일 operation에서 delete와 insert를 결합합니다. Transaction 그룹화를 위해 delete + insert로 분해할 수 있습니다.
인터페이스
interface ReplaceOperation extends Operation {
type: 'replace';
path: Path;
length: number; // 교체할 콘텐츠 길이
content: string | Node; // 새 콘텐츠
deletedContent?: any; // undo를 위해 삭제된 콘텐츠 저장
}사용법
function replaceSelection(editor: Editor, selection: Selection, newText: string) {
const operation: ReplaceOperation = {
type: 'replace',
path: selection.start,
length: selection.length,
content: newText,
deletedContent: editor.getContent(selection)
};
editor.applyOperation(operation);
}
// Replace는 delete + insert로 분해 가능
function replaceAsTransaction(editor: Editor, selection: Selection, newText: string) {
const tx = editor.beginTransaction();
tx.add({
type: 'deleteContent',
path: selection.start,
length: selection.length
});
tx.add({
type: 'insertText',
path: selection.start,
text: newText
});
tx.commit();
}