| 1 |
import { useEffect } from 'react'; |
| 2 |
import { useEditorStore } from '../store/useEditorStore'; |
| 3 |
|
| 4 |
export { useEditorStore as useEditor }; |
| 5 |
|
| 6 |
// EditorProvider wires up external event listeners that feed the store. |
| 7 |
// No React context needed — the store is global. |
| 8 |
export const EditorProvider = ({ children }) => { |
| 9 |
const setContextBlocks = useEditorStore((s) => s.setContextBlocks); |
| 10 |
const updateSelectedBlock = useEditorStore((s) => s.updateSelectedBlock); |
| 11 |
|
| 12 |
// Listen for context_blocks_changed from BlockContextPicker |
| 13 |
useEffect(() => { |
| 14 |
const appBridge = window.zipAiMcpAppBridge; |
| 15 |
if (!appBridge) return; |
| 16 |
const handler = (data) => setContextBlocks(data.context_blocks || []); |
| 17 |
appBridge.on('context_blocks_changed', handler); |
| 18 |
return () => appBridge.off('context_blocks_changed'); |
| 19 |
}, [setContextBlocks]); |
| 20 |
|
| 21 |
// Subscribe to Gutenberg block selection via wp.data |
| 22 |
useEffect(() => { |
| 23 |
if (!window.wp?.data) return; |
| 24 |
const { subscribe, select } = window.wp.data; |
| 25 |
let prevId = null; |
| 26 |
|
| 27 |
const unsubscribe = subscribe(() => { |
| 28 |
const blockEditorSelect = select('core/block-editor'); |
| 29 |
if (!blockEditorSelect) return; |
| 30 |
const id = blockEditorSelect.getSelectedBlockClientId(); |
| 31 |
if (id === prevId) return; |
| 32 |
prevId = id; |
| 33 |
if (id) { |
| 34 |
const block = blockEditorSelect.getSelectedBlock(); |
| 35 |
if (block) { |
| 36 |
const utils = window.zipAiMcpSpectraUtils; |
| 37 |
const serialized = utils?.serializeBlockLight |
| 38 |
? utils.serializeBlockLight(block) |
| 39 |
: { clientId: block.clientId, name: block.name }; |
| 40 |
updateSelectedBlock(serialized); |
| 41 |
} else { |
| 42 |
updateSelectedBlock(null); |
| 43 |
} |
| 44 |
} else { |
| 45 |
updateSelectedBlock(null); |
| 46 |
} |
| 47 |
}); |
| 48 |
|
| 49 |
return () => unsubscribe(); |
| 50 |
}, [updateSelectedBlock]); |
| 51 |
|
| 52 |
return children; |
| 53 |
}; |
| 54 |
|