ucc-utils
Version:
134 lines (125 loc) • 4.52 kB
TypeScript
import { DebounceSettings, DebouncedFunc } from 'lodash-es';
import { Ref } from 'vue';
/**
* 拖拽选中效果
*/
declare const useDragSelect: () => {};
/**
* 获取下一个ID
* @author Yuluo
* @link https://github.com/YuluoY
* @date 2024-10-05
* @param {object} opts
* @param {boolean} [opts.isOrderly=true] 是否有序id
* @returns {string}
* @example
* ```ts
* useNextId() // '1'
* useNextId({ isOrderly: false }) // '0x1g4k'
* ```
*/
declare function useNextId(opts?: {
isOrderly?: boolean;
}): string;
interface URootFontSizeOptions {
rootFontSize?: number;
isResize?: boolean;
resizeTimeout?: number;
immediate?: boolean;
debounceOpt?: DebounceSettings;
afterRefreshCallback?: (rootFontSize: number | undefined) => void;
beforeRefreshCallback?: (rootFontSize: number | undefined) => void;
}
interface URootFontSizeReturn {
rootFontSize?: number;
destory: () => void;
refreshRootFontSize: () => void;
refreshRootFontSizeDebounce?: DebouncedFunc<() => void>;
setRootFontSize: (rootFontSize: number) => void;
}
/**
* 处理根字体大小 rem 响应式布局
* @author Yuluo
* @link https://github.com/YuluoY
* @date 2024-08-24
* @param {URootFontSizeOptions} options 配置项
* @param {number} [options.rootFontSize] 根字体大小
* @param {number} [options.resizeTimeout] resize事件防抖时间, 默认300ms
* @param {boolean} [options.isResize] 是否开启resize事件, 默认true
* @param {boolean} [options.immediate] 是否立即执行, 默认false
* @param {DebounceSettings} [options.debounceOpt] lodash防抖配置, 默认{}
* @returns {URootFontSizeReturn}
* @example
* ```js
* const {
* destory, // 销毁resize事件
* rootFontSize, // 根字体大小
* refreshRootFontSize, // 刷新根字体大小函数
* refreshRootFontSizeDebounce, // lodash的防抖函数
* } = useRootFontSize({
* setRootFontSizeCallback: (rootFontSize) => {
* console.log(rootFontSize)
* },
* immediate: true,
* isResize: true,
* resizeTimeout: 300,
* debounceOpt: {
* // lodash防抖配置
* }
* })
* ```
*/
declare function useRootFontSize(options: URootFontSizeOptions): URootFontSizeReturn;
interface EventOptions extends AddEventListenerOptions {
debounce?: number;
throttle?: number;
immediate?: boolean;
}
type TargetType = EventTarget | (() => EventTarget) | Ref<EventTarget>;
type CleanupFn = () => void;
/**
* 使用addEventListener监听事件,仅在vue3中使用
* @author Yuluo
* @link https://github.com/YuluoY
* @date 2025-02-11
* @param target 监听目标(DOM元素或返回DOM元素的函数)
* @param event 事件名称
* @param callback 事件处理函数
* @param options 配置选项(包括防抖、节流等)
* @returns 清理函数,用于手动移除事件监听
* @example
* ```ts
* const cleanup = useEventListener(document.querySelector('#app'), 'click', () => {
* console.log('clicked')
* })
* // 使用ref
* const ref = ref(document.querySelector('#app'))
* const cleanup = useEventListener(ref, 'click', () => {
* console.log('clicked')
* })
*
* // 使用函数
* const cleanup = useEventListener(() => document.querySelector('#app'), 'click', () => {
* console.log('clicked')
* })
*
* * // 在需要时手动清理
* cleanup()
*
* ```
*/
declare function useEventListener(target: TargetType, event: keyof WindowEventMap, callback: EventListener, options?: EventOptions): CleanupFn;
declare const _default: {
useDragSelect: () => {};
useRootFontSize: typeof useRootFontSize;
useNextId: typeof useNextId;
useEventListener: typeof useEventListener;
};
declare const hooks_useDragSelect: typeof useDragSelect;
declare const hooks_useEventListener: typeof useEventListener;
declare const hooks_useNextId: typeof useNextId;
declare const hooks_useRootFontSize: typeof useRootFontSize;
declare namespace hooks {
export { _default as default, hooks_useDragSelect as useDragSelect, hooks_useEventListener as useEventListener, hooks_useNextId as useNextId, hooks_useRootFontSize as useRootFontSize };
}
export { _default as _, useRootFontSize as a, useNextId as b, useEventListener as c, hooks as h, useDragSelect as u };