| 1 |
import { useEditModeStore } from '@quick-edit/state/edit-mode'; |
| 2 |
import { create } from 'zustand'; |
| 3 |
|
| 4 |
export const useQuickEditStore = create((set, get) => ({ |
| 5 |
hovered: null, |
| 6 |
// Quick Edit canvas-mount target — set when the user clicks the Quick |
| 7 |
// Edit pill. See click-rule.js's module header for the full unified |
| 8 |
// selector contract; this and `agentBlock` are the two selection |
| 9 |
// slots Quick Edit and the AI Agent share. |
| 10 |
selected: null, |
| 11 |
dirty: {}, |
| 12 |
saving: false, |
| 13 |
error: null, |
| 14 |
// Ask AI staged block — see buildAgentBlockDescriptor for the shape. |
| 15 |
// Distinct from `selected` by design: a QE canvas opens a modal flow |
| 16 |
// for one block, while an Ask AI selection stages a block for a |
| 17 |
// multi-turn workflow. |
| 18 |
agentBlock: null, |
| 19 |
agentBlockCode: null, |
| 20 |
// Sticky pre-pill-action selection — set when the user clicks a tagged |
| 21 |
// block on the live front end. While set, hover on other tagged blocks |
| 22 |
// is suppressed and the bar/outline stay pinned. Cleared by an outside |
| 23 |
// click, a click on a different tagged block, or a pill action (which |
| 24 |
// hands off to `selected` / `agentBlock`). Shape mirrors buildTarget: |
| 25 |
// { el, blockType, blockId, source }. |
| 26 |
committedSelection: null, |
| 27 |
|
| 28 |
setHovered: (target) => set({ hovered: target }), |
| 29 |
setSelected: (target) => set({ selected: target, dirty: {}, error: null }), |
| 30 |
clearSelected: () => set({ selected: null, dirty: {}, error: null }), |
| 31 |
|
| 32 |
setAgentBlock: (agentBlock) => { |
| 33 |
// A staged block is invisible without the selection UI. |
| 34 |
if (agentBlock) useEditModeStore.getState().setOn(true); |
| 35 |
set({ agentBlock, agentBlockCode: null }); |
| 36 |
}, |
| 37 |
setAgentBlockCode: (agentBlockCode) => set({ agentBlockCode }), |
| 38 |
|
| 39 |
setCommittedSelection: (committedSelection) => set({ committedSelection }), |
| 40 |
|
| 41 |
setField: (fieldKey, value) => |
| 42 |
set((state) => ({ |
| 43 |
dirty: { ...state.dirty, [fieldKey]: value }, |
| 44 |
})), |
| 45 |
|
| 46 |
setSaving: (saving) => set({ saving }), |
| 47 |
setError: (error) => set({ error }), |
| 48 |
|
| 49 |
getPatches: () => { |
| 50 |
const dirty = get().dirty; |
| 51 |
return Object.entries(dirty).map(([fieldKey, value]) => ({ |
| 52 |
fieldKey, |
| 53 |
value, |
| 54 |
})); |
| 55 |
}, |
| 56 |
})); |
| 57 |
|