deepCopy: modularize and use everywhere

This commit is contained in:
jean-emmanuel 2018-08-23 21:06:27 +02:00
parent 2451efde81
commit 89dbc88a86
5 changed files with 20 additions and 36 deletions

View file

@ -5,6 +5,7 @@ var {widgets} = require('../widgets/'),
keyboardJS = require('keyboardjs'),
diff = require('deep-diff'),
widgetManager = require('../managers/widgets'),
{deepCopy} = require('../utils'),
sessionManager
const HISTORY_SIZE = 50
@ -602,10 +603,10 @@ var Editor = class Editor {
var d = diff.diff(this.historySession, sessionManager.session)
if (d) {
for (var c of JSON.parse(JSON.stringify(d))) {
for (var c of deepCopy(d)) {
diff.applyChange(this.historySession, null, c)
}
this.history.unshift(JSON.parse(JSON.stringify(d)))
this.history.unshift(deepCopy(d))
if (this.history.length > HISTORY_SIZE) this.history.pop()
}
@ -615,7 +616,7 @@ var Editor = class Editor {
this.history = []
this.historyState = -1
this.historySession = JSON.parse(JSON.stringify(sessionManager.session))
this.historySession = deepCopy(sessionManager.session)
}
@ -625,8 +626,8 @@ var Editor = class Editor {
this.historyState += 1
var d1 = JSON.parse(JSON.stringify(this.history[this.historyState])),
d2 = JSON.parse(JSON.stringify(this.history[this.historyState])),
var d1 = deepCopy(this.history[this.historyState]),
d2 = deepCopy(this.history[this.historyState]),
path
for (var i = d1.length - 1; i > -1; i--) {
@ -660,8 +661,8 @@ var Editor = class Editor {
if (this.historyState === -1) return
var d1 = JSON.parse(JSON.stringify(this.history[this.historyState])),
d2 = JSON.parse(JSON.stringify(this.history[this.historyState])),
var d1 = deepCopy(this.history[this.historyState]),
d2 = deepCopy(this.history[this.historyState]),
path
for (var i = 0; i < d1.length; i++) {

View file

@ -7,6 +7,7 @@ var EventEmitter = require('../../events/event-emitter'),
{iconify} = require('../../ui/utils'),
resize = require('../../events/resize'),
OscReceiver = require('./osc-receiver'),
{deepCopy} = require('../../utils'),
updateWidget = ()=>{}
@ -219,36 +220,15 @@ class Widget extends EventEmitter {
getValue(withPrecision) {
return Widget.deepCopy(this.value, withPrecision ? this.precision : undefined)
return deepCopy(this.value, withPrecision ? this.precision : undefined)
}
getSplit() {}
static deepCopy(obj, precision){
var copy = obj
if (obj === null) {
return obj
}
if (typeof obj === 'object') {
copy = Array.isArray(obj) ? [] : {}
for (let key in obj) {
copy[key] = Widget.deepCopy(obj[key], precision)
}
} else if (typeof obj == 'number') {
return precision === undefined ? copy : parseFloat(copy.toFixed(precision))
}
return copy
}
resolveProp(propName, propValue, storeLinks=true, originalWidget, originalPropName, context) {
propValue = propValue !== undefined ? propValue : Widget.deepCopy(this.props[propName])
propValue = propValue !== undefined ? propValue : deepCopy(this.props[propName])
originalWidget = originalWidget || this
originalPropName = originalPropName || propName

View file

@ -1,7 +1,8 @@
var Widget = require('../common/widget'),
widgetManager = require('../../managers/widgets'),
resize = require('../../events/resize'),
parser = require('../../parser')
parser = require('../../parser'),
{deepCopy} = require('../../utils')
var excludedCloneClasses = ['widget', 'absolute-position', 'ui-resizable', 'ui-draggable', 'not-editable']
@ -119,7 +120,7 @@ class Clone extends Widget {
this.container.classList.remove('empty')
this.container.classList.add(...this.cloneClass)
var clone = parser.parse([{...Widget.deepCopy(this.cloneTarget.props), ...this.getProp('props')}], this.widget, this)
var clone = parser.parse([{...deepCopy(this.cloneTarget.props), ...this.getProp('props')}], this.widget, this)
if (clone.getProp('id') === this.cloneTarget.getProp('id')) {
widgetManager.trigger('change.*', [{

View file

@ -1,5 +1,6 @@
var Widget = require('../common/widget'),
Canvas = require('../common/canvas')
Canvas = require('../common/canvas'),
{deepCopy} = require('../../utils')
module.exports = class Input extends Canvas {
@ -173,7 +174,7 @@ module.exports = class Input extends Canvas {
getStringValue() {
if (this.value === undefined) return ''
return typeof this.value != 'string' ?
JSON.stringify(Widget.deepCopy(this.value, this.precision)).replace(/,/g, ', ') :
JSON.stringify(deepCopy(this.value, this.precision)).replace(/,/g, ', ') :
this.value
}

View file

@ -1,6 +1,7 @@
var {mapToScale} = require('../utils'),
Widget = require('../common/widget'),
Canvas = require('../common/canvas')
Canvas = require('../common/canvas'),
{deepCopy} = require('../../utils')
module.exports = class _plots_base extends Canvas {
@ -203,7 +204,7 @@ module.exports = class _plots_base extends Canvas {
getValue() {
return Widget.deepCopy(this.data)
return deepCopy(this.data)
}