@vuepress/plugin-redirect 2.0.0-rc.27 → 2.0.0-rc.28

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.
@@ -1,4 +1,4 @@
1
1
  import type { VNode } from 'vue';
2
- import '../styles/language-switch.css';
2
+ import '../styles/redirect-modal.css';
3
3
  declare const _default: import("vue").DefineComponent<{}, () => VNode | null, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{}>>, {}, {}>;
4
4
  export default _default;
@@ -0,0 +1,93 @@
1
+ import { usePreferredLanguages, useScrollLock, useSessionStorage, } from '@vueuse/core';
2
+ import { computed, defineComponent, h, nextTick, onBeforeUnmount, onMounted, ref, TransitionGroup, } from 'vue';
3
+ import { useRoute, useRouteLocale, useRouter } from 'vuepress/client';
4
+ import { redirectLocaleConfig, redirectLocaleEntries } from '../define.js';
5
+ import '../styles/redirect-modal.css';
6
+ const redirectLocales = __REDIRECT_LOCALES__;
7
+ const { switchLocale } = redirectLocaleConfig;
8
+ const redirectStatusStorage = useSessionStorage('VUEPRESS_REDIRECT_LOCALES', {});
9
+ export default defineComponent({
10
+ name: 'RedirectModal',
11
+ setup() {
12
+ const languages = usePreferredLanguages();
13
+ const route = useRoute();
14
+ const router = useRouter();
15
+ const routeLocale = useRouteLocale();
16
+ const body = ref();
17
+ // lock body scroll when modal is displayed
18
+ const showModal = useScrollLock(body);
19
+ const info = computed(() => {
20
+ if (redirectLocaleEntries.some(([key]) => routeLocale.value === key))
21
+ for (const language of languages.value)
22
+ for (const [localePath, langs] of redirectLocaleEntries)
23
+ if (langs.includes(language)) {
24
+ if (localePath === routeLocale.value)
25
+ return null;
26
+ return {
27
+ lang: language,
28
+ localePath,
29
+ };
30
+ }
31
+ return null;
32
+ });
33
+ const locale = computed(() => {
34
+ if (info.value) {
35
+ const { lang, localePath } = info.value;
36
+ const locales = [
37
+ redirectLocales[routeLocale.value],
38
+ redirectLocales[localePath],
39
+ ];
40
+ return {
41
+ hint: locales.map(({ hint }) => hint.replace('$1', lang)),
42
+ switch: locales
43
+ .map(({ switch: switchText }) => switchText.replace('$1', lang))
44
+ .join(' / '),
45
+ cancel: locales.map(({ cancel }) => cancel).join(' / '),
46
+ };
47
+ }
48
+ return null;
49
+ });
50
+ const redirect = () => {
51
+ if (info.value)
52
+ router.replace(route.path.replace(routeLocale.value, info.value.localePath));
53
+ };
54
+ onMounted(async () => {
55
+ body.value = document.body;
56
+ await nextTick();
57
+ if (!redirectStatusStorage.value[routeLocale.value]) {
58
+ if (switchLocale === 'direct')
59
+ redirect();
60
+ else if (switchLocale === 'modal')
61
+ showModal.value = true;
62
+ }
63
+ });
64
+ onBeforeUnmount(() => {
65
+ showModal.value = false;
66
+ });
67
+ return () => h(TransitionGroup, { name: 'redirect-modal-fade' }, () => showModal.value
68
+ ? h('div', { key: 'mask', class: 'redirect-modal-mask' }, h('div', {
69
+ key: 'popup',
70
+ class: 'redirect-modal-wrapper',
71
+ }, [
72
+ h('div', { class: 'redirect-modal-content' }, locale.value?.hint.map((text) => h('p', text))),
73
+ h('button', {
74
+ type: 'button',
75
+ class: 'redirect-modal-action primary',
76
+ onClick: () => {
77
+ redirectStatusStorage.value[routeLocale.value] = true;
78
+ showModal.value = false;
79
+ redirect();
80
+ },
81
+ }, locale.value?.switch),
82
+ h('button', {
83
+ type: 'button',
84
+ class: 'redirect-modal-action',
85
+ onClick: () => {
86
+ redirectStatusStorage.value[routeLocale.value] = true;
87
+ showModal.value = false;
88
+ },
89
+ }, locale.value?.cancel),
90
+ ]))
91
+ : null);
92
+ },
93
+ });
@@ -1,11 +1,11 @@
1
1
  import { defineClientConfig } from 'vuepress/client';
2
- import LanguageSwitch from './components/LanguageSwitch.js';
3
- import { setupDevServerRedirect } from './composables/setupDevServerRedirect.js';
2
+ import RedirectModal from './components/RedirectModal.js';
3
+ import { setupDevServerRedirect } from './composables/index.js';
4
4
  import './styles/vars.css';
