(props: {
locatorEnabled: s.Accessor<boolean>,
setLocatorEnabledSignal(signal: s.Accessor<boolean>): void,
onComponentClick(componentId: NodeID, next: VoidFunction): void,
emit: OutputEmit,
component_registry: walker.ComponentRegistry<TEl>,
})
| 40 | } |
| 41 | |
| 42 | export function createLocator<TEl extends object>(props: { |
| 43 | locatorEnabled: s.Accessor<boolean>, |
| 44 | setLocatorEnabledSignal(signal: s.Accessor<boolean>): void, |
| 45 | onComponentClick(componentId: NodeID, next: VoidFunction): void, |
| 46 | emit: OutputEmit, |
| 47 | component_registry: walker.ComponentRegistry<TEl>, |
| 48 | }) { |
| 49 | const [enabledByPressingSignal, setEnabledByPressingSignal] = s.createSignal((): boolean => false) |
| 50 | props.setLocatorEnabledSignal(s.createMemo(() => enabledByPressingSignal()())) |
| 51 | |
| 52 | const [hoverTarget, setHoverTarget] = s.createSignal<TEl | null>(null) |
| 53 | const [devtoolsTarget, setDevtoolsTarget] = s.createSignal<locator.HighlightElementPayload>(null) |
| 54 | |
| 55 | const [highlightedComponents, setHighlightedComponents] = s.createSignal<LocatorComponent<TEl>[]>([]) |
| 56 | |
| 57 | function getLocatorComponentFromElement( |
| 58 | el: TEl, |
| 59 | ): LocatorComponent<TEl> | null { |
| 60 | let comp = walker.findComponent(props.component_registry, el) |
| 61 | return comp && { |
| 62 | location: props.component_registry.eli.getLocation(el), |
| 63 | element: el, |
| 64 | id: comp.id, |
| 65 | name: comp.name, |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | const target = s.createMemo(() => { |
| 70 | let hover = hoverTarget() |
| 71 | return hover != null |
| 72 | ? { |
| 73 | type: 'hover' as const, |
| 74 | element: hover, |
| 75 | } |
| 76 | : devtoolsTarget() |
| 77 | }, undefined, { |
| 78 | equals: (a, b) => { |
| 79 | if (a === b) return true |
| 80 | if (a == null && b == null) return true |
| 81 | if (a == null || b == null) return false |
| 82 | if (a.type !== b.type) return false |
| 83 | switch (a.type) { |
| 84 | case 'hover': return a.element === (b as any).element |
| 85 | case 'node': return a.id === (b as any).id |
| 86 | case 'element': return a.id === (b as any).id |
| 87 | } |
| 88 | }, |
| 89 | }) |
| 90 | |
| 91 | s.createEffect(defer(target, scheduleIdle(target => { |
| 92 | |
| 93 | let locator_components: LocatorComponent<TEl>[] = [] |
| 94 | |
| 95 | if (target != null) { |
| 96 | switch (target.type) { |
| 97 | case 'hover': { |
| 98 | unwrap_append(locator_components, getLocatorComponentFromElement(target.element)) |
| 99 | break |
no test coverage detected
searching dependent graphs…