From 335f30c887056387f7b9eee5eb4ab28540424253 Mon Sep 17 00:00:00 2001 From: vben Date: Fri, 7 Apr 2023 00:12:26 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BC=98=E5=8C=96=20useScrollTo?= =?UTF-8?q?=E3=80=81useWindowSizeFn?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/hooks/src/index.ts | 2 ++ packages/hooks/src/useAttrs.ts | 6 ++--- packages/hooks/src/useRefs.ts | 12 ++++++++-- .../hooks/src}/useScrollTo.ts | 23 ++++++++++--------- .../hooks/src}/useWindowSizeFn.ts | 13 +++++++---- packages/types/src/utils.ts | 4 ++-- .../Application/src/search/AppSearchModal.vue | 2 +- .../Application/src/search/useMenuSearch.ts | 2 +- .../CodeEditor/src/codemirror/CodeMirror.vue | 14 +++++++++-- .../Container/src/ScrollContainer.vue | 3 ++- .../Modal/src/components/ModalWrapper.vue | 13 ++++++----- .../Table/src/hooks/useTableScroll.ts | 5 ++-- src/hooks/core/useContext.ts | 1 - src/hooks/web/useContentHeight.ts | 6 ++--- src/hooks/web/useTitle.ts | 1 - .../default/content/useContentViewHeight.ts | 5 ++-- src/views/sys/iframe/index.vue | 4 ++-- 17 files changed, 69 insertions(+), 47 deletions(-) rename {src/hooks/event => packages/hooks/src}/useScrollTo.ts (66%) rename {src/hooks/event => packages/hooks/src}/useWindowSizeFn.ts (63%) diff --git a/packages/hooks/src/index.ts b/packages/hooks/src/index.ts index e18d6d96..71fb0c54 100644 --- a/packages/hooks/src/index.ts +++ b/packages/hooks/src/index.ts @@ -1,4 +1,6 @@ export * from './onMountedOrActivated'; export * from './useAttrs'; export * from './useRefs'; +export * from './useScrollTo'; +export * from './useWindowSizeFn'; export { useTimeoutFn } from '@vueuse/core'; diff --git a/packages/hooks/src/useAttrs.ts b/packages/hooks/src/useAttrs.ts index c9b14eac..df2118d8 100644 --- a/packages/hooks/src/useAttrs.ts +++ b/packages/hooks/src/useAttrs.ts @@ -1,7 +1,7 @@ import { type Recordable } from '@vben/types'; import { getCurrentInstance, reactive, shallowRef, watchEffect } from 'vue'; -interface Options { +interface UseAttrsOptions { excludeListeners?: boolean; excludeKeys?: string[]; excludeDefaultKeys?: boolean; @@ -14,7 +14,7 @@ function entries(obj: Recordable): [string, T][] { return Object.keys(obj).map((key: string) => [key, obj[key]]); } -function useAttrs(options: Options = {}): Recordable { +function useAttrs(options: UseAttrsOptions = {}): Recordable { const instance = getCurrentInstance(); if (!instance) return {}; @@ -40,4 +40,4 @@ function useAttrs(options: Options = {}): Recordable { return attrs; } -export { useAttrs }; +export { useAttrs, type UseAttrsOptions }; diff --git a/packages/hooks/src/useRefs.ts b/packages/hooks/src/useRefs.ts index 4f3faf28..97f1b4b2 100644 --- a/packages/hooks/src/useRefs.ts +++ b/packages/hooks/src/useRefs.ts @@ -1,7 +1,10 @@ import type { Ref } from 'vue'; import { onBeforeUpdate, shallowRef } from 'vue'; -export function useRefs(): [Ref, (index: number) => (el: HTMLElement) => void] { +function useRefs(): { + refs: Ref; + setRefs: (index: number) => (el: HTMLElement) => void; +} { const refs = shallowRef([]) as Ref; onBeforeUpdate(() => { @@ -12,5 +15,10 @@ export function useRefs(): [Ref, (index: number) => (el: HTMLElem refs.value[index] = el; }; - return [refs, setRefs]; + return { + refs, + setRefs, + }; } + +export { useRefs }; diff --git a/src/hooks/event/useScrollTo.ts b/packages/hooks/src/useScrollTo.ts similarity index 66% rename from src/hooks/event/useScrollTo.ts rename to packages/hooks/src/useScrollTo.ts index f6d5dc61..f6a95f48 100644 --- a/src/hooks/event/useScrollTo.ts +++ b/packages/hooks/src/useScrollTo.ts @@ -1,35 +1,34 @@ -import { isFunction, isUnDef } from '/@/utils/is'; -import { ref, unref } from 'vue'; +import { shallowRef, unref } from 'vue'; -export interface ScrollToParams { +interface UseScrollToOptions { el: any; to: number; duration?: number; callback?: () => any; } -const easeInOutQuad = (t: number, b: number, c: number, d: number) => { +function easeInOutQuad(t: number, b: number, c: number, d: number) { t /= d / 2; if (t < 1) { return (c / 2) * t * t + b; } t--; return (-c / 2) * (t * (t - 2) - 1) + b; -}; -const move = (el: HTMLElement, amount: number) => { +} + +function move(el: HTMLElement, amount: number) { el.scrollTop = amount; -}; +} const position = (el: HTMLElement) => { return el.scrollTop; }; -export function useScrollTo({ el, to, duration = 500, callback }: ScrollToParams) { - const isActiveRef = ref(false); +function useScrollTo({ el, to, duration = 500, callback }: UseScrollToOptions) { + const isActiveRef = shallowRef(false); const start = position(el); const change = to - start; const increment = 20; let currentTime = 0; - duration = isUnDef(duration) ? 500 : duration; const animateScroll = function () { if (!unref(isActiveRef)) { @@ -41,7 +40,7 @@ export function useScrollTo({ el, to, duration = 500, callback }: ScrollToParams if (currentTime < duration && unref(isActiveRef)) { requestAnimationFrame(animateScroll); } else { - if (callback && isFunction(callback)) { + if (callback && typeof callback === 'function') { callback(); } } @@ -57,3 +56,5 @@ export function useScrollTo({ el, to, duration = 500, callback }: ScrollToParams return { start: run, stop }; } + +export { useScrollTo, type UseScrollToOptions }; diff --git a/src/hooks/event/useWindowSizeFn.ts b/packages/hooks/src/useWindowSizeFn.ts similarity index 63% rename from src/hooks/event/useWindowSizeFn.ts rename to packages/hooks/src/useWindowSizeFn.ts index 01cdc752..d8e77105 100644 --- a/src/hooks/event/useWindowSizeFn.ts +++ b/packages/hooks/src/useWindowSizeFn.ts @@ -1,12 +1,15 @@ +import { type AnyFunction } from '@vben/types'; import { tryOnMounted, tryOnUnmounted, useDebounceFn } from '@vueuse/core'; -interface WindowSizeOptions { +interface UseWindowSizeOptions { + wait?: number; once?: boolean; immediate?: boolean; listenerOptions?: AddEventListenerOptions | boolean; } -export function useWindowSizeFn(fn: Fn, wait = 150, options?: WindowSizeOptions) { +function useWindowSizeFn(fn: AnyFunction, options: UseWindowSizeOptions = {}) { + const { wait = 150, immediate } = options; let handler = () => { fn(); }; @@ -14,7 +17,7 @@ export function useWindowSizeFn(fn: Fn, wait = 150, options?: WindowSizeOp handler = handleSize; const start = () => { - if (options && options.immediate) { + if (immediate) { handler(); } window.addEventListener('resize', handler); @@ -31,5 +34,7 @@ export function useWindowSizeFn(fn: Fn, wait = 150, options?: WindowSizeOp tryOnUnmounted(() => { stop(); }); - return [start, stop]; + return { start, stop }; } + +export { useWindowSizeFn, type UseWindowSizeOptions }; diff --git a/packages/types/src/utils.ts b/packages/types/src/utils.ts index 73e51f58..80435fc9 100644 --- a/packages/types/src/utils.ts +++ b/packages/types/src/utils.ts @@ -1,12 +1,12 @@ /** * 任意类型的异步函数 */ -type AnyPromiseFunction = (...arg: any) => PromiseLike; +type AnyPromiseFunction = (...arg: any[]) => PromiseLike; /** * 任意类型的普通函数 */ -type AnyNormalFunction = (...arg: any) => any; +type AnyNormalFunction = (...arg: any[]) => any; /** * 任意类型的函数 diff --git a/src/components/Application/src/search/AppSearchModal.vue b/src/components/Application/src/search/AppSearchModal.vue index a1546f8b..612b372d 100644 --- a/src/components/Application/src/search/AppSearchModal.vue +++ b/src/components/Application/src/search/AppSearchModal.vue @@ -81,7 +81,7 @@ const { t } = useI18n(); const { prefixCls } = useDesign('app-search-modal'); - const [refs, setRefs] = useRefs(); + const { refs, setRefs } = useRefs(); const { getIsMobile } = useAppInject(); const { handleSearch, searchResult, keyword, activeIndex, handleEnter, handleMouseenter } = diff --git a/src/components/Application/src/search/useMenuSearch.ts b/src/components/Application/src/search/useMenuSearch.ts index 1b8c71fb..c9387923 100644 --- a/src/components/Application/src/search/useMenuSearch.ts +++ b/src/components/Application/src/search/useMenuSearch.ts @@ -5,7 +5,7 @@ import { getMenus } from '/@/router/menus'; import { cloneDeep } from 'lodash-es'; import { filter, forEach } from '/@/utils/helper/treeHelper'; import { useGo } from '/@/hooks/web/usePage'; -import { useScrollTo } from '/@/hooks/event/useScrollTo'; +import { useScrollTo } from '@vben/hooks'; import { onKeyStroke, useDebounceFn } from '@vueuse/core'; import { useI18n } from '/@/hooks/web/useI18n'; diff --git a/src/components/CodeEditor/src/codemirror/CodeMirror.vue b/src/components/CodeEditor/src/codemirror/CodeMirror.vue index 426ac36e..af3017f5 100644 --- a/src/components/CodeEditor/src/codemirror/CodeMirror.vue +++ b/src/components/CodeEditor/src/codemirror/CodeMirror.vue @@ -3,10 +3,20 @@