vitepress-plugin-announcement 0.1.1 → 0.1.3

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.
package/README.md CHANGED
@@ -4,6 +4,7 @@
4
4
 
5
5
  ![](https://cdn.upyun.sugarat.top/mdImg/sugar/7af94a65e3b4dd05e61e3411daba7fdd)
6
6
 
7
+ *介绍文章:[VitePress 公告插件开发实记](https://sugarat.top/technology/works/vitepress-plugin-announcement.html) · [archive](https://web.archive.org/web/20240921095008/https://sugarat.top/technology/works/vitepress-plugin-announcement.html) · [translated](https://sugarat-top.translate.goog/technology/works/vitepress-plugin-announcement.html?_x_tr_sl=zh-CN&_x_tr_tl=en&_x_tr_hl=zh-CN&_x_tr_pto=wapp)*
7
8
  ## 使用
8
9
  安装依赖 `pnpm/npm/yarn`
9
10
  ```sh
@@ -187,6 +188,11 @@ export interface AnnouncementOptions {
187
188
  * @param to 切换到的目标路由
188
189
  */
189
190
  onRouteChanged?: (to: Route, show: Ref<boolean>) => void
191
+
192
+ /**
193
+ * 国际化
194
+ */
195
+ locales?: Record<string, Omit<AnnouncementOptions, 'locales'>>
190
196
  }
191
197
 
192
198
  export declare namespace Announcement {
@@ -1,6 +1,6 @@
1
1
  <script lang="ts" setup>
2
- import { computed, h, onMounted, ref, watch } from 'vue'
3
- import { useRoute, useRouter } from 'vitepress'
2
+ import { computed, h, ref, watch } from 'vue'
3
+ import { useData, useRoute, useRouter } from 'vitepress'
4
4
  import type { Announcement, AnnouncementOptions } from 'vitepress-plugin-announcement'
5
5
 
6
6
  // @ts-expect-error
@@ -9,26 +9,28 @@ import AnnouncementButton from './AnnouncementButton.vue'
9
9
  import AnnouncementIcon from './AnnouncementIcon.vue'
10
10
  import { inBrowser, parseStringStyle, useDebounceFn, useWindowSize } from './util'
11
11
 
12
- const popoverProps: AnnouncementOptions = announcementOptions
12
+ const { localeIndex } = useData()
13
13
 
14
- const show = ref((popoverProps?.duration ?? 0) >= 0)
14
+ const popoverProps = computed<AnnouncementOptions>(() => ({ ...announcementOptions, ...announcementOptions?.locales?.[localeIndex.value] }))
15
+
16
+ const show = ref((popoverProps.value?.duration ?? 0) >= 0)
15
17
 
16
18
  const bodyContent = computed(() => {
17
- return popoverProps?.body || []
19
+ return popoverProps.value?.body || []
18
20
  })
19
21
 
20
22
  const footerContent = computed(() => {
21
- return popoverProps?.footer || []
23
+ return popoverProps.value?.footer || []
22
24
  })
23
- const storageKey = 'vitepress-plugin-announcement'
24
- const closeFlag = `${storageKey}-close`
25
+ const storageKey = computed(() => `vitepress-plugin-announcement-${localeIndex.value}`)
26
+ const closeFlag = computed(() => `${storageKey.value}-close`)
25
27
 
26
28
  // 移动端最小化
27
29
  const { width } = useWindowSize()
28
30
  const router = useRouter()
29
31
  const route = useRoute()
30
- onMounted(() => {
31
- if (!popoverProps?.title) {
32
+ watch(popoverProps, () => {
33
+ if (!popoverProps.value?.title) {
32
34
  return
33
35
  }
34
36
 
@@ -37,50 +39,50 @@ onMounted(() => {
37
39
  }
38
40
 
39
41
  // 取旧值
40
- const oldValue = localStorage.getItem(storageKey)
41
- const newValue = JSON.stringify(popoverProps)
42
- localStorage.setItem(storageKey, newValue)
42
+ const oldValue = localStorage.getItem(storageKey.value)
43
+ const newValue = JSON.stringify(popoverProps.value)
44
+ localStorage.setItem(storageKey.value, newValue)
43
45
 
44
46
  // 移动端最小化
45
- if (width.value < 768 && popoverProps?.mobileMinify) {
47
+ if (width.value < 768 && popoverProps.value?.mobileMinify) {
46
48
  show.value = false
47
49
  return
48
50
  }
49
51
 
50
52
  // >= 0 每次都展示,区别是否自动消失
51
- if (Number(popoverProps?.duration ?? '') >= 0) {
53
+ if (Number(popoverProps.value?.duration ?? '') >= 0) {
52
54
  show.value = true
53
- if (popoverProps?.duration) {
55
+ if (popoverProps.value?.duration) {
54
56
  setTimeout(() => {
55
57
  show.value = false
56
- }, popoverProps?.duration)
58
+ }, popoverProps.value?.duration)
57
59
  }
58
60
  return
59
61
  }
60
62
 
61
- if (oldValue !== newValue && popoverProps?.duration === -1) {
63
+ if (oldValue !== newValue && popoverProps.value?.duration === -1) {
62
64
  // 当做新值处理
63
65
  show.value = true
64
- localStorage.removeItem(closeFlag)
66
+ localStorage.removeItem(closeFlag.value)
65
67
  return
66
68
  }
67
69
 
68
70
  // 新旧相等,判断是否点击过close,没点击关闭依然展示
69
- if (oldValue === newValue && popoverProps?.duration === -1 && !localStorage.getItem(closeFlag)) {
71
+ if (oldValue === newValue && popoverProps.value?.duration === -1 && !localStorage.getItem(closeFlag.value)) {
70
72
  show.value = true
71
73
  }
72
- })
74
+ }, { immediate: true })
73
75
 
74
76
  const onAfterRouteChanged = useDebounceFn(() => {
75
- popoverProps?.onRouteChanged?.(route, show)
77
+ popoverProps.value?.onRouteChanged?.(route, show)
76
78
  }, 10)
77
79
 
78
80
  watch(route, onAfterRouteChanged, { immediate: true })
79
81
 
80
82
  function handleClose() {
81
83
  show.value = false
82
- if (popoverProps?.duration === -1) {
83
- localStorage.setItem(closeFlag, `${+new Date()}`)
84
+ if (popoverProps.value?.duration === -1) {
85
+ localStorage.setItem(closeFlag.value, `${+new Date()}`)
84
86
  }
85
87
  }
86
88
 
@@ -140,7 +142,7 @@ function PopoverValue(props: { key: number; item: Announcement.Value },
140
142
  }
141
143
 
142
144
  const showReopen = computed(() => {
143
- return !show.value && (popoverProps?.reopen ?? true) && !!popoverProps.title
145
+ return !show.value && (popoverProps.value?.reopen ?? true) && !!popoverProps.value.title
144
146
  })
145
147
  </script>
146
148
 
package/dist/index.d.mts CHANGED
@@ -57,6 +57,10 @@ interface AnnouncementOptions {
57
57
  * @param to 切换到的目标路由
58
58
  */
59
59
  onRouteChanged?: (to: Route, show: Ref<boolean>) => void;
60
+ /**
61
+ * 国际化
62
+ */
63
+ locales?: Record<string, Omit<AnnouncementOptions, 'locales'>>;
60
64
  }
61
65
  declare namespace Announcement {
62
66
  interface Title {
package/dist/index.d.ts CHANGED
@@ -57,6 +57,10 @@ interface AnnouncementOptions {
57
57
  * @param to 切换到的目标路由
58
58
  */
59
59
  onRouteChanged?: (to: Route, show: Ref<boolean>) => void;
60
+ /**
61
+ * 国际化
62
+ */
63
+ locales?: Record<string, Omit<AnnouncementOptions, 'locales'>>;
60
64
  }
61
65
  declare namespace Announcement {
62
66
  interface Title {
package/dist/index.js CHANGED
@@ -89,7 +89,7 @@ import Announcement from './Announcement.vue'`);
89
89
  },
90
90
  load(id) {
91
91
  if (id === resolvedVirtualModuleId) {
92
- return `export default ${(0, import_javascript_stringify.stringify)(options)}`;
92
+ return `export default ${(0, import_javascript_stringify.stringify)(componentOptions)}`;
93
93
  }
94
94
  }
95
95
  };
package/dist/index.mjs CHANGED
@@ -54,7 +54,7 @@ import Announcement from './Announcement.vue'`);
54
54
  },
55
55
  load(id) {
56
56
  if (id === resolvedVirtualModuleId) {
57
- return `export default ${stringify(options)}`;
57
+ return `export default ${stringify(componentOptions)}`;
58
58
  }
59
59
  }
60
60
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitepress-plugin-announcement",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "vitepress plugin, Announcement, 公告窗口",
5
5
  "author": "sugar",
6
6
  "license": "MIT",
@@ -43,7 +43,7 @@
43
43
  "tinyglobby": "^0.2.6",
44
44
  "typescript": "^5.5.4",
45
45
  "vite": "^5",
46
- "vitepress": "^1.3.4",
46
+ "vitepress": "^1.4.1",
47
47
  "vue": "^3.4.26"
48
48
  },
49
49
  "scripts": {