add undo redo to form builder
This commit is contained in:
@@ -330,16 +330,21 @@ export default {
|
|||||||
this.selectedSubmitter = this.template.submitters[0]
|
this.selectedSubmitter = this.template.submitters[0]
|
||||||
},
|
},
|
||||||
mounted () {
|
mounted () {
|
||||||
|
this.undoStack = [JSON.stringify(this.template)]
|
||||||
|
this.redoStack = []
|
||||||
|
|
||||||
this.$nextTick(() => {
|
this.$nextTick(() => {
|
||||||
this.onWindowResize()
|
this.onWindowResize()
|
||||||
})
|
})
|
||||||
|
|
||||||
document.addEventListener('keyup', this.onKeyUp)
|
document.addEventListener('keyup', this.onKeyUp)
|
||||||
|
window.addEventListener('keydown', this.onKeyDown)
|
||||||
|
|
||||||
window.addEventListener('resize', this.onWindowResize)
|
window.addEventListener('resize', this.onWindowResize)
|
||||||
},
|
},
|
||||||
unmounted () {
|
unmounted () {
|
||||||
document.removeEventListener('keyup', this.onKeyUp)
|
document.removeEventListener('keyup', this.onKeyUp)
|
||||||
|
window.removeEventListener('keydown', this.onKeyDown)
|
||||||
|
|
||||||
window.removeEventListener('resize', this.onWindowResize)
|
window.removeEventListener('resize', this.onWindowResize)
|
||||||
},
|
},
|
||||||
@@ -347,6 +352,36 @@ export default {
|
|||||||
this.documentRefs = []
|
this.documentRefs = []
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
undo () {
|
||||||
|
if (this.undoStack.length > 1) {
|
||||||
|
this.undoStack.pop()
|
||||||
|
const stringData = this.undoStack[this.undoStack.length - 1]
|
||||||
|
const currentStringData = JSON.stringify(this.template)
|
||||||
|
|
||||||
|
if (stringData && stringData !== currentStringData) {
|
||||||
|
this.redoStack.push(currentStringData)
|
||||||
|
|
||||||
|
Object.assign(this.template, JSON.parse(stringData))
|
||||||
|
|
||||||
|
this.save()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
redo () {
|
||||||
|
const stringData = this.redoStack.pop()
|
||||||
|
this.lastRedoData = stringData
|
||||||
|
const currentStringData = JSON.stringify(this.template)
|
||||||
|
|
||||||
|
if (stringData && stringData !== currentStringData) {
|
||||||
|
if (this.undoStack[this.undoStack.length - 1] !== currentStringData) {
|
||||||
|
this.undoStack.push(currentStringData)
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.assign(this.template, JSON.parse(stringData))
|
||||||
|
|
||||||
|
this.save()
|
||||||
|
}
|
||||||
|
},
|
||||||
onWindowResize (e) {
|
onWindowResize (e) {
|
||||||
const breakpointLg = 1024
|
const breakpointLg = 1024
|
||||||
|
|
||||||
@@ -374,6 +409,19 @@ export default {
|
|||||||
this.selectedAreaRef.value = null
|
this.selectedAreaRef.value = null
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
onKeyDown (event) {
|
||||||
|
if ((event.metaKey && event.shiftKey && event.key === 'z') || (event.ctrlKey && event.key === 'Z')) {
|
||||||
|
event.stopImmediatePropagation()
|
||||||
|
event.preventDefault()
|
||||||
|
|
||||||
|
this.redo()
|
||||||
|
} else if ((event.ctrlKey || event.metaKey) && event.key === 'z') {
|
||||||
|
event.stopImmediatePropagation()
|
||||||
|
event.preventDefault()
|
||||||
|
|
||||||
|
this.undo()
|
||||||
|
}
|
||||||
|
},
|
||||||
removeArea (area) {
|
removeArea (area) {
|
||||||
const field = this.template.fields.find((f) => f.areas?.includes(area))
|
const field = this.template.fields.find((f) => f.areas?.includes(area))
|
||||||
|
|
||||||
@@ -385,6 +433,17 @@ export default {
|
|||||||
|
|
||||||
this.save()
|
this.save()
|
||||||
},
|
},
|
||||||
|
pushUndo () {
|
||||||
|
const stringData = JSON.stringify(this.template)
|
||||||
|
|
||||||
|
if (this.undoStack[this.undoStack.length - 1] !== stringData) {
|
||||||
|
this.undoStack.push(stringData)
|
||||||
|
|
||||||
|
if (this.lastRedoData !== stringData) {
|
||||||
|
this.redoStack = []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
onDraw (area) {
|
onDraw (area) {
|
||||||
if (this.drawField) {
|
if (this.drawField) {
|
||||||
this.drawField.areas ||= []
|
this.drawField.areas ||= []
|
||||||
@@ -597,6 +656,8 @@ export default {
|
|||||||
this.$el.closest('template-builder').dataset.template = JSON.stringify(this.template)
|
this.$el.closest('template-builder').dataset.template = JSON.stringify(this.template)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.pushUndo()
|
||||||
|
|
||||||
return this.baseFetch(`/api/templates/${this.template.id}`, {
|
return this.baseFetch(`/api/templates/${this.template.id}`, {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
|
|||||||
Reference in New Issue
Block a user