@data-fair/lib-vue 1.25.5 → 1.26.0

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,7 @@
1
+ import type { MaybeRefOrGetter } from 'vue';
2
+ type LeaveGuardOptions = {
3
+ locale?: MaybeRefOrGetter<string>;
4
+ message?: string;
5
+ };
6
+ export declare const useLeaveGuard: (isDirty: MaybeRefOrGetter<boolean>, options?: LeaveGuardOptions) => void;
7
+ export default useLeaveGuard;
package/leave-guard.js ADDED
@@ -0,0 +1,26 @@
1
+ // maybe this will be a part of vueuse someday https://github.com/vueuse/vueuse/issues/4350
2
+ import { onBeforeRouteLeave } from 'vue-router';
3
+ import { toValue, onMounted, onUnmounted } from 'vue';
4
+ const messages = {
5
+ fr: 'Attention ! La page contient des modifications non enregistrées.',
6
+ en: 'Warning ! The page contains unsaved modifications.'
7
+ };
8
+ export const useLeaveGuard = (isDirty, options) => {
9
+ const getMessage = () => options?.message ?? messages[options?.locale ? toValue(options.locale) : 'en'] ?? messages.en;
10
+ // vue router guard
11
+ onBeforeRouteLeave(() => {
12
+ if (!toValue(isDirty))
13
+ return;
14
+ return window.confirm(getMessage());
15
+ });
16
+ // browser guard
17
+ const handleWindowClose = (e) => {
18
+ if (!toValue(isDirty))
19
+ return;
20
+ e.preventDefault();
21
+ e.returnValue = getMessage();
22
+ };
23
+ onMounted(() => window.addEventListener('beforeunload', handleWindowClose));
24
+ onUnmounted(() => window.removeEventListener('beforeunload', handleWindowClose));
25
+ };
26
+ export default useLeaveGuard;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-vue",
3
- "version": "1.25.5",
3
+ "version": "1.26.0",
4
4
  "description": "Composables and other utilities for Vue applications in the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [
package/vite.d.ts CHANGED
@@ -8,6 +8,7 @@ export declare const autoImports: (string | {
8
8
  '@data-fair/lib-vue/edit-fetch.js': string[];
9
9
  '@data-fair/lib-vue/ws.js': string[];
10
10
  '@data-fair/lib-vue/async-action.js': string[];
11
+ '@data-fair/lib-vue/leave-guard.js': string[];
11
12
  '@data-fair/lib-vue/deep-diff.js': string[];
12
13
  '@data-fair/lib-vue/format/bytes.js': string[];
13
14
  })[];
package/vite.js CHANGED
@@ -13,6 +13,7 @@ export const autoImports = [
13
13
  '@data-fair/lib-vue/edit-fetch.js': ['useEditFetch'],
14
14
  '@data-fair/lib-vue/ws.js': ['useWS'],
15
15
  '@data-fair/lib-vue/async-action.js': ['useAsyncAction'],
16
+ '@data-fair/lib-vue/leave-guard.js': ['useLeaveGuard'],
16
17
  '@data-fair/lib-vue/deep-diff.js': ['computedDeepDiff', 'watchDeepDiff'],
17
18
  '@data-fair/lib-vue/format/bytes.js': ['formatBytes'],
18
19
  }