@binblink/wasmnest-plugin-sdk-mobile 0.0.0-stage → 0.0.2

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 (122) hide show
  1. package/README.md +439 -2
  2. package/README_en.md +415 -0
  3. package/bin/build.js +271 -0
  4. package/bin/cli.js +96 -0
  5. package/bin/lib.js +349 -0
  6. package/bin/manifest-gen.js +327 -0
  7. package/bin/package.js +151 -0
  8. package/bin/scaffold.js +299 -0
  9. package/dev-shell/README.md +151 -0
  10. package/dev-shell/index.html +27 -0
  11. package/dev-shell/package.json +27 -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/apps/mock-terminal/MockTerminalSlotCard.vue +43 -0
  16. package/dev-shell/src/apps/mock-terminal/MockTerminalView.vue +168 -0
  17. package/dev-shell/src/apps/mock-terminal/index.ts +50 -0
  18. package/dev-shell/src/components/DevToolbar.vue +100 -0
  19. package/dev-shell/src/components/DialogHost.vue +119 -0
  20. package/dev-shell/src/components/LogPanel.vue +80 -0
  21. package/dev-shell/src/i18n.ts +22 -0
  22. package/dev-shell/src/loader.ts +239 -0
  23. package/dev-shell/src/locale.ts +29 -0
  24. package/dev-shell/src/locales/en-shell.ts +332 -0
  25. package/dev-shell/src/locales/index.ts +70 -0
  26. package/dev-shell/src/locales/shell.ts +331 -0
  27. package/dev-shell/src/main.ts +58 -0
  28. package/dev-shell/src/mock/dialog-service.ts +58 -0
  29. package/dev-shell/src/mock/file-transfer.ts +390 -0
  30. package/dev-shell/src/mock/mobile-api.ts +369 -0
  31. package/dev-shell/src/mock/preset-tasks.ts +186 -0
  32. package/dev-shell/src/mock/session.ts +121 -0
  33. package/dev-shell/src/mock-context.ts +374 -0
  34. package/dev-shell/src/registry.ts +151 -0
  35. package/dev-shell/src/shared-runtime.ts +44 -0
  36. package/dev-shell/src/shell/ShellView.vue +52 -0
  37. package/dev-shell/src/shell/adapters/devAppSource.ts +144 -0
  38. package/dev-shell/src/shell/components/PluginContextHost.vue +40 -0
  39. package/dev-shell/src/shell/components/ShellAppCell.vue +75 -0
  40. package/dev-shell/src/shell/components/ShellAppIcon.vue +111 -0
  41. package/dev-shell/src/shell/components/ShellAppSurface.vue +46 -0
  42. package/dev-shell/src/shell/components/ShellBrandTile.vue +27 -0
  43. package/dev-shell/src/shell/components/ShellCapsuleMenu.vue +123 -0
  44. package/dev-shell/src/shell/components/ShellChip.vue +49 -0
  45. package/dev-shell/src/shell/components/ShellGroupCard.vue +17 -0
  46. package/dev-shell/src/shell/components/ShellHost.vue +75 -0
  47. package/dev-shell/src/shell/components/ShellNotificationShade.vue +501 -0
  48. package/dev-shell/src/shell/components/ShellPermissionPrompt.vue +127 -0
  49. package/dev-shell/src/shell/components/ShellPlaceholderSurface.vue +33 -0
  50. package/dev-shell/src/shell/components/ShellSection.vue +27 -0
  51. package/dev-shell/src/shell/components/ShellSheet.vue +74 -0
  52. package/dev-shell/src/shell/components/ShellStatusbar.vue +84 -0
  53. package/dev-shell/src/shell/components/screens/PluginRoutePage.vue +98 -0
  54. package/dev-shell/src/shell/components/screens/ShellAppDetailScreen.vue +359 -0
  55. package/dev-shell/src/shell/components/screens/ShellAppRunScreen.vue +90 -0
  56. package/dev-shell/src/shell/components/screens/ShellAppsScreen.vue +195 -0
  57. package/dev-shell/src/shell/components/screens/ShellHomeScreen.vue +357 -0
  58. package/dev-shell/src/shell/components/screens/ShellPermissionsScreen.vue +376 -0
  59. package/dev-shell/src/shell/components/screens/ShellSettingsScreen.vue +240 -0
  60. package/dev-shell/src/shell/components/screens/ShellSwitcherScreen.vue +559 -0
  61. package/dev-shell/src/shell/components/ui/ConfirmDialog.vue +137 -0
  62. package/dev-shell/src/shell/components/ui/Toggle.vue +53 -0
  63. package/dev-shell/src/shell/composables/useShellApps.ts +417 -0
  64. package/dev-shell/src/shell/composables/useShellNavigation.ts +197 -0
  65. package/dev-shell/src/shell/composables/useShellNotifications.ts +115 -0
  66. package/dev-shell/src/shell/composables/useShellOpen.ts +44 -0
  67. package/dev-shell/src/shell/composables/useShellOverlays.ts +101 -0
  68. package/dev-shell/src/shell/composables/useShellPreferences.ts +143 -0
  69. package/dev-shell/src/shell/composables/useShellRecent.ts +84 -0
  70. package/dev-shell/src/shell/composables/useSlotCarousel.ts +238 -0
  71. package/dev-shell/src/shell/composables/useToast.ts +33 -0
  72. package/dev-shell/src/shell/index.ts +88 -0
  73. package/dev-shell/src/shell/logger.ts +45 -0
  74. package/dev-shell/src/shell/permissions.ts +145 -0
  75. package/dev-shell/src/shell/registry.ts +220 -0
  76. package/dev-shell/src/shell/slotCards.ts +47 -0
  77. package/dev-shell/src/shell/styles/shell.css +1155 -0
  78. package/dev-shell/src/shell/surfaceIntent.ts +107 -0
  79. package/dev-shell/src/shell/types.ts +152 -0
  80. package/dev-shell/src/shell/utils.ts +54 -0
  81. package/dev-shell/src/styles/main.css +153 -0
  82. package/dev-shell/src/styles/mobile.css +981 -0
  83. package/dev-shell/src/styles/wallpaper.css +130 -0
  84. package/dev-shell/src/theme.ts +71 -0
  85. package/dev-shell/src/vite-env.d.ts +20 -0
  86. package/dev-shell/tailwind.config.js +101 -0
  87. package/dev-shell/tsconfig.json +16 -0
  88. package/dev-shell/vite.config.ts +130 -0
  89. package/dist/index.d.ts +124 -0
  90. package/dist/index.js +229 -0
  91. package/dist/types.d.ts +738 -0
  92. package/dist/types.js +0 -0
  93. package/dist/vite-plugin.d.ts +16 -0
  94. package/dist/vite-plugin.js +95 -0
  95. package/package.json +88 -3
  96. package/src/constants.ts +6 -0
  97. package/src/global-dialog.ts +168 -0
  98. package/src/index.ts +96 -0
  99. package/src/runtime.ts +44 -0
  100. package/src/types.ts +822 -0
  101. package/src/ui/AppUpdateSection.d.ts +44 -0
  102. package/src/ui/AppUpdateSection.vue +253 -0
  103. package/src/ui/PluginGlobalDialog.vue +388 -0
  104. package/src/ui/QuickCard.d.ts +33 -0
  105. package/src/ui/QuickCard.vue +167 -0
  106. package/src/ui/Select.d.ts +37 -0
  107. package/src/ui/Select.vue +307 -0
  108. package/src/ui/plugin-global-dialog.d.ts +11 -0
  109. package/src/ui/useSwipeTabs.d.ts +20 -0
  110. package/src/ui/useSwipeTabs.ts +53 -0
  111. package/src/update.ts +123 -0
  112. package/src/vite-plugin.ts +132 -0
  113. package/template/.gitignore +4 -0
  114. package/template/README.md +46 -0
  115. package/template/package.json +23 -0
  116. package/template/plugin.json +13 -0
  117. package/template/rust/Cargo.toml +19 -0
  118. package/template/rust/src/lib.rs +33 -0
  119. package/template/src/env.d.ts +24 -0
  120. package/template/src/index.ts +37 -0
  121. package/template/tsconfig.json +17 -0
  122. package/template/vite.config.ts +21 -0
