@binblink/wasmnest-plugin-sdk-desktop 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +287 -2
  2. package/README_en.md +284 -0
  3. package/bin/cli.js +602 -0
  4. package/bin/manifest-gen.js +454 -0
  5. package/bin/manifest-validate.js +432 -0
  6. package/bin/permission-vocabulary.json +5 -0
  7. package/bin/plugin-watch.js +331 -0
  8. package/bin/wasm-hash.js +124 -0
  9. package/dev-shell/README.md +121 -0
  10. package/dev-shell/index.html +12 -0
  11. package/dev-shell/package.json +28 -0
  12. package/dev-shell/pnpm-workspace.yaml +9 -0
  13. package/dev-shell/postcss.config.js +6 -0
  14. package/dev-shell/src/App.vue +90 -0
  15. package/dev-shell/src/components/LogPanel.vue +90 -0
  16. package/dev-shell/src/loader.ts +109 -0
  17. package/dev-shell/src/locale.ts +29 -0
  18. package/dev-shell/src/locales.ts +50 -0
  19. package/dev-shell/src/main.ts +46 -0
  20. package/dev-shell/src/mock/agent-hub-commands.ts +1065 -0
  21. package/dev-shell/src/mock/agent-hub.ts +42 -0
  22. package/dev-shell/src/mock/app-env.ts +102 -0
  23. package/dev-shell/src/mock/commands.ts +54 -0
  24. package/dev-shell/src/mock/domain-context.ts +46 -0
  25. package/dev-shell/src/mock/events.ts +16 -0
  26. package/dev-shell/src/mock/file-transfer.ts +512 -0
  27. package/dev-shell/src/mock/tauri-ipc.ts +109 -0
  28. package/dev-shell/src/registry.ts +90 -0
  29. package/dev-shell/src/shell/components/AppMountSlot.vue +241 -0
  30. package/dev-shell/src/shell/components/ShellIcon.vue +141 -0
  31. package/dev-shell/src/shell/components/ShellStatusBar.vue +87 -0
  32. package/dev-shell/src/shell/components/ShellToolbar.vue +287 -0
  33. package/dev-shell/src/shell/mount/appSession.ts +45 -0
  34. package/dev-shell/src/shell/mount/devAppMount.ts +80 -0
  35. package/dev-shell/src/shell/types.ts +132 -0
  36. package/dev-shell/src/styles/dev.css +27 -0
  37. package/dev-shell/src/styles/shell.css +546 -0
  38. package/dev-shell/src/styles/style.css +1132 -0
  39. package/dev-shell/src/theme.ts +140 -0
  40. package/dev-shell/src/vite-env.d.ts +24 -0
  41. package/dev-shell/tailwind.config.js +98 -0
  42. package/dev-shell/vite.config.ts +174 -0
  43. package/dist/index.d.ts +109 -0
  44. package/dist/index.js +170 -0
  45. package/dist/types.d.ts +585 -0
  46. package/dist/types.js +0 -0
  47. package/dist/vite-plugin.d.ts +25 -0
  48. package/dist/vite-plugin.js +80 -0
  49. package/package.json +89 -3
  50. package/src/config.ts +49 -0
  51. package/src/global-dialog.ts +169 -0
  52. package/src/index.ts +80 -0
  53. package/src/runtime.ts +54 -0
  54. package/src/types.ts +630 -0
  55. package/src/ui/MarkdownEditor.vue +520 -0
  56. package/src/ui/PluginGlobalDialog.vue +401 -0
  57. package/src/ui/Select.d.ts +36 -0
  58. package/src/ui/Select.vue +220 -0
  59. package/src/ui/TerminalInputRail.vue +184 -0
  60. package/src/ui/apply-select-position.ts +65 -0
  61. package/src/ui/markdown-editor-syntax.ts +234 -0
  62. package/src/ui/markdown-editor.d.ts +36 -0
  63. package/src/ui/plugin-global-dialog.d.ts +12 -0
  64. package/src/ui/select-position.ts +79 -0
  65. package/src/ui/terminal-input-rail.d.ts +30 -0
  66. package/src/ui/zoom-compensation.d.ts +9 -0
  67. package/src/ui/zoom-compensation.ts +52 -0
  68. package/src/vite-plugin.ts +124 -0
  69. package/template/README.md +39 -0
  70. package/template/package.json +23 -0
  71. package/template/plugin.json +14 -0
  72. package/template/rust/Cargo.toml +21 -0
  73. package/template/rust/src/lib.rs +39 -0
  74. package/template/src/env.d.ts +24 -0
  75. package/template/src/index.ts +37 -0
  76. package/template/tsconfig.json +17 -0
  77. package/template/vite.config.ts +21 -0
  78. package/tools/componentize/Cargo.lock +270 -0
  79. package/tools/componentize/Cargo.toml +15 -0
  80. package/tools/componentize/src/main.rs +94 -0
