import { reactive, computed, inject, InjectionKey } from 'vue'; import { StoreKey } from '../core/store'; import { bus } from '../core/events'; export const StudioKey: InjectionKey> = Symbol('StudioKey'); export function createStudio() { const store = inject(StoreKey); if (!store) { throw new Error("Lumina Studio requires Lumina Store."); } const state = reactive({ isEnabled: false, selectedElementPath: null as string | null, selectedElementSchema: null as string | null, // 'title', 'image', etc. selectionRect: null as DOMRect | null, }); const toggle = () => { state.isEnabled = !state.isEnabled; if (!state.isEnabled) { clearSelection(); } }; const selectElement = (path: string, schema: string, rect?: DOMRect) => { if (!state.isEnabled) return; state.selectedElementPath = path; state.selectedElementSchema = schema; if (rect) state.selectionRect = rect; }; const clearSelection = () => { state.selectedElementPath = null; state.selectedElementSchema = null; state.selectionRect = null; }; const currentSlide = computed(() => store.currentSlide()); // Helper to get nested value by path string "elements.0.title" const getValueByPath = (obj: any, path: string) => { return path.split('.').reduce((acc, part) => acc && acc[part], obj); }; const selectedData = computed(() => { if (!state.selectedElementPath || !currentSlide.value) return null; // If path is empty or 'root', return slide itself if (state.selectedElementPath === 'root') return currentSlide.value; return getValueByPath(currentSlide.value, state.selectedElementPath); }); const updateSelected = (payload: any) => { if (!state.selectedElementPath || !store.state.deck) return; const slideIndex = store.state.currentIndex; // If path is 'root', we are updating the slide itself const relativePath = state.selectedElementPath === 'root' ? '' : '.' + state.selectedElementPath; const fullPath = `slides.${slideIndex}${relativePath}`; // Update the store directly if (store.updateNode) { store.updateNode(fullPath, payload); } // Also emit for external listeners if needed bus.emit('studio:update', { path: fullPath, value: payload }); }; return { state, toggle, selectElement, clearSelection, selectedData, updateSelected }; } export function useStudio() { const studio = inject(StudioKey); if (!studio) { throw new Error("useStudio must be used within LuminaStudio"); } return studio; }