[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

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


Generated : Wed Apr 15 08:20:10 2026 Cross-referenced by PHPXref