Undo / Revert to historyState in UXP

My snapshot utility functions do work, I would posit that the errors are in your code or as a product of me refactoring it.
The code is also API 2/Manifest 5 compliant (although the only thing that applies to is executeAsModal and doesn’t have any relevance to the snapshot functions), and in the example above are wrapped in executeAsModal - the handleClick function wraps everything and has a pretty explicit comment explaining as such). executeAsModal isn’t new, it’s been part of UXP for nearly 3 years now I think.

I feel your frustration at the lack of a fully implemented API for UXP as yet, that said, batchplay ain’t all that bad when you get used to it, and if you wrap your batchplay in neat little utility functions you’re kinda just building your own API, easy enough to then combine them into a library for use in all projects.

As for you actual question - here’s a very basic demonstration of using my previous snapshot functions to create and revert to specific history states.

const makeSnapshot = async (name) => {
  return batchPlay(
    [
      {
        _obj: "make",
        _target: [
          {
            _ref: "snapshotClass",
          },
        ],
        from: {
          _ref: "historyState",
          _property: "currentHistoryState",
        },
        name: name,
        using: {
          _enum: "historyState",
          _value: "fullDocument",
        },
        _isCommand: true,
        _options: {},
      },
    ],
    {}
  );
};

const revertToSnapshot = async (name) => {
  return batchPlay(
    [
      {
        _obj: "select",
        _target: [
          {
            _ref: "snapshotClass",
            _name: name,
          },
        ],
        _isCommand: true,
        _options: {
          dialogOptions: "dontDisplay",
        },
      },
    ],
    {}
  );
};

const makeLayer = async () => {
  return await batchPlay(
    [
      {
        _obj: "make",
        _target: [
          {
            _ref: "layer",
          },
        ],
        _options: {
          dialogOptions: "dontDisplay",
        },
      },
    ],
    {}
  );
};

document.querySelector("#testButton").addEventListener("click", async () => {
  const document = app.activeDocument;

  console.log("Button clicked 👆");

  await core.executeAsModal(async () => {
    console.log("Layer count: ", document.layers.length);

    console.log("🛠️ Making first layer");
    await makeLayer();
    console.log("Layer count is: ", document.layers.length);
    console.log("📸 Taking first snapshot");
    await makeSnapshot("first");

    console.log("🛠️ Making second layer");
    await makeLayer();
    console.log("Layer count is: ", document.layers.length);
    console.log("📸 Taking second snapshot");
    await makeSnapshot("second");

    console.log("📷 Reverting to first snapshot");
    await revertToSnapshot("first");
    console.log("Layer count is: ", document.layers.length);
  });
});

Default snapshot

First snapshot

Second snapshot

1 Like