@tonar/vue-kit 0.0.1-canary → 0.1.0-beta.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.
@@ -1,2 +1,2 @@
1
- import { t as e } from "./js/components.BgTB9fgn.js";
2
- export { e as SoAuthority };
1
+ import { n as e, t } from "./js/components.c2ySYHVt.js";
2
+ export { e as SoAuthority, t as SoTest };
@@ -0,0 +1,44 @@
1
+ import { computed as e, ref as t } from "vue";
2
+ //#region src/composables/storage/index.ts
3
+ var n = ({ key: n, defaultValue: r, hasDiff: i, onChange: a, storage: o = localStorage, checkType: s = () => !0, beforeunload: c }) => {
4
+ let l = (e) => {
5
+ try {
6
+ let t = e ? JSON.parse(e) : r;
7
+ return s(t) ? t : r;
8
+ } catch (e) {
9
+ return console.warn("规范化出错,已使用默认值", e), r;
10
+ }
11
+ }, u = /* @__PURE__ */ new WeakSet(), d = (e) => {
12
+ let t = JSON.stringify(e);
13
+ o.setItem(n, t);
14
+ let r = new StorageEvent("storage", {
15
+ key: n,
16
+ newValue: t,
17
+ storageArea: o
18
+ });
19
+ u.add(r), window.dispatchEvent(r);
20
+ }, f = t(l(o.getItem(n) ?? "")), p = (e, t = !1) => {
21
+ (i ?? ((e, t) => e !== t))(f.value, e) && (a?.(e), f.value = e, t && d(e));
22
+ }, m = e({
23
+ get() {
24
+ return f.value;
25
+ },
26
+ set(e) {
27
+ p(e, !0);
28
+ }
29
+ }), h = (e) => {
30
+ if (!(u.has(e) || e.storageArea !== o || e.key !== n)) try {
31
+ let t = l(e.newValue ?? "");
32
+ p(t, !1);
33
+ } catch (e) {
34
+ console.error("storage事件处理出错", e);
35
+ }
36
+ };
37
+ return window.addEventListener("storage", h), c && window.addEventListener("beforeunload", c), [
38
+ m,
39
+ () => window.removeEventListener("storage", h),
40
+ () => c && window.removeEventListener("beforeunload", c)
41
+ ];
42
+ };
43
+ //#endregion
44
+ export { n as useStorage };
package/dist/index.esm.js CHANGED
@@ -1,2 +1,3 @@
1
- import { t as e } from "./js/components.BgTB9fgn.js";
2
- export { e as SoAuthority };
1
+ import { n as e, t } from "./js/components.c2ySYHVt.js";
2
+ import { useStorage as n } from "./composables.esm.js";
3
+ export { e as SoAuthority, t as SoTest, n as useStorage };
@@ -0,0 +1,47 @@
1
+ import { computed as e, createElementBlock as t, createElementVNode as n, defineComponent as r, openBlock as i, ref as a, toDisplayString as o } from "vue";
2
+ import { isNil as s } from "@tonar/utils";
3
+ //#region src/components/so-authority/composables/index.ts
4
+ var c = (e) => typeof e == "string" || typeof e == "bigint" || typeof e == "number" && Number.isFinite(e), l = (e) => c(e) || Array.isArray(e) && e.every(c), u = (e) => Array.isArray(e), d = (e, t) => {
5
+ if (!l(t)) throw TypeError("组件所需权限类型错误: 应该为 String、Number、BigInt 或 String[]、Number[]、BigInt[]");
6
+ if (!l(e)) throw TypeError("用户权限类型错误: 应该为 String、Number、BigInt 或 String[]、Number[]、BigInt[]");
7
+ let n = u(t), r = u(e);
8
+ return n && !r ? !1 : !n && r ? e.includes(t) : n && r ? t.every((t) => e.includes(t)) : Object.is(e, t);
9
+ }, f = (t) => [e(() => typeof t.requiredPermissions == "function" ? t.requiredPermissions(t.userPermissions) : s(t.requiredPermissions) || s(t.userPermissions) ? !1 : d(t.userPermissions, t.requiredPermissions))], p = r({
10
+ name: "SoAuthority",
11
+ props: {
12
+ requiredPermissions: {
13
+ type: [
14
+ String,
15
+ Number,
16
+ BigInt,
17
+ Array,
18
+ Function
19
+ ],
20
+ required: !0
21
+ },
22
+ userPermissions: {
23
+ type: [
24
+ String,
25
+ Number,
26
+ BigInt,
27
+ Array
28
+ ],
29
+ required: !0
30
+ }
31
+ },
32
+ setup(e, { slots: t }) {
33
+ let [n] = f(e);
34
+ return () => n.value ? t.default?.({
35
+ userPermissions: e.userPermissions,
36
+ isPermission: n.value
37
+ }) : null;
38
+ }
39
+ }), m = /* @__PURE__ */ r({
40
+ __name: "so-test",
41
+ setup(e) {
42
+ let r = a(0);
43
+ return console.log("SoTest", (e) => (console.log(e), r.value++, r.value)), (e, a) => (i(), t("div", null, [a[0] || (a[0] = n("h1", null, "Hello Vue 3!", -1)), n("p", null, "count: " + o(r.value), 1)]));
44
+ }
45
+ });
46
+ //#endregion
47
+ export { p as n, m as t };
@@ -1 +1,2 @@
1
1
  export { default as SoAuthority } from './so-authority';