5
5
  export default defineClientConfig({
6
6
  setup() {
7
7
  if (__VUEPRESS_DEV__)
8
8
  setupDevServerRedirect();
9
9
  },
10
- rootComponents: __REDIRECT_LOCALE_SWITCH__ ? [LanguageSwitch] : [],
10
+ rootComponents: __REDIRECT_LOCALE_SWITCH__ ? [RedirectModal] : [],
11
11
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vuepress/plugin-redirect",
3
- "version": "2.0.0-rc.27",
3
+ "version": "2.0.0-rc.28",
4
4
  "description": "VuePress plugin - redirect",
5
5
  "keywords": [
6
6
  "vuepress-plugin",
@@ -42,10 +42,10 @@
42
42
  "style": "sass src:lib --no-source-map"
43
43
  },
44
44
  "dependencies": {
45
- "@vuepress/helper": "~2.0.0-rc.27",
45
+ "@vuepress/helper": "~2.0.0-rc.28",
46
46
  "@vueuse/core": "^10.9.0",
47
47
  "cac": "^6.7.14",
48
- "vue": "^3.4.26"
48
+ "vue": "^3.4.27"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "vuepress": "2.0.0-rc.9"
@@ -53,5 +53,5 @@
53
53
  "publishConfig": {
54
54
  "access": "public"
55
55
  },
56
- "gitHead": "146dd05c696c2287eba9498ee8ec7476fabccb0b"
56
+ "gitHead": "1d717308a79aa1056168d0a70a8fc33c19c7c764"
57
57
  }
@@ -1,98 +0,0 @@
1
- import { usePreferredLanguages, useScrollLock, useSessionStorage, } from '@vueuse/core';
2
- import { computed, defineComponent, h, onBeforeUnmount, onMounted, ref, TransitionGroup, watch, } from 'vue';
3
- import { useRoute, useRouteLocale, useRouter } from 'vuepress/client';
4
- import { redirectLocaleConfig, redirectLocaleEntries } from '../define.js';
5
- import '../styles/language-switch.css';
6
- const redirectLocales = __REDIRECT_LOCALES__;
7
- const { switchLocale } = redirectLocaleConfig;
8
- const REDIRECT_LOCALE_STORAGE = useSessionStorage('VUEPRESS_REDIRECT_LOCALES', {});
9
- export default defineComponent({
10
- name: 'LanguageSwitch',
11
- setup() {
12
- const languages = usePreferredLanguages();
13
- const route = useRoute();
14
- const router = useRouter();
15
- const routeLocale = useRouteLocale();
16
- const showModal = ref(false);
17
- const info = computed(() => {
18
- if (redirectLocaleEntries.some(([key]) => routeLocale.value === key))
19
- for (const language of languages.value)
20
- for (const [localePath, langs] of redirectLocaleEntries)
21
- if (langs.includes(language)) {
22
- if (localePath === routeLocale.value)
23
- return null;
24
- return {
25
- lang: language,
26
- localePath,
27
- };
28
- }
29
- return null;
30
- });
31
- const locale = computed(() => {
32
- if (info.value) {
33
- const { lang, localePath } = info.value;
34
- const locales = [
35
- redirectLocales[routeLocale.value],
36
- redirectLocales[localePath],
37
- ];
38
- return {
39
- hint: locales.map(({ hint }) => hint.replace('$1', lang)),
40
- switch: locales
41
- .map(({ switch: switchText }) => switchText.replace('$1', lang))
42
- .join(' / '),
43
- cancel: locales.map(({ cancel }) => cancel).join(' / '),
44
- };
45
- }
46
- return null;
47
- });
48
- const targetRoute = computed(() => info.value
49
- ? route.path.replace(routeLocale.value, info.value.localePath)
50
- : null);
51
- const updateStatus = () => {
52
- REDIRECT_LOCALE_STORAGE.value[routeLocale.value] = true;
53
- showModal.value = false;
54
- };
55
- onMounted(() => {
56
- const isLocked = useScrollLock(document.body);
57
- if (!REDIRECT_LOCALE_STORAGE.value[routeLocale.value])
58
- if (info.value)
59
- if (switchLocale === 'direct')
60
- router.replace(targetRoute.value);
61
- else if (switchLocale === 'modal')
62
- showModal.value = true;
63
- else
64
- showModal.value = false;
65
- else
66
- showModal.value = false;
67
- watch(showModal, (value) => {
68
- isLocked.value = value;
69
- }, { immediate: true });
70
- onBeforeUnmount(() => {
71
- isLocked.value = false;
72
- });
73
- });
74
- return () => showModal.value
75
- ? h(TransitionGroup, { name: 'redirect-modal-fade' }, () => showModal.value
76
- ? h('div', { key: 'mask', class: 'redirect-modal-mask' }, h('div', {
77
- key: 'popup',
78
- class: 'redirect-modal-wrapper',
79
- }, [
80
- h('div', { class: 'redirect-modal-content' }, locale.value?.hint.map((text) => h('p', text))),
81
- h('button', {
82
- type: 'button',
83
- class: 'redirect-modal-action primary',
84
- onClick: () => {
85
- updateStatus();
86
- router.replace(targetRoute.value);
87
- },
88
- }, locale.value?.switch),
89
- h('button', {
90
- type: 'button',
91
- class: 'redirect-modal-action',
92
- onClick: () => updateStatus(),
93
- }, locale.value?.cancel),
94
- ]))
95
- : null)
96
- : null;
97
- },
98
- });