history-manager.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. const idb = {
  2. openDB(name, version = 1) {
  3. const request = window.indexedDB.open(name, version)
  4. return new Promise((resolve, reject) => {
  5. request.onerror = (e) => {
  6. reject(e.currentTarget.error.message)
  7. }
  8. request.onsuccess = (e) => {
  9. resolve(e.target.result)
  10. }
  11. request.onupgradeneeded = (e) => {
  12. const db = e.target.result
  13. if (!db.objectStoreNames.contains('history')) {
  14. const store = db.createObjectStore('history', { keyPath: 'id' })
  15. }
  16. }
  17. })
  18. },
  19. name: 'form-making',
  20. cursor: 0,
  21. key: 'aRpXsQD/tiA5awcoXM/2lA=='
  22. }
  23. export default {
  24. clear () {
  25. return new Promise((resolve, reject) => {
  26. idb.openDB(idb.name).then((db) => {
  27. const trans = db.transaction(['history'], 'readwrite')
  28. const historyStore = trans.objectStore('history')
  29. historyStore.clear()
  30. trans.oncomplete = (e) => {
  31. idb.cursor = 0
  32. resolve()
  33. }
  34. })
  35. })
  36. },
  37. updateLatest (data, selectedKey) {
  38. return new Promise((resolve, reject) => {
  39. idb.openDB(idb.name).then((db) => {
  40. const trans = db.transaction(['history'], 'readwrite')
  41. const historyStore = trans.objectStore('history')
  42. historyStore.put({
  43. id: idb.cursor,
  44. data: data,
  45. selected: selectedKey
  46. })
  47. trans.oncomplete = (e) => {
  48. resolve()
  49. }
  50. })
  51. })
  52. },
  53. add (data, selectedKey) {
  54. return new Promise((resolve, reject) => {
  55. idb.openDB(idb.name).then((db) => {
  56. const trans = db.transaction(['history'], 'readwrite')
  57. const historyStore = trans.objectStore('history')
  58. const id = idb.cursor + 1
  59. console.log('----', idb.cursor)
  60. const historyList = []
  61. historyStore.openCursor().onsuccess = (e) => {
  62. const cursor = e.target.result
  63. if (cursor) {
  64. historyList.push(cursor.value)
  65. cursor.continue()
  66. } else {
  67. for (let i = 0; i < historyList.length; i++) {
  68. if (historyList[i].id > idb.cursor) {
  69. historyStore.delete(historyList[i].id)
  70. }
  71. }
  72. historyStore.add({
  73. id: id,
  74. data: data,
  75. selected: selectedKey
  76. })
  77. }
  78. }
  79. trans.oncomplete = (e) => {
  80. idb.cursor = id
  81. resolve()
  82. }
  83. })
  84. })
  85. },
  86. undo () {
  87. return new Promise((resolve, reject) => {
  88. idb.openDB(idb.name).then((db) => {
  89. const trans = db.transaction(['history'], 'readwrite')
  90. const historyStore = trans.objectStore('history')
  91. console.log('idb', idb.cursor, historyStore.count())
  92. let cursor = 0, data = {
  93. list: [],
  94. config: {
  95. labelWidth: 100,
  96. labelPosition: 'right',
  97. size: 'default',
  98. customClass: '',
  99. ui: 'element',
  100. layout: 'horizontal'
  101. },
  102. }, undo = false, redo = true, key = ''
  103. if (idb.cursor > 1) {
  104. const request = historyStore.get(idb.cursor - 1)
  105. request.onsuccess = (e) => {
  106. data = request.result.data
  107. key = request.result.selected
  108. undo = true
  109. }
  110. }
  111. trans.oncomplete = (e) => {
  112. idb.cursor = idb.cursor - 1
  113. resolve({data, key, undo, redo})
  114. }
  115. })
  116. })
  117. },
  118. redo () {
  119. return new Promise((resolve, reject) => {
  120. idb.openDB(idb.name).then((db) => {
  121. const trans = db.transaction(['history'], 'readwrite')
  122. const historyStore = trans.objectStore('history')
  123. console.log(idb.cursor)
  124. let cursor = 0, data = {}, undo = true, redo = false, key = ''
  125. const countRequest = historyStore.count()
  126. countRequest.onsuccess = () => {
  127. const count = countRequest.result
  128. if (idb.cursor < count) {
  129. const request = historyStore.get(idb.cursor + 1)
  130. request.onsuccess = (e) => {
  131. data = request.result.data
  132. key = request.result.selected
  133. redo = idb.cursor + 1 == count ? false : true
  134. }
  135. }
  136. }
  137. trans.oncomplete = (e) => {
  138. idb.cursor = idb.cursor + 1
  139. resolve({data, key, undo, redo})
  140. }
  141. })
  142. })
  143. }
  144. }