Commit 2037293aa3b38ad8dc9bee4324647cb1484c1a9a
1 parent
215d8bab
refactor: refactor hooks
Showing
23 changed files
with
52 additions
and
394 deletions
CHANGELOG.zh_CN.md
1 | 1 | ## Wip |
2 | 2 | |
3 | +## (破坏性更新) Breaking changes | |
4 | + | |
5 | +- 使用 `pinia` 替换 `vuex`,`vuex-module-decorators`。 | |
6 | + | |
7 | + - 影响,之前如果有自己使用 vuex-module-decorators,需要改造为 pinia。 | |
8 | + - 原因: | |
9 | + - pinia 于 vuex5api 基本类似,且简单易懂。 | |
10 | + - 后续切换 vuex5 成本非常低,也可以当作第三方状态管理库使用 | |
11 | + | |
12 | +- 移除 `useKeyPress` 使用`vueuse`-`onKeyStroke`代替 | |
13 | +- 移除 `useDebounceFn` 使用`vueuse`-`useDebounceFn`代替 | |
14 | +- 移除 `useThrottle` 使用`vueuse`-`useThrottleFn`代替 | |
15 | + | |
16 | +### ✨ Refactor | |
17 | + | |
18 | +- 移除 `useElResize` | |
19 | + | |
3 | 20 | ### 🐛 Bug Fixes |
4 | 21 | |
5 | 22 | - 登录页样式修复 | ... | ... |
src/components/Application/src/search/useMenuSearch.ts
... | ... | @@ -3,15 +3,13 @@ import type { Menu } from '/@/router/types'; |
3 | 3 | import { ref, onBeforeMount, unref, Ref, nextTick } from 'vue'; |
4 | 4 | |
5 | 5 | import { getMenus } from '/@/router/menus'; |
6 | -import { KeyCodeEnum } from '/@/enums/keyCodeEnum'; | |
7 | 6 | |
8 | 7 | import { cloneDeep } from 'lodash-es'; |
9 | 8 | import { filter, forEach } from '/@/utils/helper/treeHelper'; |
10 | 9 | |
11 | -import { useDebounce } from '/@/hooks/core/useDebounce'; | |
12 | 10 | import { useGo } from '/@/hooks/web/usePage'; |
13 | 11 | import { useScrollTo } from '/@/hooks/event/useScrollTo'; |
14 | -import { useKeyPress } from '/@/hooks/event/useKeyPress'; | |
12 | +import { onKeyStroke, useDebounceFn } from '@vueuse/core'; | |
15 | 13 | import { useI18n } from '/@/hooks/web/useI18n'; |
16 | 14 | |
17 | 15 | export interface SearchResult { |
... | ... | @@ -41,7 +39,7 @@ export function useMenuSearch(refs: Ref<HTMLElement[]>, scrollWrap: Ref<ElRef>, |
41 | 39 | |
42 | 40 | const { t } = useI18n(); |
43 | 41 | const go = useGo(); |
44 | - const [handleSearch] = useDebounce(search, 200); | |
42 | + const handleSearch = useDebounceFn(search, 200); | |
45 | 43 | |
46 | 44 | onBeforeMount(async () => { |
47 | 45 | const list = await getMenus(); |
... | ... | @@ -146,23 +144,10 @@ export function useMenuSearch(refs: Ref<HTMLElement[]>, scrollWrap: Ref<ElRef>, |
146 | 144 | emit('close'); |
147 | 145 | } |
148 | 146 | |
149 | - useKeyPress(['enter', 'up', 'down', 'esc'], (events) => { | |
150 | - const keyCode = events.keyCode; | |
151 | - switch (keyCode) { | |
152 | - case KeyCodeEnum.UP: | |
153 | - handleUp(); | |
154 | - break; | |
155 | - case KeyCodeEnum.DOWN: | |
156 | - handleDown(); | |
157 | - break; | |
158 | - case KeyCodeEnum.ENTER: | |
159 | - handleEnter(); | |
160 | - break; | |
161 | - case KeyCodeEnum.ESC: | |
162 | - handleClose(); | |
163 | - break; | |
164 | - } | |
165 | - }); | |
147 | + onKeyStroke('Enter', handleEnter); | |
148 | + onKeyStroke('ArrowUp', handleUp); | |
149 | + onKeyStroke('ArrowDown', handleDown); | |
150 | + onKeyStroke('Escape', handleClose); | |
166 | 151 | |
167 | 152 | return { handleSearch, searchResult, keyword, activeIndex, handleMouseenter, handleEnter }; |
168 | 153 | } | ... | ... |
src/components/Icon/src/IconPicker.vue
... | ... | @@ -76,7 +76,7 @@ |
76 | 76 | import iconsData from '../data/icons.data'; |
77 | 77 | import { propTypes } from '/@/utils/propTypes'; |
78 | 78 | import { usePagination } from '/@/hooks/web/usePagination'; |
79 | - import { useDebounce } from '/@/hooks/core/useDebounce'; | |
79 | + import { useDebounceFn } from '@vueuse/core'; | |
80 | 80 | import { useI18n } from '/@/hooks/web/useI18n'; |
81 | 81 | import { useCopyToClipboard } from '/@/hooks/web/useCopyToClipboard'; |
82 | 82 | import { useMessage } from '/@/hooks/web/useMessage'; |
... | ... | @@ -123,7 +123,7 @@ |
123 | 123 | const { t } = useI18n(); |
124 | 124 | const { prefixCls } = useDesign('icon-picker'); |
125 | 125 | |
126 | - const [debounceHandleSearchChange] = useDebounce(handleSearchChange, 100); | |
126 | + const debounceHandleSearchChange = useDebounceFn(handleSearchChange, 100); | |
127 | 127 | const { clipboardRef, isSuccessRef } = useCopyToClipboard(props.value); |
128 | 128 | const { createMessage } = useMessage(); |
129 | 129 | ... | ... |
src/components/Modal/src/components/ModalWrapper.vue
... | ... | @@ -24,7 +24,6 @@ |
24 | 24 | import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn'; |
25 | 25 | import { ScrollContainer } from '/@/components/Container'; |
26 | 26 | |
27 | - // import { useElResize } from '/@/hooks/event/useElResize'; | |
28 | 27 | import { propTypes } from '/@/utils/propTypes'; |
29 | 28 | import { createModalContext } from '../hooks/useModalContext'; |
30 | 29 | ... | ... |
src/components/SimpleMenu/src/useOpenKeys.ts
... | ... | @@ -8,7 +8,7 @@ import { uniq } from 'lodash-es'; |
8 | 8 | import { getAllParentPath } from '/@/router/helper/menuHelper'; |
9 | 9 | |
10 | 10 | import { useTimeoutFn } from '/@/hooks/core/useTimeout'; |
11 | -import { useDebounce } from '/@/hooks/core/useDebounce'; | |
11 | +import { useDebounceFn } from '@vueuse/core'; | |
12 | 12 | |
13 | 13 | export function useOpenKeys( |
14 | 14 | menuState: MenuState, |
... | ... | @@ -17,7 +17,7 @@ export function useOpenKeys( |
17 | 17 | mixSider: Ref<boolean>, |
18 | 18 | collapse: Ref<boolean> |
19 | 19 | ) { |
20 | - const [debounceSetOpenKeys] = useDebounce(setOpenKeys, 50); | |
20 | + const debounceSetOpenKeys = useDebounceFn(setOpenKeys, 50); | |
21 | 21 | async function setOpenKeys(path: string) { |
22 | 22 | const native = !mixSider.value; |
23 | 23 | const menuList = toRaw(menus.value); | ... | ... |
src/components/Table/src/hooks/useTableScroll.ts
1 | -import type { BasicTableProps, TableRowSelection } from '../types/table'; | |
1 | +import type { BasicTableProps, TableRowSelection, BasicColumn } from '../types/table'; | |
2 | 2 | import type { Ref, ComputedRef } from 'vue'; |
3 | + | |
3 | 4 | import { computed, unref, ref, nextTick, watch } from 'vue'; |
4 | 5 | |
5 | 6 | import { getViewportOffset } from '/@/utils/domUtils'; |
... | ... | @@ -7,9 +8,8 @@ import { isBoolean } from '/@/utils/is'; |
7 | 8 | |
8 | 9 | import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn'; |
9 | 10 | import { useModalContext } from '/@/components/Modal'; |
10 | -import { useDebounce } from '/@/hooks/core/useDebounce'; | |
11 | -import type { BasicColumn } from '/@/components/Table'; | |
12 | 11 | import { onMountedOrActivated } from '/@/hooks/core/onMountedOrActivated'; |
12 | +import { useDebounceFn } from '@vueuse/core'; | |
13 | 13 | |
14 | 14 | export function useTableScroll( |
15 | 15 | propsRef: ComputedRef<BasicTableProps>, |
... | ... | @@ -23,7 +23,7 @@ export function useTableScroll( |
23 | 23 | const modalFn = useModalContext(); |
24 | 24 | |
25 | 25 | // Greater than animation time 280 |
26 | - const [debounceRedoHeight] = useDebounce(redoHeight, 100); | |
26 | + const debounceRedoHeight = useDebounceFn(redoHeight, 100); | |
27 | 27 | |
28 | 28 | const getCanResize = computed(() => { |
29 | 29 | const { canResize, scroll } = unref(propsRef); | ... | ... |
src/components/Tree/src/TreeHeader.vue
... | ... | @@ -41,7 +41,7 @@ |
41 | 41 | import { propTypes } from '/@/utils/propTypes'; |
42 | 42 | |
43 | 43 | import { useI18n } from '/@/hooks/web/useI18n'; |
44 | - import { useDebounce } from '/@/hooks/core/useDebounce'; | |
44 | + import { useDebounceFn } from '@vueuse/core'; | |
45 | 45 | |
46 | 46 | import { ToolbarEnum } from './enum'; |
47 | 47 | |
... | ... | @@ -128,7 +128,7 @@ |
128 | 128 | function emitChange(value?: string): void { |
129 | 129 | emit('search', value); |
130 | 130 | } |
131 | - const [debounceEmitChange] = useDebounce(emitChange, 200); | |
131 | + const debounceEmitChange = useDebounceFn(emitChange, 200); | |
132 | 132 | |
133 | 133 | function handleSearch(e: ChangeEvent): void { |
134 | 134 | debounceEmitChange(e.target.value); | ... | ... |
src/enums/keyCodeEnum.ts deleted
100644 → 0
src/hooks/core/useContext.ts
... | ... | @@ -29,19 +29,7 @@ export function createContext<T>( |
29 | 29 | const provideData = readonly ? defineReadonly(state) : state; |
30 | 30 | !createProvider && provide(key, native ? context : provideData); |
31 | 31 | |
32 | - // const Provider = createProvider | |
33 | - // ? defineComponent({ | |
34 | - // name: 'Provider', | |
35 | - // inheritAttrs: false, | |
36 | - // setup(_, { slots }) { | |
37 | - // provide(key, provideData); | |
38 | - // return () => slots.default?.(); | |
39 | - // }, | |
40 | - // }) | |
41 | - // : null; | |
42 | - | |
43 | 32 | return { |
44 | - // Provider, | |
45 | 33 | state, |
46 | 34 | }; |
47 | 35 | } | ... | ... |
src/hooks/core/useDebounce.ts deleted
100644 → 0
1 | -export interface DebounceAndThrottleOptions { | |
2 | - // 立即执行 | |
3 | - immediate?: boolean; | |
4 | - | |
5 | - // 是否为debounce | |
6 | - debounce?: boolean; | |
7 | - // 只执行一次 | |
8 | - once?: boolean; | |
9 | -} | |
10 | -export type CancelFn = () => void; | |
11 | - | |
12 | -export type DebounceAndThrottleProcedure<T extends unknown[]> = (...args: T) => unknown; | |
13 | - | |
14 | -export type DebounceAndThrottleProcedureResult<T extends unknown[]> = [ | |
15 | - DebounceAndThrottleProcedure<T>, | |
16 | - CancelFn | |
17 | -]; | |
18 | - | |
19 | -import { | |
20 | - // throttle, | |
21 | - useThrottle, | |
22 | -} from './useThrottle'; | |
23 | - | |
24 | -/** | |
25 | - * @description: Applicable in components | |
26 | - */ | |
27 | -export function useDebounce<T extends unknown[]>( | |
28 | - handle: DebounceAndThrottleProcedure<T>, | |
29 | - wait: number, | |
30 | - options: DebounceAndThrottleOptions = {} | |
31 | -): DebounceAndThrottleProcedureResult<T> { | |
32 | - return useThrottle( | |
33 | - handle, | |
34 | - wait, | |
35 | - Object.assign(options, { | |
36 | - debounce: true, | |
37 | - }) | |
38 | - ); | |
39 | -} |
src/hooks/core/useThrottle.ts deleted
100644 → 0
1 | -export interface DebounceAndThrottleOptions { | |
2 | - // 立即执行 | |
3 | - immediate?: boolean; | |
4 | - | |
5 | - // 是否为debounce | |
6 | - debounce?: boolean; | |
7 | - // 只执行一次 | |
8 | - once?: boolean; | |
9 | -} | |
10 | -export type CancelFn = () => void; | |
11 | - | |
12 | -export type DebounceAndThrottleProcedure<T extends unknown[]> = (...args: T) => unknown; | |
13 | - | |
14 | -export type DebounceAndThrottleProcedureResult<T extends unknown[]> = [ | |
15 | - DebounceAndThrottleProcedure<T>, | |
16 | - CancelFn | |
17 | -]; | |
18 | - | |
19 | -import { isFunction } from '/@/utils/is'; | |
20 | -export function throttle<T extends unknown[]>( | |
21 | - handle: DebounceAndThrottleProcedure<T>, | |
22 | - wait: number, | |
23 | - options: DebounceAndThrottleOptions = {} | |
24 | -): DebounceAndThrottleProcedureResult<T> { | |
25 | - if (!isFunction(handle)) { | |
26 | - throw new Error('handle is not Function!'); | |
27 | - } | |
28 | - let { immediate = false } = options; | |
29 | - const { once = false, debounce = false } = options; | |
30 | - let timeoutId: Nullable<TimeoutHandle>; | |
31 | - // Has it been cancelled | |
32 | - let cancelled: boolean | null = false; | |
33 | - /** | |
34 | - * @description: clear timer | |
35 | - */ | |
36 | - function clearTimer() { | |
37 | - if (timeoutId) { | |
38 | - window.clearTimeout(timeoutId); | |
39 | - timeoutId = null; | |
40 | - } | |
41 | - } | |
42 | - /** cancel exec */ | |
43 | - function cancel() { | |
44 | - clearTimer(); | |
45 | - cancelled = true; | |
46 | - } | |
47 | - // If once is true, only execute once | |
48 | - function cancelExec(): void { | |
49 | - once && cancel(); | |
50 | - } | |
51 | - function fn(this: unknown, ...args: T) { | |
52 | - // If it has been cancelled, it will not be executed | |
53 | - if (cancelled) { | |
54 | - return; | |
55 | - } | |
56 | - const exec = () => { | |
57 | - !debounce && clearTimer(); | |
58 | - handle.apply(this, args); | |
59 | - cancelExec(); | |
60 | - }; | |
61 | - if (immediate) { | |
62 | - immediate = false; | |
63 | - const callNow = !timeoutId; | |
64 | - if (callNow) { | |
65 | - exec(); | |
66 | - timeoutId = null; | |
67 | - } | |
68 | - } else { | |
69 | - debounce && clearTimer(); | |
70 | - if (!timeoutId || debounce) { | |
71 | - timeoutId = setTimeout(exec, wait); | |
72 | - } | |
73 | - } | |
74 | - } | |
75 | - return [fn, cancel]; | |
76 | -} | |
77 | - | |
78 | -export function useThrottle<T extends unknown[]>( | |
79 | - handle: DebounceAndThrottleProcedure<T>, | |
80 | - wait: number, | |
81 | - options: DebounceAndThrottleOptions = {} | |
82 | -): DebounceAndThrottleProcedureResult<T> { | |
83 | - return throttle(handle, wait, options); | |
84 | -} |
src/hooks/event/useElResize.ts deleted
100644 → 0
1 | -import { useDebounce } from '/@/hooks/core/useDebounce'; | |
2 | -import { addResizeListener, removeResizeListener } from '/@/utils/event'; | |
3 | - | |
4 | -interface WindowSizeOptions { | |
5 | - once?: boolean; | |
6 | - immediate?: boolean; | |
7 | -} | |
8 | - | |
9 | -export function useElResize<T>( | |
10 | - el: Element | typeof window, | |
11 | - fn: Fn<T>, | |
12 | - wait = 100, | |
13 | - options?: WindowSizeOptions | |
14 | -) { | |
15 | - let handler = () => { | |
16 | - fn(); | |
17 | - }; | |
18 | - const [handleSize, cancel] = useDebounce(handler, wait, options); | |
19 | - handler = wait ? handleSize : handler; | |
20 | - | |
21 | - function start() { | |
22 | - addResizeListener(el, handler); | |
23 | - } | |
24 | - function stop() { | |
25 | - removeResizeListener(el, handler); | |
26 | - cancel(); | |
27 | - } | |
28 | - | |
29 | - return [start, stop]; | |
30 | -} |
src/hooks/event/useEventListener.ts
1 | 1 | import type { Ref } from 'vue'; |
2 | 2 | |
3 | 3 | import { ref, watch, unref } from 'vue'; |
4 | -import { useDebounce } from '/@/hooks/core/useDebounce'; | |
5 | -import { useThrottle } from '/@/hooks/core/useThrottle'; | |
4 | +import { useThrottleFn, useDebounceFn } from '@vueuse/core'; | |
6 | 5 | |
7 | 6 | export type RemoveEventFn = () => void; |
8 | 7 | |
... | ... | @@ -31,7 +30,7 @@ export function useEventListener({ |
31 | 30 | if (el) { |
32 | 31 | const element: Ref<Element> = ref(el as Element); |
33 | 32 | |
34 | - const [handler] = isDebounce ? useDebounce(listener, wait) : useThrottle(listener, wait); | |
33 | + const handler = isDebounce ? useDebounceFn(listener, wait) : useThrottleFn(listener, wait); | |
35 | 34 | const realHandler = wait ? handler : listener; |
36 | 35 | const removeEventListener = (e: Element) => { |
37 | 36 | isAddRef.value = true; | ... | ... |
src/hooks/event/useKeyPress.ts deleted
100644 → 0
1 | -// https://ahooks.js.org/zh-CN/hooks/dom/use-key-press | |
2 | - | |
3 | -import type { Ref } from 'vue'; | |
4 | -import { onBeforeUnmount, onMounted, unref } from 'vue'; | |
5 | -import { noop } from '/@/utils'; | |
6 | -import { isFunction, isString, isNumber, isArray } from '/@/utils/is'; | |
7 | - | |
8 | -export type KeyPredicate = (event: KeyboardEvent) => boolean; | |
9 | -export type keyType = KeyboardEvent['keyCode'] | KeyboardEvent['key']; | |
10 | -export type KeyFilter = keyType | keyType[] | ((event: KeyboardEvent) => boolean); | |
11 | -export type EventHandler = (event: KeyboardEvent) => void; | |
12 | - | |
13 | -export type keyEvent = 'keydown' | 'keyup'; | |
14 | - | |
15 | -export type TargetElement = HTMLElement | Element | Document | Window; | |
16 | -export type Target = Ref<TargetElement>; | |
17 | - | |
18 | -export type EventOption = { | |
19 | - events?: keyEvent[]; | |
20 | - target?: Target; | |
21 | -}; | |
22 | - | |
23 | -const defaultEvents: keyEvent[] = ['keydown']; | |
24 | - | |
25 | -// 键盘事件 keyCode 别名 | |
26 | -const aliasKeyCodeMap: Recordable<number | number[]> = { | |
27 | - esc: 27, | |
28 | - tab: 9, | |
29 | - enter: 13, | |
30 | - space: 32, | |
31 | - up: 38, | |
32 | - left: 37, | |
33 | - right: 39, | |
34 | - down: 40, | |
35 | - delete: [8, 46], | |
36 | -}; | |
37 | - | |
38 | -// 键盘事件 key 别名 | |
39 | -const aliasKeyMap: Recordable<string | string[]> = { | |
40 | - esc: 'Escape', | |
41 | - tab: 'Tab', | |
42 | - enter: 'Enter', | |
43 | - space: ' ', | |
44 | - // IE11 uses key names without `Arrow` prefix for arrow keys. | |
45 | - up: ['Up', 'ArrowUp'], | |
46 | - left: ['Left', 'ArrowLeft'], | |
47 | - right: ['Right', 'ArrowRight'], | |
48 | - down: ['Down', 'ArrowDown'], | |
49 | - delete: ['Backspace', 'Delete'], | |
50 | -}; | |
51 | - | |
52 | -// 修饰键 | |
53 | -const modifierKey: Recordable<(event: KeyboardEvent) => boolean> = { | |
54 | - ctrl: (event: KeyboardEvent) => event.ctrlKey, | |
55 | - shift: (event: KeyboardEvent) => event.shiftKey, | |
56 | - alt: (event: KeyboardEvent) => event.altKey, | |
57 | - meta: (event: KeyboardEvent) => event.metaKey, | |
58 | -}; | |
59 | - | |
60 | -/** | |
61 | - * 判断按键是否激活 | |
62 | - * @param [event: KeyboardEvent]键盘事件 | |
63 | - * @param [keyFilter: any] 当前键 | |
64 | - * @returns Boolean | |
65 | - */ | |
66 | -function genFilterKey(event: any, keyFilter: any) { | |
67 | - // 浏览器自动补全 input 的时候,会触发 keyDown、keyUp 事件,但此时 event.key 等为空 | |
68 | - if (!event.key) { | |
69 | - return false; | |
70 | - } | |
71 | - | |
72 | - // 数字类型直接匹配事件的 keyCode | |
73 | - if (isNumber(keyFilter)) { | |
74 | - return event.keyCode === keyFilter; | |
75 | - } | |
76 | - // 字符串依次判断是否有组合键 | |
77 | - const genArr = keyFilter.split('.'); | |
78 | - let genLen = 0; | |
79 | - for (const key of genArr) { | |
80 | - // 组合键 | |
81 | - const genModifier = modifierKey[key]; | |
82 | - // key 别名 | |
83 | - const aliasKey = aliasKeyMap[key]; | |
84 | - // keyCode 别名 | |
85 | - const aliasKeyCode = aliasKeyCodeMap[key]; | |
86 | - /** | |
87 | - * 满足以上规则 | |
88 | - * 1. 自定义组合键别名 | |
89 | - * 2. 自定义 key 别名 | |
90 | - * 3. 自定义 keyCode 别名 | |
91 | - * 4. 匹配 key 或 keyCode | |
92 | - */ | |
93 | - if ( | |
94 | - (genModifier && genModifier(event)) || | |
95 | - (aliasKey && isArray(aliasKey) ? aliasKey.includes(event.key) : aliasKey === event.key) || | |
96 | - (aliasKeyCode && isArray(aliasKeyCode) | |
97 | - ? aliasKeyCode.includes(event.keyCode) | |
98 | - : aliasKeyCode === event.keyCode) || | |
99 | - event.key.toUpperCase() === key.toUpperCase() | |
100 | - ) { | |
101 | - genLen++; | |
102 | - } | |
103 | - } | |
104 | - return genLen === genArr.length; | |
105 | -} | |
106 | - | |
107 | -/** | |
108 | - * 键盘输入预处理方法 | |
109 | - */ | |
110 | -function genKeyFormat(keyFilter: any): KeyPredicate { | |
111 | - if (isFunction(keyFilter)) { | |
112 | - return keyFilter; | |
113 | - } | |
114 | - if (isString(keyFilter) || isNumber(keyFilter)) { | |
115 | - return (event: KeyboardEvent) => genFilterKey(event, keyFilter); | |
116 | - } | |
117 | - if (isArray(keyFilter)) { | |
118 | - return (event: KeyboardEvent) => keyFilter.some((item: any) => genFilterKey(event, item)); | |
119 | - } | |
120 | - return keyFilter ? () => true : () => false; | |
121 | -} | |
122 | - | |
123 | -export function useKeyPress( | |
124 | - keyFilter: KeyFilter, | |
125 | - eventHandler: EventHandler = noop, | |
126 | - option: EventOption = {} | |
127 | -) { | |
128 | - const { events = defaultEvents, target } = option; | |
129 | - | |
130 | - let el: TargetElement | null | undefined; | |
131 | - | |
132 | - function handler(event: any) { | |
133 | - const genGuard: KeyPredicate = genKeyFormat(keyFilter); | |
134 | - if (genGuard(event)) { | |
135 | - return eventHandler(event); | |
136 | - } | |
137 | - } | |
138 | - | |
139 | - onMounted(() => { | |
140 | - el = getTargetElement(target, window); | |
141 | - if (!el) return; | |
142 | - | |
143 | - for (const eventName of events) { | |
144 | - el.addEventListener(eventName, handler); | |
145 | - } | |
146 | - }); | |
147 | - | |
148 | - onBeforeUnmount(() => { | |
149 | - if (!el) return; | |
150 | - for (const eventName of events) { | |
151 | - el.removeEventListener(eventName, handler); | |
152 | - } | |
153 | - }); | |
154 | -} | |
155 | - | |
156 | -export function getTargetElement( | |
157 | - target?: Target, | |
158 | - defaultElement?: TargetElement | |
159 | -): TargetElement | undefined | null { | |
160 | - if (!target) { | |
161 | - return defaultElement; | |
162 | - } | |
163 | - return isFunction(target) ? target() : unref(target); | |
164 | -} |
src/hooks/event/useScroll.ts
1 | 1 | import type { Ref } from 'vue'; |
2 | 2 | |
3 | 3 | import { ref, onMounted, watch, onUnmounted } from 'vue'; |
4 | -import { useThrottle } from '/@/hooks/core/useThrottle'; | |
5 | 4 | import { isWindow, isObject } from '/@/utils/is'; |
5 | +import { useThrottleFn } from '@vueuse/core'; | |
6 | 6 | |
7 | 7 | export function useScroll( |
8 | 8 | refEl: Ref<Element | Window | null>, |
... | ... | @@ -31,8 +31,7 @@ export function useScroll( |
31 | 31 | Reflect.deleteProperty(options, 'wait'); |
32 | 32 | } |
33 | 33 | |
34 | - const [throttleHandle] = useThrottle(handler, wait); | |
35 | - handler = throttleHandle; | |
34 | + handler = useThrottleFn(handler, wait); | |
36 | 35 | } |
37 | 36 | |
38 | 37 | let stopWatch: () => void; | ... | ... |
src/hooks/event/useWindowSizeFn.ts
1 | 1 | import { tryOnMounted, tryOnUnmounted } from '@vueuse/core'; |
2 | -import { useDebounce } from '/@/hooks/core/useDebounce'; | |
2 | +import { useDebounceFn } from '@vueuse/core'; | |
3 | 3 | |
4 | 4 | interface WindowSizeOptions { |
5 | 5 | once?: boolean; |
... | ... | @@ -11,7 +11,7 @@ export function useWindowSizeFn<T>(fn: Fn<T>, wait = 150, options?: WindowSizeOp |
11 | 11 | let handler = () => { |
12 | 12 | fn(); |
13 | 13 | }; |
14 | - const [handleSize, cancel] = useDebounce(handler, wait, options); | |
14 | + const handleSize = useDebounceFn(handler, wait); | |
15 | 15 | handler = handleSize; |
16 | 16 | |
17 | 17 | const start = () => { |
... | ... | @@ -23,7 +23,6 @@ export function useWindowSizeFn<T>(fn: Fn<T>, wait = 150, options?: WindowSizeOp |
23 | 23 | |
24 | 24 | const stop = () => { |
25 | 25 | window.removeEventListener('resize', handler); |
26 | - cancel(); | |
27 | 26 | }; |
28 | 27 | |
29 | 28 | tryOnMounted(() => { | ... | ... |
src/hooks/web/useEcharts/index.ts
... | ... | @@ -4,7 +4,7 @@ import type { Ref } from 'vue'; |
4 | 4 | import { useTimeoutFn } from '/@/hooks/core/useTimeout'; |
5 | 5 | import { tryOnUnmounted } from '@vueuse/core'; |
6 | 6 | import { unref, nextTick, watch, computed, ref } from 'vue'; |
7 | -import { useDebounce } from '/@/hooks/core/useDebounce'; | |
7 | +import { useDebounceFn } from '@vueuse/core'; | |
8 | 8 | import { useEventListener } from '/@/hooks/event/useEventListener'; |
9 | 9 | import { useBreakpoint } from '/@/hooks/event/useBreakpoint'; |
10 | 10 | |
... | ... | @@ -21,8 +21,7 @@ export function useECharts( |
21 | 21 | const cacheOptions = ref<EChartsOption>({}); |
22 | 22 | let removeResizeFn: Fn = () => {}; |
23 | 23 | |
24 | - const [debounceResize] = useDebounce(resize, 200); | |
25 | - resizeFn = debounceResize; | |
24 | + resizeFn = useDebounceFn(resize, 200); | |
26 | 25 | |
27 | 26 | const getOptions = computed( |
28 | 27 | (): EChartsOption => { | ... | ... |
src/hooks/web/useFullContent.ts
... | ... | @@ -2,13 +2,14 @@ import { computed, unref } from 'vue'; |
2 | 2 | |
3 | 3 | import { useAppStore } from '/@/store/modules/app'; |
4 | 4 | |
5 | -import router from '/@/router'; | |
5 | +import { useRouter } from 'vue-router'; | |
6 | 6 | |
7 | 7 | /** |
8 | 8 | * @description: Full screen display content |
9 | 9 | */ |
10 | 10 | export const useFullContent = () => { |
11 | 11 | const appStore = useAppStore(); |
12 | + const router = useRouter(); | |
12 | 13 | const { currentRoute } = router; |
13 | 14 | |
14 | 15 | // Whether to display the content in full screen without displaying the menu | ... | ... |
src/hooks/web/useLockPage.ts
1 | 1 | import { computed, onUnmounted, unref, watchEffect } from 'vue'; |
2 | -import { useThrottle } from '/@/hooks/core/useThrottle'; | |
2 | +import { useThrottleFn } from '@vueuse/core'; | |
3 | 3 | |
4 | 4 | import { useAppStore } from '/@/store/modules/app'; |
5 | 5 | import { useLockStore } from '/@/store/modules/lock'; |
... | ... | @@ -59,7 +59,7 @@ export function useLockPage() { |
59 | 59 | clear(); |
60 | 60 | }); |
61 | 61 | |
62 | - const [keyupFn] = useThrottle(resetCalcLockTimeout, 2000); | |
62 | + const keyupFn = useThrottleFn(resetCalcLockTimeout, 2000); | |
63 | 63 | |
64 | 64 | return computed(() => { |
65 | 65 | if (unref(getLockTime)) { | ... | ... |
src/hooks/web/usePermission.ts
src/layouts/default/menu/useLayoutMenu.ts
... | ... | @@ -5,7 +5,7 @@ import { watch, unref, ref, computed } from 'vue'; |
5 | 5 | import { useRouter } from 'vue-router'; |
6 | 6 | |
7 | 7 | import { MenuSplitTyeEnum } from '/@/enums/menuEnum'; |
8 | -import { useThrottle } from '/@/hooks/core/useThrottle'; | |
8 | +import { useThrottleFn } from '@vueuse/core'; | |
9 | 9 | import { useMenuSetting } from '/@/hooks/setting/useMenuSetting'; |
10 | 10 | |
11 | 11 | import { getChildrenMenus, getCurrentParentPath, getMenus, getShallowMenus } from '/@/router/menus'; |
... | ... | @@ -20,7 +20,7 @@ export function useSplitMenu(splitType: Ref<MenuSplitTyeEnum>) { |
20 | 20 | const permissionStore = usePermissionStore(); |
21 | 21 | const { setMenuSetting, getIsHorizontal, getSplit } = useMenuSetting(); |
22 | 22 | |
23 | - const [throttleHandleSplitLeftMenu] = useThrottle(handleSplitLeftMenu, 50); | |
23 | + const throttleHandleSplitLeftMenu = useThrottleFn(handleSplitLeftMenu, 50); | |
24 | 24 | |
25 | 25 | const splitNotLeft = computed( |
26 | 26 | () => unref(splitType) !== MenuSplitTyeEnum.LEFT && !unref(getIsHorizontal) | ... | ... |
src/layouts/default/sider/useLayoutSider.ts
... | ... | @@ -5,7 +5,7 @@ import { computed, unref, onMounted, nextTick, ref } from 'vue'; |
5 | 5 | import { TriggerEnum } from '/@/enums/menuEnum'; |
6 | 6 | |
7 | 7 | import { useMenuSetting } from '/@/hooks/setting/useMenuSetting'; |
8 | -import { useDebounce } from '/@/hooks/core/useDebounce'; | |
8 | +import { useDebounceFn } from '@vueuse/core'; | |
9 | 9 | |
10 | 10 | /** |
11 | 11 | * Handle related operations of menu events |
... | ... | @@ -64,7 +64,7 @@ export function useDragLine(siderRef: Ref<any>, dragBarRef: Ref<any>, mix = fals |
64 | 64 | |
65 | 65 | onMounted(() => { |
66 | 66 | nextTick(() => { |
67 | - const [exec] = useDebounce(changeWrapWidth, 80); | |
67 | + const exec = useDebounceFn(changeWrapWidth, 80); | |
68 | 68 | exec(); |
69 | 69 | }); |
70 | 70 | }); | ... | ... |
src/views/sys/login/LoginForm.vue
... | ... | @@ -88,8 +88,7 @@ |
88 | 88 | import { useUserStore } from '/@/store/modules/user'; |
89 | 89 | import { LoginStateEnum, useLoginState, useFormRules, useFormValid } from './useLogin'; |
90 | 90 | import { useDesign } from '/@/hooks/web/useDesign'; |
91 | - import { useKeyPress } from '/@/hooks/event/useKeyPress'; | |
92 | - import { KeyCodeEnum } from '/@/enums/keyCodeEnum'; | |
91 | + import { onKeyStroke } from '@vueuse/core'; | |
93 | 92 | |
94 | 93 | export default defineComponent({ |
95 | 94 | name: 'LoginForm', |
... | ... | @@ -129,13 +128,8 @@ |
129 | 128 | }); |
130 | 129 | |
131 | 130 | const { validForm } = useFormValid(formRef); |
132 | - useKeyPress(['enter'], (events) => { | |
133 | - const keyCode = events.keyCode; | |
134 | 131 | |
135 | - if (keyCode === KeyCodeEnum.ENTER) { | |
136 | - handleLogin(); | |
137 | - } | |
138 | - }); | |
132 | + onKeyStroke('Enter', handleLogin); | |
139 | 133 | |
140 | 134 | const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN); |
141 | 135 | ... | ... |