개요
Update attributes operation은 노드의 콘텐츠나 구조를 변경하지 않고 노드 속성(이미지 src, 링크 href, 제목 레벨 등)을 수정합니다.
인터페이스
interface UpdateAttributesOperation extends Operation {
type: 'updateAttributes';
path: Path;
attributes: Record<string, any>; // 새 속성 값
previousAttributes?: Record<string, any>; // undo를 위해 이전 값 저장
}사용법
// 이미지 소스 업데이트
function updateImageSrc(editor: Editor, imagePath: Path, newSrc: string) {
const node = editor.getNodeAtPath(imagePath);
const operation: UpdateAttributesOperation = {
type: 'updateAttributes',
path: imagePath,
attributes: { src: newSrc },
previousAttributes: { src: node.attributes.src }
};
editor.applyOperation(operation);
}
// 링크 href 업데이트
function updateLinkHref(editor: Editor, linkPath: Path, newHref: string) {
const node = editor.getNodeAtPath(linkPath);
const operation: UpdateAttributesOperation = {
type: 'updateAttributes',
path: linkPath,
attributes: { href: newHref },
previousAttributes: { href: node.attributes.href }
};
editor.applyOperation(operation);
}
// 제목 레벨 업데이트
function updateHeadingLevel(editor: Editor, headingPath: Path, newLevel: number) {
const node = editor.getNodeAtPath(headingPath);
const operation: UpdateAttributesOperation = {
type: 'updateAttributes',
path: headingPath,
attributes: { level: newLevel },
previousAttributes: { level: node.attributes.level }
};
editor.applyOperation(operation);
}리버스 Operation
리버스 Operation은 이전 속성을 복원합니다:
function getInverse(operation: UpdateAttributesOperation): UpdateAttributesOperation {
return {
type: 'updateAttributes',
path: operation.path,
attributes: operation.previousAttributes || {},
previousAttributes: operation.attributes
};
}