From 3d6f6dc7c53b4eb3437a192b073c802a5e092685 Mon Sep 17 00:00:00 2001 From: bordun Date: Thu, 23 Aug 2018 14:16:02 +0200 Subject: [PATCH] editor: add undo/redo :tada: --- CHANGELOG.md | 5 + package.json | 1 + resources/docs/editing-sessions.md | 16 +++ resources/docs/extras/remote-control.md | 8 ++ src/browser/js/app/editor/context-menu.js | 7 ++ src/browser/js/app/editor/edit-field.js | 3 + src/browser/js/app/editor/index.js | 131 +++++++++++++++++++++- src/browser/js/app/managers/session.js | 2 + src/browser/js/app/remote-control.js | 21 ++++ 9 files changed, 193 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aac1892f..9904c1f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.37.0 + +- editor + - **new** keyboard shortcuts (undo, redo, copy, paste, etc) + ## 0.36.0 - bug fixes diff --git a/package.json b/package.json index 0f4771e6..23fc01f5 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "babelify": "8.0.0", "browserify": "16.2.2", "cardinal-spline-js": "2.3.9", + "deep-diff": "^1.0.2", "deep-extend": "^0.6.0", "electron": "~1.8.7", "electron-packager": "12.1.0", diff --git a/resources/docs/editing-sessions.md b/resources/docs/editing-sessions.md index 2ec2f6ab..97486078 100644 --- a/resources/docs/editing-sessions.md +++ b/resources/docs/editing-sessions.md @@ -52,3 +52,19 @@ When holding the `ctrl` key (or `command` on Mac), multiple widgets can be selec - they must share the same parent widget - context-menu actions and properties changes apply to all selected widgets - resizing / dragging will affect all selected widgets, relatively to the first selected widget + +## Keyboard shortcuts + +| Shortcut | Description | +|---|---| +| `mod + z` | undo | +| `mod + y / mod + shift + z` | redo | +| `delete` | delete selected widgets | +| `mod + c` | copy selected widgets | +| `mod + x` | cut selected widgets | +| `mod + v` | paste clipboard in selected widget | +| `mod + shift + v` | paste and increment id | +| `up, down, left, right` | move selected widgets (1 grid unit, hold `mod` for 5 grid units) | +| `alt + [up, down, left, right]` | resize selected widgets (1 grid unit, hold `mod` for 5 grid units) | + +Where `mod` is `ctrl` except on MacOs (`command`) diff --git a/resources/docs/extras/remote-control.md b/resources/docs/extras/remote-control.md index 8d67a3a3..6d2fa4c5 100644 --- a/resources/docs/extras/remote-control.md +++ b/resources/docs/extras/remote-control.md @@ -28,6 +28,14 @@ Apply a set of options to an existing widget by merging them to the widget's opt !!! warning "" Editing a widget can be cpu expensive, hence updating the UI continuously is not a good idea +## `/EDIT/UNDO` + +Undo editing action + +## `/EDIT/REDO` + +Redo editing action + ## `/EDIT/GET target id` Sends back a widget's data (JSON stringified object), including its children, to specified target. diff --git a/src/browser/js/app/editor/context-menu.js b/src/browser/js/app/editor/context-menu.js index 088f940a..6b5bb759 100644 --- a/src/browser/js/app/editor/context-menu.js +++ b/src/browser/js/app/editor/context-menu.js @@ -163,6 +163,7 @@ var handleClick = function(event) { ' Add tab': function(){ data[0].tabs.push({}) updateWidget(widget) + editor.pushHistory() } }) @@ -212,6 +213,8 @@ var handleClick = function(event) { parent.props.widgets = parent.props.widgets.slice(0, i).concat(wrap, parent.props.widgets.slice(i, parent.props.widgets.length)) editor.select(updateWidget(parent, {preventSelect: true})) + editor.pushHistory() + } } @@ -261,6 +264,8 @@ var handleClick = function(event) { data[0].widgets = data[0].widgets || [] data[0].widgets.push(newData) updateWidget(editor.selectedWidgets[0]) + editor.pushHistory() + } } @@ -275,6 +280,8 @@ var handleClick = function(event) { data[0].tabs = data[0].tabs || [] data[0].tabs.push({}) updateWidget(editor.selectedWidgets[0]) + editor.pushHistory() + } } diff --git a/src/browser/js/app/editor/edit-field.js b/src/browser/js/app/editor/edit-field.js index 0b433bf7..c3641624 100644 --- a/src/browser/js/app/editor/edit-field.js +++ b/src/browser/js/app/editor/edit-field.js @@ -93,6 +93,7 @@ module.exports = function editField(editor, widget, propName, defaultValue){ for (var w of editor.selectedWidgets) { w.props[propName] = v !== '' ? v : JSON.parse(JSON.stringify(defaultValue)) newWidgets.push(updateWidget(w, {preventSelect: editor.selectedWidgets.length > 1})) + editor.pushHistory() } if (newWidgets.length > 1) editor.select(newWidgets) @@ -150,6 +151,7 @@ module.exports = function editField(editor, widget, propName, defaultValue){ if (change) { updateWidget(widget) + editor.pushHistory() } }) @@ -165,6 +167,7 @@ module.exports = function editField(editor, widget, propName, defaultValue){ widget.props[propName].splice(index, 0, widget.props[propName].splice(oldindex, 1)[0]) updateWidget(widget) + editor.pushHistory() } }) diff --git a/src/browser/js/app/editor/index.js b/src/browser/js/app/editor/index.js index 0685103d..bdaf38c5 100644 --- a/src/browser/js/app/editor/index.js +++ b/src/browser/js/app/editor/index.js @@ -2,7 +2,12 @@ var {widgets} = require('../widgets/'), {Popup} = require('../ui/utils'), editField = require('./edit-field'), {updateWidget, incrementWidget} = require('./data-workers'), - keyboardJS = require('keyboardjs') + keyboardJS = require('keyboardjs'), + diff = require('deep-diff'), + widgetManager = require('../managers/widgets'), + sessionManager + +const HISTORY_SIZE = 50 var Editor = class Editor { @@ -32,7 +37,20 @@ var Editor = class Editor { if (this.enabledOnce) return true } + + this.history = [] + this.historyState = -1 + this.historySession = null + keyboardJS.withContext('editing', ()=>{ + keyboardJS.bind('mod + z', (e)=>{ + if (e.target.classList.contains('input')) return + this.undo() + }) + keyboardJS.bind('mod + y, mod + shift + z', (e)=>{ + if (e.target.classList.contains('input')) return + this.redo() + }) keyboardJS.bind('mod + c', (e)=>{ if (e.target.classList.contains('input')) return this.copyWidget() @@ -375,6 +393,7 @@ var Editor = class Editor { } this.select(updateWidget(parent, {preventSelect: true})) + this.pushHistory() } @@ -415,6 +434,7 @@ var Editor = class Editor { data[0].widgets = data[0].widgets.concat(pastedData) updateWidget(this.selectedWidgets[0]) + this.pushHistory() } @@ -445,6 +465,7 @@ var Editor = class Editor { data[0].widgets.push(clone) updateWidget(this.selectedWidgets[0]) + this.pushHistory() } @@ -482,6 +503,7 @@ var Editor = class Editor { } this.select(updateWidget(parent, {preventSelect: true})) + this.pushHistory() }) @@ -528,6 +550,8 @@ var Editor = class Editor { } if (w.props.width !== undefined || w.props.height !== undefined) newWidgets.push(updateWidget(w, {preventSelect: this.selectedWidgets.length > 1})) + this.pushHistory() + } if (newWidgets.length > 1) this.select(newWidgets, {preventSelect: this.selectedWidgets.length > 1}) @@ -554,12 +578,116 @@ var Editor = class Editor { } newWidgets.push(updateWidget(w, {preventSelect: this.selectedWidgets.length > 1})) + this.pushHistory() + } if (newWidgets.length > 1) this.select(newWidgets) } + + + pushHistory() { + + if (this.historyState > -1) { + this.history.splice(0, this.historyState + 1) + this.historyState = -1 + } + + var d = diff.diff(this.historySession, sessionManager.session) + + if (d) { + for (var c of JSON.parse(JSON.stringify(d))) { + diff.applyChange(this.historySession, null, c) + } + this.history.unshift(JSON.parse(JSON.stringify(d))) + if (this.history.length > HISTORY_SIZE) this.history.pop() + } + + } + + clearHistory() { + + this.history = [] + this.historyState = -1 + this.historySession = JSON.parse(JSON.stringify(sessionManager.session)) + + } + + undo() { + + if (this.historyState === this.history.length - 1) return + + this.historyState += 1 + + var d1 = JSON.parse(JSON.stringify(this.history[this.historyState])), + d2 = JSON.parse(JSON.stringify(this.history[this.historyState])), + path, pathLength + + for (var i = d1.length - 1; i > -1; i--) { + diff.revertChange(this.historySession, true, d1[i]) + diff.revertChange(sessionManager.session, true, d2[i]) + if (!path || path.length > d1[i].path.length) path = d1[i].path + } + + this.updateWidgetByPath(path) + + } + + redo() { + + if (this.historyState === -1) return + + var d1 = JSON.parse(JSON.stringify(this.history[this.historyState])), + d2 = JSON.parse(JSON.stringify(this.history[this.historyState])), + path, pathLength + + for (var i = d1.length - 1; i > -1; i--) { + diff.applyChange(this.historySession, true, d1[i]) + diff.applyChange(sessionManager.session, true, d2[i]) + if (!path || path.length > d1[i].path.length) path = d1[i].path + } + + this.updateWidgetByPath(path) + + this.historyState -= 1 + + } + + updateWidgetByPath(path) { + + var req = '.root-container' + for (var j = 1; j < path.length; j++) { + var item = path[j] + if (item === 'widgets' && j + 1 < path.length) { + req += ' > .panel' + } else if (item === 'tabs' && j + 1 < path.length) { + req += ' > .panel > .tabs-wrapper' + } else if (!isNaN(item)) { + req += ' > .widget:nth-child(' + (item + 1) + ')' + } else { + break + } + } + + var e = DOM.get(req), + w + + if (e.length) { + w = widgetManager.getWidgetByElement(e[0]) + } else { + // in case the elements are in a hidden tab (detached dom) + w = widgetManager.getWidgetById('root')[0] + } + + updateWidget(w) + + } + + + + } var editor = new Editor() @@ -567,3 +695,4 @@ var editor = new Editor() module.exports = editor require('./context-menu') +sessionManager = require('../managers/session') diff --git a/src/browser/js/app/managers/session.js b/src/browser/js/app/managers/session.js index 887c5945..8e4fffa8 100644 --- a/src/browser/js/app/managers/session.js +++ b/src/browser/js/app/managers/session.js @@ -50,6 +50,8 @@ var SessionManager = class SessionManager { } parser.parse(this.session, DOM.get('#container')[0]) + editor.clearHistory() + } catch (err) { loader.close() lobby.open() diff --git a/src/browser/js/app/remote-control.js b/src/browser/js/app/remote-control.js index dff356a9..672cd242 100644 --- a/src/browser/js/app/remote-control.js +++ b/src/browser/js/app/remote-control.js @@ -1,4 +1,5 @@ var {updateWidget} = require('./editor/data-workers'), + editor = require('./editor'), widgetManager = require('./managers/widgets'), deepExtend = require('deep-extend') @@ -25,6 +26,9 @@ var callbacks = { updateWidget(widget, {remote: true}) } + + editor.pushHistory() + }, '/EDIT/MERGE': function(args) { @@ -46,6 +50,23 @@ var callbacks = { updateWidget(widget, {remote: true}) } + + editor.pushHistory() + + }, + '/EDIT/UNDO': function(args) { + + if (READ_ONLY) return + + editor.undo() + + }, + '/EDIT/REDO': function(args) { + + if (READ_ONLY) return + + editor.redo() + }, '/EDIT/GET': function(args) {