editor.js/src/components/ot/blocks/insert.ts
2019-02-04 10:06:35 +03:00

44 lines
931 B
TypeScript

import Operation from '../base';
import { BlockDeleteOperation } from './index';
export class BlockInsertOperation extends Operation {
public static TYPE = 'block/insert';
constructor(index: number, tool: string, data: any) {
super({
type: BlockInsertOperation.TYPE,
block: index,
data: {
tool,
data,
},
});
}
public transform(operation: Operation): Operation {
const newOp = this.clone();
switch (operation.type) {
case BlockInsertOperation.TYPE:
if (operation.block <= newOp.block) {
newOp.block++;
}
break;
case BlockDeleteOperation.TYPE:
if (operation.block <= newOp.block) {
newOp.block--;
}
}
return newOp;
}
public reverse(): Operation {
const op = new BlockDeleteOperation(this.block, this.data.tool, this.data.data);
op.reversed = true;
return op;
}
}