2
+ export { default as SoTest } from './so-test/so-test.vue';
@@ -1,4 +1,5 @@
1
- type PermissionType = string | number | string[] | number[];
1
+ type PermissionValue = string | number | bigint;
2
+ type PermissionType = PermissionValue | PermissionValue[];
2
3
  export type AuthorityProps = {
3
4
  requiredPermissions: PermissionType | ((userPermissions: PermissionType) => boolean);
4
5
  userPermissions: PermissionType;
@@ -7,9 +8,9 @@ export type AuthorityProps = {
7
8
  * @author sonion
8
9
  * @description 验证是否有具有权限
9
10
  * @param {object} props - 组件props
10
- * @param {PermissionType} props.componentPermissions - 组件所需权限 常改变,每个组件需要的权限不一样
11
+ * @param {PermissionType} props.requiredPermissions - 组件所需权限 常改变,每个组件需要的权限不一样
11
12
  * @param {PermissionType} props.userPermissions - 用户具有的权限 不怎么改变
12
13
  * @returns {boolean} - 是否具有权限
13
14
  */
14
- export declare const usePermission: (props: AuthorityProps) => readonly [import('vue').Ref<boolean, boolean>, () => boolean];
15
+ export declare const usePermission: (props: AuthorityProps) => readonly [import('vue').ComputedRef<boolean>];
15
16
  export {};
@@ -0,0 +1 @@
1
+ export * from './storage';
@@ -0,0 +1,18 @@
1
+ type StorageParams<T> = {
2
+ /** 储存的key */
3
+ key: string;
4
+ /** 默认值 */
5
+ defaultValue: T;
6
+ /** 对比函数,默认对比引用是否不相同。 */
7
+ hasDiff?: (prev: T, next: T) => boolean;
8
+ /** 变化回调 */
9
+ onChange?: (val: T) => void;
10
+ /** 储存类型。 localStorage 或 sessionStorage */
11
+ storage?: typeof localStorage | typeof sessionStorage;
12
+ /** 初始化类型检查函数,检查不通过使用初始值。可避免类型不对引起的错误 */
13
+ checkType?: (val: T) => boolean;
14
+ /** tab关闭前的回调, 相同key的不同回调只有初始生效。 */
15
+ beforeunload?: () => void;
16
+ };
17
+ export declare const useStorage: <T>({ key, defaultValue, hasDiff, onChange, storage, checkType, beforeunload, }: StorageParams<T>) => readonly [import('vue').WritableComputedRef<T, T>, () => void, () => void | undefined];
18
+ export {};
@@ -1 +1,2 @@
1
1
  export * from './components';
2
+ export * from './composables';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tonar/vue-kit",
3
- "version": "0.0.1-canary",
3
+ "version": "0.1.0-beta.1",
4
4
  "private": false,
5
5
  "description": "@tonar/vue-kit: Vue 3 ESM toolkit with permission-based rendering components such as SoAuthority.",
6
6
  "keywords": [
@@ -34,16 +34,20 @@
34
34
  },
35
35
  "./composables": {
36
36
  "import": "./dist/composables.esm.js",
37
- "types": "./dist/types/composables.d.ts"
37
+ "types": "./dist/types/composables/index.d.ts"
38
38
  },
39
39
  "./components": {
40
40
  "import": "./dist/components.esm.js",
41
- "types": "./dist/types/components.d.ts"
41
+ "types": "./dist/types/components/index.d.ts"
42
42
  }
43
43
  },
44
+ "dependencies": {
45
+ "@tonar/utils": "0.1.0-beta.1"
46
+ },
44
47
  "devDependencies": {
45
48
  "@vitejs/plugin-vue": "^6.0.7",
46
- "vue": "^3.5.38"
49
+ "@vue/test-utils": "^2.4.11",
50
+ "vue": "^3.5.39"
47
51
  },
48
52
  "peerDependencies": {
49
53
  "vue": "^3.0.0"
@@ -55,6 +59,8 @@
55
59
  }
56
60
  },
57
61
  "scripts": {
58
- "build": "rimraf dist && vite build"
62
+ "build": "rimraf dist && vite build",
63
+ "test": "vitest --run",
64
+ "typecheck": "pnpm exec tsc --noEmit"
59
65
  }
60
66
  }
@@ -1,41 +0,0 @@
1
- import { defineComponent as e, ref as t } from "vue";
2
- //#region src/components/so-authority/composables/index.ts
3
- var n = (e) => typeof e == "string" || typeof e == "number" || Array.isArray(e) && !!e.length, r = (e, t) => {
4
- if (!n(t)) throw TypeError("组件所需权限类型错误: 应该为 String、Number 或 String[]");
5
- if (!n(e)) throw TypeError("用户权限类型错误: 应该为 String、Number 或 String[]");
6
- let r = Array.isArray(t), i = Array.isArray(e);
7
- return r && !i ? !1 : !r && i ? e.includes(t) : !r && !i ? Object.is(e, t) : t.every((t) => e.includes(t));
8
- }, i = (e) => {
9
- let n = t(!1);
10
- return [n, () => n.value = (typeof e.requiredPermissions == "function" ? e.requiredPermissions : r)(e.userPermissions, e.requiredPermissions)];
11
- }, a = e({
12
- name: "so-authority",
13
- props: {
14
- requiredPermissions: {
15
- type: [
16
- String,
17
- Number,
18
- Array,
19
- Function
20
- ],
21
- required: !0
22
- },
23
- userPermissions: {
24
- type: [
25
- String,
26
- Number,
27
- Array
28
- ],
29
- required: !0
30
- }
31
- },
32
- setup(e, { slots: t }) {
33
- let [n, r] = i(e);
34
- return () => r() ? t.default?.({
35
- userPermissions: e.userPermissions,
36
- isPermission: n.value
37
- }) : null;
38
- }
39
- });
40
- //#endregion
41
- export { a as t };