-
-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathIndex.vue
224 lines (193 loc) · 7.35 KB
/
Index.vue
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
219
220
221
222
223
224
<script setup lang="ts">
import { computed, onUnmounted, ref, watch } from 'vue'
import { Pane, Splitpanes } from 'splitpanes'
import { DevToolsMessagingEvents, rpc } from '@vue/devtools-core'
import { parse } from '@vue/devtools-kit'
import type { CustomInspectorNode, CustomInspectorOptions, CustomInspectorState } from '@vue/devtools-kit'
import { vTooltip } from '@vue/devtools-ui'
import Navbar from '~/components/basic/Navbar.vue'
import DevToolsHeader from '~/components/basic/DevToolsHeader.vue'
import Empty from '~/components/basic/Empty.vue'
import RootStateViewer from '~/components/state/RootStateViewer.vue'
import ComponentTree from '~/components/tree/TreeViewer.vue'
import { getValidNodeId } from '~/utils'
import { createExpandedContext } from '~/composables/toggle-expanded'
const props = defineProps<{
savedSelectedId?: string
}>()
const emit = defineEmits<{
(e: 'onSelectId', id: string): void
}>()
const { expanded: expandedTreeNodes } = createExpandedContext()
const { expanded: expandedStateNodes } = createExpandedContext('pinia-store-state')
const inspectorId = 'pinia'
const nodeActions = ref<CustomInspectorOptions['nodeActions']>([])
const actions = ref<CustomInspectorOptions['nodeActions']>([])
const selected = ref('')
const tree = ref<CustomInspectorNode[]>([])
const treeNodeLinkedList = computed(() => tree.value?.length ? dfs(tree.value?.[0]) : [])
const flattenedTreeNodes = computed(() => flattenTreeNodes(tree.value))
const flattenedTreeNodesIds = computed(() => flattenedTreeNodes.value.map(node => node.id))
const state = ref<Record<string, CustomInspectorState[]>>({})
const emptyState = computed(() => !state.value.state?.length && !state.value.getters?.length)
// tree
function dfs(node: { id: string, children?: { id: string }[] }, path: string[] = [], linkedList: string[][] = []) {
path.push(node.id)
if (node.children?.length === 0)
linkedList.push([...path])
if (Array.isArray(node.children)) {
node.children.forEach((child) => {
dfs(child, path, linkedList)
})
}
path.pop()
return linkedList
}
function getNodesByDepth(list: string[][], depth: number) {
const nodes: string[] = []
list?.forEach((item) => {
nodes.push(...item.slice(0, depth + 1))
})
return [...new Set(nodes)]
}
function flattenTreeNodes(tree: CustomInspectorNode[]) {
const res: CustomInspectorNode[] = []
const find = (treeNode: CustomInspectorNode[]) => {
treeNode?.forEach((item) => {
res.push(item)
if (item.children?.length)
find(item.children)
})
}
find(tree)
return res
}
function getNodeActions() {
rpc.value.getInspectorNodeActions(inspectorId).then((actions) => {
nodeActions.value = actions
})
}
function getActions() {
rpc.value.getInspectorActions(inspectorId).then((_actions) => {
actions.value = _actions
})
}
getNodeActions()
getActions()
function callNodeAction(index: number) {
rpc.value.callInspectorNodeAction(inspectorId, index, selected.value)
}
function callAction(index: number) {
rpc.value.callInspectorAction(inspectorId, index)
}
function filterEmptyState(data: Record<string, unknown[] | undefined>) {
for (const key in data) {
if (!data[key]?.length)
delete data[key]
}
return data
}
function getPiniaState(nodeId: string) {
rpc.value.getInspectorState({ inspectorId, nodeId }).then((data) => {
const parsedData = parse(data!)
if (!parsedData)
return
// @ts-expect-error skip type check
state.value = filterEmptyState(parsedData)
expandedStateNodes.value = Array.from({ length: Object.keys(state.value).length }, (_, i) => `${i}`)
})
}
function clearPiniaState() {
state.value = {}
}
watch(selected, () => {
clearPiniaState()
getPiniaState(selected.value)
emit('onSelectId', selected.value)
})
const getPiniaInspectorTree = () => {
rpc.value.getInspectorTree({ inspectorId, filter: '' }).then((_data) => {
const data = parse(_data!)
tree.value = data
if (!selected.value && data.length) {
selected.value = getValidNodeId(tree.value, props.savedSelectedId) || data[0].id
getPiniaState(selected.value)
expandedTreeNodes.value = getNodesByDepth(treeNodeLinkedList.value, 1)
}
})
}
getPiniaInspectorTree()
function onInspectorTreeUpdated(_data: string) {
const data = parse(_data) as {
inspectorId: string
rootNodes: CustomInspectorNode[]
}
if (data.inspectorId !== inspectorId || !data.rootNodes.length)
return
tree.value = data.rootNodes as unknown as CustomInspectorNode[]
if (!flattenedTreeNodesIds.value.includes(selected.value)) {
selected.value = data.rootNodes[0].id
expandedTreeNodes.value = getNodesByDepth(treeNodeLinkedList.value, 1)
getPiniaState(data.rootNodes[0].id)
}
}
function onInspectorStateUpdated(_data: string) {
const data = parse(_data) as {
inspectorId: string
state: CustomInspectorState
nodeId: string
}
if (data.inspectorId !== inspectorId)
return
const _state = data.state
// @ts-expect-error skip type check
state.value = filterEmptyState({
state: _state.state,
getters: _state.getters,
})
}
rpc.functions.on(DevToolsMessagingEvents.INSPECTOR_TREE_UPDATED, onInspectorTreeUpdated)
rpc.functions.on(DevToolsMessagingEvents.INSPECTOR_STATE_UPDATED, onInspectorStateUpdated)
onUnmounted(() => {
rpc.functions.off(DevToolsMessagingEvents.INSPECTOR_TREE_UPDATED, onInspectorTreeUpdated)
rpc.functions.off(DevToolsMessagingEvents.INSPECTOR_STATE_UPDATED, onInspectorStateUpdated)
})
</script>
<template>
<div class="h-full flex flex-col">
<DevToolsHeader doc-link="https://pinia.vuejs.org/" github-repo-link="https://github.com/vuejs/pinia">
<Navbar />
</DevToolsHeader>
<Splitpanes class="flex-1 overflow-auto">
<Pane border="r base" size="40" h-full>
<div class="h-full flex flex-col p2">
<div v-if="actions?.length" class="mb-1 flex justify-end pb-1" border="b dashed base">
<div class="flex items-center gap-2 px-1">
<div v-for="(action, index) in actions" :key="index" v-tooltip.bottom-end="{ content: action.tooltip }" class="flex items-center gap1" @click="callAction(index)">
<i :class="`i-ic-baseline-${action.icon.replace(/\_/g, '-')}`" cursor-pointer op70 text-base hover:op100 />
</div>
</div>
</div>
<div class="no-scrollbar flex-1 select-none overflow-scroll">
<ComponentTree v-model="selected" :data="tree" />
</div>
</div>
</Pane>
<Pane size="60">
<div class="h-full flex flex-col p2">
<div v-if="nodeActions?.length" class="mb-1 flex justify-end pb-1" border="b dashed base">
<div class="flex items-center gap-2 px-1">
<div v-for="(action, index) in nodeActions" :key="index" v-tooltip.bottom-end="{ content: action.tooltip }" class="flex items-center gap1" @click="callNodeAction(index)">
<i :class="`i-ic-baseline-${action.icon.replace(/\_/g, '-')}`" cursor-pointer op70 text-base hover:op100 />
</div>
</div>
</div>
<RootStateViewer v-if="selected && !emptyState" class="no-scrollbar flex-1 select-none overflow-scroll" :data="state" :node-id="selected" :inspector-id="inspectorId" expanded-state-id="pinia-store-state" />
<Empty v-else>
No Data
</Empty>
</div>
</Pane>
</Splitpanes>
</div>
</template>