|
1
|
import { useContext } from "react" |
|
2
|
import { LayoutCustomizerHostContext } from "../providers/customizer-host-provider/customizer-host-context" |
|
3
|
|
|
4
|
/** |
|
5
|
* DOM node where a composer's customizer controls portal into for the given |
|
6
|
* `location`. `null` if no matching host is mounted (e.g. the slot lives in a |
|
7
|
* dropdown that is currently closed, or `LayoutComposer` is used outside the |
|
8
|
* shell). |
|
9
|
*/ |
|
10
|
export const useLayoutCustomizerTriggerHost = ( |
|
11
|
location: string |
|
12
|
): HTMLElement | null => { |
|
13
|
return useContext(LayoutCustomizerHostContext)?.hosts[location] ?? null |
|
14
|
} |
|
15
|
|
|
16
|
/** |
|
17
|
* Shared single-edit-session registry. Composers register their id while |
|
18
|
* editing so others can lock their trigger to avoid multiple edits at once. |
|
19
|
*/ |
|
20
|
export const useLayoutCustomizerActiveEditor = (): { |
|
21
|
activeEditor: string | null |
|
22
|
setActiveEditor: (id: string | null) => void |
|
23
|
} => { |
|
24
|
const ctx = useContext(LayoutCustomizerHostContext) |
|
25
|
return { |
|
26
|
activeEditor: ctx?.activeEditor ?? null, |
|
27
|
setActiveEditor: ctx?.setActiveEditor ?? (() => {}), |
|
28
|
} |
|
29
|
} |
|
30
|
|
|
31
|
/** |
|
32
|
* One-shot command bus connecting the central `CustomizerMenu` to the |
|
33
|
* composers. The menu sets a host's `customizeId` as the request; the matching |
|
34
|
* composer reads it, enters edit mode, and clears it. |
|
35
|
*/ |
|
36
|
export const useLayoutEditRequest = (): { |
|
37
|
editRequest: string | null |
|
38
|
requestEdit: (id: string | null) => void |
|
39
|
} => { |
|
40
|
const ctx = useContext(LayoutCustomizerHostContext) |
|
41
|
return { |
|
42
|
editRequest: ctx?.editRequest ?? null, |
|
43
|
requestEdit: ctx?.requestEdit ?? (() => {}), |
|
44
|
} |
|
45
|
} |