You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
8.3 KiB

1 year ago
  1. /******/ (function() { // webpackBootstrap
  2. /******/ "use strict";
  3. /******/ var __webpack_modules__ = ({
  4. /***/ 9127:
  5. /***/ (function(module) {
  6. module.exports = window["wp"]["isShallowEqual"];
  7. /***/ })
  8. /******/ });
  9. /************************************************************************/
  10. /******/ // The module cache
  11. /******/ var __webpack_module_cache__ = {};
  12. /******/
  13. /******/ // The require function
  14. /******/ function __webpack_require__(moduleId) {
  15. /******/ // Check if module is in cache
  16. /******/ var cachedModule = __webpack_module_cache__[moduleId];
  17. /******/ if (cachedModule !== undefined) {
  18. /******/ return cachedModule.exports;
  19. /******/ }
  20. /******/ // Create a new module (and put it into the cache)
  21. /******/ var module = __webpack_module_cache__[moduleId] = {
  22. /******/ // no module.id needed
  23. /******/ // no module.loaded needed
  24. /******/ exports: {}
  25. /******/ };
  26. /******/
  27. /******/ // Execute the module function
  28. /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
  29. /******/
  30. /******/ // Return the exports of the module
  31. /******/ return module.exports;
  32. /******/ }
  33. /******/
  34. /************************************************************************/
  35. /******/ /* webpack/runtime/compat get default export */
  36. /******/ !function() {
  37. /******/ // getDefaultExport function for compatibility with non-harmony modules
  38. /******/ __webpack_require__.n = function(module) {
  39. /******/ var getter = module && module.__esModule ?
  40. /******/ function() { return module['default']; } :
  41. /******/ function() { return module; };
  42. /******/ __webpack_require__.d(getter, { a: getter });
  43. /******/ return getter;
  44. /******/ };
  45. /******/ }();
  46. /******/
  47. /******/ /* webpack/runtime/define property getters */
  48. /******/ !function() {
  49. /******/ // define getter functions for harmony exports
  50. /******/ __webpack_require__.d = function(exports, definition) {
  51. /******/ for(var key in definition) {
  52. /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  53. /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  54. /******/ }
  55. /******/ }
  56. /******/ };
  57. /******/ }();
  58. /******/
  59. /******/ /* webpack/runtime/hasOwnProperty shorthand */
  60. /******/ !function() {
  61. /******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
  62. /******/ }();
  63. /******/
  64. /******/ /* webpack/runtime/make namespace object */
  65. /******/ !function() {
  66. /******/ // define __esModule on exports
  67. /******/ __webpack_require__.r = function(exports) {
  68. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  69. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  70. /******/ }
  71. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  72. /******/ };
  73. /******/ }();
  74. /******/
  75. /************************************************************************/
  76. var __webpack_exports__ = {};
  77. // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
  78. !function() {
  79. __webpack_require__.r(__webpack_exports__);
  80. /* harmony export */ __webpack_require__.d(__webpack_exports__, {
  81. /* harmony export */ createUndoManager: function() { return /* binding */ createUndoManager; }
  82. /* harmony export */ });
  83. /* harmony import */ var _wordpress_is_shallow_equal__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(9127);
  84. /* harmony import */ var _wordpress_is_shallow_equal__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_wordpress_is_shallow_equal__WEBPACK_IMPORTED_MODULE_0__);
  85. /**
  86. * WordPress dependencies
  87. */
  88. /** @typedef {import('./types').HistoryRecord} HistoryRecord */
  89. /** @typedef {import('./types').HistoryChange} HistoryChange */
  90. /** @typedef {import('./types').HistoryChanges} HistoryChanges */
  91. /** @typedef {import('./types').UndoManager} UndoManager */
  92. /**
  93. * Merge changes for a single item into a record of changes.
  94. *
  95. * @param {Record< string, HistoryChange >} changes1 Previous changes
  96. * @param {Record< string, HistoryChange >} changes2 NextChanges
  97. *
  98. * @return {Record< string, HistoryChange >} Merged changes
  99. */
  100. function mergeHistoryChanges(changes1, changes2) {
  101. /**
  102. * @type {Record< string, HistoryChange >}
  103. */
  104. const newChanges = {
  105. ...changes1
  106. };
  107. Object.entries(changes2).forEach(([key, value]) => {
  108. if (newChanges[key]) {
  109. newChanges[key] = {
  110. ...newChanges[key],
  111. to: value.to
  112. };
  113. } else {
  114. newChanges[key] = value;
  115. }
  116. });
  117. return newChanges;
  118. }
  119. /**
  120. * Adds history changes for a single item into a record of changes.
  121. *
  122. * @param {HistoryRecord} record The record to merge into.
  123. * @param {HistoryChanges} changes The changes to merge.
  124. */
  125. const addHistoryChangesIntoRecord = (record, changes) => {
  126. const existingChangesIndex = record?.findIndex(({
  127. id: recordIdentifier
  128. }) => {
  129. return typeof recordIdentifier === 'string' ? recordIdentifier === changes.id : _wordpress_is_shallow_equal__WEBPACK_IMPORTED_MODULE_0___default()(recordIdentifier, changes.id);
  130. });
  131. const nextRecord = [...record];
  132. if (existingChangesIndex !== -1) {
  133. // If the edit is already in the stack leave the initial "from" value.
  134. nextRecord[existingChangesIndex] = {
  135. id: changes.id,
  136. changes: mergeHistoryChanges(nextRecord[existingChangesIndex].changes, changes.changes)
  137. };
  138. } else {
  139. nextRecord.push(changes);
  140. }
  141. return nextRecord;
  142. };
  143. /**
  144. * Creates an undo manager.
  145. *
  146. * @return {UndoManager} Undo manager.
  147. */
  148. function createUndoManager() {
  149. /**
  150. * @type {HistoryRecord[]}
  151. */
  152. let history = [];
  153. /**
  154. * @type {HistoryRecord}
  155. */
  156. let stagedRecord = [];
  157. /**
  158. * @type {number}
  159. */
  160. let offset = 0;
  161. const dropPendingRedos = () => {
  162. history = history.slice(0, offset || undefined);
  163. offset = 0;
  164. };
  165. const appendStagedRecordToLatestHistoryRecord = () => {
  166. var _history$index;
  167. const index = history.length === 0 ? 0 : history.length - 1;
  168. let latestRecord = (_history$index = history[index]) !== null && _history$index !== void 0 ? _history$index : [];
  169. stagedRecord.forEach(changes => {
  170. latestRecord = addHistoryChangesIntoRecord(latestRecord, changes);
  171. });
  172. stagedRecord = [];
  173. history[index] = latestRecord;
  174. };
  175. /**
  176. * Checks whether a record is empty.
  177. * A record is considered empty if it the changes keep the same values.
  178. * Also updates to function values are ignored.
  179. *
  180. * @param {HistoryRecord} record
  181. * @return {boolean} Whether the record is empty.
  182. */
  183. const isRecordEmpty = record => {
  184. const filteredRecord = record.filter(({
  185. changes
  186. }) => {
  187. return Object.values(changes).some(({
  188. from,
  189. to
  190. }) => typeof from !== 'function' && typeof to !== 'function' && !_wordpress_is_shallow_equal__WEBPACK_IMPORTED_MODULE_0___default()(from, to));
  191. });
  192. return !filteredRecord.length;
  193. };
  194. return {
  195. /**
  196. * Record changes into the history.
  197. *
  198. * @param {HistoryRecord=} record A record of changes to record.
  199. * @param {boolean} isStaged Whether to immediately create an undo point or not.
  200. */
  201. addRecord(record, isStaged = false) {
  202. const isEmpty = !record || isRecordEmpty(record);
  203. if (isStaged) {
  204. if (isEmpty) {
  205. return;
  206. }
  207. record.forEach(changes => {
  208. stagedRecord = addHistoryChangesIntoRecord(stagedRecord, changes);
  209. });
  210. } else {
  211. dropPendingRedos();
  212. if (stagedRecord.length) {
  213. appendStagedRecordToLatestHistoryRecord();
  214. }
  215. if (isEmpty) {
  216. return;
  217. }
  218. history.push(record);
  219. }
  220. },
  221. undo() {
  222. if (stagedRecord.length) {
  223. dropPendingRedos();
  224. appendStagedRecordToLatestHistoryRecord();
  225. }
  226. const undoRecord = history[history.length - 1 + offset];
  227. if (!undoRecord) {
  228. return;
  229. }
  230. offset -= 1;
  231. return undoRecord;
  232. },
  233. redo() {
  234. const redoRecord = history[history.length + offset];
  235. if (!redoRecord) {
  236. return;
  237. }
  238. offset += 1;
  239. return redoRecord;
  240. },
  241. hasUndo() {
  242. return !!history[history.length - 1 + offset];
  243. },
  244. hasRedo() {
  245. return !!history[history.length + offset];
  246. }
  247. };
  248. }
  249. }();
  250. (window.wp = window.wp || {}).undoManager = __webpack_exports__;
  251. /******/ })()
  252. ;