File size: 1,185 Bytes
4d70170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { ref } from 'vue'
import { BridgeEvents } from '@vue-devtools/shared-utils'
import { useBridge } from '@front/features/bridge'
import { setComponentOpen, useComponentRequests } from '.'

export function useComponentPick() {
  const { bridge, onBridge } = useBridge()
  const { selectComponent, requestComponentTree } = useComponentRequests()

  const pickingComponent = ref(false)

  function startPickingComponent() {
    pickingComponent.value = true
    bridge.send(BridgeEvents.TO_BACK_COMPONENT_PICK)
  }

  function stopPickingComponent() {
    pickingComponent.value = false
    bridge.send(BridgeEvents.TO_BACK_COMPONENT_PICK_CANCELED)
  }

  onBridge(BridgeEvents.TO_FRONT_COMPONENT_PICK, ({ id, parentIds }) => {
    pickingComponent.value = false
    selectComponent(id)
    parentIds.reverse().forEach((id) => {
      // Ignore root
      if (id.endsWith('root')) {
        return
      }
      setComponentOpen(id, true)
      requestComponentTree(id)
    })
  })

  onBridge(BridgeEvents.TO_FRONT_COMPONENT_PICK_CANCELED, () => {
    pickingComponent.value = false
  })

  return {
    pickingComponent,
    startPickingComponent,
    stopPickingComponent,
  }
}