editor: add undo/redo 🎉

This commit is contained in:
bordun 2018-08-23 14:16:02 +02:00
parent 8dfa652eb5
commit b75114100c
9 changed files with 193 additions and 1 deletions

View file

@ -1,5 +1,10 @@
# Changelog
## 0.37.0
- editor
- **new** keyboard shortcuts (undo, redo, copy, paste, etc)
## 0.36.0
- bug fixes

View file

@ -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",

View file

@ -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`)

View file

@ -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.

View file

@ -163,6 +163,7 @@ var handleClick = function(event) {
'<i class="fa fa-plus"></i> 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()
}
}

View file

@ -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()
}
})

View file

@ -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')

View file

@ -50,6 +50,8 @@ var SessionManager = class SessionManager {
}
parser.parse(this.session, DOM.get('#container')[0])
editor.clearHistory()
} catch (err) {
loader.close()
lobby.open()

View file

@ -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) {