前端快照实现方案

简介

snapshot 翻译为快照,用于直观获取页面在某个运行时的状态,将执行操作前后的快照进行存储,可以轻松实现页面状态的重做、撤销功能。

本文主要介绍 snapshot 工具实现的原理,以及其在项目中的使用。

设计

要实现页面状态的历史记录、重做、撤销,需要支持以下几个属性和方法

属性

  • 历史记录:存储历史的页面状态,包含页面初始化的状态 到 上一个页面状态
  • 撤销记录:存储重做的每一个操作记录,用于撤销后恢复
  • 当前记录:临时存储当前页面状态,主要用于下一次操作后,需要将其存储到历史记录
  • 上次插入数据时间:插入时间间隔太小时,需要额外处理
ini 复制代码
 // 历史记录
 recordList: string[] = []
 ​
 // 撤销记录,用于重做
 redoList: string[] = []
 ​
 // 当前记录用 currentRecord 变量暂时存储,当用户修改时,再存放到 recordList
 currentRecord = ''
 ​
 // 上次插入数据时间
 time = 0

方法

存储历史记录 push

当用户操作后,更新历史记录。需要考虑以下几点。

  • 当前操作时间距离上次插入时间小于 100 ms 时,则替换当前记录并取消执行添加
  • 如果当前记录有值,则说明上一次操作是手动插入,将之前缓存的当前记录推入 recordList,并且需要清空重做记录
  • 将当前状态存储到当前记录
  • 设置最大历史记录容量,当超过时,将最先插入的数据删除
kotlin 复制代码
 push(record: PageData) {
   const nowTime = Date.now()
   // 防止添加重复的时间,当添加间隔小于 100ms 时,则替换当前记录并取消执行添加
   if (this.time + 100 > nowTime) {
     try {
       // 将 json 转成字符串存储
       this.currentRecord = JSON.stringify(record)
     } catch (error) {
       return false
     }
 ​
     return false
   }
 ​
   this.time = nowTime
 ​
   // 判断之前是否已经存在currentRecord记录,有则存储到recordList
   if (this.currentRecord) {
     this.recordList.push(this.currentRecord)
     // 增加记录后则应该清空重做记录
     this.redoList.splice(0, this.redoList.length)
   }
 ​
   // 存储当前记录
   this.currentRecord = JSON.stringify(record)
 ​
   // 最多存储 30 条记录,超过则删除之前的记录
   if (this.recordList.length > 30) {
     this.recordList.unshift()
   }
   return true
 }

撤销操作 undo

当用户操作后,依赖 push 时存储的历史记录列表,将页面状态回退到上一次的状态,需要注意以下几点:

  • 当历史记录没有时,直接返回
  • 从历史记录中取出最后一次存储数据
  • 若当前记录存在,需要将其存放到重做记录列表
  • 需要清空当前记录,防止重复添加,因为撤销后,也会执行 push 存储历史记录方法
kotlin 复制代码
 undo() {
   // 没有记录时,返回 false
   if (this.recordList.length === 0) {
     return null
   }
 ​
   const record = this.recordList.pop()
 ​
   // 将当前记录添加到重做记录里面
   if (this.currentRecord) {
     this.redoList.push(this.currentRecord)
   }
 ​
   // 丢弃当前记录,防止重复添加
   this.currentRecord = ''
   return JSON.parse(record as string) as PageData
 }

重做操作 redo

当用户操作后,依赖 redoList 列表,将页面状态回退到撤销前的状态,需要注意以下几点:

  • 当重做记录没有时,直接返回
  • 从重做记录里取出最后一次存储数据
  • 如果当前记录有值,需要将其放到历史记录列表
  • 需要清空当前记录,防止重复添加,因为重做后,也会执行 push 存储历史记录方法
kotlin 复制代码
 redo() {
   // 没有重做记录时,返回 false
   if (this.redoList.length === 0) {
     return null
   }
 ​
   const record = this.redoList.pop()
   // 添加到重做记录里面
   if (this.currentRecord) {
     this.recordList.push(this.currentRecord)
   }
 ​
   // 丢弃当前记录,防止重复添加
   this.currentRecord = ''
   return JSON.parse(record as string) as PageData
 }

过程演示

假设数据列表为 [1, 2, 3, 4],当前属性值分别为:

ini 复制代码
 recordList = [1, 2, 3]
 redoList = []
 currentRecord = 4

1、手动添加 5,则会执行 push 方法,执行后属性值分别为

ini 复制代码
 recordList = [1, 2, 3, 4]
 redoList = []
 currentRecord = 5

2、执行1次撤销,则先会执行 undo,执行后属性值分别为

