@vue-spark/app-helpers 0.1.0-beta.2 → 0.2.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.
@@ -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,6 +1,15 @@
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';
6
+ interface TabsHelperRemoveOptions {
7
+ /**
8
+ * 当前激活的标签被移除时,是否激活下一个标签
9
+ * @default true
10
+ */
11
+ activeNext?: boolean;
12
+ }
4
13
  interface TabsHelperOptions<TabData extends {}> {
5
14
  /**
6
15
  * 判断标签是否可移除,返回假值时,标签将不可移除
@@ -38,6 +47,11 @@ interface TabsHelper<TabData extends {}> {
38
47
  * 配置项
39
48
  */
40
49
  options: TabsHelperOptions<TabData>;
50
+ /**
51
+ * 响应式副作用域,创建响应式变量或监听响应式数据时应在 `scope.run()` 中执行,
52
+ * 在合适时需要调用 `scope.stop()` 释放响应式副作用
53
+ */
54
+ scope: EffectScope;
41
55
  /**
42
56
  * 当前激活的标签
43
57
  */
@@ -54,6 +68,10 @@ interface TabsHelper<TabData extends {}> {
54
68
  * @param tabData 标签数据
55
69
  */
56
70
  setTabData: (tab: TabType, tabData: TabData) => void;
71
+ /**
72
+ * 获取标签数量
73
+ */
74
+ getTabsCount: () => number;
57
75
  /**
58
76
  * 获取所有标签和标签数据
59
77
  */
@@ -78,8 +96,15 @@ interface TabsHelper<TabData extends {}> {
78
96
  * 添加标签和标签数据,遇到重复的标签将会跳过,否则会在添加后更新激活标签
79
97
  * @param tab 标签
80
98
  * @param tabData 标签数据
99
+ * @param options 选项
81
100
  */
82
- addTab: (tab: TabType, tabData: TabData) => void;
101
+ addTab: (tab: TabType, tabData: TabData, options?: {
102
+ /**
103
+ * 是否激活该标签
104
+ * @default true
105
+ */
106
+ active?: boolean;
107
+ }) => void;
83
108
  /**
84
109
  * 判断是否存在指定标签
85
110
  * @param tab 标签
@@ -108,19 +133,22 @@ interface TabsHelper<TabData extends {}> {
108
133
  /**
109
134
  * 移除指定标签,若移除的是当前激活标签则移除前会自动激活下一个标签
110
135
  * @param tab 指定标签,默认为当前激活的标签
136
+ * @param options 选项
111
137
  */
112
- removeTab: (tab?: TabType) => Promise<void>;
138
+ removeTab: (tab?: TabType, options?: TabsHelperRemoveOptions) => Promise<void>;
113
139
  /**
114
140
  * 移除除过指定标签的其他可移除的标签,并将指定标签设置为激活标签
115
141
  * @param tab 指定标签,默认为当前激活的标签
142
+ * @param options 选项
116
143
  */
117
- removeOtherTabs: (tab?: TabType) => Promise<void>;
144
+ removeOtherTabs: (tab?: TabType, options?: TabsHelperRemoveOptions) => Promise<void>;
118
145
  /**
119
146
  * 移除指定标签的指定方向侧的所有可移除的标签,若当前激活的标签存在被移除的标签内,则将指定标签设置为激活标签
120
147
  * @param tab 指定标签,默认为当前激活的标签
148
+ * @param options 选项
121
149
  */
122
- removeSideTabs: (side: TabsSideType, tab?: TabType) => Promise<void>;
150
+ removeSideTabs: (side: TabsSideType, tab?: TabType, options?: TabsHelperRemoveOptions) => Promise<void>;
123
151
  }
124
152
  declare function createTabsHelper<TabData extends {}>(userOptions?: TabsHelperOptions<TabData>): TabsHelper<TabData>;
125
153
  //#endregion
126
- export { TabsHelper, TabsHelperOptions, TabsSideType, createTabsHelper };
154
+ export { TabsHelper, TabsHelperOptions, TabsHelperRemoveOptions, TabsSideType, createTabsHelper };
@@ -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,13 +66,10 @@ 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
- addTab(targetTab, tabData) {
69
+ addTab(targetTab, tabData, { active = true } = {}) {
65
70
  if (!helper.hasTab(targetTab)) {
66
71
  setTab(targetTab, tabData);
67
- setActiveTab(targetTab);
72
+ active && setActiveTab(targetTab);
68
73
  }
69
74
  },
70
75
  hasTab(targetTab) {
@@ -96,28 +101,28 @@ function createTabsHelper(userOptions = {}) {
96
101
  }
97
102
  return valid;
98
103
  },
99
- async removeTab(targetTab = helper.activeTab) {
104
+ async removeTab(targetTab = helper.activeTab, { activeNext = true } = {}) {
100
105
  if (tabMap.value.size <= 1 || !targetTab || !canRemoveTab(targetTab) || !await helper.tryRemoveTabs([targetTab])) return;
101
106
  const tabs = helper.getTabs();
102
107
  const index = helper.indexOf(targetTab);
103
108
  const isActive = targetTab === helper.activeTab;
104
109
  const nextTab = isActive ? tabs[index - 1] || tabs[index + 1] : null;
105
- nextTab && setActiveTab(nextTab);
110
+ nextTab && activeNext && setActiveTab(nextTab);
106
111
  removeTabs([targetTab]);
107
112
  },
108
- async removeOtherTabs(targetTab = helper.activeTab) {
113
+ async removeOtherTabs(targetTab = helper.activeTab, { activeNext = true } = {}) {
109
114
  if (!targetTab) return;
110
115
  const otherTabs = helper.getTabs().filter(([tab]) => tab !== targetTab && canRemoveTab(tab));
111
116
  if (!await helper.tryRemoveTabs(otherTabs)) return;
112
- targetTab !== helper.activeTab && setActiveTab(targetTab);
117
+ activeNext && targetTab !== helper.activeTab && setActiveTab(targetTab);
113
118
  removeTabs(otherTabs);
114
119
  },
115
- async removeSideTabs(side, targetTab = helper.activeTab) {
120
+ async removeSideTabs(side, targetTab = helper.activeTab, { activeNext = true } = {}) {
116
121
  if (!targetTab) return;
117
122
  const sideTabs = helper.getSideTabs(targetTab, side).filter(canRemoveTab);
118
123
  if (!await helper.tryRemoveTabs(sideTabs)) return;
119
124
  const activeInSide = targetTab !== helper.activeTab && sideTabs.some(([tab]) => tab === helper.activeTab);
120
- activeInSide && setActiveTab(targetTab);
125
+ activeInSide && activeNext && setActiveTab(targetTab);
121
126
  removeTabs(sideTabs);
122
127
  }
123
128
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vue-spark/app-helpers",
3
3
  "type": "module",
4
- "version": "0.1.0-beta.2",
4
+ "version": "0.2.0",
5
5
  "description": "Lightweight Helpers for Vue 3 Application Development.",
6
6
  "author": "leihaohao <https://github.com/l246804>",
7
7
  "license": "MIT",
@@ -43,13 +43,14 @@
43
43
  },
44
44
  "devDependencies": {
45
45
  "@antfu/eslint-config": "^4.14.1",
46
+ "@sxzz/prettier-config": "^2.2.3",
46
47
  "@tsconfig/node22": "^22.0.2",
47
48
  "@types/node": "^22.15.17",
48
49
  "@vue/tsconfig": "^0.7.0",
49
50
  "bumpp": "^10.1.0",
50
51
  "eslint": "^9.26.0",
51
52
  "eslint-plugin-format": "^1.0.1",
52
- "happy-dom": "^17.4.7",
53
+ "happy-dom": "^20.0.0",
53
54
  "lint-staged": "^16.1.2",
54
55
  "prettier": "^3.5.3",
55
56
  "simple-git-hooks": "^2.13.0",