| 1 |
import { isEmbedded } from '@shared/lib/embedded-guard'; |
| 2 |
import { track } from '@shared/lib/track'; |
| 3 |
import { create } from 'zustand'; |
| 4 |
import { persist } from 'zustand/middleware'; |
| 5 |
|
| 6 |
const HTML_CLASS = 'extendify-quick-edit-on'; |
| 7 |
|
| 8 |
const DEFAULT_ON = !!window.extQuickEditData?.defaultOn; |
| 9 |
|
| 10 |
// One origin serves many sites over its life. |
| 11 |
export const STORAGE_KEY = `extendify-quick-edit-mode-${window.extSharedData?.siteId}`; |
| 12 |
|
| 13 |
export const useEditModeStore = create()( |
| 14 |
persist( |
| 15 |
(set, get) => ({ |
| 16 |
on: DEFAULT_ON, |
| 17 |
setOn: (on) => { |
| 18 |
const next = !!on; |
| 19 |
if (get().on === next) return; |
| 20 |
set({ on: next }); |
| 21 |
}, |
| 22 |
toggle: () => set({ on: !get().on }), |
| 23 |
}), |
| 24 |
{ |
| 25 |
name: STORAGE_KEY, |
| 26 |
partialize: ({ on }) => ({ on }), |
| 27 |
}, |
| 28 |
), |
| 29 |
); |
| 30 |
|
| 31 |
// This chunk also ships in the Agent bundle, so guarding the class here keeps |
| 32 |
// the page from ever looking edit-on inside another tool's iframe (Customizer |
| 33 |
// preview, page-builder previews) regardless of which bundle loaded it. |
| 34 |
const applyHtmlClass = (on) => |
| 35 |
document.documentElement.classList.toggle(HTML_CLASS, on && !isEmbedded()); |
| 36 |
|
| 37 |
useEditModeStore.subscribe((state) => { |
| 38 |
applyHtmlClass(state.on); |
| 39 |
track('quick_edit_status', { value: state.on ? 'on' : 'off' }); |
| 40 |
}); |
| 41 |
applyHtmlClass(useEditModeStore.getState().on); |
| 42 |
|