Skip to main content

Undo, redo and a clean starting point

Add Undo and Redo buttons to a modular layout editor. Let the customer load a saved design as a starting point, then undo their edits back to that design.

Use window.mimeeqApp.observers.history.canUndo and window.mimeeqApp.observers.history.canRedo to enable the buttons. Call window.mimeeqApp.actions.goBack() and window.mimeeqApp.actions.redo() to move through history. After loading a saved design and waiting for its scene work to finish, call window.mimeeqApp.actions.resetHistoryBaseline() to make that design the first entry.

Complete example​

Replace YOUR_MODULAR_SHORT_CODE with the short code for an inline modular embed. To try the load flow, enter a saved scene short code for the same modular product. Use the built-in scene controls to make edits between Undo and Redo clicks.

<form id="load-design">
<label>Saved scene short code <input name="sceneCode" required /></label>
<button type="submit" disabled>Load as starting point</button>
</form>
<button id="undo-edit" type="button" disabled>Undo</button>
<button id="redo-edit" type="button" disabled>Redo</button>
<p id="history-message" role="status"></p>
<mmq-embed
short-code="YOUR_MODULAR_SHORT_CODE"
locale="en"
render-at-mount
hide-history></mmq-embed>

<script>
document.addEventListener(
'mimeeq-app-loaded',
() => {
const { actions, observers } = window.mimeeqApp;
const { history, status } = observers;
const form = document.querySelector('form#load-design');
if (!(form instanceof HTMLFormElement)) return;
const load = form.querySelector('button');
const undo = document.querySelector('button#undo-edit');
const redo = document.querySelector('button#redo-edit');
const message = document.getElementById('history-message');
if (
!load ||
!(undo instanceof HTMLButtonElement) ||
!(redo instanceof HTMLButtonElement) ||
!message
)
return;
let working = false;

function sceneIsIdle() {
return Boolean(status.firstRender.getValue() && status.settled.getValue());
}

const render = () => {
const unavailable = working || !sceneIsIdle();
load.disabled = unavailable;
undo.disabled = unavailable || history.canUndo.getValue() !== true;
redo.disabled = unavailable || history.canRedo.getValue() !== true;
};

function waitForIdleScene() {
if (sceneIsIdle()) return Promise.resolve();
return new Promise((resolve) => {
const check = () => {
if (sceneIsIdle()) {
queueMicrotask(() => {
subscriptions.forEach((subscription) => subscription.unsubscribe());
resolve(undefined);
});
}
};
const subscriptions = [status.firstRender, status.settled].map(
(observer) => observer.subscribe(check),
);
});
}

/** @param {() => Promise<void>} action */
const run = async (action) => {
if (working || !sceneIsIdle()) return;
working = true;
message.textContent = '';
render();

try {
await action();
await waitForIdleScene();
} catch (error) {
message.textContent =
error instanceof Error ? error.message : 'Could not update the scene.';
} finally {
working = false;
render();
}
};

undo.addEventListener('click', () => {
if (history.canUndo.getValue()) void run(actions.goBack);
});
redo.addEventListener('click', () => {
if (history.canRedo.getValue()) void run(actions.redo);
});

form.addEventListener('submit', (event) => {
event.preventDefault();
const code = String(new FormData(form).get('sceneCode') ?? '').trim();
if (!code) return;

void run(async () => {
let loaded = false;
/** @param {Event} event */
function onLoad(event) {
if (event instanceof CustomEvent && event.detail?.shortCode === code) loaded = true;
}
document.addEventListener('mimeeq-modular-load-scene', onLoad);
try {
await actions.loadModularScene(code);
} finally {
document.removeEventListener('mimeeq-modular-load-scene', onLoad);
}

if (!loaded) throw new Error('The saved scene could not be loaded.');
await waitForIdleScene();
const baselineSet = await actions.resetHistoryBaseline();
message.textContent = baselineSet
? 'Loaded design. Undo starts here.'
: 'Loaded design. The scene was still busy, so earlier steps stay in the history.';
});
});

const subscriptions = [
history.canUndo.subscribe(render),
history.canRedo.subscribe(render),
...[status.firstRender, status.settled].map((observer) => observer.subscribe(render)),
];
window.addEventListener(
'pagehide',
() => {
subscriptions.forEach((subscription) => subscription.unsubscribe());
},
{ once: true },
);
},
{ once: true },
);
</script>
<script src="https://cdn.mimeeq.com/read_models/embed/app-embed.js" async></script>

How it works​

  1. Listen for mimeeq-app-loaded on document before loading the embed script. The event makes the API available. The scene observer determines when it is ready for interaction.
  2. Render the buttons from the two history flags and scene state. Disable the controls while your own action is running and while the scene loads or processes work.
  3. Call goBack() for Undo and redo() for Redo. These actions can queue a modular restore. Keep using scene status and the history flags as the controls update.
  4. Load the saved design with loadModularScene(code). The example checks for the matching mimeeq-modular-load-scene event before resetting. A payload with missing product elements can cause the load to return without replacing the scene.
  5. Wait for the scene to finish loading and processing queued work, then await resetHistoryBaseline(). This keeps the scene in place, clears previous Undo and Redo steps, and makes the loaded design the new Undo floor. Use the same sequence when your preset button loads a saved scene.

Observer payloads​

Subscriptions emit { newValue, oldValue } and immediately replay the current value. The first emission can omit oldValue. Use getValue() for a synchronous read. Treat the values as read-only snapshots.

Observer fieldTypeMeaning and empty state
history.canUndobooleantrue when an earlier step is available and the scene loader is inactive. At the baseline and during scene loading it is false.
history.canRedobooleantrue when a forward step is available and the scene loader is inactive. At the newest step and during scene loading it is false.
status.firstRenderbooleanThe mounted scene has drawn its first frame.
status.settledbooleanThe main product is loaded, the canvas is unblocked, no drag is active, and no 3D tasks are pending.
status.busybooleanAt least one 3D task is unfinished.

Listen on document. Use observer values for button state. History events report the action and can fire before the 3D restore finishes.

Eventevent.detailWhen it fires
mimeeq-modular-history-go-backNo payloadA modular Undo starts restoring the earlier scene.
mimeeq-modular-history-go-forwardNo payloadA modular Redo starts restoring the later scene.
mimeeq-modular-load-scene{ code: string; productId: string; shortCode: string | null }The saved scene is accepted and its scene work is queued. code is the modular product prefix. shortCode is null for a load without a saved scene code.
mimeeq-history-go-back, mimeeq-history-go-forwardHistoryItemThe corresponding standard configurator history actions.

resetHistoryBaseline() updates the history observers and has no dedicated DOM event. The existing reset() action still returns a standard product to its original configuration and can emit mimeeq-history-reset.

Edge cases​

  • A new edit after Undo discards the forward branch. Read canRedo again to update the UI.
  • goBack() and redo() do nothing at their respective history boundaries in a modular scene. An overlapping modular restore is also ignored.
  • resetHistoryBaseline() leaves history as it is while a product or scene loads or a modular restore is still active. Call it after your own load finishes and scene status settles. It resolves to true when the baseline was set and false when it left history as it is.
  • Keep the old baseline if a load fails. The example resets history only after the requested scene is accepted.

See Programmatic modular scene control, Modular events, and the Observers guide.