ini 复制代码
 recordList = [1, 2, 3]
 redoList = [5]
 currentRecord = ''

然后执行 push,将 4 push 进去,执行后属性值分别为

ini 复制代码
 recordList = [1, 2, 3]
 redoList = [5]
 currentRecord = 4

3、执行第2次撤销,则先会执行 undo,执行后属性值分别为

ini 复制代码
 recordList = [1, 2]
 redoList = [5, 4]
 currentRecord = ''

然后执行 push,将 3 push 进去,执行后属性值分别为

ini 复制代码
 recordList = [1, 2]
 redoList = [5, 4]
 currentRecord = 3

4、执行1次重做,则先会执行 redo,执行后属性值分别为

ini 复制代码
 recordList = [1, 2, 3]
 redoList = [5]
 currentRecord = ''

然后执行 push,将 4 push 进去,执行后属性值分别为

ini 复制代码
 recordList = [1, 2, 3]
 redoList = [5]
 currentRecord = 4

5、手动添加 6,则会执行 push 方法,执行后属性值分别为

ini 复制代码
 recordList = [1, 2, 3, 4]
 redoList = []
 currentRecord = 6

完整代码

kotlin 复制代码
 export default class Snapshot {
     // 历史记录
     recordList: string[] = []
 ​
     // 撤销记录,用于重做
     redoList: string[] = []
 ​
     // 当前记录用 currentRecord 变量暂时存储,当用户修改时,再存放到 recordList
     currentRecord = ''
 ​
     // 上次插入数据时间
     time = 0
 ​
     push(record: PageData) {
         const nowTime = Date.now()
         // 防止添加重复的时间,当添加间隔小于 100ms 时,则替换当前记录并取消执行添加
         if (this.time + 100 > nowTime) {
             try {
                 // 将 json 转成字符串存储
                 this.currentRecord = JSON.stringify(record)
             } catch (error) {
                 return false
             }
 ​
             return false
         }
 ​
         this.time = nowTime
 ​
         // 判断之前是否已经存在currentRecord记录,有则存储到recordList
         if (this.currentRecord) {
             this.recordList.push(this.currentRecord)
             // 增加记录后则应该清空重做记录
             this.redoList.splice(0, this.redoList.length)
         }
 ​
         try {
             // 将 json 转成字符串存储
             this.currentRecord = JSON.stringify(record)
         } catch (error) {
             return
         }
 ​
         // 最多存储 30 条记录,超过则删除之前的记录
         if (this.recordList.length > 30) {
             this.recordList.unshift()
         }
         return true
     }
 ​
     undo() {
         // 没有记录时,返回 false
         if (this.recordList.length === 0) {
             return null
         }
 ​
         const record = this.recordList.pop()
 ​
         // 将当前记录添加到重做记录里面
         if (this.currentRecord) {
             this.redoList.push(this.currentRecord)
         }
 ​
         // 丢弃当前记录,防止重复添加
         this.currentRecord = ''
         return JSON.parse(record as string) as PageData
     }
 ​
     redo() {
         // 没有重做记录时,返回 false
         if (this.redoList.length === 0) {
             return null
         }
 ​
         const record = this.redoList.pop()
         // 添加到重做记录里面
         if (this.currentRecord) {
             this.recordList.push(this.currentRecord)
         }
 ​
         // 丢弃当前记录,防止重复添加
         this.currentRecord = ''
         return JSON.parse(record as string) as PageData
     }
 }

在线演示

codepen.io/belle-peng/...

相关推荐
diemeng11191 小时前
AI前端开发技能变革时代:效率与创新的新范式
前端·人工智能
bin91533 小时前
DeepSeek 助力 Vue 开发:打造丝滑的复制到剪贴板(Copy to Clipboard)
前端·javascript·vue.js·ecmascript·deepseek
晴空万里藏片云4 小时前
elment Table多级表头固定列后,合计行错位显示问题解决
前端·javascript·vue.js
曦月合一4 小时前
html中iframe标签 隐藏滚动条
前端·html·iframe
奶球不是球5 小时前
el-button按钮的loading状态设置
前端·javascript
kidding7235 小时前
前端VUE3的面试题
前端·typescript·compositionapi·fragment·teleport·suspense
无责任此方_修行中6 小时前
每周见闻分享:杂谈AI取代程序员
javascript·资讯
Σίσυφος19007 小时前
halcon 条形码、二维码识别、opencv识别
前端·数据库
学代码的小前端7 小时前
0基础学前端-----CSS DAY13
前端·css
dorabighead8 小时前
JavaScript 高级程序设计 读书笔记(第三章)
开发语言·javascript·ecmascript