@@ -0,0 +1,44 @@
1
+ import type { DefineComponent } from 'vue'
2
+ import type { AppUpdateCheckResult } from '../types'
3
+
4
+ /** 默认更新卡片文案(应用可覆盖任意字段;`{version}` 由组件替换) */
5
+ export interface AppUpdateLabels {
6
+ title: string
7
+ check: string
8
+ checking: string
9
+ upToDate: string
10
+ available: string
11
+ incompatible: string
12
+ failed: string
13
+ unsupported: string
14
+ reasonNoCompat: string
15
+ reasonMountContract: string
16
+ reasonHostRuntime: string
17
+ reasonHostRuntimeInvalid: string
18
+ }
19
+
20
+ /** AppUpdateSection 组件 props */
21
+ export interface AppUpdateSectionProps {
22
+ /** 当前版本(可选;不传时用检查结果的 currentVersion) */
23
+ currentVersion?: string
24
+ /** 自定义检查器;缺省使用宿主 `PluginContext.appUpdate.check` */
25
+ check?: () => Promise<AppUpdateCheckResult>
26
+ /** 文案覆盖(缺省按系统语言内置中 / 英) */
27
+ labels?: Partial<AppUpdateLabels>
28
+ disabled?: boolean
29
+ }
30
+
31
+ /**
32
+ * 应用更新检查卡片(SDK 默认实现)
33
+ *
34
+ * 用法(应用设置页直接嵌入):
35
+ * ```vue
36
+ * <AppUpdateSection />
37
+ * ```
38
+ * - 数据面 = 宿主 `PluginContext.appUpdate.check`(GitHub Release 统一判定)
39
+ * - 应用自实现更新检查时传 `check` 覆盖,或完全不用本组件
40
+ * - 更新动作统一在宿主应用管理页执行(需停用应用 + 换目录 + 重新审批)
41
+ */
42
+ export declare const AppUpdateSection: DefineComponent<AppUpdateSectionProps>
43
+
44
+ export default AppUpdateSection
@@ -0,0 +1,253 @@
1
+ <template>
2
+ <!--
3
+ 应用更新检查(SDK 默认实现)
4
+
5
+ 应用设置页直接嵌入即可:宿主提供 GitHub Release 判定,本组件只负责状态与
6
+ 展示。应用想自实现时不必使用本组件(或传 `check` 覆盖数据面)。
7
+ -->
8
+ <section class="wasmnest-update" data-testid="sdk-app-update">
9
+ <div class="wasmnest-update__row">
10
+ <div class="wasmnest-update__main">
11
+ <span v-if="text.title" class="wasmnest-update__title">{{ text.title }}</span>
12
+ <span v-if="versionLabel" class="wasmnest-update__version">{{ versionLabel }}</span>
13
+ </div>
14
+ <button
15
+ type="button"
16
+ class="wasmnest-update__button"
17
+ :disabled="disabled || checking || !supported"
18
+ :aria-busy="checking"
19
+ data-testid="sdk-app-update-check"
20
+ @click="onCheck"
21
+ >
22
+ {{ checking ? text.checking : text.check }}
23
+ </button>
24
+ </div>
25
+ <p
26
+ v-if="statusLabel"
27
+ class="wasmnest-update__status"
28
+ :class="{ 'is-error': isBad, 'is-available': status === 'available' }"
29
+ data-testid="sdk-app-update-status"
30
+ >
31
+ {{ statusLabel }}
32
+ </p>
33
+ <p
34
+ v-if="reasonLabel"
35
+ class="wasmnest-update__status is-error"
36
+ data-testid="sdk-app-update-reason"
37
+ >
38
+ {{ reasonLabel }}
39
+ </p>
40
+ </section>
41
+ </template>
42
+
43
+ <script setup lang="ts">
44
+ /**
45
+ * AppUpdateSection — SDK 默认应用更新卡片(双端同形)
46
+ *
47
+ * 数据面:`PluginContext.appUpdate.check`(宿主统一实现)或调用方传入的 `check`
48
+ * (应用自实现)。状态机在 `useAppUpdate`;本组件只做展示,不发起安装——
49
+ * 更新动作统一收口在宿主应用管理页(需要先停用应用、替换插件目录并重新审批)。
50
+ *
51
+ * 视觉:只依赖 `--mobile-*` / 桌面 `--bg-card` 等 token 的回退组合,不引入
52
+ * 任何一端专属组件;应用可在外层继续套自己的卡片样式。
53
+ */
54
+ import { computed } from 'vue'
55
+ import { useAppUpdate } from '../update'
56
+ import type { AppUpdateCheckResult } from '../types'
57
+
58
+ /** 文案包(应用可覆盖任意字段;`{version}` 由组件替换) */
59
+ export interface AppUpdateLabels {
60
+ title: string
61
+ check: string
62
+ checking: string
63
+ upToDate: string
64
+ available: string
65
+ incompatible: string
66
+ failed: string
67
+ unsupported: string
68
+ reasonNoCompat: string
69
+ reasonMountContract: string
70
+ reasonHostRuntime: string
71
+ reasonHostRuntimeInvalid: string
72
+ }
73
+
74
+ const props = withDefaults(
75
+ defineProps<{
76
+ /** 当前版本(可选;不传时用检查结果的 currentVersion) */
77
+ currentVersion?: string
78
+ /** 自定义检查器;缺省使用宿主 `PluginContext.appUpdate.check` */
79
+ check?: () => Promise<AppUpdateCheckResult>
80
+ /** 文案覆盖(缺省按系统语言内置中 / 英) */
81
+ labels?: Partial<AppUpdateLabels>
82
+ disabled?: boolean
83
+ }>(),
84
+ { currentVersion: '', check: undefined, labels: undefined, disabled: false },
85
+ )
86
+
87
+ const ZH: AppUpdateLabels = {
88
+ title: '版本更新',
89
+ check: '检查更新',
90
+ checking: '正在检查…',
91
+ upToDate: '已是最新版本(v{version})',
92
+ available: '发现新版本 v{version};请在应用管理中更新',
93
+ incompatible: '发现新版本 v{version},但与当前平台不兼容',
94
+ failed: '检查失败,请稍后重试',
95
+ unsupported: '当前平台不提供更新检查',
96
+ reasonNoCompat: '该发布物缺少兼容声明(旧版发布格式),无法确认与当前平台兼容',
97
+ reasonMountContract: '要求挂载契约 {required},当前平台为 {current}',
98
+ reasonHostRuntime: '要求宿主运行时 {required},当前为 {current};请先升级平台',
99
+ reasonHostRuntimeInvalid: '兼容声明版本形态异常({required}),已拒绝',
100
+ }
101
+
102
+ const EN: AppUpdateLabels = {
103
+ title: 'Version update',
104
+ check: 'Check for updates',
105
+ checking: 'Checking…',
106
+ upToDate: 'Up to date (v{version})',
107
+ available: 'v{version} available; update it in App Management',
108
+ incompatible: 'v{version} found, but it is incompatible with this platform',
109
+ failed: 'Update check failed, try again later',
110
+ unsupported: 'Update check is not available on this platform',
111
+ reasonNoCompat:
112
+ 'The release lacks a compatibility declaration (legacy format); compatibility cannot be confirmed',
113
+ reasonMountContract: 'Requires mount contract {required}; this platform is {current}',
114
+ reasonHostRuntime:
115
+ 'Requires host runtime {required}; this platform is {current}. Upgrade the platform first',
116
+ reasonHostRuntimeInvalid: 'Malformed compatibility version ({required}); refused',
117
+ }
118
+
119
+ const text = computed<AppUpdateLabels>(() => {
120
+ const isZh = typeof navigator !== 'undefined' && navigator.language.toLowerCase().startsWith('zh')
121
+ return { ...(isZh ? ZH : EN), ...(props.labels ?? {}) }
122
+ })
123
+
124
+ const {
125
+ status,
126
+ latestVersion,
127
+ currentVersion,
128
+ reason,
129
+ checking,
130
+ supported,
131
+ check: runCheck,
132
+ } = useAppUpdate({ check: props.check })
133
+
134
+ const versionLabel = computed(() => {
135
+ const v = props.currentVersion || currentVersion.value
136
+ return v ? `v${v}` : ''
137
+ })
138
+
139
+ function fill(template: string, params: Record<string, string | undefined>): string {
140
+ return template.replace(/\{(\w+)\}/g, (_, key: string) => params[key] ?? '')
141
+ }
142
+
143
+ const statusLabel = computed(() => {
144
+ if (!supported.value) return text.value.unsupported
145
+ switch (status.value) {
146
+ case 'checking':
147
+ return text.value.checking
148
+ case 'up-to-date':
149
+ return fill(text.value.upToDate, {
150
+ version: latestVersion.value || currentVersion.value || '',
151
+ })
152
+ case 'available':
153
+ return fill(text.value.available, { version: latestVersion.value || '' })
154
+ case 'incompatible':
155
+ return fill(text.value.incompatible, { version: latestVersion.value || '' })
156
+ case 'error':
157
+ return text.value.failed
158
+ default:
159
+ return ''
160
+ }
161
+ })
162
+
163
+ const isBad = computed(() => status.value === 'error' || status.value === 'incompatible')
164
+
165
+ const reasonLabel = computed(() => {
166
+ const r = reason.value
167
+ if (!r) return ''
168
+ const params = { ...(r.params ?? {}), version: latestVersion.value }
169
+ if (r.key.endsWith('noCompat')) return text.value.reasonNoCompat
170
+ if (r.key.endsWith('mountContract')) return fill(text.value.reasonMountContract, params)
171
+ if (r.key.endsWith('hostRuntimeInvalid')) return fill(text.value.reasonHostRuntimeInvalid, params)
172
+ if (r.key.endsWith('hostRuntime')) return fill(text.value.reasonHostRuntime, params)
173
+ return ''
174
+ })
175
+
176
+ function onCheck(): void {
177
+ void runCheck()
178
+ }
179
+
180
+ defineExpose({ check: runCheck })
181
+ </script>
182
+
183
+ <style scoped>
184
+ .wasmnest-update {
185
+ border: 1px solid var(--mobile-border, var(--border, rgba(127, 127, 127, 0.28)));
186
+ background: var(--mobile-bg-card, var(--bg-card, transparent));
187
+ border-radius: var(--mobile-radius-lg, 12px);
188
+ padding: 10px 12px;
189
+ display: flex;
190
+ flex-direction: column;
191
+ gap: 6px;
192
+ }
193
+
194
+ .wasmnest-update__row {
195
+ display: flex;
196
+ align-items: center;
197
+ gap: 10px;
198
+ }
199
+
200
+ .wasmnest-update__main {
201
+ flex: 1;
202
+ min-width: 0;
203
+ display: flex;
204
+ align-items: baseline;
205
+ gap: 8px;
206
+ }
207
+
208
+ .wasmnest-update__title {
209
+ font-size: 14px;
210
+ font-weight: 600;
211
+ color: var(--mobile-text-primary, var(--text-primary, inherit));
212
+ }
213
+
214
+ .wasmnest-update__version {
215
+ font-size: 12px;
216
+ color: var(--mobile-text-secondary, var(--text-secondary, #888));
217
+ font-variant-numeric: tabular-nums;
218
+ }
219
+
220
+ .wasmnest-update__button {
221
+ flex-shrink: 0;
222
+ min-height: var(--mobile-touch-target-min, 32px);
223
+ padding: 0 12px;
224
+ border-radius: var(--mobile-radius-md, 8px);
225
+ border: 1px solid var(--mobile-border, var(--border, rgba(127, 127, 127, 0.28)));
226
+ background: transparent;
227
+ color: var(--mobile-accent, var(--brand, #4f8cff));
228
+ font-size: 12px;
229
+ font-weight: 600;
230
+ cursor: pointer;
231
+ transition: opacity 0.2s ease;
232
+ }
233
+
234
+ .wasmnest-update__button:disabled {
235
+ opacity: 0.5;
236
+ cursor: not-allowed;
237
+ }
238
+
239
+ .wasmnest-update__status {
240
+ margin: 0;
241
+ font-size: 12px;
242
+ line-height: 1.5;
243
+ color: var(--mobile-text-secondary, var(--text-secondary, #888));
244
+ }
245
+
246
+ .wasmnest-update__status.is-available {
247
+ color: var(--mobile-accent, var(--brand, #4f8cff));
248
+ }
249
+
250
+ .wasmnest-update__status.is-error {
251
+ color: var(--mobile-error, var(--danger, #e5484d));
252
+ }
253
+ </style>
@@ -0,0 +1,388 @@
1
+ <template>
2
+ <Teleport to="body">
3
+ <Transition name="pgd">
4
+ <div
5
+ v-if="entry"
6
+ class="pgd-overlay mobile-ui"
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-5" :class="{ 'pgd-head--closable': entry.closable !== false }">
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-3">
25
+ <div v-if="entry.message" class="pgd-message whitespace-pre-line">{{ 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-5">
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
+ * - FIFO 排队(global-dialog 控制器)、可选定时自动关闭 + 倒计时
80
+ *
81
+ * 移动端样式适配:使用 --mobile-* design token,卡片圆角 16px,适配安全区,
82
+ * 按钮触摸目标 44px,文案换行 whitespace-pre-line 支持指纹等多行提示。
83
+ *
84
+ * 主题作用域:浅色 token 定义在 `html:not(.dark) .mobile-ui`(mobile.css),
85
+ * 而本组件 Teleport 到 body(逃逸 .mobile-ui 布局根),故 overlay 根元素必须
86
+ * 挂 `mobile-ui` 类才能吃到浅色/色板 token——与 FsAuthDialog 同模式;
87
+ * 深色为 :root 默认值不受影响。按钮随 --mobile-accent 深浅翻转(跟随主题色)。
88
+ *
89
+ * 渲染完全由 getGlobalDialog / subscribeGlobalDialog 驱动。
90
+ */
91
+ import { computed, defineComponent, h, onBeforeUnmount, onMounted, provide, ref, watch } from 'vue'
92
+ import {
93
+ closeGlobalDialog,
94
+ getGlobalDialog,
95
+ resolveDialogDeadline,
96
+ subscribeGlobalDialog,
97
+ type GlobalDialogEntry,
98
+ } from '../global-dialog'
99
+ import { getRouter } from '../runtime'
100
+ import type { PluginDialogAction } from '../types'
101
+
102
+ const entry = ref<GlobalDialogEntry | null>(getGlobalDialog())
103
+ let unsubscribe: (() => void) | null = null
104
+
105
+ const now = ref(Date.now())
106
+ let tickTimer: ReturnType<typeof setInterval> | null = null
107
+
108
+ const countdownSeconds = computed<number | null>(() => {
109
+ const e = entry.value
110
+ if (!e) return null
111
+ const deadline = resolveDialogDeadline(e)
112
+ if (deadline == null) return null
113
+ return Math.max(0, Math.ceil((deadline - now.value) / 1000))
114
+ })
115
+
116
+ const countdownText = computed<string>(() => {
117
+ const e = entry.value
118
+ const seconds = countdownSeconds.value
119
+ // 倒计时可用判定:标签须含 {seconds} 占位符且秒数可计算,否则整行提示不渲染
120
+ // (无占位符的静态文案会与正文重复;秒数不可用说明无 deadline,无倒计时可言)
121
+ if (!e?.countdownLabel || !e.countdownLabel.includes('{seconds}') || seconds == null) return ''
122
+ return e.countdownLabel.replace('{seconds}', String(seconds))
123
+ })
124
+
125
+ function startTicking(): void {
126
+ stopTicking()
127
+ now.value = Date.now()
128
+ tickTimer = setInterval(() => {
129
+ now.value = Date.now()
130
+ }, 500)
131
+ }
132
+
133
+ function stopTicking(): void {
134
+ if (tickTimer) {
135
+ clearInterval(tickTimer)
136
+ tickTimer = null
137
+ }
138
+ }
139
+
140
+ function closeCurrent(): void {
141
+ const e = entry.value
142
+ if (!e) return
143
+ closeGlobalDialog(e._id)
144
+ }
145
+
146
+ function onBackdrop(): void {
147
+ const e = entry.value
148
+ if (!e || e.closable === false || e.closeOnBackdrop === false) return
149
+ closeGlobalDialog(e._id)
150
+ }
151
+
152
+ function onKeydown(ev: KeyboardEvent): void {
153
+ if (ev.key !== 'Escape') return
154
+ const e = entry.value
155
+ if (!e || e.closable === false) return
156
+ closeGlobalDialog(e._id)
157
+ }
158
+
159
+ async function onAction(action: PluginDialogAction): Promise<void> {
160
+ try {
161
+ await action.onClick?.()
162
+ } catch (err) {
163
+ console.error('[PluginDialog] action failed, dialog kept open:', err)
164
+ return
165
+ }
166
+ closeCurrent()
167
+ if (action.navigateTo) {
168
+ try {
169
+ getRouter()?.push(action.navigateTo)
170
+ } catch (err) {
171
+ console.warn('[PluginDialog] navigate failed:', err)
172
+ }
173
+ }
174
+ }
175
+
176
+ const ContentShell = defineComponent({
177
+ name: 'PluginDialogContentShell',
178
+ props: {
179
+ entry: { type: Object, required: true },
180
+ },
181
+ setup(props) {
182
+ provide('pluginContext', (props.entry as GlobalDialogEntry).pluginContext)
183
+ const content = () =>
184
+ h(
185
+ (props.entry as GlobalDialogEntry).content,
186
+ { ...(props.entry as GlobalDialogEntry).props, onClose: () => closeCurrent() },
187
+ )
188
+ return content
189
+ },
190
+ })
191
+
192
+ onMounted(() => {
193
+ unsubscribe = subscribeGlobalDialog((e) => {
194
+ entry.value = e
195
+ if (e) startTicking()
196
+ else stopTicking()
197
+ })
198
+ window.addEventListener('keydown', onKeydown)
199
+ })
200
+
201
+ watch(
202
+ () => entry.value?._id,
203
+ () => {
204
+ now.value = Date.now()
205
+ },
206
+ )
207
+
208
+ onBeforeUnmount(() => {
209
+ unsubscribe?.()
210
+ unsubscribe = null
211
+ stopTicking()
212
+ window.removeEventListener('keydown', onKeydown)
213
+ })
214
+ </script>
215
+
216
+ <style scoped>
217
+ .pgd-overlay {
218
+ position: fixed;
219
+ inset: 0;
220
+ z-index: 100;
221
+ display: flex;
222
+ align-items: center;
223
+ justify-content: center;
224
+ padding: 24px;
225
+ padding-bottom: calc(24px + var(--safe-area-bottom, 0px));
226
+ background: var(--mobile-overlay-heavy, rgba(0, 0, 0, 0.5));
227
+ backdrop-filter: blur(4px);
228
+ }
229
+
230
+ .pgd-card {
231
+ position: relative;
232
+ width: 100%;
233
+ display: flex;
234
+ flex-direction: column;
235
+ max-height: 85vh;
236
+ overflow: auto;
237
+ background: var(--mobile-bg-card);
238
+ border: 1px solid var(--mobile-border);
239
+ border-radius: 16px;
240
+ box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
241
+ }
242
+
243
+ .pgd-head {
244
+ display: flex;
245
+ align-items: baseline;
246
+ justify-content: space-between;
247
+ gap: 12px;
248
+ }
249
+
250
+ /* 有关闭按钮(closable)时给标题行右侧让位:绝对定位的 .pgd-close 占右上角
251
+ (top 12px + 右 12px + 28px 宽),否则倒计时/长标题会与 X 重叠 */
252
+ .pgd-head--closable {
253
+ padding-right: 44px;
254
+ }
255
+
256
+ .pgd-title {
257
+ font-size: 16px;
258
+ font-weight: 600;
259
+ color: var(--mobile-text-primary);
260
+ line-height: 1.4;
261
+ }
262
+
263
+ .pgd-countdown {
264
+ font-size: 12px;
265
+ color: var(--mobile-text-secondary);
266
+ flex-shrink: 0;
267
+ white-space: nowrap;
268
+ transition: color 0.2s ease;
269
+ font-variant-numeric: tabular-nums;
270
+ }
271
+
272
+ .pgd-countdown--urgent {
273
+ color: var(--mobile-danger-color, #ef4444);
274
+ font-weight: 600;
275
+ }
276
+
277
+ .pgd-message {
278
+ margin: 0;
279
+ font-size: 14px;
280
+ line-height: 1.55;
281
+ color: var(--mobile-text-secondary);
282
+ overflow-wrap: anywhere;
283
+ }
284
+
285
+ .pgd-actions {
286
+ display: flex;
287
+ justify-content: flex-end;
288
+ gap: 8px;
289
+ margin-top: 4px;
290
+ }
291
+
292
+ .pgd-btn {
293
+ display: inline-flex;
294
+ align-items: center;
295
+ justify-content: center;
296
+ gap: 6px;
297
+ min-height: 44px;
298
+ padding: 8px 16px;
299
+ background: var(--mobile-bg-primary, #f3f4f6);
300
+ color: var(--mobile-text-primary);
301
+ border: 1px solid var(--mobile-border);
302
+ border-radius: 12px;
303
+ font-size: 14px;
304
+ font-weight: 500;
305
+ cursor: pointer;
306
+ transition: border-color 0.2s ease, background-color 0.2s ease, opacity 0.2s ease;
307
+ flex-shrink: 0;
308
+ flex: 1;
309
+ }
310
+
311
+ .pgd-btn:hover {
312
+ border-color: var(--mobile-accent);
313
+ }
314
+
315
+ .pgd-btn:disabled {
316
+ opacity: 0.5;
317
+ cursor: not-allowed;
318
+ border-color: var(--mobile-border);
319
+ background: var(--mobile-bg-primary);
320
+ color: var(--mobile-text-secondary);
321
+ }
322
+
323
+ .pgd-btn--primary {
324
+ background: var(--mobile-accent);
325
+ border-color: var(--mobile-accent);
326
+ color: var(--mobile-text-on-accent, #fff);
327
+ }
328
+
329
+ .pgd-btn--primary:hover {
330
+ filter: brightness(0.95);
331
+ }
332
+
333
+ .pgd-btn--danger {
334
+ background: var(--mobile-danger-color, #ef4444);
335
+ border-color: var(--mobile-danger-color, #ef4444);
336
+ color: #fff;
337
+ }
338
+
339
+ .pgd-btn--ghost {
340
+ border-color: transparent;
341
+ background: transparent;
342
+ opacity: 0.65;
343
+ }
344
+
345
+ .pgd-close {
346
+ position: absolute;
347
+ top: 12px;
348
+ right: 12px;
349
+ padding: 6px;
350
+ border: none;
351
+ background: transparent;
352
+ color: var(--mobile-text-muted);
353
+ border-radius: 8px;
354
+ cursor: pointer;
355
+ transition: color 0.2s ease, background-color 0.2s ease;
356
+ }
357
+
358
+ .pgd-close:hover {
359
+ color: var(--mobile-text-primary);
360
+ background: var(--mobile-bg-primary);
361
+ }
362
+
363
+ .pgd-close-ico {
364
+ width: 16px;
365
+ height: 16px;
366
+ display: block;
367
+ }
368
+
369
+ .pgd-enter-active,
370
+ .pgd-leave-active {
371
+ transition: opacity 0.2s ease;
372
+ }
373
+
374
+ .pgd-enter-active .pgd-card,
375
+ .pgd-leave-active .pgd-card {
376
+ transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
377
+ }
378
+
379
+ .pgd-enter-from,
380
+ .pgd-leave-to {
381
+ opacity: 0;
382
+ }
383
+
384
+ .pgd-enter-from .pgd-card,
385
+ .pgd-leave-to .pgd-card {
386
+ transform: translateY(8px) scale(0.98);
387
+ }
388
+ </style>