UNPKG

@lcap/asl

Version:

NetEase Application Specific Language

365 lines 14.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.History = void 0; const __1 = require(".."); const undoredo_1 = __importDefault(require("../../service/undoredo")); const LogicItem_1 = require("../logic/LogicItem"); /** * 后续考虑优化可逆操作 * @param operation * @returns */ function getOperationHandler(operation) { const map = { /* Element */ 'Element.create': () => ({ context: __1.vertexsMap.get(operation.operationAfterImage.parentId), undoAction: 'Element.removeChild', undoActionParam: 'After', redoAction: 'Element.addChild', redoActionParam: 'After', }), 'Element.addChild': () => ({ context: __1.vertexsMap.get(operation.operationAfterImage.parentId), undoAction: 'Element.removeChild', undoActionParam: 'After', redoAction: 'Element.addChild', redoActionParam: 'After', }), 'Element.delete': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.parentId), undoAction: 'Element.addChild', undoActionParam: 'Before', redoAction: 'Element.removeChild', redoActionParam: 'Before', }), 'Element.removeChild': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.parentId), undoAction: 'Element.addChild', undoActionParam: 'Before', redoAction: 'Element.removeChild', redoActionParam: 'Before', }), 'Element.update': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.id), undoAction: 'Element.update', undoActionParam: 'Before', redoAction: 'Element.update', redoActionParam: 'After', }), 'Element.move': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.id), undoAction: 'Element.move', undoActionParam: 'Before', redoAction: 'Element.move', redoActionParam: 'After', }), /* Attr */ 'Attr.create': () => ({ context: __1.vertexsMap.get(operation.operationAfterImage.elementId), undoAction: 'Element.deleteAttr', undoActionParam: 'After', redoAction: 'Element.addAttr', redoActionParam: 'After', }), 'Element.addAttr': () => ({ context: __1.vertexsMap.get(operation.operationAfterImage.elementId), undoAction: 'Element.deleteAttr', undoActionParam: 'After', redoAction: 'Element.addAttr', redoActionParam: 'After', }), 'Attr.delete': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.elementId), undoAction: 'Element.addAttr', undoActionParam: 'Before', redoAction: 'Element.deleteAttr', redoActionParam: 'Before', }), 'Element.deleteAttr': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.elementId), undoAction: 'Element.addAttr', undoActionParam: 'Before', redoAction: 'Element.deleteAttr', redoActionParam: 'Before', }), 'Attr.update': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.id), undoAction: 'Attr.update', undoActionParam: 'Before', redoAction: 'Attr.update', redoActionParam: 'After', }), /* Event */ 'Event.create': () => ({ context: __1.vertexsMap.get(operation.operationAfterImage.elementId), undoAction: 'Element.deleteEvent', undoActionParam: 'After', redoAction: 'Element.addEvent', redoActionParam: 'After', }), 'Element.addEvent': () => ({ context: __1.vertexsMap.get(operation.operationAfterImage.elementId), undoAction: 'Element.deleteEvent', undoActionParam: 'After', redoAction: 'Element.addEvent', redoActionParam: 'After', }), 'Event.delete': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.elementId), undoAction: 'Element.addEvent', undoActionParam: 'Before', redoAction: 'Element.deleteEvent', redoActionParam: 'Before', }), 'Element.deleteEvent': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.elementId), undoAction: 'Element.addEvent', undoActionParam: 'Before', redoAction: 'Element.deleteEvent', redoActionParam: 'Before', }), 'Event.update': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.id), undoAction: 'Event.update', undoActionParam: 'Before', redoAction: 'Event.update', redoActionParam: 'After', }), /* Directive */ 'Directive.create': () => ({ context: __1.vertexsMap.get(operation.operationAfterImage.elementId), undoAction: 'Element.deleteDirective', undoActionParam: 'After', redoAction: 'Element.addDirective', redoActionParam: 'After', }), 'Element.addDirective': () => ({ context: __1.vertexsMap.get(operation.operationAfterImage.elementId), undoAction: 'Element.deleteDirective', undoActionParam: 'After', redoAction: 'Element.addDirective', redoActionParam: 'After', }), 'Directive.delete': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.elementId), undoAction: 'Element.addDirective', undoActionParam: 'Before', redoAction: 'Element.deleteDirective', redoActionParam: 'Before', }), 'Element.deleteDirective': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.elementId), undoAction: 'Element.addDirective', undoActionParam: 'Before', redoAction: 'Element.deleteDirective', redoActionParam: 'Before', }), 'Directive.update': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.id), undoAction: 'Directive.update', undoActionParam: 'Before', redoAction: 'Directive.update', redoActionParam: 'After', }), /* Lifecycle */ 'Lifecycle.create': () => ({ context: __1.vertexsMap.get(operation.operationAfterImage.viewId), undoAction: 'Element.removeLifecycle', undoActionParam: 'After', redoAction: 'Element.addLifecycle', redoActionParam: 'After', }), 'View.addLifecycle': () => ({ context: __1.vertexsMap.get(operation.operationAfterImage.viewId), undoAction: 'View.removeLifecycle', undoActionParam: 'After', redoAction: 'View.addLifecycle', redoActionParam: 'After', }), 'Lifecycle.delete': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.viewId), undoAction: 'View.addLifecycle', undoActionParam: 'Before', redoAction: 'View.removeLifecycle', redoActionParam: 'Before', }), 'View.removeLifecycle': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.viewId), undoAction: 'View.addLifecycle', undoActionParam: 'Before', redoAction: 'View.removeLifecycle', redoActionParam: 'Before', }), 'Lifecycle.update': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.id), undoAction: 'Lifecycle.update', undoActionParam: 'Before', redoAction: 'Lifecycle.update', redoActionParam: 'After', }), /* LogicItem */ 'LogicItem.create': () => ({ context: __1.vertexsMap.get(operation.operationAfterImage.logicId), undoAction: 'Logic.removeItem', undoActionParam: 'After', redoAction: 'Logic.addItem', redoActionParam: 'After', }), 'Logic.addItem': () => ({ context: __1.vertexsMap.get(operation.operationAfterImage.logicId), undoAction: 'Logic.removeItem', undoActionParam: 'After', redoAction: 'Logic.addItem', redoActionParam: 'After', }), 'LogicItem.delete': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.logicId), undoAction: 'Logic.addItem', undoActionParam: 'Before', redoAction: 'Logic.removeItem', redoActionParam: 'Before', }), 'LogicItem.removeItem': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.logicId), undoAction: 'Logic.addItem', undoActionParam: 'Before', redoAction: 'Logic.removeItem', redoActionParam: 'Before', }), 'LogicItem.update': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.id), undoAction: 'LogicItem.update', undoActionParam: 'Before', redoAction: 'LogicItem.update', redoActionParam: 'After', }), 'LogicItem.move': () => ({ context: __1.vertexsMap.get(operation.operationBeforeImage.id), undoAction: 'LogicItem.move', undoActionParam: 'Before', redoAction: 'LogicItem.move', redoActionParam: 'After', }), 'LogicItem.paste': () => ({ context: LogicItem_1.LogicItem, undoAction: 'View.undoPaste', undoActionParam: 'After', redoAction: 'View.redoPaste', redoActionParam: 'After', }), 'View.mergeBlock': () => ({ context: __1.vertexsMap.get(operation.operationAfterImage.id), undoAction: 'View.undoMergeBlock', undoActionParam: 'After', redoAction: 'View.redoMergeBlock', redoActionParam: 'After', }), }; return map[operation.operationAction](); } class History { constructor(source) { this.app = undefined; this.undoCount = 0; this.redoCount = 0; this.undoStack = []; this.redoStack = []; Object.assign(this, source); } /** * 加载 undoRedo 栈 * @param operation 失败时用于前端模拟 */ async load(operation) { try { const result = await undoredo_1.default.undoRedoInfo({ headers: { appId: this.app.id, }, }); this.undoCount = result.undoNumber; this.redoCount = result.redoNumber; } catch (e) { } } async undo() { this.app.emit('saving'); let operation; try { const result = await undoredo_1.default.undo({ headers: { appId: this.app.id, }, }); operation = result.operation; } catch (e) { return; } const oh = getOperationHandler(operation); let image = operation[`operation${oh.undoActionParam}Image`]; if (operation.operationAction === 'LogicItem.move') { // 移动 QueryJoinExpression 会同时修改 onExpressionList if (image.type === 'QueryJoinExpression') { const context = oh.context; context.assign(LogicItem_1.LogicItem.from(image, context.logic, context.parent)); } image = image.moveToLogicItem; } operation.activeImage = image; if (operation.operationAction === 'LogicItem.paste') operation.activeImage = operation.operationAfterImage.logicItems[0]; await oh.context[oh.undoAction.split('.').pop()](image, { actionMode: __1.ACTION_MODE.undoRedo, }); return operation; } async redo() { this.app.emit('saving'); let operation; try { const result = await undoredo_1.default.redo({ headers: { appId: this.app.id, }, }); operation = result.operation; } catch (e) { return; } const oh = getOperationHandler(operation); let image = operation[`operation${oh.redoActionParam}Image`]; if (operation.operationAction === 'LogicItem.move') { // 移动 QueryJoinExpression 会同时修改 onExpressionList const { operationBeforeImage } = operation; if (operationBeforeImage.type === 'QueryJoinExpression') { const context = oh.context; context.onExpressionList = [LogicItem_1.LogicItem.from(operationBeforeImage.onExpressionList[0], context.logic, context)]; context.onExpressionList[0].left.propertyRef = ''; context.onExpressionList[0].right.propertyRef = ''; } image = image.moveToLogicItem; } operation.activeImage = image; if (operation.operationAction === 'LogicItem.paste') operation.activeImage = operation.operationAfterImage.logicItems[0]; await oh.context[oh.redoAction.split('.').pop()](image, { actionMode: __1.ACTION_MODE.undoRedo, }); return operation; } clear() { this.undoStack = []; this.redoStack = []; this.undoCount = 0; this.redoCount = 0; } } exports.History = History; exports.default = History; //# sourceMappingURL=History.js.map