History of actions (CTRL+Z, CTRL+SHIFT+Z)! Fixing a lot of bugs in the process
This commit is contained in:
parent
d0a4494038
commit
a2ff0da4cf
10 changed files with 311 additions and 36 deletions
128
qml/js/historylib.js
Normal file
128
qml/js/historylib.js
Normal file
|
@ -0,0 +1,128 @@
|
|||
/**
|
||||
* Logarithm Graph Creator - Create graphs with logarithm scales.
|
||||
* Copyright (C) 2020 Ad5001
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// This library helps containing actions to be undone or redone (in other words, editing history)
|
||||
// Each type of event is repertoried as an action that can be listed for everything that's undoable.
|
||||
.pragma library
|
||||
|
||||
.import "objects.js" as Objects
|
||||
.import "utils.js" as Utils
|
||||
|
||||
class Action {
|
||||
// Type of the action done.
|
||||
static type(){return 'Unknown'}
|
||||
|
||||
// TargetName is the name of the object that's targeted by the event.
|
||||
constructor(targetName = "", targetType = "Point") {
|
||||
this.targetName = targetName
|
||||
this.targetType = targetType
|
||||
}
|
||||
|
||||
undo() {}
|
||||
|
||||
redo() {}
|
||||
|
||||
getReadableString() {
|
||||
return 'Unknown action'
|
||||
}
|
||||
}
|
||||
|
||||
class CreateNewObject extends Action {
|
||||
// Action used for the creation of an object
|
||||
static type(){return 'CreateNewObject'}
|
||||
|
||||
constructor(targetName = "", targetType = "Point", properties = []) {
|
||||
super(targetName, targetType)
|
||||
this.targetProperties = properties
|
||||
}
|
||||
|
||||
undo() {
|
||||
var targetIndex = Objects.getObjectsName(this.targetType).indexOf(this.targetName)
|
||||
Objects.currentObjects[this.targetType][targetIndex].delete()
|
||||
Objects.currentObjects[this.targetType].splice(targetIndex, 1)
|
||||
}
|
||||
|
||||
redo() {
|
||||
Objects.createNewRegisteredObject(this.targetType, this.targetProperties)
|
||||
}
|
||||
|
||||
getReadableString() {
|
||||
return `New ${this.targetType} ${this.targetName} created.`
|
||||
}
|
||||
}
|
||||
|
||||
class DeleteObject extends CreateNewObject {
|
||||
// Action used at the deletion of an object. Basicly the same thing as creating a new object, except Redo & Undo are reversed.
|
||||
static type(){return 'DeleteObject'}
|
||||
|
||||
undo() {
|
||||
super.redo()
|
||||
}
|
||||
|
||||
redo() {
|
||||
super.undo()
|
||||
}
|
||||
|
||||
getReadableString() {
|
||||
return `${this.targetType} ${this.targetName} deleted.`
|
||||
}
|
||||
}
|
||||
|
||||
class EditedProperty extends Action {
|
||||
// Action used everytime an object's property has been changed
|
||||
static type(){return 'EditedProperty'}
|
||||
|
||||
constructor(targetName = "", targetType = "Point", targetProperty = "visible", previousValue = false, newValue = true) {
|
||||
super(targetName, targetType)
|
||||
this.targetProperty = targetProperty
|
||||
this.targetPropertyReadable = Utils.camelCase2readable(this.targetProperty)
|
||||
this.previousValue = previousValue
|
||||
this.newValue = newValue
|
||||
}
|
||||
|
||||
undo() {
|
||||
Objects.getObjectByName(this.targetName, this.targetType)[this.targetProperty] = this.previousValue
|
||||
}
|
||||
|
||||
redo() {
|
||||
Objects.getObjectByName(this.targetName, this.targetType)[this.targetProperty] = this.newValue
|
||||
}
|
||||
|
||||
getReadableString() {
|
||||
var prev = this.previousValue == null ? ""+this.previousValue : this.previousValue.toString()
|
||||
var next = this.newValue == null ? ""+this.newValue : this.newValue.toString()
|
||||
return `${this.targetPropertyReadable} of ${this.targetType} ${this.targetName} changed from "${prev}" to "${next}".`
|
||||
}
|
||||
}
|
||||
|
||||
class EditedVisibility extends EditedProperty {
|
||||
// Action used everytime an object's property has been changed
|
||||
static type(){return 'EditedVisibility'}
|
||||
|
||||
constructor(targetName = "", targetType = "Point", newValue = true) {
|
||||
super(targetName, targetType, "visible", !newValue, newValue)
|
||||
}
|
||||
|
||||
getReadableString() {
|
||||
if(this.newValue) {
|
||||
return `${this.targetType} ${this.targetName} shown.`
|
||||
} else {
|
||||
return `${this.targetType} ${this.targetName} hidden.`
|
||||
}
|
||||
}
|
||||
}
|
|
@ -580,7 +580,7 @@ class MinusDomain extends Domain {
|
|||
static import(frm) {
|
||||
var domains = frm.trim().split("∖")
|
||||
if(domains.length == 1) domains = frm.trim().split("\\") // Fallback
|
||||
var dom1 = parseDomain(domains.pop())
|
||||
var dom1 = parseDomain(domains.shift())
|
||||
var dom2 = parseDomain(domains.join('∪'))
|
||||
return new MinusDomain(dom1, dom2)
|
||||
}
|
||||
|
|
|
@ -22,6 +22,8 @@
|
|||
.import "mathlib.js" as MathLib
|
||||
.import "parameters.js" as P
|
||||
|
||||
var history = null
|
||||
var HistoryLib = null
|
||||
|
||||
function getNewName(allowedLetters) {
|
||||
var newid = 0
|
||||
|
@ -375,6 +377,7 @@ class GainBode extends ExecutableObject {
|
|||
om_0.name = getNewName('ω')
|
||||
om_0.labelContent = 'name'
|
||||
om_0.color = this.color
|
||||
history.addToHistory(new HistoryLib.CreateNewObject(om_0.name, 'Point', om_0.export()))
|
||||
labelPosition = 'below'
|
||||
}
|
||||
om_0.requiredBy.push(this)
|
||||
|
@ -654,6 +657,7 @@ class PhaseBode extends ExecutableObject {
|
|||
om_0.color = this.color
|
||||
om_0.labelContent = 'name'
|
||||
om_0.labelPosition = this.phase.execute() >= 0 ? 'bottom' : 'top'
|
||||
history.addToHistory(new HistoryLib.CreateNewObject(om_0.name, 'Point', om_0.export()))
|
||||
labelPosition = 'below'
|
||||
}
|
||||
om_0.requiredBy.push(this)
|
||||
|
@ -886,14 +890,9 @@ class CursorX extends DrawableObject {
|
|||
static type(){return 'X Cursor'}
|
||||
static typeMultiple(){return 'X Cursors'}
|
||||
static properties() {
|
||||
var elementTypes = Object.keys(currentObjects).filter(objType => types[objType].prototype instanceof ExecutableObject)
|
||||
var elementNames = ['']
|
||||
elementTypes.forEach(function(elemType){
|
||||
elementNames = elementNames.concat(currentObjects[elemType].map(obj => obj.name))
|
||||
})
|
||||
return {
|
||||
'x': 'Expression',
|
||||
'targetElement': new P.Enum(...elementNames),
|
||||
'targetElement': new P.ObjectType('ExecutableObject'),
|
||||
'labelPosition': new P.Enum('left', 'right'),
|
||||
'approximate': 'Boolean',
|
||||
'rounding': 'number',
|
||||
|
@ -917,6 +916,9 @@ class CursorX extends DrawableObject {
|
|||
if(typeof x == 'number' || typeof x == 'string') x = new MathLib.Expression(x.toString())
|
||||
this.x = x
|
||||
this.targetElement = targetElement
|
||||
if(typeof targetElement == "string") {
|
||||
this.targetElement = getObjectByName(targetElement, elementTypes)
|
||||
}
|
||||
this.labelPosition = labelPosition
|
||||
this.displayStyle = displayStyle
|
||||
this.targetValuePosition = targetValuePosition
|
||||
|
@ -924,17 +926,17 @@ class CursorX extends DrawableObject {
|
|||
|
||||
export() {
|
||||
return [this.name, this.visible, this.color.toString(), this.labelContent,
|
||||
this.x.toEditableString(), this.targetElement, this.labelPosition,
|
||||
this.x.toEditableString(), this.targetElement == null ? null : this.targetElement.name, this.labelPosition,
|
||||
this.approximate, this.rounding, this.displayStyle, this.targetValuePosition]
|
||||
}
|
||||
|
||||
getReadableString() {
|
||||
if(this.getTargetElement() == null) return `${this.name} = ${this.x.toString()}`
|
||||
if(this.targetElement == null) return `${this.name} = ${this.x.toString()}`
|
||||
return `${this.name} = ${this.x.toString()}\n${this.getTargetValueLabel()}`
|
||||
}
|
||||
|
||||
getTargetValueLabel() {
|
||||
var t = this.getTargetElement()
|
||||
var t = this.targetElement
|
||||
var approx = ''
|
||||
if(this.approximate) {
|
||||
approx = t.execute(this.x.execute())
|
||||
|
@ -944,12 +946,6 @@ class CursorX extends DrawableObject {
|
|||
(this.approximate ? ' ≈ ' + approx : '')
|
||||
}
|
||||
|
||||
getTargetElement() {
|
||||
// TODO: Use the dependency system instead.
|
||||
var elementTypes = Object.keys(currentObjects).filter(objType => types[objType].prototype instanceof ExecutableObject)
|
||||
return getObjectByName(this.targetElement, elementTypes)
|
||||
}
|
||||
|
||||
getLabel() {
|
||||
switch(this.labelContent) {
|
||||
case 'name':
|
||||
|
@ -1005,10 +1001,10 @@ class CursorX extends DrawableObject {
|
|||
break;
|
||||
}
|
||||
|
||||
if(this.targetValuePosition == 'Next to target' && this.getTargetElement() != null) {
|
||||
if(this.targetValuePosition == 'Next to target' && this.targetElement != null) {
|
||||
var text = this.getTargetValueLabel()
|
||||
var textSize = canvas.measureText(ctx, text)
|
||||
var ypox = canvas.y2px(this.getTargetElement().execute(this.x.execute()))
|
||||
var ypox = canvas.y2px(this.targetElement.execute(this.x.execute()))
|
||||
switch(this.labelPosition) {
|
||||
case 'left':
|
||||
canvas.drawVisibleText(ctx, text, xpos-textSize.width-5, ypox+textSize.height)
|
||||
|
@ -1303,8 +1299,11 @@ var currentObjects = {}
|
|||
function getObjectByName(objName, objType = null) {
|
||||
var objectTypes = Object.keys(currentObjects)
|
||||
if(typeof objType == 'string') {
|
||||
if(currentObjects[objType] == undefined) return null
|
||||
objectTypes = [objType]
|
||||
if(objType == "ExecutableObject") {
|
||||
objectTypes = getExecutableTypes()
|
||||
} else if(currentObjects[objType] != undefined) {
|
||||
objectTypes = [objType]
|
||||
}
|
||||
}
|
||||
if(Array.isArray(objType)) objectTypes = objType
|
||||
var retObj = null
|
||||
|
@ -1318,10 +1317,23 @@ function getObjectByName(objName, objType = null) {
|
|||
}
|
||||
|
||||
function getObjectsName(objType) {
|
||||
if(objType == "ExecutableObject") {
|
||||
var types = getExecutableTypes()
|
||||
var elementNames = ['']
|
||||
types.forEach(function(elemType){
|
||||
elementNames = elementNames.concat(currentObjects[elemType].map(obj => obj.name))
|
||||
})
|
||||
console.log(elementNames)
|
||||
return elementNames
|
||||
}
|
||||
if(currentObjects[objType] == undefined) return []
|
||||
return currentObjects[objType].map(obj => obj.name)
|
||||
}
|
||||
|
||||
function getExecutableTypes() {
|
||||
return Object.keys(currentObjects).filter(objType => types[objType].prototype instanceof ExecutableObject)
|
||||
}
|
||||
|
||||
function createNewRegisteredObject(objType, args=[]) {
|
||||
if(Object.keys(types).indexOf(objType) == -1) return null // Object type does not exist.
|
||||
var newobj = new types[objType](...args)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue