@tonar/vue-kit 0.1.0-beta.2 → 0.2.7

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,3 @@
1
+ import { n as so_authority_default, t as _sfc_main } from "./js/components.Pdybuv0e.js";
2
+
3
+ export { so_authority_default as SoAuthority, _sfc_main as SoTest };
@@ -0,0 +1,60 @@
1
+ import { computed, ref } from "vue";
2
+
3
+ //#region src/composables/storage/index.ts
4
+ const useStorage = ({ key, defaultValue, hasDiff, onChange, storage = localStorage, checkType = () => true, beforeunload }) => {
5
+ const normalizedValue = (val) => {
6
+ try {
7
+ const saved = val ? JSON.parse(val) : defaultValue;
8
+ return checkType(saved) ? saved : defaultValue;
9
+ } catch (err) {
10
+ console.warn("规范化出错,已使用默认值", err);
11
+ return defaultValue;
12
+ }
13
+ };
14
+ const selfDispatchedEvents = /* @__PURE__ */ new WeakSet();
15
+ const dispatchPersist = (value) => {
16
+ const newValue = JSON.stringify(value);
17
+ storage.setItem(key, newValue);
18
+ const event = new StorageEvent("storage", {
19
+ key,
20
+ newValue,
21
+ storageArea: storage
22
+ });
23
+ selfDispatchedEvents.add(event);
24
+ window.dispatchEvent(event);
25
+ };
26
+ const internalRef = ref(normalizedValue(storage.getItem(key) ?? ""));
27
+ const setInternalRef = (value, dispatch = false) => {
28
+ if (!(hasDiff ?? ((prev, next) => prev !== next))(internalRef.value, value)) return;
29
+ onChange?.(value);
30
+ internalRef.value = value;
31
+ dispatch && dispatchPersist(value);
32
+ };
33
+ const storedValue = computed({
34
+ get() {
35
+ return internalRef.value;
36
+ },
37
+ set(value) {
38
+ setInternalRef(value, true);
39
+ }
40
+ });
41
+ const handleStorageChange = (event) => {
42
+ if (selfDispatchedEvents.has(event) || event.storageArea !== storage || event.key !== key) return;
43
+ try {
44
+ const newValue = normalizedValue(event.newValue ?? "");
45
+ setInternalRef(newValue, false);
46
+ } catch (error) {
47
+ console.error("storage事件处理出错", error);
48
+ }
49
+ };
50
+ window.addEventListener("storage", handleStorageChange);
51
+ beforeunload && window.addEventListener("beforeunload", beforeunload);
52
+ return [
53
+ storedValue,
54
+ () => window.removeEventListener("storage", handleStorageChange),
55
+ () => beforeunload && window.removeEventListener("beforeunload", beforeunload)
56
+ ];
57
+ };
58
+
59
+ //#endregion
60
+ export { useStorage };
@@ -0,0 +1,4 @@
1
+ import { n as so_authority_default, t as _sfc_main } from "./js/components.Pdybuv0e.js";
2
+ import { useStorage } from "./composables.es.js";
3
+
4
+ export { so_authority_default as SoAuthority, _sfc_main as SoTest, useStorage };
@@ -0,0 +1,114 @@
1
+ import { computed, createElementBlock, createElementVNode, defineComponent, openBlock, ref, toDisplayString } from "vue";
2
+ import { isNil } from "@tonar/utils";
3
+
4
+ //#region src/components/so-authority/composables/index.ts
5
+ /**
6
+ * @author sonion
7
+ * @description 检查是否为合法权限值
8
+ * @param val - 待检查的值
9
+ */
10
+ const isPermissionValue = (val) => typeof val === "string" || typeof val === "bigint" || typeof val === "number" && Number.isFinite(val);
11
+ /**
12
+ * @author sonion
13
+ * @description 权限类型检查
14
+ * @param {unknown} val - 权限
15
+ * @returns {boolean} - 类型是否有错误
16
+ */
17
+ const isPermissionType = (val) => isPermissionValue(val) || Array.isArray(val) && val.every(isPermissionValue);
18
+ const isArray = (val) => Array.isArray(val);
19
+ /**
20
+ * @author sonion
21
+ * @description 验证是否具有权限
22
+ * @param {PermissionType} userPermissions - 用户具有的权限 不怎么改变
23
+ * @param {PermissionType} requiredPermissions - 组件所需权限 常改变,每个组件需要的权限不一样
24
+ * @returns {boolean} - 是否具有权限
25
+ */
26
+ const permissionVerify = (userPermissions, requiredPermissions) => {
27
+ if (!isPermissionType(requiredPermissions)) throw new TypeError(`组件所需权限类型错误: 应该为 String、Number、BigInt 或 String[]、Number[]、BigInt[]`);
28
+ if (!isPermissionType(userPermissions)) throw new TypeError(`用户权限类型错误: 应该为 String、Number、BigInt 或 String[]、Number[]、BigInt[]`);
29
+ const isRequiredArray = isArray(requiredPermissions);
30
+ const isUserArray = isArray(userPermissions);
31
+ if (isRequiredArray && !isUserArray) return false;
32
+ if (!isRequiredArray && isUserArray) return userPermissions.includes(requiredPermissions);
33
+ if (isRequiredArray && isUserArray) return requiredPermissions.every((item) => userPermissions.includes(item));
34
+ return Object.is(userPermissions, requiredPermissions);
35
+ };
36
+ /**
37
+ * @author sonion
38
+ * @description 验证是否有具有权限
39
+ * @param {object} props - 组件props
40
+ * @param {PermissionType} props.requiredPermissions - 组件所需权限 常改变,每个组件需要的权限不一样
41
+ * @param {PermissionType} props.userPermissions - 用户具有的权限 不怎么改变
42
+ * @returns {boolean} - 是否具有权限
43
+ */
44
+ const usePermission = (props) => {
45
+ return [computed(() => {
46
+ if (typeof props.requiredPermissions === "function") return props.requiredPermissions(props.userPermissions);
47
+ if (isNil(props.requiredPermissions) || isNil(props.userPermissions)) return false;
48
+ return permissionVerify(props.userPermissions, props.requiredPermissions);
49
+ })];
50
+ };
51
+
52
+ //#endregion
53
+ //#region src/components/so-authority/index.ts
54
+ var so_authority_default = defineComponent({
55
+ name: "SoAuthority",
56
+ props: {
57
+ /** 组件需要的权限 */
58
+ requiredPermissions: {
59
+ type: [
60
+ String,
61
+ Number,
62
+ BigInt,
63
+ Array,
64
+ Function
65
+ ],
66
+ required: true
67
+ },
68
+ /** 用户具有的权限 */
69
+ userPermissions: {
70
+ type: [
71
+ String,
72
+ Number,
73
+ BigInt,
74
+ Array
75
+ ],
76
+ required: true
77
+ }
78
+ },
79
+ setup(props, { slots }) {
80
+ const [isPermission] = usePermission(props);
81
+ return () => {
82
+ return isPermission.value ? slots.default?.({
83
+ userPermissions: props.userPermissions,
84
+ isPermission: isPermission.value
85
+ }) : null;
86
+ };
87
+ }
88
+ });
89
+
90
+ //#endregion
91
+ //#region src/components/so-test/so-test.vue
92
+ const _sfc_main = /*@__PURE__*/ defineComponent({
93
+ __name: "so-test",
94
+ setup(__props) {
95
+ const count = ref(0);
96
+ /**
97
+ * @author sonion
98
+ * @description 点击事件处理函数
99
+ * @param {number} a - 一个数字
100
+ */
101
+ const handleClick = (a) => {
102
+ console.log(a);
103
+ count.value++;
104
+ return count.value;
105
+ };
106
+ console.log("SoTest", handleClick);
107
+ return (_ctx, _cache) => {
108
+ return openBlock(), createElementBlock("div", null, [_cache[0] || (_cache[0] = createElementVNode("h1", null, "Hello Vue 3!", -1)), createElementVNode("p", null, "count: " + toDisplayString(count.value), 1)]);
109
+ };
110
+ }
111
+ });
112
+
113
+ //#endregion
114
+ export { so_authority_default as n, _sfc_main as t };
@@ -1,2 +1,3 @@
1
- export { default as SoAuthority } from './so-authority';
2
- export { default as SoTest } from './so-test/so-test.vue';
1
+ import _default from "./so-authority/index.js";
2
+ import _sfc_main from "./so-test/so-test.js";
3
+ export { _default as SoAuthority, _sfc_main as SoTest };
@@ -1,16 +1,9 @@
1
+ //#region src/components/so-authority/composables/index.d.ts
1
2
  type PermissionValue = string | number | bigint;
2
3
  type PermissionType = PermissionValue | PermissionValue[];
3
- export type AuthorityProps = {
4
- requiredPermissions: PermissionType | ((userPermissions: PermissionType) => boolean);
5
- userPermissions: PermissionType;
4
+ type AuthorityProps = {
5
+ requiredPermissions: PermissionType | ((userPermissions: PermissionType) => boolean);
6
+ userPermissions: PermissionType;
6
7
  };
7
- /**
8
- * @author sonion
9
- * @description 验证是否有具有权限
10
- * @param {object} props - 组件props
11
- * @param {PermissionType} props.requiredPermissions - 组件所需权限 常改变,每个组件需要的权限不一样
12
- * @param {PermissionType} props.userPermissions - 用户具有的权限 不怎么改变
13
- * @returns {boolean} - 是否具有权限
14
- */
15
- export declare const usePermission: (props: AuthorityProps) => readonly [import('vue').ComputedRef<boolean>];
16
- export {};
8
+ //#endregion
9
+ export { AuthorityProps };
@@ -1,28 +1,30 @@
1
- import { PropType } from 'vue';
2
- import { AuthorityProps } from './composables';
3
- declare const _default: import('vue').DefineComponent<import('vue').ExtractPropTypes<{
4
- /** 组件需要的权限 */
5
- requiredPermissions: {
6
- type: PropType<AuthorityProps["requiredPermissions"]>;
7
- required: true;
8
- };
9
- /** 用户具有的权限 */
10
- userPermissions: {
11
- type: PropType<AuthorityProps["userPermissions"]>;
12
- required: true;
13
- };
14
- }>, () => import('vue').VNode<import('vue').RendererNode, import('vue').RendererElement, {
15
- [key: string]: any;
16
- }>[] | null | undefined, {}, {}, {}, import('vue').ComponentOptionsMixin, import('vue').ComponentOptionsMixin, {}, string, import('vue').PublicProps, Readonly<import('vue').ExtractPropTypes<{
17
- /** 组件需要的权限 */
18
- requiredPermissions: {
19
- type: PropType<AuthorityProps["requiredPermissions"]>;
20
- required: true;
21
- };
22
- /** 用户具有的权限 */
23
- userPermissions: {
24
- type: PropType<AuthorityProps["userPermissions"]>;
25
- required: true;
26
- };
27
- }>> & Readonly<{}>, {}, {}, {}, {}, string, import('vue').ComponentProvideOptions, true, {}, any>;
28
- export default _default;
1
+ import { AuthorityProps } from "./composables/index.js";
2
+ import { PropType } from "vue";
3
+ //#region src/components/so-authority/index.d.ts
4
+ declare const _default: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
5
+ /** 组件需要的权限 */
6
+ requiredPermissions: {
7
+ type: PropType<AuthorityProps['requiredPermissions']>;
8
+ required: true;
9
+ };
10
+ /** 用户具有的权限 */
11
+ userPermissions: {
12
+ type: PropType<AuthorityProps['userPermissions']>;
13
+ required: true;
14
+ };
15
+ }>, () => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
16
+ [key: string]: any;
17
+ }>[] | null | undefined, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
18
+ /** 组件需要的权限 */
19
+ requiredPermissions: {
20
+ type: PropType<AuthorityProps['requiredPermissions']>;
21
+ required: true;
22
+ };
23
+ /** 用户具有的权限 */
24
+ userPermissions: {
25
+ type: PropType<AuthorityProps['userPermissions']>;
26
+ required: true;
27
+ };
28
+ }>> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
29
+ //#endregion
30
+ export { _default as default };
@@ -1 +1,2 @@
1
- export * from './storage';
1
+ import { useStorage } from "./storage/index.js";
2
+ export { useStorage };
@@ -1,18 +1,20 @@
1
+ //#region src/composables/storage/index.d.ts
1
2
  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;
3
+ /** 储存的key */
4
+ key: string;
5
+ /** 默认值 */
6
+ defaultValue: T;
7
+ /** 对比函数,默认对比引用是否不相同。 */
8
+ hasDiff?: (prev: T, next: T) => boolean;
9
+ /** 变化回调 */
10
+ onChange?: (val: T) => void;
11
+ /** 储存类型。 localStorage 或 sessionStorage */
12
+ storage?: Storage;
13
+ /** 初始化类型检查函数,检查不通过使用初始值。可避免类型不对引起的错误 */
14
+ checkType?: (val: T) => boolean;
15
+ /** tab关闭前的回调, 相同key的不同回调只有初始生效。 */
16
+ beforeunload?: () => void;
16
17
  };
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 {};
18
+ declare const useStorage: <T>({ key, defaultValue, hasDiff, onChange, storage, checkType, beforeunload }: StorageParams<T>) => readonly [import("vue").WritableComputedRef<T, T>, () => void, () => void | undefined];
19
+ //#endregion
20
+ export { useStorage };
@@ -1,2 +1,5 @@
1
- export * from './components';
2
- export * from './composables';
1
+ import _default from "./components/so-authority/index.js";
2
+ import _sfc_main from "./components/so-test/so-test.js";
3
+ import "./components/index.js";
4
+ import { useStorage } from "./composables/storage/index.js";
5
+ export { _default as SoAuthority, _sfc_main as SoTest, useStorage };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tonar/vue-kit",
3
- "version": "0.1.0-beta.2",
3
+ "version": "0.2.7",
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": [
@@ -25,29 +25,31 @@
25
25
  "sideEffects": [
26
26
  "**/*.css"
27
27
  ],
