vite-plugin-refresh-guard 0.2.2 → 0.3.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.
@@ -0,0 +1,396 @@
1
+ <script setup lang="ts">
2
+ import { computed, ref, watch } from 'vue'
3
+
4
+ function parseBulletPoints(text: string | null | undefined): string[] {
5
+ if (!text || typeof text !== 'string') return []
6
+ const lines = text.split('\n')
7
+ const bullets: string[] = []
8
+ for (const line of lines) {
9
+ const trimmed = line.trim()
10
+ const m = trimmed.match(/^(?:[-*+•]|\d+\.)\s+(.+)$/)
11
+ if (m && m[1]) {
12
+ const item = m[1].trim()
13
+ if (item) bullets.push(item)
14
+ }
15
+ }
16
+ return bullets
17
+ }
18
+
19
+ const props = withDefaults(defineProps<{
20
+ /** 控制卡片显隐。默认:true */
21
+ visible?: boolean
22
+ /** 显式版本号,如 "v1.2.0" */
23
+ version?: string | null
24
+ /** 版本发布日期,如 "2026-10-18" */
25
+ date?: string | null
26
+ /** 更新日志正文(Markdown 字符串)。若未显式指定 bulletPoints,将自动提炼其中的列表项 */
27
+ content?: string | null
28
+ /** 手工指定的更新要点列表 */
29
+ bulletPoints?: string[]
30
+ /** 最多展示的 bullet 要点条数。默认:4 */
31
+ maxBullets?: number
32
+ /** 卡片标题。默认:"✨ 新版本上线" */
33
+ title?: string
34
+ /** "查看完整更新日志 / 查看更多" 的外链 URL 或跳转目标。若传入则显示查看更多链接 */
35
+ viewMoreUrl?: string
36
+ /** "查看更多" 按钮或链接文案。默认:"查看完整更新日志" */
37
+ viewMoreText?: string
38
+ /** "立即刷新" 按钮文案。默认:"立即刷新" */
39
+ refreshText?: string
40
+ /** 是否允许关闭/暂不更新。默认:true */
41
+ closable?: boolean
42
+ /** 卡片布局:'floating'(右下角浮动卡片)或 'inline'(流式嵌入)。默认:'floating' */
43
+ layout?: 'floating' | 'inline'
44
+ /** 显式指定深色/浅色外观。null/undefined 则跟随系统的 prefers-color-scheme */
45
+ dark?: boolean | null
46
+ }>(), {
47
+ visible: true,
48
+ version: null,
49
+ date: null,
50
+ content: null,
51
+ bulletPoints: undefined,
52
+ maxBullets: 4,
53
+ title: '✨ 新版本上线',
54
+ viewMoreUrl: '',
55
+ viewMoreText: '查看完整更新日志',
56
+ refreshText: '立即刷新',
57
+ closable: true,
58
+ layout: 'floating',
59
+ dark: null,
60
+ })
61
+
62
+ const emit = defineEmits<{
63
+ refresh: []
64
+ viewMore: []
65
+ close: []
66
+ }>()
67
+
68
+ const dismissed = ref(false)
69
+
70
+ // 当版本发生变化时,重置关闭状态
71
+ watch(() => props.version, () => {
72
+ dismissed.value = false
73
+ })
74
+
75
+ const isShown = computed(() => props.visible && !dismissed.value)
76
+
77
+ const bullets = computed(() => {
78
+ if (props.bulletPoints && props.bulletPoints.length > 0) {
79
+ return props.bulletPoints
80
+ }
81
+ if (props.content) {
82
+ return parseBulletPoints(props.content)
83
+ }
84
+ return []
85
+ })
86
+
87
+ const displayedBullets = computed(() => bullets.value.slice(0, props.maxBullets))
88
+ const remainingCount = computed(() => Math.max(0, bullets.value.length - props.maxBullets))
89
+
90
+ function handleClose() {
91
+ dismissed.value = true
92
+ emit('close')
93
+ }
94
+
95
+ function handleViewMore(e: Event) {
96
+ emit('viewMore')
97
+ if (!props.viewMoreUrl) {
98
+ e.preventDefault()
99
+ }
100
+ }
101
+ </script>
102
+
103
+ <template>
104
+ <component :is="layout === 'floating' ? 'Teleport' : 'div'" :to="layout === 'floating' ? 'body' : undefined">
105
+ <div
106
+ v-if="isShown"
107
+ class="rg-card-wrapper"
108
+ :class="[
109
+ layout === 'floating' ? 'rg-card-floating' : 'rg-card-inline',
110
+ { 'rg-dark': dark === true, 'rg-light': dark === false }
111
+ ]"
112
+ role="region"
113
+ aria-label="新版本更新通知"
114
+ >
115
+ <div class="rg-card">
116
+ <!-- 头部区域 -->
117
+ <div class="rg-card-header">
118
+ <div class="rg-card-title-group">
119
+ <span class="rg-card-title">{{ title }}</span>
120
+ <span v-if="version" class="rg-card-badge">{{ version }}</span>
121
+ <span v-if="date" class="rg-card-date">{{ date }}</span>
122
+ </div>
123
+ <button
124
+ v-if="closable"
125
+ type="button"
126
+ class="rg-card-close"
127
+ aria-label="关闭更新提示"
128
+ @click="handleClose"
129
+ >
130
+ ✕
131
+ </button>
132
+ </div>
133
+
134
+ <!-- 正文要点列表 -->
135
+ <div class="rg-card-body">
136
+ <ul v-if="displayedBullets.length > 0" class="rg-bullet-list">
137
+ <li v-for="(b, i) in displayedBullets" :key="i" class="rg-bullet-item">
138
+ <span class="rg-bullet-dot" aria-hidden="true">•</span>
139
+ <span class="rg-bullet-text">{{ b }}</span>
140
+ </li>
141
+ </ul>
142
+ <p v-else-if="content" class="rg-card-fallback-text">{{ content }}</p>
143
+ <p v-else class="rg-card-fallback-text">系统已部署新版本,刷新页面即可使用最新功能与优化。</p>
144
+
145
+ <div v-if="remainingCount > 0" class="rg-remaining-note">
146
+ <span>还有 {{ remainingCount }} 项更新改进</span>
147
+ </div>
148
+ </div>
149
+
150
+ <!-- 底部操作栏 -->
151
+ <div class="rg-card-footer">
152
+ <a
153
+ v-if="viewMoreUrl || $attrs.onViewMore"
154
+ :href="viewMoreUrl || 'javascript:void(0)'"
155
+ class="rg-card-link"
156
+ @click="handleViewMore"
157
+ >
158
+ {{ viewMoreText }} →
159
+ </a>
160
+ <span v-else class="rg-card-spacer" />
161
+
162
+ <button
163
+ type="button"
164
+ class="rg-card-refresh-btn"
165
+ @click="emit('refresh')"
166
+ >
167
+ {{ refreshText }}
168
+ </button>
169
+ </div>
170
+ </div>
171
+ </div>
172
+ </component>
173
+ </template>
174
+
175
+ <style scoped>
176
+ .rg-card-wrapper {
177
+ box-sizing: border-box;
178
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
179
+ }
180
+
181
+ .rg-card-floating {
182
+ position: fixed;
183
+ right: 20px;
184
+ bottom: 20px;
185
+ z-index: 9998;
186
+ width: 360px;
187
+ max-width: calc(100vw - 32px);
188
+ animation: rg-slide-up 0.28s cubic-bezier(0.16, 1, 0.3, 1);
189
+ }
190
+
191
+ .rg-card-inline {
192
+ position: relative;
193
+ width: 100%;
194
+ margin: 12px 0;
195
+ }
196
+
197
+ @keyframes rg-slide-up {
198
+ from {
199
+ opacity: 0;
200
+ transform: translateY(16px) scale(0.97);
201
+ }
202
+ to {
203
+ opacity: 1;
204
+ transform: translateY(0) scale(1);
205
+ }
206
+ }
207
+
208
+ .rg-card {
209
+ padding: 16px 18px;
210
+ border-radius: var(--rg-radius, 14px);
211
+ background: var(--rg-card-bg, #ffffff);
212
+ color: var(--rg-card-fg, #111827);
213
+ border: 1px solid var(--rg-card-border, #e5e7eb);
214
+ box-shadow: var(--rg-card-shadow, 0 10px 30px -4px rgba(0, 0, 0, 0.12), 0 4px 10px -2px rgba(0, 0, 0, 0.05));
215
+ backdrop-filter: blur(8px);
216
+ }
217
+
218
+ .rg-card-header {
219
+ display: flex;
220
+ align-items: center;
221
+ justify-content: space-between;
222
+ margin-bottom: 12px;
223
+ }
224
+
225
+ .rg-card-title-group {
226
+ display: flex;
227
+ align-items: center;
228
+ gap: 8px;
229
+ flex-wrap: wrap;
230
+ }
231
+
232
+ .rg-card-title {
233
+ font-size: 14.5px;
234
+ font-weight: 700;
235
+ letter-spacing: -0.01em;
236
+ color: inherit;
237
+ }
238
+
239
+ .rg-card-badge {
240
+ display: inline-flex;
241
+ align-items: center;
242
+ padding: 2px 7px;
243
+ border-radius: 9999px;
244
+ font-size: 11px;
245
+ font-weight: 600;
246
+ font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
247
+ background: var(--rg-badge-bg, rgba(37, 99, 235, 0.1));
248
+ color: var(--rg-badge-fg, #2563eb);
249
+ }
250
+
251
+ .rg-card-date {
252
+ font-size: 11.5px;
253
+ color: var(--rg-muted-fg, #6b7280);
254
+ }
255
+
256
+ .rg-card-close {
257
+ display: inline-flex;
258
+ align-items: center;
259
+ justify-content: center;
260
+ width: 22px;
261
+ height: 22px;
262
+ padding: 0;
263
+ border: none;
264
+ border-radius: 6px;
265
+ background: transparent;
266
+ color: var(--rg-muted-fg, #9ca3af);
267
+ font-size: 13px;
268
+ cursor: pointer;
269
+ transition: all 0.15s ease;
270
+ }
271
+
272
+ .rg-card-close:hover {
273
+ background: var(--rg-hover-bg, rgba(0, 0, 0, 0.06));
274
+ color: var(--rg-card-fg, #111827);
275
+ }
276
+
277
+ .rg-card-body {
278
+ margin-bottom: 14px;
279
+ }
280
+
281
+ .rg-bullet-list {
282
+ margin: 0;
283
+ padding: 0;
284
+ list-style: none;
285
+ display: flex;
286
+ flex-direction: column;
287
+ gap: 6px;
288
+ }
289
+
290
+ .rg-bullet-item {
291
+ display: flex;
292
+ align-items: flex-start;
293
+ gap: 6px;
294
+ font-size: 13px;
295
+ line-height: 1.5;
296
+ color: var(--rg-body-fg, #374151);
297
+ }
298
+
299
+ .rg-bullet-dot {
300
+ flex-shrink: 0;
301
+ font-size: 14px;
302
+ line-height: 1.4;
303
+ color: var(--rg-accent, #2563eb);
304
+ }
305
+
306
+ .rg-bullet-text {
307
+ flex: 1;
308
+ word-break: break-word;
309
+ }
310
+
311
+ .rg-card-fallback-text {
312
+ margin: 0;
313
+ font-size: 13px;
314
+ line-height: 1.55;
315
+ color: var(--rg-body-fg, #4b5563);
316
+ }
317
+
318
+ .rg-remaining-note {
319
+ margin-top: 6px;
320
+ padding-left: 12px;
321
+ font-size: 11.5px;
322
+ color: var(--rg-muted-fg, #6b7280);
323
+ }
324
+
325
+ .rg-card-footer {
326
+ display: flex;
327
+ align-items: center;
328
+ justify-content: space-between;
329
+ gap: 12px;
330
+ padding-top: 10px;
331
+ border-top: 1px solid var(--rg-card-border, #f3f4f6);
332
+ }
333
+
334
+ .rg-card-spacer {
335
+ flex: 1;
336
+ }
337
+
338
+ .rg-card-link {
339
+ font-size: 12.5px;
340
+ font-weight: 500;
341
+ color: var(--rg-accent, #2563eb);
342
+ text-decoration: none;
343
+ transition: opacity 0.15s ease;
344
+ }
345
+
346
+ .rg-card-link:hover {
347
+ opacity: 0.82;
348
+ text-decoration: underline;
349
+ }
350
+
351
+ .rg-card-refresh-btn {
352
+ padding: 6px 14px;
353
+ border: none;
354
+ border-radius: var(--rg-btn-radius, 8px);
355
+ background: var(--rg-accent, #2563eb);
356
+ color: var(--rg-accent-fg, #ffffff);
357
+ font-size: 12.5px;
358
+ font-weight: 600;
359
+ cursor: pointer;
360
+ transition: all 0.15s ease;
361
+ white-space: nowrap;
362
+ }
363
+
364
+ .rg-card-refresh-btn:hover {
365
+ filter: brightness(0.92);
366
+ }
367
+
368
+ .rg-card-refresh-btn:active {
369
+ transform: scale(0.98);
370
+ }
371
+
372
+ /* 深色模式适配 */
373
+ .rg-card-wrapper.rg-dark .rg-card {
374
+ background: var(--rg-card-bg-dark, #1f2937);
375
+ color: var(--rg-card-fg-dark, #f9fafb);
376
+ border-color: var(--rg-card-border-dark, #374151);
377
+ box-shadow: 0 10px 30px -4px rgba(0, 0, 0, 0.4);
378
+ }
379
+ .rg-card-wrapper.rg-dark .rg-bullet-item { color: var(--rg-body-fg-dark, #d1d5db); }
380
+ .rg-card-wrapper.rg-dark .rg-card-badge { background: rgba(59, 130, 246, 0.2); color: #60a5fa; }
381
+ .rg-card-wrapper.rg-dark .rg-card-footer { border-color: #374151; }
382
+ .rg-card-wrapper.rg-dark .rg-card-close:hover { background: rgba(255, 255, 255, 0.1); color: #fff; }
383
+
384
+ @media (prefers-color-scheme: dark) {
385
+ .rg-card-wrapper:not(.rg-light) .rg-card {
386
+ background: var(--rg-card-bg-dark, #1f2937);
387
+ color: var(--rg-card-fg-dark, #f9fafb);
388
+ border-color: var(--rg-card-border-dark, #374151);
389
+ box-shadow: 0 10px 30px -4px rgba(0, 0, 0, 0.4);
390
+ }
391
+ .rg-card-wrapper:not(.rg-light) .rg-bullet-item { color: var(--rg-body-fg-dark, #d1d5db); }
392
+ .rg-card-wrapper:not(.rg-light) .rg-card-badge { background: rgba(59, 130, 246, 0.2); color: #60a5fa; }
393
+ .rg-card-wrapper:not(.rg-light) .rg-card-footer { border-color: #374151; }
394
+ .rg-card-wrapper:not(.rg-light) .rg-card-close:hover { background: rgba(255, 255, 255, 0.1); color: #fff; }
395
+ }
396
+ </style>
@@ -1,5 +1,7 @@
1
- import { UpdateChecker } from '../core/checker';
2
- import type { VersionCheckerOptions, VersionInfo } from '../core/types';
1
+ import { UpdateChecker, fetchChangelog, parseChangelog, extractBulletPoints } from '../core/checker';
2
+ import type { VersionCheckerOptions, VersionInfo, ParsedChangelog, ChangelogVersionEntry, UpdateCardProps } from '../core/types';
3
+ export { fetchChangelog, parseChangelog, extractBulletPoints };
4
+ export type { VersionCheckerOptions, VersionInfo, ParsedChangelog, ChangelogVersionEntry, UpdateCardProps, };
3
5
  /**
4
6
  * Vue composable wrapping the framework-agnostic UpdateChecker. Mounts/unmounts the checker
5
7
  * with the component's lifecycle automatically.
@@ -9,9 +11,11 @@ import type { VersionCheckerOptions, VersionInfo } from '../core/types';
9
11
  export declare function useVersionCheck(currentVersion: string, options?: VersionCheckerOptions): {
10
12
  hasUpdate: import("vue").Ref<boolean, boolean>;
11
13
  latestInfo: import("vue").ShallowRef<VersionInfo | null, VersionInfo | null>;
14
+ changelog: import("vue").ShallowRef<ParsedChangelog | null, ParsedChangelog | null>;
12
15
  mode: import("../core/types").UpdateMode;
13
16
  applyUpdate: () => void;
14
17
  checkNow: () => Promise<boolean>;
18
+ fetchChangelog: (url?: string) => Promise<ParsedChangelog>;
15
19
  /** Feed a vite-plugin-pwa `onNeedRefresh` (or any other out-of-band) signal into the same mode logic. */
16
20
  notifyExternalUpdate: (info?: VersionInfo) => void;
17
21
  checker: UpdateChecker;
@@ -1,33 +1,48 @@
1
1
  import {
2
2
  UpdateChecker
3
- } from "../chunk-ULA5FWKW.js";
3
+ } from "../chunk-CGDGH2ML.js";
4
+ import {
5
+ extractBulletPoints,
6
+ fetchChangelog,
7
+ parseChangelog
8
+ } from "../chunk-JLHX4WLS.js";
4
9
 
5
10
  // src/vue/useVersionCheck.ts
6
11
  import { onMounted, onUnmounted, ref, shallowRef } from "vue";
7
12
  function useVersionCheck(currentVersion, options = {}) {
8
13
  const hasUpdate = ref(false);
9
14
  const latestInfo = shallowRef(null);
15
+ const changelog = shallowRef(null);
10
16
  const checker = new UpdateChecker(currentVersion, options);
11
17
  const off = checker.on("update-available", (info) => {
12
18
  hasUpdate.value = true;
13
19
  latestInfo.value = info;
14
20
  });
21
+ const offChangelog = checker.on("changelog-loaded", (parsed) => {
22
+ changelog.value = parsed;
23
+ });
15
24
  onMounted(() => checker.start());
16
25
  onUnmounted(() => {
17
26
  checker.stop();
18
27
  off();
28
+ offChangelog();
19
29
  });
20
30
  return {
21
31
  hasUpdate,
22
32
  latestInfo,
33
+ changelog,
23
34
  mode: options.mode ?? "toast-auto",
24
35
  applyUpdate: () => checker.applyUpdate(),
25
36
  checkNow: () => checker.checkNow(),
37
+ fetchChangelog: (url) => checker.fetchChangelog(url),
26
38
  /** Feed a vite-plugin-pwa `onNeedRefresh` (or any other out-of-band) signal into the same mode logic. */
27
39
  notifyExternalUpdate: (info) => checker.notifyExternalUpdate(info),
28
40
  checker
29
41
  };
30
42
  }
31
43
  export {
44
+ extractBulletPoints,
45
+ fetchChangelog,
46
+ parseChangelog,
32
47
  useVersionCheck
33
48
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-refresh-guard",
3
- "version": "0.2.2",
3
+ "version": "0.3.1",
4
4
  "$schema": "https://raw.githubusercontent.com/vitejs/vite-plugin-registry/refs/heads/main/data/schema/extended-package-json.schema.json",
5
5
  "description": "Vite plugin for web update notification — detect a new version on every page load and prompt users to reload: silent auto reload, force refresh, toast prompt, or blocking modal. Framework-agnostic core with Vue and React adapters. Vite 应用版本更新检测与自动刷新方案。",
6
6
  "type": "module",
@@ -41,6 +41,7 @@
41
41
  "import": "./dist/vue/useVersionCheck.js"
42
42
  },
43
43
  "./vue/UpdatePrompt.vue": "./dist/vue/UpdatePrompt.vue",
44
+ "./vue/UpdateCard.vue": "./dist/vue/UpdateCard.vue",
44
45
  "./react": {
45
46
  "types": "./dist/react/useVersionCheck.d.ts",
46
47
  "import": "./dist/react/useVersionCheck.js"
@@ -48,6 +49,10 @@
48
49
  "./react/UpdatePrompt": {
49
50
  "types": "./dist/react/UpdatePrompt.d.ts",
50
51
  "import": "./dist/react/UpdatePrompt.js"
52
+ },
53
+ "./react/UpdateCard": {
54
+ "types": "./dist/react/UpdateCard.d.ts",
55
+ "import": "./dist/react/UpdateCard.js"
51
56
  }
52
57
  },
53
58
  "files": [
@@ -57,7 +62,7 @@
57
62
  "CHANGELOG.md"
58
63
  ],
59
64
  "scripts": {
60
- "build": "tsup src/index.ts src/vue/useVersionCheck.ts src/react/useVersionCheck.ts src/react/UpdatePrompt.tsx --format esm --clean && tsc -p tsconfig.build.json && node scripts/copy-assets.mjs",
65
+ "build": "tsup src/index.ts src/vue/useVersionCheck.ts src/react/useVersionCheck.ts src/react/UpdatePrompt.tsx src/react/UpdateCard.tsx --format esm --clean && tsc -p tsconfig.build.json && node scripts/copy-assets.mjs",
61
66
  "typecheck": "tsc --noEmit",
62
67
  "test": "vitest run",
63
68
  "compat": "npm run typecheck && npm test && npm run build",