@@ -0,0 +1,401 @@
1
+ <template>
2
+ <Teleport to="body">
3
+ <Transition name="pgd">
4
+ <div
5
+ v-if="entry"
6
+ class="pgd-overlay"
7
+ role="dialog"
8
+ aria-modal="true"
9
+ @click.self="onBackdrop"
10
+ >
11
+ <div class="pgd-card" :class="entry.widthClass ?? 'max-w-md'">
12
+ <!-- 预设模式:标题 + 倒计时 + 正文 + 动作按钮 -->
13
+ <template v-if="!entry.content">
14
+ <div class="pgd-head px-5 pt-4">
15
+ <span class="pgd-title">{{ entry.title }}</span>
16
+ <span
17
+ v-if="countdownText"
18
+ class="pgd-countdown"
19
+ :class="{ 'pgd-countdown--urgent': countdownSeconds !== null && countdownSeconds <= 10 }"
20
+ >
21
+ {{ countdownText }}
22
+ </span>
23
+ </div>
24
+ <div class="px-5 py-4">
25
+ <div v-if="entry.message" class="pgd-message">{{ entry.message }}</div>
26
+ <div
27
+ v-else-if="!entry.actions?.length && !countdownText"
28
+ class="pgd-message pgd-message--empty"
29
+ >
30
+ {{ '' }}
31
+ </div>
32
+ </div>
33
+ <div v-if="entry.actions?.length" class="pgd-actions px-5 pb-4">
34
+ <button
35
+ v-for="(action, i) in entry.actions"
36
+ :key="i"
37
+ class="pgd-btn"
38
+ :class="`pgd-btn--${action.kind ?? 'default'}`"
39
+ :disabled="action.disabled"
40
+ @click="onAction(action)"
41
+ >
42
+ {{ action.label }}
43
+ </button>
44
+ </div>
45
+ </template>
46
+
47
+ <!-- 组件模式:任意插件组件 + pluginContext -->
48
+ <content-shell v-else :entry="entry" :class="entry.bodyClass ?? 'p-0'" />
49
+
50
+ <!-- 关闭按钮(closable 时) -->
51
+ <button
52
+ v-if="entry.closable !== false"
53
+ class="pgd-close"
54
+ :aria-label="''"
55
+ @click="closeCurrent"
56
+ >
57
+ <svg fill="none" stroke="currentColor" viewBox="0 0 24 24" class="pgd-close-ico">
58
+ <path
59
+ stroke-linecap="round"
60
+ stroke-linejoin="round"
61
+ stroke-width="2"
62
+ d="M6 18L18 6M6 6l12 12"
63
+ />
64
+ </svg>
65
+ </button>
66
+ </div>
67
+ </div>
68
+ </Transition>
69
+ </Teleport>
70
+ </template>
71
+
72
+ <script setup lang="ts">
73
+ /**
74
+ * PluginGlobalDialog — 宿主全局弹窗(通用能力,桌面宿主 + dev-shell 共用)
75
+ *
76
+ * 两种模式:
77
+ * - 预设模式:title / message / icon / actions,常见确认/拒绝类请求开箱即用;
78
+ * - 组件模式:content 传入插件任意 Vue 组件(provide('pluginContext')),
79
+ * props 经句柄 update() 热更新。
80
+ *
81
+ * 能力:FIFO 排队(控制器)、可选定时自动关闭 + 倒计时展示({seconds} 占位文案)、
82
+ * 动作按钮(primary/danger/ghost 变体 + 点击后宿主 router 跳转)、
83
+ * Escape/遮罩/右上角关闭(closable 控制)、z-50 覆盖层 + 令牌化样式。
84
+ *
85
+ * 渲染完全由 getGlobalDialog / subscribeGlobalDialog 驱动——组件不透明地
86
+ * 持有任何业务状态,插件侧经 context.ui.showDialog 操控。
87
+ */
88
+ import { computed, defineComponent, h, onBeforeUnmount, onMounted, provide, ref, watch } from 'vue'
89
+ import {
90
+ closeGlobalDialog,
91
+ getGlobalDialog,
92
+ resolveDialogDeadline,
93
+ subscribeGlobalDialog,
94
+ type GlobalDialogEntry,
95
+ } from '../global-dialog'
96
+ import { getRouter } from '../runtime'
97
+ import type { PluginDialogAction } from '../types'
98
+
99
+ /** 当前弹窗(订阅驱动) */
100
+ const entry = ref<GlobalDialogEntry | null>(getGlobalDialog())
101
+ let unsubscribe: (() => void) | null = null
102
+
103
+ /** 倒计时展示 = 截止时刻 - 当前时刻;仅在 entry 配置了 timeoutSec / deadlineAt 时非空 */
104
+ const now = ref(Date.now())
105
+ let tickTimer: ReturnType<typeof setInterval> | null = null
106
+
107
+ const countdownSeconds = computed<number | null>(() => {
108
+ const e = entry.value
109
+ if (!e) return null
110
+ const deadline = resolveDialogDeadline(e)
111
+ if (deadline == null) return null
112
+ return Math.max(0, Math.ceil((deadline - now.value) / 1000))
113
+ })
114
+
115
+ const countdownText = computed<string>(() => {
116
+ const e = entry.value
117
+ const seconds = countdownSeconds.value
118
+ if (!e?.countdownLabel || seconds == null) return ''
119
+ return e.countdownLabel.replace('{seconds}', String(seconds))
120
+ })
121
+
122
+ function startTicking(): void {
123
+ stopTicking()
124
+ now.value = Date.now()
125
+ tickTimer = setInterval(() => {
126
+ now.value = Date.now()
127
+ }, 500)
128
+ }
129
+
130
+ function stopTicking(): void {
131
+ if (tickTimer) {
132
+ clearInterval(tickTimer)
133
+ tickTimer = null
134
+ }
135
+ }
136
+
137
+ /** 关闭当前弹窗(closable 阻止之外的显式关闭路径统一走这里) */
138
+ function closeCurrent(): void {
139
+ const e = entry.value
140
+ if (!e) return
141
+ closeGlobalDialog(e._id)
142
+ }
143
+
144
+ function onBackdrop(): void {
145
+ const e = entry.value
146
+ if (!e || e.closable === false || e.closeOnBackdrop === false) return
147
+ closeGlobalDialog(e._id)
148
+ }
149
+
150
+ function onKeydown(ev: KeyboardEvent): void {
151
+ if (ev.key !== 'Escape') return
152
+ const e = entry.value
153
+ if (!e || e.closable === false) return
154
+ closeGlobalDialog(e._id)
155
+ }
156
+
157
+ /** 预设模式动作点击:onClick 抛错则保持弹窗打开(可重试),成功才关闭并可选跳转 */
158
+ async function onAction(action: PluginDialogAction): Promise<void> {
159
+ try {
160
+ await action.onClick?.()
161
+ } catch (err) {
162
+ console.error('[PluginDialog] action failed, dialog kept open:', err)
163
+ return
164
+ }
165
+ closeCurrent()
166
+ if (action.navigateTo) {
167
+ try {
168
+ getRouter()?.push(action.navigateTo)
169
+ } catch (err) {
170
+ console.warn('[PluginDialog] navigate failed:', err)
171
+ }
172
+ }
173
+ }
174
+
175
+ /**
176
+ * 组件模式外壳:每个条目(:key=_id)新建实例,provide('pluginContext') 供
177
+ * 插件内容组件 inject;内容 emit('close') 关闭弹窗(与预设 actions 同语义)。
178
+ */
179
+ const ContentShell = defineComponent({
180
+ name: 'PluginDialogContentShell',
181
+ props: {
182
+ entry: { type: Object, required: true },
183
+ },
184
+ setup(props) {
185
+ provide('pluginContext', (props.entry as GlobalDialogEntry).pluginContext)
186
+ const content = () =>
187
+ h(
188
+ (props.entry as GlobalDialogEntry).content,
189
+ { ...(props.entry as GlobalDialogEntry).props, onClose: () => closeCurrent() },
190
+ )
191
+ return content
192
+ },
193
+ })
194
+
195
+ // ==================== 订阅与全局键盘 ====================
196
+
197
+ onMounted(() => {
198
+ unsubscribe = subscribeGlobalDialog((e) => {
199
+ entry.value = e
200
+ if (e) startTicking()
201
+ else stopTicking()
202
+ })
203
+ window.addEventListener('keydown', onKeydown)
204
+ })
205
+
206
+ watch(
207
+ () => entry.value?._id,
208
+ () => {
209
+ // entry 变化(排队接替)时刷新一次展示起点,countdown 从新条目 deadline 重算
210
+ now.value = Date.now()
211
+ },
212
+ )
213
+
214
+ onBeforeUnmount(() => {
215
+ unsubscribe?.()
216
+ unsubscribe = null
217
+ stopTicking()
218
+ window.removeEventListener('keydown', onKeydown)
219
+ })
220
+ </script>
221
+
222
+ <style scoped>
223
+ .pgd-overlay {
224
+ position: fixed;
225
+ inset: 0;
226
+ z-index: 50;
227
+ display: flex;
228
+ align-items: center;
229
+ justify-content: center;
230
+ padding: 24px;
231
+ background: rgba(0, 0, 0, 0.5);
232
+ backdrop-filter: blur(4px);
233
+ }
234
+
235
+ .pgd-card {
236
+ position: relative;
237
+ width: 100%;
238
+ display: flex;
239
+ flex-direction: column;
240
+ max-height: 85vh;
241
+ overflow: auto;
242
+ background: var(--bg-card);
243
+ border: 1px solid var(--border);
244
+ border-radius: var(--radius-card);
245
+ box-shadow: var(--shadow-card);
246
+ }
247
+
248
+ /* 预设模式内部排版(组件模式由插件内容自带布局) */
249
+ .pgd-head {
250
+ display: flex;
251
+ align-items: baseline;
252
+ justify-content: space-between;
253
+ gap: 12px;
254
+ }
255
+
256
+ .pgd-title {
257
+ font-size: 15px;
258
+ font-weight: 600;
259
+ color: var(--text-primary);
260
+ }
261
+
262
+ .pgd-countdown {
263
+ font-size: 12px;
264
+ color: var(--text-secondary);
265
+ flex-shrink: 0;
266
+ white-space: nowrap;
267
+ transition: color 0.2s ease;
268
+ font-variant-numeric: tabular-nums;
269
+ }
270
+
271
+ .pgd-countdown--urgent {
272
+ color: var(--color-danger);
273
+ font-weight: 600;
274
+ }
275
+
276
+ .pgd-message {
277
+ margin: 0;
278
+ font-size: 13px;
279
+ line-height: 1.55;
280
+ color: var(--text-secondary);
281
+ overflow-wrap: anywhere;
282
+ }
283
+
284
+ .pgd-actions {
285
+ display: flex;
286
+ justify-content: flex-end;
287
+ gap: 8px;
288
+ margin-top: 4px;
289
+ }
290
+
291
+ .pgd-btn {
292
+ display: inline-flex;
293
+ align-items: center;
294
+ gap: 6px;
295
+ padding: 5px 14px;
296
+ background: var(--bg-hover);
297
+ color: var(--text-primary);
298
+ border: 1px solid var(--border);
299
+ border-radius: var(--radius-button);
300
+ font-size: 12px;
301
+ cursor: pointer;
302
+ transition: border-color 0.2s ease, background-color 0.2s ease, opacity 0.2s ease;
303
+ flex-shrink: 0;
304
+ }
305
+
306
+ .pgd-btn:hover {
307
+ border-color: var(--color-primary);
308
+ background: var(--bg-card);
309
+ }
310
+
311
+ .pgd-btn:disabled {
312
+ opacity: 0.5;
313
+ cursor: not-allowed;
314
+ border-color: var(--border);
315
+ background: var(--bg-hover);
316
+ color: var(--text-secondary);
317
+ }
318
+
319
+ .pgd-btn:disabled:hover {
320
+ border-color: var(--border);
321
+ background: var(--bg-hover);
322
+ }
323
+
324
+ .pgd-btn--primary {
325
+ background: var(--color-primary);
326
+ border-color: var(--color-primary);
327
+ color: var(--color-primary-contrast);
328
+ }
329
+
330
+ .pgd-btn--primary:hover {
331
+ background: var(--color-primary-hover);
332
+ border-color: var(--color-primary-hover);
333
+ }
334
+
335
+ .pgd-btn--danger {
336
+ background: var(--color-danger);
337
+ border-color: var(--color-danger);
338
+ }
339
+
340
+ .pgd-btn--danger:hover {
341
+ background: color-mix(in srgb, var(--color-danger) 85%, black);
342
+ }
343
+
344
+ .pgd-btn--ghost {
345
+ border-color: transparent;
346
+ background: transparent;
347
+ opacity: 0.65;
348
+ }
349
+
350
+ .pgd-btn--ghost:hover {
351
+ opacity: 1;
352
+ border-color: color-mix(in srgb, var(--color-danger) 45%, var(--border));
353
+ color: var(--color-danger);
354
+ background: var(--color-danger-light);
355
+ }
356
+
357
+ .pgd-close {
358
+ position: absolute;
359
+ top: 10px;
360
+ right: 10px;
361
+ padding: 4px;
362
+ border: none;
363
+ background: transparent;
364
+ color: var(--text-tertiary);
365
+ border-radius: 6px;
366
+ cursor: pointer;
367
+ transition: color 0.2s ease, background-color 0.2s ease;
368
+ }
369
+
370
+ .pgd-close:hover {
371
+ color: var(--text-primary);
372
+ background: var(--bg-hover);
373
+ }
374
+
375
+ .pgd-close-ico {
376
+ width: 15px;
377
+ height: 15px;
378
+ display: block;
379
+ }
380
+
381
+ /* 进出场:遮罩淡入 + 卡片上浮(GPU 合成属性) */
382
+ .pgd-enter-active,
383
+ .pgd-leave-active {
384
+ transition: opacity 0.2s ease;
385
+ }
386
+
387
+ .pgd-enter-active .pgd-card,
388
+ .pgd-leave-active .pgd-card {
389
+ transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
390
+ }
391
+
392
+ .pgd-enter-from,
393
+ .pgd-leave-to {
394
+ opacity: 0;
395
+ }
396
+
397
+ .pgd-enter-from .pgd-card,
398
+ .pgd-leave-to .pgd-card {
399
+ transform: translateY(8px);
400
+ }
401
+ </style>
@@ -0,0 +1,36 @@
1
+ import type { DefineComponent } from 'vue'
2
+
3
+ /** 下拉选项(value 同时作为 v-model 绑定值) */
4
+ export interface SelectOption {
5
+ value: string | number
6
+ label: string
7
+ }
8
+
9
+ /** Select 组件 props */
10
+ export interface SelectProps {
11
+ modelValue: string | number
12
+ /** 顶部标签文本(可选) */
13
+ label?: string
14
+ options: SelectOption[]
15
+ /** 未选中时的占位文本;设置后下拉顶部出现"清除选择"行 */
16
+ placeholder?: string
17
+ disabled?: boolean
18
+ required?: boolean
19
+ error?: string
20
+ /** md:宿主表单默认(--input-height 36px);sm:插件紧凑布局(32px/12px) */
21
+ size?: 'md' | 'sm'
22
+ }
23
+
24
+ /**
25
+ * 宿主共享下拉选择组件(替代原生 <select>)
26
+ *
27
+ * 用法:
28
+ * ```vue
29
+ * <Select v-model="value" :options="options" size="sm" @open="refresh" />
30
+ * ```
31
+ * - 触发 `update:modelValue`(选中/清除)
32
+ * - 触发 `open`(下拉展开时,可用于静默刷新选项)
33
+ */
34
+ export declare const Select: DefineComponent<SelectProps>
35
+
36
+ export default Select
@@ -0,0 +1,220 @@
1
+ <template>
2
+ <div class="form-group">
3
+ <label
4
+ v-if="label"
5
+ :for="id"
6
+ class="block text-xs font-medium mb-1.5 text-[var(--text-secondary)]"
7
+ >
8
+ {{ label }}
9
+ <span v-if="required" class="text-red-500">*</span>
10
+ </label>
11
+
12
+ <div ref="triggerRef" class="relative">
13
+ <!-- Trigger -->
14
+ <button
15
+ :id="id"
16
+ type="button"
17
+ :disabled="disabled"
18
+ class="w-full border text-left transition-all duration-200 outline-none cursor-pointer bg-[var(--bg-input)] text-[var(--text-primary)] focus:border-brand focus:shadow-input-focus shadow-xs dark:shadow-none flex items-center justify-between gap-2"
19
+ :class="[
20
+ triggerSizeCls,
21
+ error ? 'border-red-500' : 'border-[var(--border-input)]',
22
+ { 'opacity-50 cursor-not-allowed': disabled },
23
+ ]"
24
+ @click="toggle"
25
+ @keydown.down.prevent="open"
26
+ @keydown.up.prevent="open"
27
+ @keydown.escape="close"
28
+ >
29
+ <span :class="{ 'text-[var(--text-tertiary)]': !selectedLabel }">
30
+ {{ selectedLabel || placeholder || '' }}
31
+ </span>
32
+ <svg
33
+ class="w-5 h-5 flex-shrink-0 text-[var(--text-tertiary)] transition-transform duration-200"
34
+ :class="{ 'rotate-180': isOpen }"
35
+ fill="none"
36
+ stroke="currentColor"
37
+ viewBox="0 0 24 24"
38
+ >
39
+ <path
40
+ stroke-linecap="round"
41
+ stroke-linejoin="round"
42
+ stroke-width="2"
43
+ d="M19 9l-7 7-7-7"
44
+ />
45
+ </svg>
46
+ </button>
47
+
48
+ <!-- Dropdown Panel:定位由 applySelectPanelPosition 命令式写入(top/left/width + 列表 maxHeight) -->
49
+ <Teleport to="body">
50
+ <div
51
+ v-show="isOpen"
52
+ ref="panelRef"
53
+ class="fixed z-[60] bg-[var(--bg-card)] border border-[var(--border-input)] rounded-input shadow-card overflow-hidden transition-opacity duration-150"
54
+ :class="isOpen ? 'opacity-100' : 'opacity-0 pointer-events-none'"
55
+ >
56
+ <ul ref="panelListRef" class="py-1 overflow-y-auto">
57
+ <li
58
+ v-if="placeholder"
59
+ :class="['text-[var(--text-tertiary)] cursor-default select-none', optionRowCls]"
60
+ @click="select('')"
61
+ >
62
+ {{ placeholder }}
63
+ </li>
64
+ <li
65
+ v-for="(option, index) in options"
66
+ :key="option.value"
67
+ :class="[
68
+ optionRowCls,
69
+ 'select-none transition-colors duration-150',
70
+ option.disabled
71
+ ? 'opacity-50 cursor-not-allowed text-[var(--text-tertiary)]'
72
+ : 'cursor-pointer hover:bg-[var(--color-primary-light)] hover:text-brand hover:font-medium',
73
+ !option.disabled && hoveredIndex === index
74
+ ? 'bg-[var(--color-primary-light)] text-brand font-medium'
75
+ : 'text-[var(--text-primary)]',
76
+ ]"
77
+ @mouseenter="hoveredIndex = option.disabled ? -1 : index"
78
+ @mouseleave="hoveredIndex = -1"
79
+ @click="option.disabled ? null : select(option.value)"
80
+ >
81
+ {{ option.label }}
82
+ </li>
83
+ </ul>
84
+ </div>
85
+ </Teleport>
86
+ </div>
87
+
88
+ <!-- Error Message -->
89
+ <p v-if="error" class="mt-1 text-xs text-red-500">{{ error }}</p>
90
+ </div>
91
+ </template>
92
+
93
+ <script setup lang="ts">
94
+ /**
95
+ * Select - 宿主共享下拉选择组件(新版)
96
+ *
97
+ * 替代原生 <select>,hover 样式完全由 CSS token 控制,适配深色/浅色主题。
98
+ * 同时提供给插件 SDK(@binblink/wasmnest-plugin-sdk-desktop/ui)供插件引用。
99
+ *
100
+ * 组件只负责 UI 与交互;展开定位(视口翻转 / maxHeight 收缩 / 水平夹持 /
101
+ * Linux 根 zoom 坐标换算)全部封装在 apply-select-position.ts。
102
+ *
103
+ * - size="sm":插件紧凑布局(32px/12px);默认 md 与宿主表单一致(--input-height)
104
+ * - open 事件:下拉展开时触发(插件可借此静默刷新选项,无原生组件禁用打断问题)
105
+ */
106
+ import { ref, computed, onMounted, onUnmounted, nextTick } from 'vue'
107
+ import { applySelectPanelPosition } from './apply-select-position'
108
+
109
+ export interface SelectOption {
110
+ value: string | number
111
+ label: string
112
+ /** 选项是否禁用——禁用项仍会展示但不可选中,鼠标点击不触发选择 */
113
+ disabled?: boolean
114
+ }
115
+
116
+ export interface Props {
117
+ modelValue: string | number
118
+ label?: string
119
+ options: SelectOption[]
120
+ placeholder?: string
121
+ disabled?: boolean
122
+ required?: boolean
123
+ error?: string
124
+ /** md:宿主表单默认(--input-height 36px);sm:插件紧凑布局(32px/12px) */
125
+ size?: 'md' | 'sm'
126
+ }
127
+
128
+ const props = withDefaults(defineProps<Props>(), {
129
+ disabled: false,
130
+ required: false,
131
+ size: 'md',
132
+ })
133
+
134
+ const emit = defineEmits(['update:modelValue', 'open'])
135
+
136
+ const id = `select-${Math.random().toString(36).slice(2, 9)}`
137
+ const isOpen = ref(false)
138
+ const hoveredIndex = ref(-1)
139
+ const triggerRef = ref<HTMLElement | null>(null)
140
+ const panelRef = ref<HTMLElement | null>(null)
141
+ const panelListRef = ref<HTMLElement | null>(null)
142
+
143
+ const selectedLabel = computed(() => {
144
+ const opt = props.options.find((o) => o.value === props.modelValue)
145
+ return opt?.label ?? ''
146
+ })
147
+
148
+ // 尺寸分支:sm 为插件紧凑布局(与插件控件 h-8/text-xs 一致),md 与宿主表单控件一致
149
+ const triggerSizeCls = computed(() =>
150
+ props.size === 'sm'
151
+ ? 'h-8 px-2 text-xs rounded-[6px]'
152
+ : 'h-[var(--input-height)] px-4 text-sm rounded-input',
153
+ )
154
+ const optionRowCls = computed(() =>
155
+ props.size === 'sm' ? 'px-2 py-1.5 text-xs' : 'px-4 py-2.5 text-sm',
156
+ )
157
+
158
+ function positionPanel() {
159
+ if (!triggerRef.value || !panelRef.value || !panelListRef.value) return
160
+ applySelectPanelPosition({
161
+ trigger: triggerRef.value,
162
+ panel: panelRef.value,
163
+ list: panelListRef.value,
164
+ })
165
+ }
166
+
167
+ function toggle() {
168
+ if (props.disabled) return
169
+ isOpen.value ? close() : open()
170
+ }
171
+
172
+ function open() {
173
+ if (props.disabled) return
174
+ isOpen.value = true
175
+ hoveredIndex.value = -1
176
+ emit('open')
177
+ // 等面板随 v-show 渲染可见后再定位(同步测量,同帧写回,无闪帧)
178
+ nextTick(positionPanel)
179
+ }
180
+
181
+ function close() {
182
+ isOpen.value = false
183
+ hoveredIndex.value = -1
184
+ }
185
+
186
+ function select(value: string | number) {
187
+ emit('update:modelValue', value)
188
+ close()
189
+ }
190
+
191
+ function onClickOutside(e: MouseEvent) {
192
+ const target = e.target as HTMLElement
193
+ if (isOpen.value && !triggerRef.value?.contains(target) && !panelRef.value?.contains(target)) {
194
+ close()
195
+ }
196
+ }
197
+
198
+ /**
199
+ * 滚动关闭:scroll 事件不冒泡,但捕获阶段监听(capture:true)仍会收到
200
+ * 任意后代元素的滚动。面板自身的选项列表滚动(选项过多需滚动查看)不应
201
+ * 关闭下拉——否则一滚面板就消失,无法选中可视区外的选项;
202
+ * 其余滚动(页面/输入区/消息列表)视为失焦关闭,保持原意图
203
+ */
204
+ function onScroll(e: Event) {
205
+ if (panelRef.value?.contains(e.target as Node)) return
206
+ close()
207
+ }
208
+
209
+ onMounted(() => {
210
+ document.addEventListener('mousedown', onClickOutside)
211
+ window.addEventListener('resize', close)
212
+ window.addEventListener('scroll', onScroll, true)
213
+ })
214
+
215
+ onUnmounted(() => {
216
+ document.removeEventListener('mousedown', onClickOutside)
217
+ window.removeEventListener('resize', close)
218
+ window.removeEventListener('scroll', onScroll, true)
219
+ })
220
+ </script>