Files
drawNET/static/js/lib/x6-plugin-history.js
T
2026-04-21 13:28:36 +09:00

2 lines
11 KiB
JavaScript

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@antv/x6")):"function"==typeof define&&define.amd?define(["exports","@antv/x6"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).X6PluginHistory={},t.X6)}(this,(function(t,e){"use strict";function i(t,e,i,n){var s,o=arguments.length,a=o<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,i):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(t,e,i,n);else for(var h=t.length-1;h>=0;h--)(s=t[h])&&(a=(o<3?s(a):o>3?s(e,i,a):s(e,i))||a);return o>3&&a&&Object.defineProperty(e,i,a),a}e.Graph.prototype.isHistoryEnabled=function(){const t=this.getPlugin("history");return!!t&&t.isEnabled()},e.Graph.prototype.enableHistory=function(){const t=this.getPlugin("history");return t&&t.enable(),this},e.Graph.prototype.disableHistory=function(){const t=this.getPlugin("history");return t&&t.disable(),this},e.Graph.prototype.toggleHistory=function(t){const e=this.getPlugin("history");return e&&e.toggleEnabled(t),this},e.Graph.prototype.undo=function(t){const e=this.getPlugin("history");return e&&e.undo(t),this},e.Graph.prototype.redo=function(t){const e=this.getPlugin("history");return e&&e.redo(t),this},e.Graph.prototype.undoAndCancel=function(t){const e=this.getPlugin("history");return e&&e.cancel(t),this},e.Graph.prototype.canUndo=function(){const t=this.getPlugin("history");return!!t&&t.canUndo()},e.Graph.prototype.canRedo=function(){const t=this.getPlugin("history");return!!t&&t.canRedo()},e.Graph.prototype.cleanHistory=function(t){const e=this.getPlugin("history");return e&&e.clean(t),this},e.Graph.prototype.getHistoryStackSize=function(){return this.getPlugin("history").getSize()},e.Graph.prototype.getUndoStackSize=function(){return this.getPlugin("history").getUndoSize()},e.Graph.prototype.getRedoStackSize=function(){return this.getPlugin("history").getRedoSize()},e.Graph.prototype.getUndoRemainSize=function(){return this.getPlugin("history").getUndoRemainSize()};class n extends e.Basecoat{constructor(t={}){super(),this.name="history",this.batchCommands=null,this.batchLevel=0,this.lastBatchIndex=-1,this.freezed=!1,this.stackSize=0,this.handlers=[];const{stackSize:e=0}=t;this.stackSize=e,this.options=s.getOptions(t),this.validator=new n.Validator({history:this,cancelInvalid:this.options.cancelInvalid})}init(t){this.graph=t,this.model=this.graph.model,this.clean(),this.startListening()}isEnabled(){return!this.disabled}enable(){this.disabled&&(this.options.enabled=!0)}disable(){this.disabled||(this.options.enabled=!1)}toggleEnabled(t){return null!=t?t!==this.isEnabled()&&(t?this.enable():this.disable()):this.isEnabled()?this.disable():this.enable(),this}undo(t={}){if(!this.disabled){const e=this.undoStack.pop();e&&(this.revertCommand(e,t),this.redoStack.push(e),this.notify("undo",e,t))}return this}redo(t={}){if(!this.disabled){const e=this.redoStack.pop();e&&(this.applyCommand(e,t),this.undoStackPush(e),this.notify("redo",e,t))}return this}cancel(t={}){if(!this.disabled){const e=this.undoStack.pop();e&&(this.revertCommand(e,t),this.redoStack=[],this.notify("cancel",e,t))}return this}getSize(){return this.stackSize}getUndoRemainSize(){const t=this.undoStack.length;return this.stackSize-t}getUndoSize(){return this.undoStack.length}getRedoSize(){return this.redoStack.length}canUndo(){return!this.disabled&&this.undoStack.length>0}canRedo(){return!this.disabled&&this.redoStack.length>0}clean(t={}){return this.undoStack=[],this.redoStack=[],this.notify("clean",null,t),this}get disabled(){return!0!==this.options.enabled}validate(t,...e){return this.validator.validate(t,...e),this}startListening(){this.model.on("batch:start",this.initBatchCommand,this),this.model.on("batch:stop",this.storeBatchCommand,this),this.options.eventNames&&this.options.eventNames.forEach(((t,e)=>{this.handlers[e]=this.addCommand.bind(this,t),this.model.on(t,this.handlers[e])})),this.validator.on("invalid",(t=>this.trigger("invalid",t)))}stopListening(){this.model.off("batch:start",this.initBatchCommand,this),this.model.off("batch:stop",this.storeBatchCommand,this),this.options.eventNames&&(this.options.eventNames.forEach(((t,e)=>{this.model.off(t,this.handlers[e])})),this.handlers.length=0),this.validator.off("invalid")}createCommand(t){return{batch:!!t&&t.batch,data:{}}}revertCommand(t,i){this.freezed=!0;const n=Array.isArray(t)?s.sortBatchCommands(t):[t];for(let t=n.length-1;t>=0;t-=1){const s=n[t],o=Object.assign(Object.assign({},i),e.ObjectExt.pick(s.options,this.options.revertOptionsList||[]));this.executeCommand(s,!0,o)}this.freezed=!1}applyCommand(t,i){this.freezed=!0;const n=Array.isArray(t)?s.sortBatchCommands(t):[t];for(let t=0;t<n.length;t+=1){const s=n[t],o=Object.assign(Object.assign({},i),e.ObjectExt.pick(s.options,this.options.applyOptionsList||[]));this.executeCommand(s,!1,o)}this.freezed=!1}executeCommand(t,i,n){const o=this.model,a=o.getCell(t.data.id),h=t.event;if(s.isAddEvent(h)&&i||s.isRemoveEvent(h)&&!i)a&&a.remove(n);else if(s.isAddEvent(h)&&!i||s.isRemoveEvent(h)&&i){const e=t.data;e.node?o.addNode(e.props,n):e.edge&&o.addEdge(e.props,n)}else if(s.isChangeEvent(h)){const e=t.data,s=e.key;if(s&&a){const t=i?e.prev[s]:e.next[s];if("attrs"===e.key){this.ensureUndefinedAttrs(t,i?e.next[s]:e.prev[s])&&(n.dirty=!0)}a.prop(s,t,n)}}else{const s=this.options.executeCommand;s&&e.FunctionExt.call(s,this,t,i,n)}}addCommand(t,i){if(this.freezed||this.disabled)return;const n=i,o=n.options||{};if(o.dryrun)return;if(s.isAddEvent(t)&&this.options.ignoreAdd||s.isRemoveEvent(t)&&this.options.ignoreRemove||s.isChangeEvent(t)&&this.options.ignoreChange)return;const a=this.options.beforeAddCommand;if(null!=a&&!1===e.FunctionExt.call(a,this,t,i))return;"cell:change:*"===t&&(t=`cell:change:${n.key}`);const h=n.cell,d=e.Model.isModel(h);let r;if(this.batchCommands){r=this.batchCommands[Math.max(this.lastBatchIndex,0)];const e=d&&!r.modelChange||r.data.id!==h.id,i=r.event!==t;if(this.lastBatchIndex>=0&&(e||i)){const e=this.batchCommands.findIndex((e=>(d&&e.modelChange||e.data.id===h.id)&&e.event===t));e<0||s.isAddEvent(t)||s.isRemoveEvent(t)?r=this.createCommand({batch:!0}):(r=this.batchCommands[e],this.batchCommands.splice(e,1)),this.batchCommands.push(r),this.lastBatchIndex=this.batchCommands.length-1}}else r=this.createCommand({batch:!1});if(s.isAddEvent(t)||s.isRemoveEvent(t)){const i=r.data;return r.event=t,r.options=o,i.id=h.id,i.props=e.ObjectExt.cloneDeep(h.toJSON()),h.isEdge()?i.edge=!0:h.isNode()&&(i.node=!0),this.push(r,o)}if(s.isChangeEvent(t)){const n=i.key,s=r.data;return r.batch&&r.event||(r.event=t,r.options=o,s.key=n,null==s.prev&&(s.prev={}),s.prev[n]=e.ObjectExt.cloneDeep(h.previous(n)),d?r.modelChange=!0:s.id=h.id),null==s.next&&(s.next={}),s.next[n]=e.ObjectExt.cloneDeep(h.prop(n)),this.push(r,o)}const c=this.options.afterAddCommand;c&&e.FunctionExt.call(c,this,t,i,r),this.push(r,o)}initBatchCommand(t){this.freezed||(this.batchCommands?this.batchLevel+=1:(this.batchCommands=[this.createCommand({batch:!0})],this.batchLevel=0,this.lastBatchIndex=-1))}storeBatchCommand(t){if(!this.freezed)if(this.batchCommands&&this.batchLevel<=0){const e=this.filterBatchCommand(this.batchCommands);e.length>0&&(this.redoStack=[],this.undoStackPush(e),this.consolidateCommands(),this.notify("add",e,t)),this.batchCommands=null,this.lastBatchIndex=-1,this.batchLevel=0}else this.batchCommands&&this.batchLevel>0&&(this.batchLevel-=1)}filterBatchCommand(t){let i=t.slice();const n=[];for(;i.length>0;){const t=i.shift(),o=t.event,a=t.data.id;if(null!=o&&(null!=a||t.modelChange)){if(s.isAddEvent(o)){const t=i.findIndex((t=>s.isRemoveEvent(t.event)&&t.data.id===a));if(t>=0){i=i.filter(((e,i)=>t<i||e.data.id!==a));continue}}else if(s.isRemoveEvent(o)){const t=i.findIndex((t=>s.isAddEvent(t.event)&&t.data.id===a));if(t>=0){i.splice(t,1);continue}}else if(s.isChangeEvent(o)){const i=t.data;if(e.ObjectExt.isEqual(i.prev,i.next))continue}n.push(t)}}return n}notify(t,e,i){const n=null==e?null:Array.isArray(e)?e:[e];this.emit(t,{cmds:n,options:i}),this.graph.trigger(`history:${t}`,{cmds:n,options:i}),this.emit("change",{cmds:n,options:i}),this.graph.trigger("history:change",{cmds:n,options:i})}push(t,e){this.redoStack=[],t.batch?(this.lastBatchIndex=Math.max(this.lastBatchIndex,0),this.emit("batch",{cmd:t,options:e})):(this.undoStackPush(t),this.consolidateCommands(),this.notify("add",t,e))}consolidateCommands(){var t;const e=this.undoStack[this.undoStack.length-1],i=this.undoStack[this.undoStack.length-2];if(!Array.isArray(e))return;const n=new Set(e.map((t=>t.event)));if(2!==n.size||!n.has("cell:change:parent")||!n.has("cell:change:children"))return;if(!e.every((t=>{var e;return t.batch&&(null===(e=t.options)||void 0===e?void 0:e.ui)})))return;if(!Array.isArray(i)||1!==i.length)return;const s=i[0];"cell:change:position"===s.event&&(null===(t=s.options)||void 0===t?void 0:t.ui)&&(i.push(...e),this.undoStack.pop())}undoStackPush(t){0!==this.stackSize?(this.undoStack.length>=this.stackSize&&this.undoStack.shift(),this.undoStack.push(t)):this.undoStack.push(t)}ensureUndefinedAttrs(t,e){let i=!1;return null!==t&&null!==e&&"object"==typeof t&&"object"==typeof e&&Object.keys(e).forEach((n=>{void 0===t[n]&&void 0!==e[n]?(t[n]=void 0,i=!0):"object"==typeof t[n]&&"object"==typeof e[n]&&(i=this.ensureUndefinedAttrs(t[n],e[n]))})),i}dispose(){this.validator.dispose(),this.clean(),this.stopListening(),this.off()}}var s;i([e.Basecoat.dispose()],n.prototype,"dispose",null),function(t){class n extends e.Basecoat{constructor(t){super(),this.map={},this.command=t.history,this.cancelInvalid=!1!==t.cancelInvalid,this.command.on("add",this.onCommandAdded,this)}onCommandAdded({cmds:t}){return Array.isArray(t)?t.every((t=>this.isValidCommand(t))):this.isValidCommand(t)}isValidCommand(t){if(t.options&&!1===t.options.validation)return!0;const e=t.event&&this.map[t.event]||[];let i=null;return e.forEach((e=>{let n=0;const s=o=>{const a=e[n];n+=1;try{if(!a)return void(i=o);a(o,t,s)}catch(o){s(o)}};s(i)})),!i||(this.cancelInvalid&&this.command.cancel(),this.emit("invalid",{err:i}),!1)}validate(t,...e){const i=Array.isArray(t)?t:t.split(/\s+/);return e.forEach((t=>{if("function"!=typeof t)throw new Error(`${i.join(" ")} requires callback functions.`)})),i.forEach((t=>{null==this.map[t]&&(this.map[t]=[]),this.map[t].push(e)})),this}dispose(){this.command.off("add",this.onCommandAdded,this)}}i([e.Basecoat.dispose()],n.prototype,"dispose",null),t.Validator=n}(n||(n={})),function(t){t.isAddEvent=function(t){return"cell:added"===t},t.isRemoveEvent=function(t){return"cell:removed"===t},t.isChangeEvent=function(t){return null!=t&&t.startsWith("cell:change:")},t.getOptions=function(e){const i=["cell:added","cell:removed","cell:change:*"],n=["batch:start","batch:stop"],s=e.eventNames?e.eventNames.filter((e=>!(t.isChangeEvent(e)||i.includes(e)||n.includes(e)))):i;return Object.assign(Object.assign({enabled:!0},e),{eventNames:s,applyOptionsList:e.applyOptionsList||["propertyPath"],revertOptionsList:e.revertOptionsList||["propertyPath"]})},t.sortBatchCommands=function(e){const i=[];for(let n=0,s=e.length;n<s;n+=1){const s=e[n];let o=null;if(t.isAddEvent(s.event)){const t=s.data.id;for(let i=0;i<n;i+=1)if(e[i].data.id===t){o=i;break}}null!==o?i.splice(o,0,s):i.push(s)}return i}}(s||(s={})),t.History=n}));