@vue-spark/app-helpers 0.1.0-beta.1 → 0.1.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.
package/README.md CHANGED
@@ -10,7 +10,7 @@ npm i @vue-spark/app-helpers
10
10
 
11
11
  ## 使用方式
12
12
 
13
- 请参考 [./src](./src) 目录下的各工具的**README.md**文件。
13
+ 请参考 [src](./src) 目录下的各工具的 **README.md** 文件。
14
14
 
15
15
  ## License
16
16
 
@@ -1,5 +1,5 @@
1
1
  import { ensureArray } from "../utils-BwZlLmaw.js";
2
- import { defineComponent, inject, shallowRef, triggerRef } from "vue";
2
+ import { defineComponent, effectScope, inject, shallowRef, triggerRef } from "vue";
3
3
 
4
4
  //#region src/permission/injection.ts
5
5
  const permissionKey = Symbol("permission key");
@@ -60,7 +60,8 @@ const PERMISSION_CODE_ALL = "*";
60
60
  * @param options permission options
61
61
  */
62
62
  function createPermission(options = {}) {
63
- const codeSet = shallowRef(new Set(ensureArray(options.initialCodes ?? [])));
63
+ const scope = effectScope(true);
64
+ const codeSet = scope.run(() => shallowRef(new Set(ensureArray(options.initialCodes ?? []))));
64
65
  const permission = {
65
66
  get codes() {
66
67
  return [...codeSet.value];
@@ -83,7 +84,10 @@ function createPermission(options = {}) {
83
84
  install(app) {
84
85
  app.config.globalProperties.$permission = permission;
85
86
  app.provide(permissionKey, permission);
86
- app.onUnmount(() => permission.clear());
87
+ app.onUnmount(() => {
88
+ permission.clear();
89
+ scope.stop();
90
+ });
87
91
  }
88
92
  };
89
93
  return permission;
@@ -1,3 +1,5 @@
1
+ import { EffectScope } from "vue";
2
+
1
3
  //#region src/tabs/index.d.ts
2
4
  type TabType = string;
3
5
  type TabsSideType = 'left' | 'right';
@@ -38,6 +40,11 @@ interface TabsHelper<TabData extends {}> {
38
40
  * 配置项
39
41
  */
40
42
  options: TabsHelperOptions<TabData>;
43
+ /**
44
+ * 响应式副作用域,创建响应式变量或监听响应式数据时应在 `scope.run()` 中执行,
45
+ * 在合适时需要调用 `scope.stop()` 释放响应式副作用
46
+ */
47
+ scope: EffectScope;
41
48
  /**
42
49
  * 当前激活的标签
43
50
  */
@@ -54,6 +61,10 @@ interface TabsHelper<TabData extends {}> {
54
61
  * @param tabData 标签数据
55
62
  */
56
63
  setTabData: (tab: TabType, tabData: TabData) => void;
64
+ /**
65
+ * 获取标签数量
66
+ */
67
+ getTabsCount: () => number;
57
68
  /**
58
69
  * 获取所有标签和标签数据
59
70
  */
@@ -75,7 +86,7 @@ interface TabsHelper<TabData extends {}> {
75
86
  */
76
87
  getSideTabs: (tab: TabType, side: TabsSideType) => [tab: TabType, tabData: TabData][];
77
88
  /**
78
- * 添加标签和标签数据,遇到重复的标签仅更新标签数据
89
+ * 添加标签和标签数据,遇到重复的标签将会跳过,否则会在添加后更新激活标签
79
90
  * @param tab 标签
80
91
  * @param tabData 标签数据
81
92
  */
@@ -1,5 +1,5 @@
1
1
  import { assign, isArray } from "../utils-BwZlLmaw.js";
2
- import { shallowRef, triggerRef } from "vue";
2
+ import { effectScope, shallowRef, triggerRef } from "vue";
3
3
 
4
4
  //#region src/tabs/index.ts
5
5
  const defaultOptions = {
@@ -8,8 +8,9 @@ const defaultOptions = {
8
8
  };
9
9
  function createTabsHelper(userOptions = {}) {
10
10
  const options = assign({}, defaultOptions, userOptions);
11
- const activeTab = shallowRef();
12
- const tabMap = shallowRef(new Map());
11
+ const scope = effectScope(true);
12
+ const activeTab = scope.run(() => shallowRef());
13
+ const tabMap = scope.run(() => shallowRef(new Map()));
13
14
  const getRealTab = (targetTab) => {
14
15
  return isArray(targetTab) ? targetTab[0] : targetTab;
15
16
  };
@@ -35,6 +36,7 @@ function createTabsHelper(userOptions = {}) {
35
36
  };
36
37
  const helper = {
37
38
  options,
39
+ scope,
38
40
  get activeTab() {
39
41
  return activeTab.value;
40
42
  },
@@ -47,9 +49,15 @@ function createTabsHelper(userOptions = {}) {
47
49
  setTabData(targetTab, tabData) {
48
50
  helper.hasTab(targetTab) && setTab(targetTab, tabData);
49
51
  },
52
+ getTabsCount() {
53
+ return tabMap.value.size;
54
+ },
50
55
  getTabs() {
51
56
  return [...tabMap.value];
52
57
  },
58
+ setTabs(tabs) {
59
+ tabMap.value = new Map(tabs);
60
+ },
53
61
  indexOf(targetTab) {
54
62
  return [...tabMap.value.keys()].indexOf(targetTab);
55
63
  },
@@ -58,12 +66,11 @@ function createTabsHelper(userOptions = {}) {
58
66
  const index = helper.indexOf(targetTab);
59
67
  return side === "left" ? tabs.slice(0, index) : tabs.slice(index + 1);
60
68
  },
61
- setTabs(tabs) {
62
- tabMap.value = new Map(tabs);
63
- },
64
69
  addTab(targetTab, tabData) {
65
- setTab(targetTab, tabData);
66
- setActiveTab(targetTab);
70
+ if (!helper.hasTab(targetTab)) {
71
+ setTab(targetTab, tabData);
72
+ setActiveTab(targetTab);
73
+ }
67
74
  },
68
75
  hasTab(targetTab) {
69
76
  return tabMap.value.has(targetTab);
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@vue-spark/app-helpers",
3
3
  "type": "module",
4
- "version": "0.1.0-beta.1",
4
+ "version": "0.1.0",
5
+ "packageManager": "pnpm@10.11.0",
5
6
  "description": "Lightweight Helpers for Vue 3 Application Development.",
6
7
  "author": "leihaohao <https://github.com/l246804>",
7
8
  "license": "MIT",
@@ -22,8 +23,8 @@
22
23
  "tabs"
23
24
  ],
24
25
  "exports": {
25
- "./package.json": "./package.json",
26
- "./*": "./dist/*/index.js"
26
+ "./*": "./dist/*/index.js",
27
+ "./package.json": "./package.json"
27
28
  },
28
29
  "files": [
29
30
  "dist"
@@ -32,6 +33,17 @@
32
33
  "access": "public",
33
34
  "registry": "https://registry.npmjs.org"
34
35
  },
36
+ "scripts": {
37
+ "dev": "tsdown --watch",
38
+ "build": "tsdown",
39
+ "test": "vitest",
40
+ "typecheck": "tsc --noEmit",
41
+ "format": "prettier . --write --cache --cache-location ./node_modules/.cache/.prettier-cache",
42
+ "lint": "eslint . --cache --cache-location ./node_modules/.cache/.eslint-cache",
43
+ "lint:fix": "pnpm run lint --fix",
44
+ "release": "bumpp && pnpm publish",
45
+ "prepublishOnly": "pnpm run build"
46
+ },
35
47
  "peerDependencies": {
36
48
  "vue": "^3.5.0",
37
49
  "vue-router": "^4.5.0"
@@ -43,6 +55,7 @@
43
55
  },
44
56
  "devDependencies": {
45
57
  "@antfu/eslint-config": "^4.14.1",
58
+ "@sxzz/prettier-config": "^2.2.3",
46
59
  "@tsconfig/node22": "^22.0.2",
47
60
  "@types/node": "^22.15.17",
48
61
  "@vue/tsconfig": "^0.7.0",
@@ -62,15 +75,5 @@
62
75
  },
63
76
  "simple-git-hooks": {
64
77
  "pre-commit": "npx lint-staged"
65
- },
66
- "scripts": {
67
- "dev": "tsdown --watch",
68
- "build": "tsdown",
69
- "test": "vitest",
70
- "typecheck": "tsc --noEmit",
71
- "format": "prettier . --write --cache --cache-location ./node_modules/.cache/.prettier-cache",
72
- "lint": "eslint . --cache --cache-location ./node_modules/.cache/.eslint-cache",
73
- "lint:fix": "pnpm run lint --fix",
74
- "release": "bumpp && pnpm publish"
75
78
  }
76
- }
79
+ }