[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  (function() {
   2  "use strict";
   3  var wp;
   4  (wp ||= {}).undoManager = (() => {
   5    var __create = Object.create;
   6    var __defProp = Object.defineProperty;
   7    var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
   8    var __getOwnPropNames = Object.getOwnPropertyNames;
   9    var __getProtoOf = Object.getPrototypeOf;
  10    var __hasOwnProp = Object.prototype.hasOwnProperty;
  11    var __commonJS = (cb, mod) => function __require() {
  12      return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
  13    };
  14    var __export = (target, all) => {
  15      for (var name in all)
  16        __defProp(target, name, { get: all[name], enumerable: true });
  17    };
  18    var __copyProps = (to, from, except, desc) => {
  19      if (from && typeof from === "object" || typeof from === "function") {
  20        for (let key of __getOwnPropNames(from))
  21          if (!__hasOwnProp.call(to, key) && key !== except)
  22            __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  23      }
  24      return to;
  25    };
  26    var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  27      // If the importer is in node compatibility mode or this is not an ESM
  28      // file that has been converted to a CommonJS file using a Babel-
  29      // compatible transform (i.e. "__esModule" has not been set), then set
  30      // "default" to the CommonJS "module.exports" for node compatibility.
  31      isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  32      mod
  33    ));
  34    var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  35  
  36    // package-external:@wordpress/is-shallow-equal
  37    var require_is_shallow_equal = __commonJS({
  38      "package-external:@wordpress/is-shallow-equal"(exports, module) {
  39        module.exports = window.wp.isShallowEqual;
  40      }
  41    });
  42  
  43    // packages/undo-manager/build-module/index.mjs
  44    var index_exports = {};
  45    __export(index_exports, {
  46      createUndoManager: () => createUndoManager
  47    });
  48    var import_is_shallow_equal = __toESM(require_is_shallow_equal(), 1);
  49    function mergeHistoryChanges(changes1, changes2) {
  50      const newChanges = { ...changes1 };
  51      Object.entries(changes2).forEach(([key, value]) => {
  52        if (newChanges[key]) {
  53          newChanges[key] = { ...newChanges[key], to: value.to };
  54        } else {
  55          newChanges[key] = value;
  56        }
  57      });
  58      return newChanges;
  59    }
  60    var addHistoryChangesIntoRecord = (record, changes) => {
  61      const existingChangesIndex = record?.findIndex(
  62        ({ id: recordIdentifier }) => {
  63          return typeof recordIdentifier === "string" ? recordIdentifier === changes.id : (0, import_is_shallow_equal.isShallowEqual)(recordIdentifier, changes.id);
  64        }
  65      );
  66      const nextRecord = [...record];
  67      if (existingChangesIndex !== -1) {
  68        nextRecord[existingChangesIndex] = {
  69          id: changes.id,
  70          changes: mergeHistoryChanges(
  71            nextRecord[existingChangesIndex].changes,
  72            changes.changes
  73          )
  74        };
  75      } else {
  76        nextRecord.push(changes);
  77      }
  78      return nextRecord;
  79    };
  80    function createUndoManager() {
  81      let history = [];
  82      let stagedRecord = [];
  83      let offset = 0;
  84      const dropPendingRedos = () => {
  85        history = history.slice(0, offset || void 0);
  86        offset = 0;
  87      };
  88      const appendStagedRecordToLatestHistoryRecord = () => {
  89        const index = history.length === 0 ? 0 : history.length - 1;
  90        let latestRecord = history[index] ?? [];
  91        stagedRecord.forEach((changes) => {
  92          latestRecord = addHistoryChangesIntoRecord(latestRecord, changes);
  93        });
  94        stagedRecord = [];
  95        history[index] = latestRecord;
  96      };
  97      const isRecordEmpty = (record) => {
  98        const filteredRecord = record.filter(({ changes }) => {
  99          return Object.values(changes).some(
 100            ({ from, to }) => typeof from !== "function" && typeof to !== "function" && !(0, import_is_shallow_equal.isShallowEqual)(from, to)
 101          );
 102        });
 103        return !filteredRecord.length;
 104      };
 105      return {
 106        addRecord(record, isStaged = false) {
 107          const isEmpty = !record || isRecordEmpty(record);
 108          if (isStaged) {
 109            if (isEmpty) {
 110              return;
 111            }
 112            record.forEach((changes) => {
 113              stagedRecord = addHistoryChangesIntoRecord(
 114                stagedRecord,
 115                changes
 116              );
 117            });
 118          } else {
 119            dropPendingRedos();
 120            if (stagedRecord.length) {
 121              appendStagedRecordToLatestHistoryRecord();
 122            }
 123            if (isEmpty) {
 124              return;
 125            }
 126            history.push(record);
 127          }
 128        },
 129        undo() {
 130          if (stagedRecord.length) {
 131            dropPendingRedos();
 132            appendStagedRecordToLatestHistoryRecord();
 133          }
 134          const undoRecord = history[history.length - 1 + offset];
 135          if (!undoRecord) {
 136            return;
 137          }
 138          offset -= 1;
 139          return undoRecord;
 140        },
 141        redo() {
 142          const redoRecord = history[history.length + offset];
 143          if (!redoRecord) {
 144            return;
 145          }
 146          offset += 1;
 147          return redoRecord;
 148        },
 149        hasUndo() {
 150          return !!history[history.length - 1 + offset];
 151        },
 152        hasRedo() {
 153          return !!history[history.length + offset];
 154        }
 155      };
 156    }
 157    return __toCommonJS(index_exports);
 158  })();
 159  (window.wp ||= {}).undoManager = wp.undoManager;
 160  })();


Generated : Tue Aug 18 08:20:25 2026 Cross-referenced by PHPXref