|
1 | 1 | <script setup lang="ts"> |
2 | | -import type { ClientScriptEntry, DevToolsDockEntry } from '@vitejs/devtools-kit' |
| 2 | +import type { DevToolsDockEntry } from '@vitejs/devtools-kit' |
| 3 | +import type { ImportScriptContext } from '@vitejs/devtools-kit/client' |
3 | 4 | import type { DockProps } from './DockProps' |
4 | 5 | import { useEventListener, useScreenSafeArea } from '@vueuse/core' |
5 | 6 | import { computed, onMounted, reactive, ref, toRefs, useTemplateRef, watchEffect } from 'vue' |
@@ -215,28 +216,46 @@ const panelStyle = computed(() => { |
215 | 216 | }) |
216 | 217 |
|
217 | 218 | // TODO: use a visual module to host all the imports |
218 | | -function importScript(entry: ClientScriptEntry) { |
219 | | - return import(/* @vite-ignore */ entry.importFrom) |
| 219 | +function importScript(entry: DevToolsDockEntry): Promise<(context: ImportScriptContext) => void | Promise<void>> { |
| 220 | + const id = entry.id |
| 221 | + return import(/* vite-ignore */ ['/.devtools', 'imports'].join('-')) |
220 | 222 | .then((module) => { |
221 | | - return module[entry.importName || 'default'] |
| 223 | + const importsMap = module.importsMap as Record<string, () => Promise<() => void>> |
| 224 | + const importFn = importsMap[id] |
| 225 | + if (!importFn) { |
| 226 | + return Promise.reject(new Error(`[VITE DEVTOOLS] No import found for id: ${id}`)) |
| 227 | + } |
| 228 | + return importFn() |
222 | 229 | }) |
223 | 230 | .catch((error) => { |
224 | 231 | // TODO: maybe popup a error toast here? |
225 | | - console.error('[VITE DEVTOOLS] Error importing action', error) |
| 232 | + console.error('[VITE DEVTOOLS] Error executing import action', error) |
226 | 233 | return Promise.reject(error) |
227 | 234 | }) |
228 | 235 | } |
229 | 236 |
|
230 | 237 | function onSelected(entry: DevToolsDockEntry) { |
231 | | - if (entry?.type === 'action') { |
232 | | - return importScript(entry.import).then(fn => fn()) |
233 | | - } |
234 | | -
|
235 | | - state.value.dockEntry = entry |
| 238 | + const context: ImportScriptContext = reactive({ |
| 239 | + dockEntry: entry, |
| 240 | + // @ts-expect-error cast for unwraping |
| 241 | + dockState: computed<'active' | 'inactive'>({ |
| 242 | + get() { |
| 243 | + return state.value.dockEntry?.id === entry.id ? 'active' : 'inactive' |
| 244 | + }, |
| 245 | + set(val) { |
| 246 | + if (val === 'active') |
| 247 | + state.value.dockEntry = entry |
| 248 | + else if (state.value.dockEntry?.id === entry.id) |
| 249 | + state.value.dockEntry = undefined |
| 250 | + }, |
| 251 | + }), |
| 252 | + hidePanel() { |
| 253 | + state.value.open = false |
| 254 | + }, |
| 255 | + }) |
236 | 256 |
|
237 | | - // TODO: handle for each type of entry |
238 | | - if ('import' in entry && entry.import) { |
239 | | - importScript(entry.import) |
| 257 | + if (entry?.type === 'action') { |
| 258 | + return importScript(entry).then(fn => fn(context)) |
240 | 259 | } |
241 | 260 | } |
242 | 261 |
|
|
0 commit comments