history.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import beautifier from '@utils/json-beautifier'
  2. export default {
  3. data() {
  4. return {
  5. historySteps: {
  6. index: 0,
  7. maxStep: 20,
  8. steps: [],
  9. storage: false
  10. }
  11. }
  12. },
  13. watch: {
  14. historySteps: {
  15. handler(val) {
  16. if (val.storage) localStorage.setItem("avue-form-history", beautifier(val))
  17. else localStorage.removeItem("avue-form-history")
  18. },
  19. deep: true
  20. }
  21. },
  22. methods: {
  23. initHistory(data) {
  24. if (data.storage) {
  25. const history = localStorage.getItem("avue-form-history")
  26. if (history) {
  27. this.historySteps = eval("(" + history + ")")
  28. const { index, steps } = this.historySteps
  29. return this.deepClone(steps[index])
  30. }
  31. }
  32. this.historySteps = {
  33. ...this.historySteps,
  34. ...data
  35. }
  36. const { index, steps } = this.historySteps
  37. return this.deepClone(steps[index])
  38. },
  39. handleHistoryChange(data) {
  40. if (this.historySteps.index == this.historySteps.maxStep - 1) this.historySteps.steps.shift()
  41. else this.historySteps.index++
  42. this.historySteps.steps[this.historySteps.index] = this.deepClone(data)
  43. if (this.historySteps.index < this.historySteps.steps.length - 1) {
  44. this.historySteps.steps = this.historySteps.steps.slice(0, this.historySteps.index + 1)
  45. }
  46. },
  47. handleUndo() {
  48. if (this.historySteps.index != 0) this.historySteps.index--
  49. return this.deepClone(this.historySteps.steps[this.historySteps.index])
  50. },
  51. handleRedo() {
  52. if (this.historySteps.index != (this.historySteps.steps.length - 1)) this.historySteps.index++
  53. return this.deepClone(this.historySteps.steps[this.historySteps.index])
  54. }
  55. }
  56. }