PluginProbe
Extendify / 3.1.1
Extendify v3.1.1
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / QuickEdit / state / undo.js

undo.js in Extendify 3.1.1, at src/QuickEdit/state/undo.js

116 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 // Entries are shaped like the save endpoint's request body so replay is just
2 // save(entry). Cap protects localStorage size.
3 import { __, sprintf } from '@wordpress/i18n';
4 import {
5 save,
6 saveProduct,
7 saveSiteIdentity,
8 saveWpFormsField,
9 saveWpNavigationItem,
10 } from '../lib/api';
11 import { track } from '../lib/insights';
12
13 const STORAGE_KEY = 'extendify-quick-edit-undo-stack-v1';
14 const MAX_DEPTH = 5;
15 const ANNOUNCE_KEY = 'extendify-quick-edit-undo-announce';
16
17 const loadStack = () => {
18 try {
19 const raw = window.localStorage.getItem(STORAGE_KEY);
20 const arr = raw ? JSON.parse(raw) : [];
21 return Array.isArray(arr) ? arr : [];
22 } catch (_) {
23 return [];
24 }
25 };
26
27 const saveStack = (stack) => {
28 try {
29 window.localStorage.setItem(STORAGE_KEY, JSON.stringify(stack));
30 } catch (_) {
31 // quota / private mode
32 }
33 };
34
35 export const pushUndo = (entry) => {
36 if (!entry) return;
37 const kind = entry.kind || (entry.rawBlock ? 'block' : 'patches');
38 const stack = loadStack();
39 stack.push({ ...entry, kind, ts: Date.now() });
40 while (stack.length > MAX_DEPTH) stack.shift();
41 saveStack(stack);
42 };
43
44 const popUndo = () => {
45 const stack = loadStack();
46 const entry = stack.pop();
47 saveStack(stack);
48 return entry;
49 };
50
51 export const getStackDepth = () => loadStack().length;
52
53 // Returns false on empty stack so callers can show feedback.
54 export const performUndo = () => {
55 const entry = popUndo();
56 if (!entry) return false;
57 // Site identity lives in wp_options, not post_content; route to its own endpoint.
58 let promise;
59 if (entry.identityReplay) {
60 promise = saveSiteIdentity(entry.beforeValues || {});
61 } else if (entry.productReplay) {
62 // Routes through WCProductController; price entries carry { regular, sale }.
63 promise = saveProduct({
64 productId: entry.productId,
65 field: entry.field,
66 value: entry.beforeValue,
67 });
68 } else if (entry.navReplay) {
69 // Routes through wp-navigation; ref-based nav items live in their own CPT.
70 promise = saveWpNavigationItem({
71 navPostId: entry.navPostId,
72 itemIndex: entry.itemIndex,
73 blockType: entry.blockType,
74 patches: entry.patches,
75 });
76 } else if (entry.wpformsReplay) {
77 // WPForms fields live in the form's serialized JSON, not post_content;
78 // replay through the shallow-merge changes-bag the forward save used.
79 promise = saveWpFormsField({
80 formId: entry.formId,
81 fieldId: entry.fieldId,
82 changes: entry.changes,
83 });
84 } else {
85 const { kind: _kind, ts: _ts, ...payload } = entry;
86 promise = save(payload);
87 }
88 promise
89 .then(() => {
90 track('undo', { kind: entry.kind });
91 // aria-live announcement survives the reload via sessionStorage.
92 try {
93 window.sessionStorage.setItem(
94 ANNOUNCE_KEY,
95 __('Reverted last change.', 'extendify-local'),
96 );
97 } catch (_) {
98 // quota / private mode
99 }
100 window.location.reload();
101 })
102 .catch((err) => {
103 track('undo_failed', { kind: entry.kind });
104 window.alert(
105 sprintf(
106 // translators: %s is the underlying error message.
107 __('Undo failed: %s', 'extendify-local'),
108 err?.message || __('unknown error', 'extendify-local'),
109 ),
110 );
111 });
112 return true;
113 };
114
115 export const ANNOUNCE_STORAGE_KEY = ANNOUNCE_KEY;
116