Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | /** * DesignModeContext — propagates design mode state through the component tree. * When active=false, all design concerns are zero-cost no-ops. */ import type {ICardConfig, IEnrichedFieldSchema} from '@feasibleone/blong'; import {createContext, use, useCallback, useState, type ReactNode} from 'react'; import type {LayoutConfig} from '../hooks/useLayout.js'; export type DesignElementType = 'card' | 'field' | 'deck' | 'widget'; export interface IDesignElement { id: string; type: DesignElementType; label?: string; } export interface ILayoutEditorConfig { cards: Record<string, ICardConfig>; layouts: Record<string, LayoutConfig>; /** Per-field schema overrides (title, widget.type, readOnly, required, etc.) */ schema?: Record<string, Partial<IEnrichedFieldSchema>>; } export interface IHistoryEntry { config: ILayoutEditorConfig; description: string; } export interface IDesignModeContextValue { active: boolean; selected: IDesignElement | null; select: (element: IDesignElement | null) => void; config: ILayoutEditorConfig; updateConfig: (patch: Partial<ILayoutEditorConfig>) => void; permission: string; /** Undo / redo */ canUndo: boolean; canRedo: boolean; undo: () => void; redo: () => void; pushHistory: (description: string) => void; /** Saving */ saving: boolean; saveConfig: () => Promise<void>; } export const DesignModeContext = createContext<IDesignModeContextValue | null>(null); export function useDesignModeContext(): IDesignModeContextValue { const ctx = use(DesignModeContext); if (!ctx) throw new Error( '[blong-browser] useDesignModeContext must be used inside DesignModeProvider', ); return ctx; } /** Inert context returned when design mode is inactive (avoids conditional hook calls) */ const inertContext: IDesignModeContextValue = { active: false, selected: null, select: () => undefined, config: {cards: {}, layouts: {}, schema: {}}, updateConfig: () => undefined, permission: 'portal.design', canUndo: false, canRedo: false, undo: () => undefined, redo: () => undefined, pushHistory: () => undefined, saving: false, saveConfig: async () => undefined, }; export interface IDesignModeProviderProps { active: boolean; permission?: string; initialConfig: ILayoutEditorConfig; onSave?: (config: ILayoutEditorConfig) => Promise<void>; children: ReactNode; } export function DesignModeProvider({ active, permission = 'portal.design', initialConfig, onSave, children, }: IDesignModeProviderProps) { const [selected, setSelected] = useState<IDesignElement | null>(null); const [config, setConfig] = useState<ILayoutEditorConfig>(initialConfig); const [history, setHistory] = useState<IHistoryEntry[]>([]); const [historyIndex, setHistoryIndex] = useState(-1); const [saving, setSaving] = useState(false); const updateConfig = useCallback((patch: Partial<ILayoutEditorConfig>) => { setConfig(prev => ({...prev, ...patch})); }, []); const pushHistory = useCallback( (description: string) => { setHistory(prev => { const newHistory = prev.slice(0, historyIndex + 1); return [...newHistory, {config, description}]; }); setHistoryIndex(prev => prev + 1); }, [config, historyIndex], ); const undo = useCallback(() => { if (historyIndex >= 0) { setConfig(history[historyIndex].config); setHistoryIndex(prev => prev - 1); } }, [history, historyIndex]); const redo = useCallback(() => { if (historyIndex < history.length - 1) { const next = history[historyIndex + 1]; setConfig(next.config); setHistoryIndex(prev => prev + 1); } }, [history, historyIndex]); const saveConfig = useCallback(async () => { if (!onSave) return; setSaving(true); try { await onSave(config); } finally { setSaving(false); } }, [onSave, config]); if (!active) { return ( <DesignModeContext value={inertContext}>{children}</DesignModeContext> ); } const value: IDesignModeContextValue = { active, selected, select: setSelected, config, updateConfig, permission, canUndo: historyIndex >= 0, canRedo: historyIndex < history.length - 1, undo, redo, pushHistory, saving, saveConfig, }; return <DesignModeContext value={value}>{children}</DesignModeContext>; } |