File size: 5,765 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
import { computed, ref } from 'vue'
import { useRoute } from 'vue-router'
import { useApps } from '@front/features/apps'
import type { Bridge } from '@vue-devtools/shared-utils'
import { BridgeEvents, getStorage, parse, searchDeepInObject, setStorage } from '@vue-devtools/shared-utils'
import { getBridge, useBridge } from '@front/features/bridge'
export interface InspectorFromBackend {
id: string
appId: string
pluginId: string
label: string
icon: string
treeFilterPlaceholder: string
stateFilterPlaceholder: string
noSelectionText: string
actions?: {
icon: string
tooltip?: string
}[]
nodeActions?: {
icon: string
tooltip?: string
}[]
}
export interface Inspector extends InspectorFromBackend {
rootNodes: any[]
treeFilter: string
selectedNodeId: string
selectedNode: any
stateFilter: string
state: any
expandedMap: Record<string, boolean>
}
const SELECTED_NODES_STORAGE = 'custom-inspector-selected-nodes'
let selectedIdsStorage = {}
function inspectorFactory(options: InspectorFromBackend): Inspector {
return {
...options,
rootNodes: [],
treeFilter: '',
selectedNodeId: selectedIdsStorage[options.id] || null,
selectedNode: null,
stateFilter: '',
state: null,
expandedMap: {},
}
}
const inspectors = ref<Inspector[]>([])
export function useInspectors() {
const { currentAppId } = useApps()
const currentAppInspectors = computed(() => inspectors.value.filter(i => i.appId === currentAppId.value))
return {
inspectors: currentAppInspectors,
}
}
export function useCurrentInspector() {
const route = useRoute()
const { inspectors } = useInspectors()
const { bridge } = useBridge()
const currentInspector = computed(() => inspectors.value.find(i => i.id === route.params.inspectorId))
const filteredState = computed(() => {
if (currentInspector.value?.stateFilter) {
const result = {}
for (const groupKey in currentInspector.value.state) {
const group = currentInspector.value.state[groupKey]
const groupFields = group.filter(el => searchDeepInObject({
[el.key]: el.value,
}, currentInspector.value?.stateFilter))
if (groupFields.length) {
result[groupKey] = groupFields
}
}
return result
}
else {
return currentInspector.value?.state
}
})
function selectNode(node) {
currentInspector.value.selectedNodeId = node.id
currentInspector.value.selectedNode = node
selectedIdsStorage[currentInspector.value.id] = node.id
setStorage(SELECTED_NODES_STORAGE, selectedIdsStorage)
fetchState(currentInspector.value)
}
function refreshInspector() {
refreshTree()
refreshState()
}
function refreshTree() {
fetchTree(currentInspector.value)
}
function refreshState() {
fetchState(currentInspector.value)
}
function editState(path: string, payload: any, type: string) {
bridge.send(BridgeEvents.TO_BACK_CUSTOM_INSPECTOR_EDIT_STATE, {
inspectorId: currentInspector.value.id,
appId: currentInspector.value.appId,
nodeId: currentInspector.value.selectedNodeId,
path,
type,
payload,
})
}
return {
currentInspector,
filteredState,
selectNode,
refreshInspector,
refreshTree,
refreshState,
editState,
}
}
function fetchInspectors() {
getBridge().send(BridgeEvents.TO_BACK_CUSTOM_INSPECTOR_LIST, {})
}
function fetchTree(inspector: Inspector) {
if (!inspector) {
return
}
getBridge().send(BridgeEvents.TO_BACK_CUSTOM_INSPECTOR_TREE, {
inspectorId: inspector.id,
appId: inspector.appId,
treeFilter: inspector.treeFilter,
})
}
function fetchState(inspector: Inspector) {
if (!inspector || !inspector.selectedNodeId) {
return
}
getBridge().send(BridgeEvents.TO_BACK_CUSTOM_INSPECTOR_STATE, {
inspectorId: inspector.id,
appId: inspector.appId,
nodeId: inspector.selectedNodeId,
})
}
export function resetInspectors() {
inspectors.value = []
fetchInspectors()
}
export function setupCustomInspectorBridgeEvents(bridge: Bridge) {
selectedIdsStorage = getStorage(SELECTED_NODES_STORAGE, {})
bridge.on(BridgeEvents.TO_FRONT_CUSTOM_INSPECTOR_LIST, ({ inspectors: list }) => {
list.forEach((inspector) => {
if (!inspectors.value.some(i => i.id === inspector.id && i.appId === inspector.appId)) {
inspectors.value.push(inspectorFactory(inspector))
}
})
})
bridge.on(BridgeEvents.TO_FRONT_CUSTOM_INSPECTOR_ADD, () => {
fetchInspectors()
})
bridge.on(BridgeEvents.TO_FRONT_CUSTOM_INSPECTOR_TREE, ({ appId, inspectorId, rootNodes }) => {
const inspector = inspectors.value.find(i => i.id === inspectorId && i.appId === appId)
if (!inspector) {
console.warn(`Inspector ${inspectorId} not found`)
return
}
inspector.rootNodes = rootNodes
})
bridge.on(BridgeEvents.TO_FRONT_CUSTOM_INSPECTOR_STATE, ({ appId, inspectorId, state }) => {
const inspector = inspectors.value.find(i => i.id === inspectorId && i.appId === appId)
if (!inspector) {
console.warn(`Inspector ${inspectorId} not found`)
return
}
inspector.state = parse(state)
})
bridge.on(BridgeEvents.TO_FRONT_CUSTOM_INSPECTOR_SELECT_NODE, ({ appId, inspectorId, nodeId }) => {
const inspector = inspectors.value.find(i => i.id === inspectorId && i.appId === appId)
if (!inspector) {
console.warn(`Inspector ${inspectorId} not found`)
return
}
inspector.selectedNodeId = nodeId
inspector.selectedNode = null
selectedIdsStorage[inspector.id] = nodeId
setStorage(SELECTED_NODES_STORAGE, selectedIdsStorage)
fetchState(inspector)
})
resetInspectors()
}
|