From: Vincent Vanwaelscappel Date: Fri, 17 Mar 2023 16:47:18 +0000 (+0100) Subject: wip #5814 @3 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=4f56b369c0066d8ae0b8f70fe9c9b607de53e72b;p=fluidbook-toolbox.git wip #5814 @3 --- diff --git a/resources/linkeditor/js/linkeditor.js b/resources/linkeditor/js/linkeditor.js index 87f6643a3..60dea6e08 100644 --- a/resources/linkeditor/js/linkeditor.js +++ b/resources/linkeditor/js/linkeditor.js @@ -19,6 +19,7 @@ import LinkeditorForm from './linkeditor.form'; import LinkeditorVersions from './linkeditor.versions'; import LinkeditorPopup from './linkeditor.popup'; import LinkeditorLayers from "./linkeditor.layers"; +import LinkeditorUndo from './linkeditor.undo'; window.$ = window.jQuery = require('jquery'); window.key = require('keymaster-reloaded'); @@ -100,6 +101,7 @@ LinkEditor.prototype = { this.versions = new LinkeditorVersions(this); this.layers = new LinkeditorLayers(this); this.popup = new LinkeditorPopup(this); + this.undo = new LinkeditorUndo(this); this.initEvents(); this.initIcons(); @@ -279,8 +281,14 @@ LinkEditor.prototype = { i.select(); }, - hasChanged: function () { + hasChanged: function (push) { + if (push === undefined) { + push = true; + } this.save.hasChanged(); + if (push === true) { + this.undo.pushState(); + } this.updateFBElements(true); }, @@ -502,7 +510,11 @@ LinkEditor.prototype = { text: text, timeout: timeout, }).show(); - } + }, + + getCurrentPage: function () { + return this.currentPage; + }, } diff --git a/resources/linkeditor/js/linkeditor.links.js b/resources/linkeditor/js/linkeditor.links.js index 170e81877..fa17ad9c1 100644 --- a/resources/linkeditor/js/linkeditor.links.js +++ b/resources/linkeditor/js/linkeditor.links.js @@ -321,7 +321,7 @@ LinkeditorLinks.prototype = { data.top += offset.y; data.left += offset.x; - $this.selectLink($this.addLink(data)); + $this.selectLink($this.addLink(data, false)); }); this.linkeditor.hasChanged(); @@ -469,6 +469,7 @@ LinkeditorLinks.prototype = { }); this.linkeditor.form.updateLinkForm(); this.resizeLinkPos = null; + this.linkeditor.hasChanged(); }, moveResizeLink: function () { @@ -622,6 +623,7 @@ LinkeditorLinks.prototype = { } this.moveDragLink(); this.dragLinkPos = null; + this.linkeditor.hasChanged(); }, moveDragLink: function () { @@ -762,25 +764,30 @@ LinkeditorLinks.prototype = { link.page++; link.left -= $this.linkeditor.pw; } - if (link.page != page) { - return; - } if (side === 'right') { link.page--; link.left = parseInt(link.left) + $this.linkeditor.pw; } - $this.addLink(link); + if (link.page != page) { + return; + } + + $this.addLink(link, false); }); this.updateLayers(); + this.linkeditor.undo.initState(); }, - addLink: function (link) { + + addLink: function (link, triggerChange) { + if (triggerChange === undefined) { + triggerChange = true; + } let change = false; if (link.uid === undefined) { link.uid = this.linkeditor.utils.generateUID(); change = true; } else if (!this.hasUIDLink(link.uid)) { - console.log('::)', link); change = true; } if (change) { @@ -799,7 +806,7 @@ LinkeditorLinks.prototype = { let e = $(''); $(e).attr(attrs); $("#linkeditor-links").append(e); - if (change) { + if (triggerChange && change) { this.linkeditor.rulers.updateMagnetValues(); this.linkeditor.hasChanged(); } @@ -844,7 +851,7 @@ LinkeditorLinks.prototype = { }, duplicateLinkClick: function () { - let link = this.addLink(this._duplicateLink()); + let link = this.addLink(this._duplicateLink(), true); this.deselectAllLinks(); this.selectLink($(link)); @@ -858,7 +865,7 @@ LinkeditorLinks.prototype = { if (overwriteData !== undefined) { $.extend(data, overwriteData); } - let link = this.addLink(data); + let link = this.addLink(data, false); this.deselectAllLinks(); this.selectLink($(link)); @@ -1007,7 +1014,7 @@ LinkeditorLinks.prototype = { var b = axis === 'x' ? 'left' : 'top'; var l = axis === 'x' ? 'width' : 'height'; - var max = -10000000; + var max = -100000000; var min = 100000000; this.getCurrentSelection().each(function () { min = Math.min(min, parseFloat($(this).attr('fb-' + b))); @@ -1038,7 +1045,29 @@ LinkeditorLinks.prototype = { } link.attr('fb-left', rect.x).attr('fb-top', rect.y).attr('fb-width', rect.width).attr('fb-height', rect.height); this.linkeditor.hasChanged(); - } + }, + + getCurrentState: function () { + var data = []; + $(".link:not(.pendingCreate):not([fb-width=0]):not([fb-height=0])").each(function () { + data.push(LINKS[$(this).attr('fb-uid')]); + }); + data.sort(function (a, b) { + return a.toString().localeCompare(b.toString()); + }); + return JSON.stringify(data); + }, + + setCurrentState: function (state) { + let links = JSON.parse(state); + this.clear(); + var $this = this; + $.each(links, function (k, link) { + $this.addLink(link, false); + LINKS[link.uid] = link; + }); + this.linkeditor.hasChanged(false); + }, }; module.exports = LinkeditorLinks; diff --git a/resources/linkeditor/js/linkeditor.undo.js b/resources/linkeditor/js/linkeditor.undo.js new file mode 100644 index 000000000..0e1a4d7e6 --- /dev/null +++ b/resources/linkeditor/js/linkeditor.undo.js @@ -0,0 +1,81 @@ +function LinkeditorUndo(linkeditor) { + this.linkeditor = linkeditor; + this.ignoreStatesChanges = false; + this.states = []; + this.indexes = []; + this.init(); +} + +LinkeditorUndo.prototype = { + init: function () { + }, + + initState: function () { + if (this.states[this.linkeditor.getCurrentPage()] === undefined || this.states[this.linkeditor.getCurrentPage()] === null) { + this.indexes[this.linkeditor.getCurrentPage()] = 0; + this.pushState(); + } + }, + + pushState: function () { + if (this.ignoreStatesChanges) { + return; + } + + let index = this.indexes[this.linkeditor.getCurrentPage()]; + if (index === 0) { + this.states[this.linkeditor.getCurrentPage()] = []; + } + + let cs = this.linkeditor.links.getCurrentState(); + let ps = this.states[this.linkeditor.getCurrentPage()][index - 1]; + if (ps == cs) { + return; + } + if (index > 0 && index < this.states[this.linkeditor.getCurrentPage()].length) { + this.states[this.linkeditor.getCurrentPage()] = this.states[this.linkeditor.getCurrentPage()].slice(0, index); + } + this.states[this.linkeditor.getCurrentPage()].push(cs); + this.indexes[this.linkeditor.getCurrentPage()]++; + + console.log('push current index', index, 'states length', this.states[this.linkeditor.getCurrentPage()].length); + }, + + undo: function () { + let index = this.indexes[this.linkeditor.getCurrentPage()]; + if (index <= 1) { + return; + } + index--; + let state = this.states[this.linkeditor.getCurrentPage()][index - 1]; + this.ignoreStatesChanges = true; + this.linkeditor.links.setCurrentState(state); + var $this = this; + setTimeout(function () { + $this.ignoreStatesChanges = false; + }, 500); + this.indexes[this.linkeditor.getCurrentPage()] = index; + + console.log('undo : current index', index, 'states length', this.states[this.linkeditor.getCurrentPage()].length); + }, + + redo: function () { + let index = this.indexes[this.linkeditor.getCurrentPage()]; + if (index >= this.states[this.linkeditor.getCurrentPage()].length) { + return; + } + let state = this.states[this.linkeditor.getCurrentPage()][index]; + this.ignoreStatesChanges = true; + this.linkeditor.links.setCurrentState(state); + var $this = this; + setTimeout(function () { + $this.ignoreStatesChanges = false; + }, 500); + index++; + this.indexes[this.linkeditor.getCurrentPage()] = index; + console.log('redo : current index', index, 'states length', this.states[this.linkeditor.getCurrentPage()].length); + } +} + + +module.exports = LinkeditorUndo; diff --git a/resources/views/fluidbook_publication/link_editor.blade.php b/resources/views/fluidbook_publication/link_editor.blade.php index 1618fadee..e007ccd6b 100644 --- a/resources/views/fluidbook_publication/link_editor.blade.php +++ b/resources/views/fluidbook_publication/link_editor.blade.php @@ -147,6 +147,12 @@