import { ExtendedType, Node, Path, Range } from '..' import isPlainObject from 'is-plain-object' export type BaseInsertNodeOperation = { type: 'insert_node' path: Path node: Node, anchorId?: string; } export type InsertNodeOperation = ExtendedType< 'InsertNodeOperation', BaseInsertNodeOperation > export type BaseInsertTextOperation = { type: 'insert_text' path: Path offset: number text: string } export type InsertTextOperation = ExtendedType< 'InsertTextOperation', BaseInsertTextOperation > export type BaseMergeNodeOperation = { type: 'merge_node' path: Path position: number properties: Partial } export type MergeNodeOperation = ExtendedType< 'MergeNodeOperation', BaseMergeNodeOperation > export type BaseMoveNodeOperation = { type: 'move_node' path: Path newPath: Path } export type MoveNodeOperation = ExtendedType< 'MoveNodeOperation', BaseMoveNodeOperation > export type BaseRemoveNodeOperation = { type: 'remove_node' path: Path node: Node } export type RemoveNodeOperation = ExtendedType< 'RemoveNodeOperation', BaseRemoveNodeOperation > export type BaseRemoveTextOperation = { type: 'remove_text' path: Path offset: number text: string } export type RemoveTextOperation = ExtendedType< 'RemoveTextOperation', BaseRemoveTextOperation > export type BaseSetNodeOperation = { type: 'set_node' path: Path properties: Partial newProperties: Partial } export type SetNodeOperation = ExtendedType< 'SetNodeOperation', BaseSetNodeOperation > export type BaseSetSelectionOperation = | { type: 'set_selection' properties: null newProperties: Range } | { type: 'set_selection' properties: Partial newProperties: Partial } | { type: 'set_selection' properties: Range newProperties: null } export type SetSelectionOperation = ExtendedType< 'SetSelectionOperation', BaseSetSelectionOperation > export type BaseSplitNodeOperation = { type: 'split_node' path: Path position: number properties: Partial anchorId?: string } export type SplitNodeOperation = ExtendedType< 'SplitNodeOperation', BaseSplitNodeOperation > export type NodeOperation = | InsertNodeOperation | MergeNodeOperation | MoveNodeOperation | RemoveNodeOperation | SetNodeOperation | SplitNodeOperation export type SelectionOperation = SetSelectionOperation export type TextOperation = InsertTextOperation | RemoveTextOperation export type Operation = NodeOperation | SelectionOperation | TextOperation export interface OperationInterface { isNodeOperation: (value: any) => value is NodeOperation isOperation: (value: any) => value is Operation isOperationList: (value: any) => value is Operation[] isSelectionOperation: (value: any) => value is SelectionOperation isTextOperation: (value: any) => value is TextOperation inverse: (op: Operation) => Operation } export const Operation: OperationInterface = { isNodeOperation(value: any): value is NodeOperation { return Operation.isOperation(value) && value.type.endsWith('_node') }, isOperation(value: any): value is Operation { if (!isPlainObject(value)) { return false } switch (value.type) { case 'insert_node': return Path.isPath(value.path) && Node.isNode(value.node) case 'insert_text': return ( typeof value.offset === 'number' && typeof value.text === 'string' && Path.isPath(value.path) ) case 'merge_node': return ( typeof value.position === 'number' && Path.isPath(value.path) && isPlainObject(value.properties) ) case 'move_node': return Path.isPath(value.path) && Path.isPath(value.newPath) case 'remove_node': return Path.isPath(value.path) && Node.isNode(value.node) case 'remove_text': return ( typeof value.offset === 'number' && typeof value.text === 'string' && Path.isPath(value.path) ) case 'set_node': return ( Path.isPath(value.path) && isPlainObject(value.properties) && isPlainObject(value.newProperties) ) case 'set_selection': return ( (value.properties === null && Range.isRange(value.newProperties)) || (value.newProperties === null && Range.isRange(value.properties)) || (isPlainObject(value.properties) && isPlainObject(value.newProperties)) ) case 'split_node': return ( Path.isPath(value.path) && typeof value.position === 'number' && isPlainObject(value.properties) ) default: return false } }, isOperationList(value: any): value is Operation[] { return ( Array.isArray(value) && value.every(val => Operation.isOperation(val)) ) }, isSelectionOperation(value: any): value is SelectionOperation { return Operation.isOperation(value) && value.type.endsWith('_selection') }, isTextOperation(value: any): value is TextOperation { return Operation.isOperation(value) && value.type.endsWith('_text') }, inverse(op: Operation): Operation { switch (op.type) { case 'insert_node': { return { ...op, type: 'remove_node' } } case 'insert_text': { return { ...op, type: 'remove_text' } } case 'merge_node': { return { ...op, type: 'split_node', path: Path.previous(op.path) } } case 'move_node': { const { newPath, path } = op if (Path.equals(newPath, path)) { return op } if (Path.isSibling(path, newPath)) { return { ...op, path: newPath, newPath: path } } const inversePath = Path.transform(path, op)! const inverseNewPath = Path.transform(Path.next(path), op)! return { ...op, path: inversePath, newPath: inverseNewPath } } case 'remove_node': { return { ...op, type: 'insert_node' } } case 'remove_text': { return { ...op, type: 'insert_text' } } case 'set_node': { const { properties, newProperties } = op return { ...op, properties: newProperties, newProperties: properties } } case 'set_selection': { const { properties, newProperties } = op if (properties == null) { return { ...op, properties: newProperties as Range, newProperties: null, } } else if (newProperties == null) { return { ...op, properties: null, newProperties: properties as Range, } } else { return { ...op, properties: newProperties, newProperties: properties } } } case 'split_node': { return { ...op, type: 'merge_node', path: Path.next(op.path) } } } }, }