[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/dist/ -> undo-manager.js (source)

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


Generated : Wed Dec 25 08:20:01 2024 Cross-referenced by PHPXref