28
- "module": "dist/index.esm.js",
28
+ "module": "dist/index.es.js",
29
29
  "types": "dist/types/index.d.ts",
30
30
  "exports": {
31
31
  ".": {
32
- "import": "./dist/index.esm.js",
32
+ "import": "./dist/index.es.js",
33
33
  "types": "./dist/types/index.d.ts"
34
34
  },
35
35
  "./composables": {
36
- "import": "./dist/composables.esm.js",
36
+ "import": "./dist/composables.es.js",
37
37
  "types": "./dist/types/composables/index.d.ts"
38
38
  },
39
39
  "./components": {
40
- "import": "./dist/components.esm.js",
40
+ "import": "./dist/components.es.js",
41
41
  "types": "./dist/types/components/index.d.ts"
42
42
  }
43
43
  },
44
44
  "dependencies": {
45
- "@tonar/utils": "0.1.0-beta.2"
45
+ "@tonar/utils": "0.2.7"
46
46
  },
47
47
  "devDependencies": {
48
- "@vitejs/plugin-vue": "^6.0.7",
48
+ "@typescript/typescript6": "^6.0.2",
49
49
  "@vue/test-utils": "^2.4.11",
50
- "vue": "^3.5.39"
50
+ "unplugin-vue": "^7.2.0",
51
+ "vue": "^3.5.40",
52
+ "vue-tsc": "^3.3.8"
51
53
  },
52
54
  "peerDependencies": {
53
55
  "vue": "^3.0.0"
@@ -59,7 +61,7 @@
59
61
  }
60
62
  },
61
63
  "scripts": {
62
- "build": "rimraf dist && vite build",
64
+ "build": "tsdown",
63
65
  "test": "vitest --run",
64
66
  "typecheck": "pnpm exec tsc --noEmit"
65
67
  }
@@ -1,2 +0,0 @@
1
- import { n as e, t } from "./js/components.c2ySYHVt.js";
2
- export { e as SoAuthority, t as SoTest };
@@ -1,44 +0,0 @@
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 DELETED
@@ -1,3 +0,0 @@
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 };
@@ -1,47 +0,0 @@
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 };