新增前端vue
This commit is contained in:
126
web-vue/packages/core/hooks/component/useFormItem.ts
Normal file
126
web-vue/packages/core/hooks/component/useFormItem.ts
Normal file
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||
* No deletion without permission, or be held responsible to law.
|
||||
* @description 超强兼容表单字段各种数据类型的支持
|
||||
* @author Vben、ThinkGem
|
||||
*/
|
||||
import type { UnwrapRef, Ref } from 'vue';
|
||||
import { reactive, readonly, computed, getCurrentInstance, watchEffect, unref, nextTick, toRaw } from 'vue';
|
||||
|
||||
import { isEqual } from 'lodash-es';
|
||||
import { isEmpty, isNumber, isObject } from '@jeesite/core/utils/is';
|
||||
|
||||
export function useRuleFormItem<T extends Recordable>(
|
||||
props: T,
|
||||
key: keyof T = 'value',
|
||||
changeEvent = 'change',
|
||||
emitData?: Ref<any[]>,
|
||||
) {
|
||||
const instance = getCurrentInstance();
|
||||
const emit = instance?.emit;
|
||||
const compName = instance?.type?.name || 'unknown';
|
||||
const emitsOptions = instance?.['emitsOptions'] || {};
|
||||
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
const hasChangeEmit = hasOwnProperty.call(emitsOptions, changeEvent);
|
||||
const hasUpdateValueEmit = hasOwnProperty.call(emitsOptions, 'update:value');
|
||||
const hasUpdateLabelValueEmit = hasOwnProperty.call(emitsOptions, 'update:labelValue');
|
||||
|
||||
const isMultiple = computed(() => {
|
||||
if (['JeeSiteCheckboxGroup'].includes(compName)) {
|
||||
return true;
|
||||
}
|
||||
if (
|
||||
['JeeSiteSelect', 'JeeSiteTreeSelect'].includes(compName) &&
|
||||
(props.mode === 'multiple' || props.mode === 'tags' || props.treeCheckable === true)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
const isDictType = computed(() => !isEmpty(props.dictType));
|
||||
|
||||
const innerState = reactive({
|
||||
value: props[key],
|
||||
});
|
||||
|
||||
const defaultState = readonly(innerState);
|
||||
|
||||
const setState = (val: UnwrapRef<T[keyof T]>): void => {
|
||||
innerState.value = val as T[keyof T];
|
||||
};
|
||||
|
||||
watchEffect(() => {
|
||||
innerState.value = props[key];
|
||||
});
|
||||
|
||||
const state: any = computed({
|
||||
get() {
|
||||
let value = toRaw(innerState.value) as any;
|
||||
if (!value) return undefined;
|
||||
if (props.labelInValue) {
|
||||
const values: Recordable = [];
|
||||
if (isMultiple.value && !(value instanceof Object) && !(value instanceof Array)) {
|
||||
const vals = (value as string)?.split(',');
|
||||
const labs = (props.labelValue as string)?.split(',');
|
||||
for (const i in vals) {
|
||||
values.push({ value: vals && vals[i], label: labs && labs[i] });
|
||||
}
|
||||
value = values as T[keyof T];
|
||||
} else if (!isObject(value) && !(value instanceof Object) && !(value instanceof Array)) {
|
||||
value = { value: String(value), label: props.labelValue };
|
||||
} else if (value instanceof Array) {
|
||||
for (const i in value) {
|
||||
if (isObject(value[i])) break;
|
||||
values.push({ value: value[i] });
|
||||
}
|
||||
if (values.length > 0) {
|
||||
value = values as T[keyof T];
|
||||
}
|
||||
}
|
||||
} else if (isMultiple.value && !(value instanceof Object) && !(value instanceof Array)) {
|
||||
value = (value as string).split(',');
|
||||
} else if (isDictType.value && isNumber(value)) {
|
||||
value = String(value);
|
||||
}
|
||||
// console.log('innerState', value);
|
||||
innerState.value = value as T[keyof T];
|
||||
return innerState.value;
|
||||
},
|
||||
set(value: any) {
|
||||
if (isEqual(value, defaultState.value)) return;
|
||||
innerState.value = value as T[keyof T];
|
||||
nextTick(() => {
|
||||
const extData = toRaw(unref(emitData)) || [];
|
||||
if (!value) {
|
||||
hasChangeEmit && emit?.(changeEvent, undefined, undefined, ...extData);
|
||||
hasUpdateValueEmit && emit?.('update:value', undefined);
|
||||
hasUpdateLabelValueEmit && emit?.('update:labelValue', undefined);
|
||||
return;
|
||||
}
|
||||
// console.log('values', value);
|
||||
const values = value instanceof Array ? value : [value];
|
||||
if (props.labelInValue) {
|
||||
const vals: Recordable[] = [];
|
||||
const labs: Recordable[] = [];
|
||||
for (const item of values) {
|
||||
vals.push(item.value);
|
||||
labs.push(item.label);
|
||||
}
|
||||
const value = vals.length > 0 ? vals.join(',') : undefined;
|
||||
const labelValue = labs.length > 0 ? labs.join(',') : undefined;
|
||||
hasChangeEmit && emit?.(changeEvent, value, labelValue, ...extData);
|
||||
hasUpdateValueEmit && emit?.('update:value', value);
|
||||
hasUpdateLabelValueEmit && emit?.('update:labelValue', labelValue);
|
||||
} else {
|
||||
const value = values.length > 0 ? values.join(',') : undefined;
|
||||
hasChangeEmit && emit?.(changeEvent, value, undefined, ...extData);
|
||||
hasUpdateValueEmit && emit?.('update:value', value);
|
||||
hasUpdateLabelValueEmit && emit?.('update:labelValue', undefined);
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
return [state, setState, defaultState];
|
||||
}
|
||||
18
web-vue/packages/core/hooks/component/usePageContext.ts
Normal file
18
web-vue/packages/core/hooks/component/usePageContext.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import type { InjectionKey, ComputedRef, Ref } from 'vue';
|
||||
import { createContext, useContext } from '@jeesite/core/hooks/core/useContext';
|
||||
|
||||
export interface PageContextProps {
|
||||
contentHeight: ComputedRef<number>;
|
||||
pageHeight: Ref<number>;
|
||||
setPageHeight: (height: number) => Promise<void>;
|
||||
}
|
||||
|
||||
const key: InjectionKey<PageContextProps> = Symbol();
|
||||
|
||||
export function createPageContext(context: PageContextProps) {
|
||||
return createContext<PageContextProps>(context, key, { native: true });
|
||||
}
|
||||
|
||||
export function usePageContext() {
|
||||
return useContext<PageContextProps>(key);
|
||||
}
|
||||
18
web-vue/packages/core/hooks/core/onMountedOrActivated.ts
Normal file
18
web-vue/packages/core/hooks/core/onMountedOrActivated.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { nextTick, onMounted, onActivated } from 'vue';
|
||||
|
||||
export function onMountedOrActivated(hook: Fn) {
|
||||
let mounted: boolean;
|
||||
|
||||
onMounted(() => {
|
||||
hook();
|
||||
nextTick(() => {
|
||||
mounted = true;
|
||||
});
|
||||
});
|
||||
|
||||
onActivated(() => {
|
||||
if (mounted) {
|
||||
hook();
|
||||
}
|
||||
});
|
||||
}
|
||||
41
web-vue/packages/core/hooks/core/useAttrs.ts
Normal file
41
web-vue/packages/core/hooks/core/useAttrs.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { getCurrentInstance, reactive, shallowRef, watchEffect } from 'vue';
|
||||
import type { Ref } from 'vue';
|
||||
|
||||
interface Params {
|
||||
excludeListeners?: boolean;
|
||||
excludeKeys?: string[];
|
||||
excludeDefaultKeys?: boolean;
|
||||
}
|
||||
|
||||
const DEFAULT_EXCLUDE_KEYS = ['class', 'style'];
|
||||
const LISTENER_PREFIX = /^on[A-Z]/;
|
||||
|
||||
export function entries<T>(obj: Recordable<T>): [string, T][] {
|
||||
return Object.keys(obj).map((key: string) => [key, obj[key]]);
|
||||
}
|
||||
|
||||
export function useAttrs(params: Params = {}): Ref<Recordable> | any {
|
||||
const instance = getCurrentInstance();
|
||||
if (!instance) return {};
|
||||
|
||||
const { excludeListeners = false, excludeKeys = [], excludeDefaultKeys = true } = params;
|
||||
const attrs = shallowRef({});
|
||||
const allExcludeKeys = excludeKeys.concat(excludeDefaultKeys ? DEFAULT_EXCLUDE_KEYS : []);
|
||||
|
||||
// Since attrs are not reactive, make it reactive instead of doing in `onUpdated` hook for better performance
|
||||
instance.attrs = reactive(instance.attrs);
|
||||
|
||||
watchEffect(() => {
|
||||
const res = entries(instance.attrs).reduce((acm, [key, val]) => {
|
||||
if (!allExcludeKeys.includes(key) && !(excludeListeners && LISTENER_PREFIX.test(key))) {
|
||||
acm[key] = val;
|
||||
}
|
||||
|
||||
return acm;
|
||||
}, {} as Recordable);
|
||||
|
||||
attrs.value = res;
|
||||
});
|
||||
|
||||
return attrs;
|
||||
}
|
||||
38
web-vue/packages/core/hooks/core/useContext.ts
Normal file
38
web-vue/packages/core/hooks/core/useContext.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import {
|
||||
InjectionKey,
|
||||
provide,
|
||||
inject,
|
||||
reactive,
|
||||
readonly as defineReadonly,
|
||||
// defineComponent,
|
||||
UnwrapRef,
|
||||
} from 'vue';
|
||||
|
||||
export interface CreateContextOptions {
|
||||
readonly?: boolean;
|
||||
createProvider?: boolean;
|
||||
native?: boolean;
|
||||
}
|
||||
|
||||
type ShallowUnwrap<T> = {
|
||||
[P in keyof T]: UnwrapRef<T[P]>;
|
||||
};
|
||||
|
||||
export function createContext<T>(context: any, key: InjectionKey<T> = Symbol(), options: CreateContextOptions = {}) {
|
||||
const { readonly = true, createProvider = false, native = false } = options;
|
||||
|
||||
const state = reactive(context);
|
||||
const provideData = readonly ? defineReadonly(state) : state;
|
||||
!createProvider && provide(key, native ? context : provideData);
|
||||
|
||||
return {
|
||||
state,
|
||||
};
|
||||
}
|
||||
|
||||
export function useContext<T>(key: InjectionKey<T>, native?: boolean): T;
|
||||
export function useContext<T>(key: InjectionKey<T>, defaultValue?: any, native?: boolean): T;
|
||||
|
||||
export function useContext<T>(key: InjectionKey<T> = Symbol(), defaultValue?: any): ShallowUnwrap<T> {
|
||||
return inject(key, defaultValue || {});
|
||||
}
|
||||
17
web-vue/packages/core/hooks/core/useLockFn.ts
Normal file
17
web-vue/packages/core/hooks/core/useLockFn.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { ref, unref } from 'vue';
|
||||
|
||||
export function useLockFn<P extends any[] = any[], V = any>(fn: (...args: P) => Promise<V>) {
|
||||
const lockRef = ref(false);
|
||||
return async function (...args: P) {
|
||||
if (unref(lockRef)) return;
|
||||
lockRef.value = true;
|
||||
try {
|
||||
const ret = await fn(...args);
|
||||
lockRef.value = false;
|
||||
return ret;
|
||||
} catch (e) {
|
||||
lockRef.value = false;
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
}
|
||||
24
web-vue/packages/core/hooks/core/useRefs.ts
Normal file
24
web-vue/packages/core/hooks/core/useRefs.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import type { ComponentPublicInstance, Ref } from 'vue';
|
||||
import { onBeforeUpdate, shallowRef } from 'vue';
|
||||
|
||||
function useRefs<T = HTMLElement>(): {
|
||||
refs: Ref<T[]>;
|
||||
setRefs: (index: number) => (el: Element | ComponentPublicInstance | null) => void;
|
||||
} {
|
||||
const refs = shallowRef([]) as Ref<T[]>;
|
||||
|
||||
onBeforeUpdate(() => {
|
||||
refs.value = [];
|
||||
});
|
||||
|
||||
const setRefs = (index: number) => (el: Element | ComponentPublicInstance | null) => {
|
||||
refs.value[index] = el as T;
|
||||
};
|
||||
|
||||
return {
|
||||
refs,
|
||||
setRefs,
|
||||
};
|
||||
}
|
||||
|
||||
export { useRefs };
|
||||
45
web-vue/packages/core/hooks/core/useTimeout.ts
Normal file
45
web-vue/packages/core/hooks/core/useTimeout.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { ref, watch } from 'vue';
|
||||
import { tryOnUnmounted } from '@vueuse/core';
|
||||
import { isFunction } from '@jeesite/core/utils/is';
|
||||
|
||||
export function useTimeoutFn(handle: Fn<any>, wait: number, native = false) {
|
||||
if (!isFunction(handle)) {
|
||||
throw new Error('handle is not Function!');
|
||||
}
|
||||
|
||||
const { readyRef, stop, start } = useTimeoutRef(wait);
|
||||
if (native) {
|
||||
handle();
|
||||
} else {
|
||||
watch(
|
||||
readyRef,
|
||||
(maturity) => {
|
||||
maturity && handle();
|
||||
},
|
||||
{ immediate: false },
|
||||
);
|
||||
}
|
||||
return { readyRef, stop, start };
|
||||
}
|
||||
|
||||
export function useTimeoutRef(wait: number) {
|
||||
const readyRef = ref(false);
|
||||
|
||||
let timer: TimeoutHandle;
|
||||
function stop(): void {
|
||||
readyRef.value = false;
|
||||
timer && window.clearTimeout(timer);
|
||||
}
|
||||
function start(): void {
|
||||
stop();
|
||||
timer = setTimeout(() => {
|
||||
readyRef.value = true;
|
||||
}, wait);
|
||||
}
|
||||
|
||||
start();
|
||||
|
||||
tryOnUnmounted(stop);
|
||||
|
||||
return { readyRef, stop, start };
|
||||
}
|
||||
89
web-vue/packages/core/hooks/event/useBreakpoint.ts
Normal file
89
web-vue/packages/core/hooks/event/useBreakpoint.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import { ref, computed, ComputedRef, unref } from 'vue';
|
||||
import { useEventListener } from '@jeesite/core/hooks/event/useEventListener';
|
||||
import { screenMap, sizeEnum, screenEnum } from '@jeesite/core/enums/breakpointEnum';
|
||||
|
||||
let globalScreenRef: ComputedRef<sizeEnum | undefined>;
|
||||
let globalWidthRef: ComputedRef<number>;
|
||||
let globalRealWidthRef: ComputedRef<number>;
|
||||
|
||||
export interface CreateCallbackParams {
|
||||
screen: ComputedRef<sizeEnum | undefined>;
|
||||
width: ComputedRef<number>;
|
||||
realWidth: ComputedRef<number>;
|
||||
screenEnum: typeof screenEnum;
|
||||
screenMap: Map<sizeEnum, number>;
|
||||
sizeEnum: typeof sizeEnum;
|
||||
}
|
||||
|
||||
export function useBreakpoint() {
|
||||
return {
|
||||
screenRef: computed(() => unref(globalScreenRef)),
|
||||
widthRef: globalWidthRef,
|
||||
screenEnum,
|
||||
realWidthRef: globalRealWidthRef,
|
||||
};
|
||||
}
|
||||
|
||||
// Just call it once
|
||||
export function createBreakpointListen(fn?: (opt: CreateCallbackParams) => void) {
|
||||
const screenRef = ref<sizeEnum>(sizeEnum.XL);
|
||||
const realWidthRef = ref(window.innerWidth);
|
||||
|
||||
function getWindowWidth() {
|
||||
const width = document.body.clientWidth;
|
||||
const xs = screenMap.get(sizeEnum.XS)!;
|
||||
const sm = screenMap.get(sizeEnum.SM)!;
|
||||
const md = screenMap.get(sizeEnum.MD)!;
|
||||
const lg = screenMap.get(sizeEnum.LG)!;
|
||||
const xl = screenMap.get(sizeEnum.XL)!;
|
||||
if (width < xs) {
|
||||
screenRef.value = sizeEnum.XS;
|
||||
} else if (width < sm) {
|
||||
screenRef.value = sizeEnum.SM;
|
||||
} else if (width < md) {
|
||||
screenRef.value = sizeEnum.MD;
|
||||
} else if (width < lg) {
|
||||
screenRef.value = sizeEnum.LG;
|
||||
} else if (width < xl) {
|
||||
screenRef.value = sizeEnum.XL;
|
||||
} else {
|
||||
screenRef.value = sizeEnum.XXL;
|
||||
}
|
||||
realWidthRef.value = width;
|
||||
}
|
||||
|
||||
useEventListener({
|
||||
el: window,
|
||||
name: 'resize',
|
||||
|
||||
listener: () => {
|
||||
getWindowWidth();
|
||||
resizeFn();
|
||||
},
|
||||
// wait: 100,
|
||||
});
|
||||
|
||||
getWindowWidth();
|
||||
globalScreenRef = computed(() => unref(screenRef));
|
||||
globalWidthRef = computed((): number => screenMap.get(unref(screenRef)!)!);
|
||||
globalRealWidthRef = computed((): number => unref(realWidthRef));
|
||||
|
||||
function resizeFn() {
|
||||
fn?.({
|
||||
screen: globalScreenRef,
|
||||
width: globalWidthRef,
|
||||
realWidth: globalRealWidthRef,
|
||||
screenEnum,
|
||||
screenMap,
|
||||
sizeEnum,
|
||||
});
|
||||
}
|
||||
|
||||
resizeFn();
|
||||
return {
|
||||
screenRef: globalScreenRef,
|
||||
screenEnum,
|
||||
widthRef: globalWidthRef,
|
||||
realWidthRef: globalRealWidthRef,
|
||||
};
|
||||
}
|
||||
57
web-vue/packages/core/hooks/event/useEventListener.ts
Normal file
57
web-vue/packages/core/hooks/event/useEventListener.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import type { Ref } from 'vue';
|
||||
import { ref, watch, unref } from 'vue';
|
||||
import { useThrottleFn, useDebounceFn } from '@vueuse/core';
|
||||
|
||||
export type RemoveEventFn = () => void;
|
||||
export interface UseEventParams {
|
||||
el?: Element | Ref<Element | undefined> | Window | any;
|
||||
name: string;
|
||||
listener: EventListener;
|
||||
options?: boolean | AddEventListenerOptions;
|
||||
autoRemove?: boolean;
|
||||
isDebounce?: boolean;
|
||||
wait?: number;
|
||||
}
|
||||
export function useEventListener({
|
||||
el = window,
|
||||
name,
|
||||
listener,
|
||||
options,
|
||||
autoRemove = true,
|
||||
isDebounce = true,
|
||||
wait = 80,
|
||||
}: UseEventParams): { removeEvent: RemoveEventFn } {
|
||||
let remove: RemoveEventFn = () => {};
|
||||
const isAddRef = ref(false);
|
||||
|
||||
if (el) {
|
||||
const element = ref(el as Element) as Ref<Element>;
|
||||
|
||||
const handler = isDebounce ? useDebounceFn(listener, wait) : useThrottleFn(listener, wait);
|
||||
const realHandler = wait ? handler : listener;
|
||||
const removeEventListener = (e: Element) => {
|
||||
isAddRef.value = true;
|
||||
e.removeEventListener(name, realHandler, options);
|
||||
};
|
||||
const addEventListener = (e: Element) => e.addEventListener(name, realHandler, options);
|
||||
|
||||
const removeWatch = watch(
|
||||
element,
|
||||
(v, _ov, cleanUp) => {
|
||||
if (v) {
|
||||
!unref(isAddRef) && addEventListener(v);
|
||||
cleanUp(() => {
|
||||
autoRemove && removeEventListener(v);
|
||||
});
|
||||
}
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
remove = () => {
|
||||
removeEventListener(element.value);
|
||||
removeWatch();
|
||||
};
|
||||
}
|
||||
return { removeEvent: remove };
|
||||
}
|
||||
48
web-vue/packages/core/hooks/event/useIntersectionObserver.ts
Normal file
48
web-vue/packages/core/hooks/event/useIntersectionObserver.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { Ref, watchEffect, ref } from 'vue';
|
||||
|
||||
interface IntersectionObserverProps {
|
||||
target: Ref<Element | null | undefined>;
|
||||
root?: Ref<any>;
|
||||
onIntersect: IntersectionObserverCallback;
|
||||
rootMargin?: string;
|
||||
threshold?: number;
|
||||
}
|
||||
|
||||
export function useIntersectionObserver({
|
||||
target,
|
||||
root,
|
||||
onIntersect,
|
||||
rootMargin = '0px',
|
||||
threshold = 0.1,
|
||||
}: IntersectionObserverProps) {
|
||||
let cleanup = () => {};
|
||||
const observer: Ref<Nullable<IntersectionObserver>> = ref(null);
|
||||
const stopEffect = watchEffect(() => {
|
||||
cleanup();
|
||||
|
||||
observer.value = new IntersectionObserver(onIntersect, {
|
||||
root: root ? root.value : null,
|
||||
rootMargin,
|
||||
threshold,
|
||||
});
|
||||
|
||||
const current = target.value;
|
||||
|
||||
current && observer.value.observe(current);
|
||||
|
||||
cleanup = () => {
|
||||
if (observer.value) {
|
||||
observer.value.disconnect();
|
||||
target.value && observer.value.unobserve(target.value);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
observer,
|
||||
stop: () => {
|
||||
cleanup();
|
||||
stopEffect();
|
||||
},
|
||||
};
|
||||
}
|
||||
65
web-vue/packages/core/hooks/event/useScroll.ts
Normal file
65
web-vue/packages/core/hooks/event/useScroll.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import type { Ref } from 'vue';
|
||||
|
||||
import { ref, onMounted, watch, onUnmounted } from 'vue';
|
||||
import { isWindow, isObject } from '@jeesite/core/utils/is';
|
||||
import { useThrottleFn } from '@vueuse/core';
|
||||
|
||||
export function useScroll(
|
||||
refEl: Ref<Element | Window | null>,
|
||||
options?: {
|
||||
wait?: number;
|
||||
leading?: boolean;
|
||||
trailing?: boolean;
|
||||
},
|
||||
) {
|
||||
const refX = ref(0);
|
||||
const refY = ref(0);
|
||||
let handler = () => {
|
||||
if (isWindow(refEl.value)) {
|
||||
refX.value = refEl.value.scrollX;
|
||||
refY.value = refEl.value.scrollY;
|
||||
} else if (refEl.value) {
|
||||
refX.value = (refEl.value as Element).scrollLeft;
|
||||
refY.value = (refEl.value as Element).scrollTop;
|
||||
}
|
||||
};
|
||||
|
||||
if (isObject(options)) {
|
||||
let wait = 0;
|
||||
if (options.wait && options.wait > 0) {
|
||||
wait = options.wait;
|
||||
Reflect.deleteProperty(options, 'wait');
|
||||
}
|
||||
|
||||
handler = useThrottleFn(handler, wait);
|
||||
}
|
||||
|
||||
let stopWatch: () => void;
|
||||
onMounted(() => {
|
||||
stopWatch = watch(
|
||||
refEl,
|
||||
(el, prevEl, onCleanup) => {
|
||||
if (el) {
|
||||
el.addEventListener('scroll', handler);
|
||||
} else if (prevEl) {
|
||||
prevEl.removeEventListener('scroll', handler);
|
||||
}
|
||||
onCleanup(() => {
|
||||
refX.value = refY.value = 0;
|
||||
el && el.removeEventListener('scroll', handler);
|
||||
});
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
refEl.value && refEl.value.removeEventListener('scroll', handler);
|
||||
});
|
||||
|
||||
function stop() {
|
||||
stopWatch && stopWatch();
|
||||
}
|
||||
|
||||
return { refX, refY, stop };
|
||||
}
|
||||
59
web-vue/packages/core/hooks/event/useScrollTo.ts
Normal file
59
web-vue/packages/core/hooks/event/useScrollTo.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { isFunction, isUnDef } from '@jeesite/core/utils/is';
|
||||
import { ref, unref } from 'vue';
|
||||
|
||||
export interface ScrollToParams {
|
||||
el: any;
|
||||
to: number;
|
||||
duration?: number;
|
||||
callback?: () => any;
|
||||
}
|
||||
|
||||
const 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) => {
|
||||
el.scrollTop = amount;
|
||||
};
|
||||
|
||||
const position = (el: HTMLElement) => {
|
||||
return el.scrollTop;
|
||||
};
|
||||
export function useScrollTo({ el, to, duration = 500, callback }: ScrollToParams) {
|
||||
const isActiveRef = ref(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)) {
|
||||
return;
|
||||
}
|
||||
currentTime += increment;
|
||||
const val = easeInOutQuad(currentTime, start, change, duration);
|
||||
move(el, val);
|
||||
if (currentTime < duration && unref(isActiveRef)) {
|
||||
requestAnimationFrame(animateScroll);
|
||||
} else {
|
||||
if (callback && isFunction(callback)) {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
};
|
||||
const run = () => {
|
||||
isActiveRef.value = true;
|
||||
animateScroll();
|
||||
};
|
||||
|
||||
const stop = () => {
|
||||
isActiveRef.value = false;
|
||||
};
|
||||
|
||||
return { start: run, stop };
|
||||
}
|
||||
36
web-vue/packages/core/hooks/event/useWindowSizeFn.ts
Normal file
36
web-vue/packages/core/hooks/event/useWindowSizeFn.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { tryOnMounted, tryOnUnmounted } from '@vueuse/core';
|
||||
import { useDebounceFn } from '@vueuse/core';
|
||||
|
||||
interface WindowSizeOptions {
|
||||
once?: boolean;
|
||||
immediate?: boolean;
|
||||
listenerOptions?: AddEventListenerOptions | boolean;
|
||||
}
|
||||
|
||||
export function useWindowSizeFn(fn: Fn, wait = 150, options?: WindowSizeOptions) {
|
||||
let handler = () => {
|
||||
fn();
|
||||
};
|
||||
const handleSize = useDebounceFn(handler, wait);
|
||||
handler = handleSize;
|
||||
|
||||
const start = () => {
|
||||
if (options && options.immediate) {
|
||||
handler();
|
||||
}
|
||||
window.addEventListener('resize', handler);
|
||||
};
|
||||
|
||||
const stop = () => {
|
||||
window.removeEventListener('resize', handler);
|
||||
};
|
||||
|
||||
tryOnMounted(() => {
|
||||
start();
|
||||
});
|
||||
|
||||
tryOnUnmounted(() => {
|
||||
stop();
|
||||
});
|
||||
return [start, stop];
|
||||
}
|
||||
51
web-vue/packages/core/hooks/setting/index.ts
Normal file
51
web-vue/packages/core/hooks/setting/index.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import type { GlobConfig } from '@jeesite/types/config';
|
||||
|
||||
import { getAppEnvConfig } from '@jeesite/core/utils/env';
|
||||
|
||||
let globCache: Readonly<GlobConfig>;
|
||||
export const useGlobSetting = (): Readonly<GlobConfig> => {
|
||||
if (globCache) return globCache;
|
||||
|
||||
const {
|
||||
VITE_GLOB_APP_TITLE,
|
||||
VITE_GLOB_API_URL,
|
||||
VITE_GLOB_APP_SHORT_NAME,
|
||||
VITE_GLOB_API_URL_PREFIX,
|
||||
// VITE_GLOB_UPLOAD_URL,
|
||||
VITE_GLOB_ADMIN_PATH,
|
||||
VITE_FILE_PREVIEW,
|
||||
} = getAppEnvConfig();
|
||||
|
||||
const ctxPath = ((): string => {
|
||||
let ctx = VITE_GLOB_API_URL + VITE_GLOB_API_URL_PREFIX;
|
||||
let idx = ctx.indexOf('://');
|
||||
if (idx != -1) {
|
||||
ctx = ctx.substring(idx + 3);
|
||||
}
|
||||
idx = ctx.indexOf('/');
|
||||
if (idx != -1) {
|
||||
ctx = ctx.substring(idx);
|
||||
} else {
|
||||
ctx = '';
|
||||
}
|
||||
return ctx;
|
||||
})();
|
||||
|
||||
const adminPath = VITE_GLOB_ADMIN_PATH as string;
|
||||
const ctxAdminPath = ctxPath + adminPath;
|
||||
|
||||
// Take global configuration
|
||||
const glob: Readonly<GlobConfig> = {
|
||||
title: VITE_GLOB_APP_TITLE,
|
||||
apiUrl: VITE_GLOB_API_URL,
|
||||
shortName: VITE_GLOB_APP_SHORT_NAME,
|
||||
urlPrefix: VITE_GLOB_API_URL_PREFIX,
|
||||
// uploadUrl: VITE_GLOB_UPLOAD_URL,
|
||||
ctxPath: ctxPath,
|
||||
adminPath: adminPath,
|
||||
ctxAdminPath: ctxAdminPath,
|
||||
filePreview: VITE_FILE_PREVIEW || 'true',
|
||||
};
|
||||
globCache = glob;
|
||||
return glob as Readonly<GlobConfig>;
|
||||
};
|
||||
93
web-vue/packages/core/hooks/setting/useHeaderSetting.ts
Normal file
93
web-vue/packages/core/hooks/setting/useHeaderSetting.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import type { HeaderSetting } from '@jeesite/types/config';
|
||||
|
||||
import { computed, unref } from 'vue';
|
||||
|
||||
import { useAppStore } from '@jeesite/core/store/modules/app';
|
||||
|
||||
import { useMenuSetting } from '@jeesite/core/hooks/setting/useMenuSetting';
|
||||
import { useRootSetting } from '@jeesite/core/hooks/setting/useRootSetting';
|
||||
import { useFullContent } from '@jeesite/core/hooks/web/useFullContent';
|
||||
import { MenuModeEnum } from '@jeesite/core/enums/menuEnum';
|
||||
|
||||
export function useHeaderSetting() {
|
||||
const { getFullContent } = useFullContent();
|
||||
const appStore = useAppStore();
|
||||
|
||||
const getShowFullHeaderRef = computed(() => {
|
||||
return (
|
||||
!unref(getFullContent) &&
|
||||
unref(getShowMixHeaderRef) &&
|
||||
unref(getShowHeader) &&
|
||||
!unref(getIsTopMenu) &&
|
||||
!unref(getIsMixSidebar)
|
||||
);
|
||||
});
|
||||
|
||||
const getUnFixedAndFull = computed(() => !unref(getFixed) && !unref(getShowFullHeaderRef));
|
||||
|
||||
const getShowInsetHeaderRef = computed(() => {
|
||||
const need = !unref(getFullContent) && unref(getShowHeader);
|
||||
return (need && !unref(getShowMixHeaderRef)) || (need && unref(getIsTopMenu)) || (need && unref(getIsMixSidebar));
|
||||
});
|
||||
|
||||
const { getMenuMode, getSplit, getShowHeaderTrigger, getIsSidebarType, getIsMixSidebar, getIsTopMenu } =
|
||||
useMenuSetting();
|
||||
const { getShowBreadCrumb, getShowLogo } = useRootSetting();
|
||||
|
||||
const getShowMixHeaderRef = computed(() => !unref(getIsSidebarType) && unref(getShowHeader));
|
||||
|
||||
const getShowDoc = computed(() => appStore.getHeaderSetting.showDoc);
|
||||
|
||||
const getHeaderTheme = computed(() => appStore.getHeaderSetting.theme);
|
||||
|
||||
const getShowHeader = computed(() => appStore.getHeaderSetting.show);
|
||||
|
||||
const getFixed = computed(() => appStore.getHeaderSetting.fixed);
|
||||
|
||||
const getHeaderBgColor = computed(() => appStore.getHeaderSetting.bgColor);
|
||||
|
||||
const getShowSearch = computed(() => appStore.getHeaderSetting.showSearch);
|
||||
|
||||
const getUseLockPage = computed(() => appStore.getHeaderSetting.useLockPage);
|
||||
|
||||
const getShowFullScreen = computed(() => appStore.getHeaderSetting.showFullScreen);
|
||||
|
||||
const getShowNotice = computed(() => appStore.getHeaderSetting.showNotice);
|
||||
|
||||
const getShowBread = computed(() => {
|
||||
return unref(getMenuMode) !== MenuModeEnum.HORIZONTAL && unref(getShowBreadCrumb) && !unref(getSplit);
|
||||
});
|
||||
|
||||
const getShowHeaderLogo = computed(() => {
|
||||
return unref(getShowLogo) && !unref(getIsSidebarType) && !unref(getIsMixSidebar);
|
||||
});
|
||||
|
||||
const getShowContent = computed(() => {
|
||||
return unref(getShowBread) || unref(getShowHeaderTrigger);
|
||||
});
|
||||
|
||||
// Set header configuration
|
||||
function setHeaderSetting(headerSetting: Partial<HeaderSetting>) {
|
||||
appStore.setProjectConfig({ headerSetting });
|
||||
}
|
||||
return {
|
||||
setHeaderSetting,
|
||||
|
||||
getShowDoc,
|
||||
getShowSearch,
|
||||
getHeaderTheme,
|
||||
getUseLockPage,
|
||||
getShowFullScreen,
|
||||
getShowNotice,
|
||||
getShowBread,
|
||||
getShowContent,
|
||||
getShowHeaderLogo,
|
||||
getShowHeader,
|
||||
getFixed,
|
||||
getShowMixHeaderRef,
|
||||
getShowFullHeaderRef,
|
||||
getShowInsetHeaderRef,
|
||||
getUnFixedAndFull,
|
||||
getHeaderBgColor,
|
||||
};
|
||||
}
|
||||
158
web-vue/packages/core/hooks/setting/useMenuSetting.ts
Normal file
158
web-vue/packages/core/hooks/setting/useMenuSetting.ts
Normal file
@@ -0,0 +1,158 @@
|
||||
import type { MenuSetting } from '@jeesite/types/config';
|
||||
|
||||
import { computed, unref, ref } from 'vue';
|
||||
|
||||
import { useAppStore } from '@jeesite/core/store/modules/app';
|
||||
|
||||
import { SIDE_BAR_MINI_WIDTH, SIDE_BAR_SHOW_TIT_MINI_WIDTH } from '@jeesite/core/enums/appEnum';
|
||||
import { MenuModeEnum, MenuTypeEnum, TriggerEnum } from '@jeesite/core/enums/menuEnum';
|
||||
import { useFullContent } from '@jeesite/core/hooks/web/useFullContent';
|
||||
|
||||
const mixSideHasChildren = ref(false);
|
||||
|
||||
export function useMenuSetting() {
|
||||
const { getFullContent: fullContent } = useFullContent();
|
||||
const appStore = useAppStore();
|
||||
|
||||
const getShowSidebar = computed(() => {
|
||||
return (
|
||||
// unref(getSplit) ||
|
||||
unref(getShowMenu) && unref(getMenuMode) !== MenuModeEnum.HORIZONTAL && !unref(fullContent)
|
||||
);
|
||||
});
|
||||
|
||||
const getCollapsed = computed(() => appStore.getMenuSetting.collapsed);
|
||||
|
||||
const getMenuType = computed(() => appStore.getMenuSetting.type);
|
||||
|
||||
const getMenuMode = computed(() => appStore.getMenuSetting.mode);
|
||||
|
||||
const getMenuFixed = computed(() => appStore.getMenuSetting.fixed);
|
||||
|
||||
const getShowMenu = computed(() => appStore.getMenuSetting.show);
|
||||
|
||||
const getMenuHidden = computed(() => appStore.getMenuSetting.hidden);
|
||||
|
||||
const getMenuWidth = computed(() => appStore.getMenuSetting.menuWidth);
|
||||
|
||||
const getTrigger = computed(() => appStore.getMenuSetting.trigger);
|
||||
|
||||
const getMenuTheme = computed(() => appStore.getMenuSetting.theme);
|
||||
|
||||
const getSplit = computed(() => appStore.getMenuSetting.split);
|
||||
|
||||
const getMenuBgColor = computed(() => appStore.getMenuSetting.bgColor);
|
||||
|
||||
const getMixSideTrigger = computed(() => appStore.getMenuSetting.mixSideTrigger);
|
||||
|
||||
const getCanDrag = computed(() => appStore.getMenuSetting.canDrag);
|
||||
|
||||
const getAccordion = computed(() => appStore.getMenuSetting.accordion);
|
||||
|
||||
const getMixSideFixed = computed(() => appStore.getMenuSetting.mixSideFixed);
|
||||
|
||||
const getTopMenuAlign = computed(() => appStore.getMenuSetting.topMenuAlign);
|
||||
|
||||
const getCloseMixSidebarOnChange = computed(() => appStore.getMenuSetting.closeMixSidebarOnChange);
|
||||
|
||||
const getIsSidebarType = computed(() => unref(getMenuType) === MenuTypeEnum.SIDEBAR);
|
||||
|
||||
const getIsTopMenu = computed(() => unref(getMenuType) === MenuTypeEnum.TOP_MENU);
|
||||
|
||||
const getCollapsedShowTitle = computed(() => appStore.getMenuSetting.collapsedShowTitle);
|
||||
|
||||
const getShowTopMenu = computed(() => {
|
||||
return unref(getMenuMode) === MenuModeEnum.HORIZONTAL || unref(getSplit);
|
||||
});
|
||||
|
||||
const getShowHeaderTrigger = computed(() => {
|
||||
if (unref(getMenuType) === MenuTypeEnum.TOP_MENU || !unref(getShowMenu) || unref(getMenuHidden)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return unref(getTrigger) === TriggerEnum.HEADER;
|
||||
});
|
||||
|
||||
const getIsHorizontal = computed(() => {
|
||||
return unref(getMenuMode) === MenuModeEnum.HORIZONTAL;
|
||||
});
|
||||
|
||||
const getIsMixSidebar = computed(() => {
|
||||
return unref(getMenuType) === MenuTypeEnum.MIX_SIDEBAR;
|
||||
});
|
||||
|
||||
const getIsMixMode = computed(() => {
|
||||
return unref(getMenuMode) === MenuModeEnum.INLINE && unref(getMenuType) === MenuTypeEnum.MIX;
|
||||
});
|
||||
|
||||
const getRealWidth = computed(() => {
|
||||
if (unref(getIsMixSidebar)) {
|
||||
return unref(getCollapsed) && !unref(getMixSideFixed) ? unref(getMiniWidthNumber) : unref(getMenuWidth);
|
||||
}
|
||||
return unref(getCollapsed) ? unref(getMiniWidthNumber) : unref(getMenuWidth);
|
||||
});
|
||||
|
||||
const getMiniWidthNumber = computed(() => {
|
||||
const { collapsedShowTitle } = appStore.getMenuSetting;
|
||||
return collapsedShowTitle ? SIDE_BAR_SHOW_TIT_MINI_WIDTH : SIDE_BAR_MINI_WIDTH;
|
||||
});
|
||||
|
||||
const getCalcContentWidth = computed(() => {
|
||||
const width =
|
||||
unref(getIsTopMenu) || !unref(getShowMenu) || (unref(getSplit) && unref(getMenuHidden))
|
||||
? 0
|
||||
: unref(getIsMixSidebar)
|
||||
? (unref(getCollapsed) ? SIDE_BAR_MINI_WIDTH : SIDE_BAR_SHOW_TIT_MINI_WIDTH) +
|
||||
(unref(getMixSideFixed) && unref(mixSideHasChildren) ? unref(getRealWidth) : 0)
|
||||
: unref(getRealWidth);
|
||||
|
||||
return `calc(100% - ${unref(width)}px)`;
|
||||
});
|
||||
|
||||
// Set menu configuration
|
||||
function setMenuSetting(menuSetting: Partial<MenuSetting>): void {
|
||||
appStore.setProjectConfig({ menuSetting });
|
||||
}
|
||||
|
||||
function toggleCollapsed() {
|
||||
setMenuSetting({
|
||||
collapsed: !unref(getCollapsed),
|
||||
});
|
||||
}
|
||||
return {
|
||||
setMenuSetting,
|
||||
|
||||
toggleCollapsed,
|
||||
|
||||
getMenuFixed,
|
||||
getRealWidth,
|
||||
getMenuType,
|
||||
getMenuMode,
|
||||
getShowMenu,
|
||||
getCollapsed,
|
||||
getMiniWidthNumber,
|
||||
getCalcContentWidth,
|
||||
getMenuWidth,
|
||||
getTrigger,
|
||||
getSplit,
|
||||
getMenuTheme,
|
||||
getCanDrag,
|
||||
getCollapsedShowTitle,
|
||||
getIsHorizontal,
|
||||
getIsSidebarType,
|
||||
getAccordion,
|
||||
getShowTopMenu,
|
||||
getShowHeaderTrigger,
|
||||
getTopMenuAlign,
|
||||
getMenuHidden,
|
||||
getIsTopMenu,
|
||||
getMenuBgColor,
|
||||
getShowSidebar,
|
||||
getIsMixMode,
|
||||
getIsMixSidebar,
|
||||
getCloseMixSidebarOnChange,
|
||||
getMixSideTrigger,
|
||||
getMixSideFixed,
|
||||
mixSideHasChildren,
|
||||
};
|
||||
}
|
||||
31
web-vue/packages/core/hooks/setting/useMultipleTabSetting.ts
Normal file
31
web-vue/packages/core/hooks/setting/useMultipleTabSetting.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import type { MultiTabsSetting } from '@jeesite/types/config';
|
||||
|
||||
import { computed } from 'vue';
|
||||
|
||||
import { useAppStore } from '@jeesite/core/store/modules/app';
|
||||
|
||||
export function useMultipleTabSetting() {
|
||||
const appStore = useAppStore();
|
||||
|
||||
const getShowMultipleTab = computed(() => appStore.getMultiTabsSetting.show);
|
||||
|
||||
const getTabsStyle = computed(() => appStore.getMultiTabsSetting.style);
|
||||
|
||||
const getShowQuick = computed(() => appStore.getMultiTabsSetting.showQuick);
|
||||
|
||||
const getShowRedo = computed(() => appStore.getMultiTabsSetting.showRedo);
|
||||
|
||||
const getShowFold = computed(() => appStore.getMultiTabsSetting.showFold);
|
||||
|
||||
function setMultipleTabSetting(multiTabsSetting: Partial<MultiTabsSetting>) {
|
||||
appStore.setProjectConfig({ multiTabsSetting });
|
||||
}
|
||||
return {
|
||||
setMultipleTabSetting,
|
||||
getShowMultipleTab,
|
||||
getTabsStyle,
|
||||
getShowQuick,
|
||||
getShowRedo,
|
||||
getShowFold,
|
||||
};
|
||||
}
|
||||
90
web-vue/packages/core/hooks/setting/useRootSetting.ts
Normal file
90
web-vue/packages/core/hooks/setting/useRootSetting.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import type { ProjectConfig } from '@jeesite/types/config';
|
||||
|
||||
import { computed } from 'vue';
|
||||
|
||||
import { useAppStore } from '@jeesite/core/store/modules/app';
|
||||
import { ContentEnum, ThemeEnum } from '@jeesite/core/enums/appEnum';
|
||||
|
||||
type RootSetting = Omit<ProjectConfig, 'locale' | 'headerSetting' | 'menuSetting' | 'multiTabsSetting'>;
|
||||
|
||||
export function useRootSetting() {
|
||||
const appStore = useAppStore();
|
||||
|
||||
const getPageLoading = computed(() => appStore.getPageLoading);
|
||||
|
||||
const getOpenKeepAlive = computed(() => appStore.getProjectConfig.openKeepAlive);
|
||||
|
||||
const getSettingButtonPosition = computed(() => appStore.getProjectConfig.settingButtonPosition);
|
||||
|
||||
const getCanEmbedIFramePage = computed(() => appStore.getProjectConfig.canEmbedIFramePage);
|
||||
|
||||
const getPermissionMode = computed(() => appStore.getProjectConfig.permissionMode);
|
||||
|
||||
const getShowLogo = computed(() => appStore.getProjectConfig.showLogo);
|
||||
|
||||
const getContentMode = computed(() => appStore.getProjectConfig.contentMode);
|
||||
|
||||
const getUseOpenBackTop = computed(() => appStore.getProjectConfig.useOpenBackTop);
|
||||
|
||||
const getShowSettingButton = computed(() => appStore.getProjectConfig.showSettingButton);
|
||||
|
||||
const getUseErrorHandle = computed(() => appStore.getProjectConfig.useErrorHandle);
|
||||
|
||||
const getShowFooter = computed(() => appStore.getProjectConfig.showFooter);
|
||||
|
||||
const getShowBreadCrumb = computed(() => appStore.getProjectConfig.showBreadCrumb);
|
||||
|
||||
const getThemeColor = computed(() => appStore.getProjectConfig.themeColor);
|
||||
|
||||
const getShowBreadCrumbIcon = computed(() => appStore.getProjectConfig.showBreadCrumbIcon);
|
||||
|
||||
const getFullContent = computed(() => appStore.getProjectConfig.fullContent);
|
||||
|
||||
const getColorWeak = computed(() => appStore.getProjectConfig.colorWeak);
|
||||
|
||||
const getGrayMode = computed(() => appStore.getProjectConfig.grayMode);
|
||||
|
||||
const getLockTime = computed(() => appStore.getProjectConfig.lockTime);
|
||||
|
||||
const getShowDarkModeToggle = computed(() => appStore.getProjectConfig.showDarkModeToggle);
|
||||
|
||||
const getDarkMode = computed(() => appStore.getDarkMode);
|
||||
|
||||
const getLayoutContentMode = computed(() =>
|
||||
appStore.getProjectConfig.contentMode === ContentEnum.FULL ? ContentEnum.FULL : ContentEnum.FIXED,
|
||||
);
|
||||
|
||||
function setRootSetting(setting: Partial<RootSetting>) {
|
||||
appStore.setProjectConfig(setting);
|
||||
}
|
||||
|
||||
function setDarkMode(mode: ThemeEnum) {
|
||||
appStore.setDarkMode(mode);
|
||||
}
|
||||
return {
|
||||
setRootSetting,
|
||||
|
||||
getSettingButtonPosition,
|
||||
getFullContent,
|
||||
getColorWeak,
|
||||
getGrayMode,
|
||||
getLayoutContentMode,
|
||||
getPageLoading,
|
||||
getOpenKeepAlive,
|
||||
getCanEmbedIFramePage,
|
||||
getPermissionMode,
|
||||
getShowLogo,
|
||||
getUseErrorHandle,
|
||||
getShowBreadCrumb,
|
||||
getShowBreadCrumbIcon,
|
||||
getUseOpenBackTop,
|
||||
getShowSettingButton,
|
||||
getShowFooter,
|
||||
getContentMode,
|
||||
getLockTime,
|
||||
getThemeColor,
|
||||
getDarkMode,
|
||||
setDarkMode,
|
||||
getShowDarkModeToggle,
|
||||
};
|
||||
}
|
||||
32
web-vue/packages/core/hooks/setting/useTransitionSetting.ts
Normal file
32
web-vue/packages/core/hooks/setting/useTransitionSetting.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import type { TransitionSetting } from '@jeesite/types/config';
|
||||
|
||||
import { computed } from 'vue';
|
||||
|
||||
import { useAppStore } from '@jeesite/core/store/modules/app';
|
||||
|
||||
export function useTransitionSetting() {
|
||||
const appStore = useAppStore();
|
||||
|
||||
const getEnableTransition = computed(() => appStore.getTransitionSetting?.enable);
|
||||
|
||||
// const getOpenNProgress = computed(() => appStore.getTransitionSetting?.openNProgress);
|
||||
|
||||
const getOpenPageLoading = computed((): boolean => {
|
||||
return !!appStore.getTransitionSetting?.openPageLoading;
|
||||
});
|
||||
|
||||
const getBasicTransition = computed(() => appStore.getTransitionSetting?.basicTransition);
|
||||
|
||||
function setTransitionSetting(transitionSetting: Partial<TransitionSetting>) {
|
||||
appStore.setProjectConfig({ transitionSetting });
|
||||
}
|
||||
|
||||
return {
|
||||
setTransitionSetting,
|
||||
|
||||
getEnableTransition,
|
||||
// getOpenNProgress,
|
||||
getOpenPageLoading,
|
||||
getBasicTransition,
|
||||
};
|
||||
}
|
||||
10
web-vue/packages/core/hooks/web/useAppInject.ts
Normal file
10
web-vue/packages/core/hooks/web/useAppInject.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { useAppProviderContext } from '@jeesite/core/components/Application';
|
||||
import { computed, unref } from 'vue';
|
||||
|
||||
export function useAppInject() {
|
||||
const values = useAppProviderContext();
|
||||
|
||||
return {
|
||||
getIsMobile: computed(() => unref(values.isMobile)),
|
||||
};
|
||||
}
|
||||
190
web-vue/packages/core/hooks/web/useContentHeight.ts
Normal file
190
web-vue/packages/core/hooks/web/useContentHeight.ts
Normal file
@@ -0,0 +1,190 @@
|
||||
import { ComputedRef, isRef, nextTick, Ref, ref, unref, watch } from 'vue';
|
||||
import { onMountedOrActivated } from '@jeesite/core/hooks/core/onMountedOrActivated';
|
||||
import { useWindowSizeFn } from '@jeesite/core/hooks/event/useWindowSizeFn';
|
||||
import { useLayoutHeight } from '@jeesite/core/layouts/default/content/useContentViewHeight';
|
||||
import { getViewportOffset } from '@jeesite/core/utils/domUtils';
|
||||
import { isNumber, isString } from '@jeesite/core/utils/is';
|
||||
|
||||
export interface CompensationHeight {
|
||||
// 使用 layout Footer 高度作为判断补偿高度的条件
|
||||
useLayoutFooter: boolean;
|
||||
// refs HTMLElement
|
||||
elements?: Ref[];
|
||||
}
|
||||
|
||||
type Upward = number | string | null | undefined;
|
||||
|
||||
/**
|
||||
* 动态计算内容高度,根据锚点dom最下坐标到屏幕最下坐标,根据传入dom的高度、padding、margin等值进行动态计算
|
||||
* 最终获取合适的内容高度
|
||||
*
|
||||
* @param flag 用于开启计算的响应式标识
|
||||
* @param anchorRef 锚点组件 Ref<ElRef | ComponentRef>
|
||||
* @param subtractHeightRefs 待减去高度的组件列表 Ref<ElRef | ComponentRef>
|
||||
* @param substractSpaceRefs 待减去空闲空间(margins/paddings)的组件列表 Ref<ElRef | ComponentRef>
|
||||
* @param offsetHeightRef 计算偏移的响应式高度,计算高度时将直接减去此值
|
||||
* @param upwardSpace 向上递归减去空闲空间的 层级 或 直到指定class为止 数值为2代表向上递归两次|数值为ant-layout表示向上递归直到碰见.ant-layout为止
|
||||
* @returns 响应式高度
|
||||
*/
|
||||
export function useContentHeight(
|
||||
flag: ComputedRef<boolean>,
|
||||
anchorRef: Ref,
|
||||
subtractHeightRefs: Ref[],
|
||||
substractSpaceRefs: Ref[],
|
||||
upwardSpace: Ref<Upward> | ComputedRef<Upward> | Upward = 0,
|
||||
offsetHeightRef: Ref<number> = ref(0),
|
||||
) {
|
||||
const contentHeight: Ref<Nullable<number>> = ref(null);
|
||||
const { footerHeightRef: layoutFooterHeightRef } = useLayoutHeight();
|
||||
let compensationHeight: CompensationHeight = {
|
||||
useLayoutFooter: true,
|
||||
};
|
||||
|
||||
const setCompensation = (params: CompensationHeight) => {
|
||||
compensationHeight = params;
|
||||
};
|
||||
|
||||
function redoHeight() {
|
||||
nextTick(() => {
|
||||
calcContentHeight();
|
||||
});
|
||||
}
|
||||
|
||||
function calcSubtractSpace(element: Element | null | undefined, direction: 'all' | 'top' | 'bottom' = 'all'): number {
|
||||
function numberPx(px: string) {
|
||||
return Number(px.replace(/[^\d]/g, ''));
|
||||
}
|
||||
let subtractHeight = 0;
|
||||
const ZERO_PX = '0px';
|
||||
if (element) {
|
||||
const cssStyle = getComputedStyle(element);
|
||||
const marginTop = numberPx(cssStyle?.marginTop ?? ZERO_PX);
|
||||
const marginBottom = numberPx(cssStyle?.marginBottom ?? ZERO_PX);
|
||||
const paddingTop = numberPx(cssStyle?.paddingTop ?? ZERO_PX);
|
||||
const paddingBottom = numberPx(cssStyle?.paddingBottom ?? ZERO_PX);
|
||||
if (direction === 'all') {
|
||||
subtractHeight += marginTop;
|
||||
subtractHeight += marginBottom;
|
||||
subtractHeight += paddingTop;
|
||||
subtractHeight += paddingBottom;
|
||||
} else if (direction === 'top') {
|
||||
subtractHeight += marginTop;
|
||||
subtractHeight += paddingTop;
|
||||
} else {
|
||||
subtractHeight += marginBottom;
|
||||
subtractHeight += paddingBottom;
|
||||
}
|
||||
}
|
||||
return subtractHeight;
|
||||
}
|
||||
|
||||
function getEl(element: any): Nullable<HTMLDivElement> {
|
||||
if (element == null) {
|
||||
return null;
|
||||
}
|
||||
return (element instanceof HTMLDivElement ? element : element.$el) as HTMLDivElement;
|
||||
}
|
||||
|
||||
async function calcContentHeight() {
|
||||
if (!flag.value) {
|
||||
return;
|
||||
}
|
||||
// Add a delay to get the correct height
|
||||
await nextTick();
|
||||
|
||||
const anchorEl = getEl(unref(anchorRef));
|
||||
if (!anchorEl) {
|
||||
return;
|
||||
}
|
||||
const { bottomIncludeBody } = getViewportOffset(anchorEl);
|
||||
|
||||
// substract elements height
|
||||
let substractHeight = 0;
|
||||
subtractHeightRefs.forEach((item) => {
|
||||
substractHeight += getEl(unref(item))?.offsetHeight ?? 0;
|
||||
});
|
||||
|
||||
// subtract margins / paddings
|
||||
let substractSpaceHeight = calcSubtractSpace(anchorEl) ?? 0;
|
||||
substractSpaceRefs.forEach((item) => {
|
||||
substractSpaceHeight += calcSubtractSpace(getEl(unref(item)));
|
||||
});
|
||||
|
||||
// upwardSpace
|
||||
let upwardSpaceHeight = 0;
|
||||
function upward(element: Element | null, upwardLvlOrClass: number | string | null | undefined) {
|
||||
if (element && upwardLvlOrClass) {
|
||||
const parent = element.parentElement;
|
||||
if (parent) {
|
||||
if (isString(upwardLvlOrClass)) {
|
||||
if (!parent.classList.contains(upwardLvlOrClass)) {
|
||||
upwardSpaceHeight += calcSubtractSpace(parent, 'bottom');
|
||||
upward(parent, upwardLvlOrClass);
|
||||
} else {
|
||||
upwardSpaceHeight += calcSubtractSpace(parent, 'bottom');
|
||||
}
|
||||
} else if (isNumber(upwardLvlOrClass)) {
|
||||
if (upwardLvlOrClass > 0) {
|
||||
upwardSpaceHeight += calcSubtractSpace(parent, 'bottom');
|
||||
upward(parent, --upwardLvlOrClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isRef(upwardSpace)) {
|
||||
upward(anchorEl, unref(upwardSpace));
|
||||
} else {
|
||||
upward(anchorEl, upwardSpace);
|
||||
}
|
||||
|
||||
let height =
|
||||
bottomIncludeBody -
|
||||
unref(layoutFooterHeightRef) -
|
||||
unref(offsetHeightRef) -
|
||||
substractHeight -
|
||||
substractSpaceHeight -
|
||||
upwardSpaceHeight;
|
||||
|
||||
// compensation height
|
||||
const calcCompensationHeight = () => {
|
||||
compensationHeight.elements?.forEach((item) => {
|
||||
height += getEl(unref(item))?.offsetHeight ?? 0;
|
||||
});
|
||||
};
|
||||
if (compensationHeight.useLayoutFooter && unref(layoutFooterHeightRef) > 0) {
|
||||
calcCompensationHeight();
|
||||
} else {
|
||||
calcCompensationHeight();
|
||||
}
|
||||
|
||||
const fixHeight = -1;
|
||||
|
||||
contentHeight.value = height + fixHeight;
|
||||
}
|
||||
|
||||
onMountedOrActivated(() => {
|
||||
nextTick(() => {
|
||||
calcContentHeight();
|
||||
});
|
||||
});
|
||||
useWindowSizeFn(
|
||||
() => {
|
||||
calcContentHeight();
|
||||
},
|
||||
50,
|
||||
{ immediate: true },
|
||||
);
|
||||
watch(
|
||||
() => [layoutFooterHeightRef.value],
|
||||
() => {
|
||||
calcContentHeight();
|
||||
},
|
||||
{
|
||||
flush: 'post',
|
||||
immediate: true,
|
||||
},
|
||||
);
|
||||
|
||||
return { redoHeight, setCompensation, contentHeight };
|
||||
}
|
||||
12
web-vue/packages/core/hooks/web/useContextMenu.ts
Normal file
12
web-vue/packages/core/hooks/web/useContextMenu.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { onUnmounted, getCurrentInstance } from 'vue';
|
||||
import { createContextMenu, destroyContextMenu } from '@jeesite/core/components/ContextMenu';
|
||||
import type { ContextMenuItem } from '@jeesite/core/components/ContextMenu';
|
||||
export type { ContextMenuItem };
|
||||
export function useContextMenu(authRemove = true) {
|
||||
if (getCurrentInstance() && authRemove) {
|
||||
onUnmounted(() => {
|
||||
destroyContextMenu();
|
||||
});
|
||||
}
|
||||
return [createContextMenu, destroyContextMenu];
|
||||
}
|
||||
69
web-vue/packages/core/hooks/web/useCopyToClipboard.ts
Normal file
69
web-vue/packages/core/hooks/web/useCopyToClipboard.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { ref, watch } from 'vue';
|
||||
|
||||
import { isDef } from '@jeesite/core/utils/is';
|
||||
interface Options {
|
||||
target?: HTMLElement;
|
||||
}
|
||||
export function useCopyToClipboard(initial?: string) {
|
||||
const clipboardRef = ref(initial || '');
|
||||
const isSuccessRef = ref(false);
|
||||
const copiedRef = ref(false);
|
||||
|
||||
watch(
|
||||
clipboardRef,
|
||||
(str?: string) => {
|
||||
if (isDef(str)) {
|
||||
copiedRef.value = true;
|
||||
isSuccessRef.value = copyTextToClipboard(str);
|
||||
}
|
||||
},
|
||||
{ immediate: !!initial, flush: 'sync' },
|
||||
);
|
||||
|
||||
return { clipboardRef, isSuccessRef, copiedRef };
|
||||
}
|
||||
|
||||
export function copyTextToClipboard(input: string, { target = document.body }: Options = {}) {
|
||||
const element = document.createElement('textarea');
|
||||
const previouslyFocusedElement = document.activeElement;
|
||||
|
||||
element.value = input;
|
||||
|
||||
element.setAttribute('readonly', '');
|
||||
|
||||
(element.style as any).contain = 'strict';
|
||||
element.style.position = 'absolute';
|
||||
element.style.left = '-9999px';
|
||||
element.style.fontSize = '12pt';
|
||||
|
||||
const selection = document.getSelection();
|
||||
let originalRange;
|
||||
if (selection && selection.rangeCount > 0) {
|
||||
originalRange = selection.getRangeAt(0);
|
||||
}
|
||||
|
||||
target.append(element);
|
||||
element.select();
|
||||
|
||||
element.selectionStart = 0;
|
||||
element.selectionEnd = input.length;
|
||||
|
||||
let isSuccess = false;
|
||||
try {
|
||||
isSuccess = document.execCommand('copy');
|
||||
} catch (e: any) {
|
||||
throw new Error(e);
|
||||
}
|
||||
|
||||
element.remove();
|
||||
|
||||
if (originalRange && selection) {
|
||||
selection.removeAllRanges();
|
||||
selection.addRange(originalRange);
|
||||
}
|
||||
|
||||
if (previouslyFocusedElement) {
|
||||
(previouslyFocusedElement as HTMLElement).focus();
|
||||
}
|
||||
return isSuccess;
|
||||
}
|
||||
26
web-vue/packages/core/hooks/web/useDesign.ts
Normal file
26
web-vue/packages/core/hooks/web/useDesign.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { useAppProviderContext } from '@jeesite/core/components/Application';
|
||||
import { theme } from 'ant-design-vue';
|
||||
// import { computed } from 'vue';
|
||||
// import { lowerFirst } from 'lodash-es';
|
||||
|
||||
export function useDesign(scope: string) {
|
||||
const values = useAppProviderContext();
|
||||
const token = theme.useToken();
|
||||
// const $style = cssModule ? useCssModule() : {};
|
||||
|
||||
// const style: Record<string, string> = {};
|
||||
// if (cssModule) {
|
||||
// Object.keys($style).forEach((key) => {
|
||||
// // const moduleCls = $style[key];
|
||||
// const k = key.replace(new RegExp(`^${values.prefixCls}-?`, 'ig'), '');
|
||||
// style[lowerFirst(k)] = $style[key];
|
||||
// });
|
||||
// }
|
||||
return {
|
||||
// prefixCls: computed(() => `${values.prefixCls}-${scope}`),
|
||||
prefixCls: `${values.prefixCls}-${scope}`,
|
||||
prefixVar: values.prefixCls,
|
||||
hashId: token.hashId.value,
|
||||
// style,
|
||||
};
|
||||
}
|
||||
117
web-vue/packages/core/hooks/web/useECharts.ts
Normal file
117
web-vue/packages/core/hooks/web/useECharts.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import type { EChartsOption } from 'echarts';
|
||||
import { onActivated, Ref } from 'vue';
|
||||
import { useTimeoutFn } from '@jeesite/core/hooks/core/useTimeout';
|
||||
import { tryOnUnmounted } from '@vueuse/core';
|
||||
import { unref, nextTick, watch, computed, ref } from 'vue';
|
||||
import { useDebounceFn } from '@vueuse/core';
|
||||
import { useEventListener } from '@jeesite/core/hooks/event/useEventListener';
|
||||
import { useBreakpoint } from '@jeesite/core/hooks/event/useBreakpoint';
|
||||
import echarts from '@jeesite/core/utils/lib/echarts';
|
||||
import { useRootSetting } from '@jeesite/core/hooks/setting/useRootSetting';
|
||||
|
||||
export function useECharts(elRef: Ref<HTMLDivElement>, theme: 'light' | 'dark' | 'default' = 'default') {
|
||||
const { getDarkMode: getSysDarkMode } = useRootSetting();
|
||||
|
||||
const getDarkMode = computed(() => {
|
||||
return theme === 'default' ? getSysDarkMode.value : theme;
|
||||
});
|
||||
let chartInstance: echarts.ECharts | null = null;
|
||||
let resizeFn: Fn = resize;
|
||||
const cacheOptions = ref({}) as Ref<EChartsOption>;
|
||||
let removeResizeFn: Fn = () => {};
|
||||
|
||||
resizeFn = useDebounceFn(resize, 200);
|
||||
|
||||
const getOptions = computed(() => {
|
||||
if (getDarkMode.value !== 'dark') {
|
||||
return cacheOptions.value as EChartsOption;
|
||||
}
|
||||
return {
|
||||
backgroundColor: 'transparent',
|
||||
...cacheOptions.value,
|
||||
} as EChartsOption;
|
||||
});
|
||||
|
||||
function initCharts(t = theme) {
|
||||
const el = unref(elRef);
|
||||
if (!el || !unref(el)) {
|
||||
return;
|
||||
}
|
||||
|
||||
chartInstance = echarts.init(el, t);
|
||||
const { removeEvent } = useEventListener({
|
||||
el: window,
|
||||
name: 'resize',
|
||||
listener: resizeFn,
|
||||
});
|
||||
removeResizeFn = removeEvent;
|
||||
const { widthRef, screenEnum } = useBreakpoint();
|
||||
if (unref(widthRef) <= screenEnum.MD || el.offsetHeight === 0) {
|
||||
useTimeoutFn(() => {
|
||||
resizeFn();
|
||||
}, 30);
|
||||
}
|
||||
}
|
||||
|
||||
function setOptions(options: EChartsOption, clear = true) {
|
||||
cacheOptions.value = options;
|
||||
if (unref(elRef)?.offsetHeight === 0) {
|
||||
useTimeoutFn(() => {
|
||||
setOptions(unref(getOptions));
|
||||
}, 30);
|
||||
return;
|
||||
}
|
||||
nextTick(() => {
|
||||
useTimeoutFn(() => {
|
||||
if (!chartInstance) {
|
||||
initCharts(getDarkMode.value as 'default');
|
||||
|
||||
if (!chartInstance) return;
|
||||
}
|
||||
clear && chartInstance?.clear();
|
||||
|
||||
chartInstance?.setOption(unref(getOptions));
|
||||
}, 30);
|
||||
});
|
||||
}
|
||||
|
||||
function resize() {
|
||||
chartInstance?.resize();
|
||||
}
|
||||
|
||||
onActivated(() => {
|
||||
resize();
|
||||
});
|
||||
|
||||
watch(
|
||||
() => getDarkMode.value,
|
||||
(theme) => {
|
||||
if (chartInstance) {
|
||||
chartInstance.dispose();
|
||||
initCharts(theme as 'default');
|
||||
setOptions(cacheOptions.value);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
tryOnUnmounted(() => {
|
||||
if (!chartInstance) return;
|
||||
removeResizeFn();
|
||||
chartInstance.dispose();
|
||||
chartInstance = null;
|
||||
});
|
||||
|
||||
function getInstance(): echarts.ECharts | null {
|
||||
if (!chartInstance) {
|
||||
initCharts(getDarkMode.value as 'default');
|
||||
}
|
||||
return chartInstance;
|
||||
}
|
||||
|
||||
return {
|
||||
setOptions,
|
||||
resize,
|
||||
echarts,
|
||||
getInstance,
|
||||
};
|
||||
}
|
||||
28
web-vue/packages/core/hooks/web/useFullContent.ts
Normal file
28
web-vue/packages/core/hooks/web/useFullContent.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { computed, unref } from 'vue';
|
||||
|
||||
import { useAppStore } from '@jeesite/core/store/modules/app';
|
||||
|
||||
import { useRouter } from 'vue-router';
|
||||
|
||||
/**
|
||||
* @description: Full screen display content
|
||||
*/
|
||||
export const useFullContent = () => {
|
||||
const appStore = useAppStore();
|
||||
const router = useRouter();
|
||||
const { currentRoute } = router;
|
||||
|
||||
// Whether to display the content in full screen without displaying the menu
|
||||
const getFullContent = computed(() => {
|
||||
// Query parameters, the full screen is displayed when the address bar has a full parameter
|
||||
const route = unref(currentRoute);
|
||||
const query = route.query;
|
||||
if (query && Reflect.has(query, '__full__')) {
|
||||
return true;
|
||||
}
|
||||
// Return to the configuration in the configuration file
|
||||
return appStore.getProjectConfig.fullContent;
|
||||
});
|
||||
|
||||
return { getFullContent };
|
||||
};
|
||||
57
web-vue/packages/core/hooks/web/useI18n.ts
Normal file
57
web-vue/packages/core/hooks/web/useI18n.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { i18n } from '@jeesite/core/locales/setupI18n';
|
||||
|
||||
type I18nGlobalTranslation = {
|
||||
(key: string): string;
|
||||
(key: string, locale: string): string;
|
||||
(key: string, locale: string, list: unknown[]): string;
|
||||
(key: string, locale: string, named: Record<string, unknown>): string;
|
||||
(key: string, list: unknown[]): string;
|
||||
(key: string, named: Record<string, unknown>): string;
|
||||
};
|
||||
|
||||
type I18nTranslationRestParameters = [string, any];
|
||||
|
||||
function getKey(namespace: string | undefined, key: string) {
|
||||
if (!namespace) {
|
||||
return key;
|
||||
}
|
||||
if (key.startsWith(namespace)) {
|
||||
return key;
|
||||
}
|
||||
return `${key}`;
|
||||
}
|
||||
|
||||
export function useI18n(namespace?: string): {
|
||||
t: I18nGlobalTranslation;
|
||||
} {
|
||||
const normalFn = {
|
||||
t: (key: string) => {
|
||||
return getKey(namespace, key);
|
||||
},
|
||||
};
|
||||
|
||||
if (!i18n) {
|
||||
return normalFn;
|
||||
}
|
||||
|
||||
const { t, ...methods } = i18n.global;
|
||||
|
||||
const tt = t as (arg0: string, ...arg: I18nTranslationRestParameters) => string;
|
||||
|
||||
const tFn: I18nGlobalTranslation = (key: string, ...arg: any[]) => {
|
||||
if (!key) return '';
|
||||
if (!key.includes('.') && !namespace) return key;
|
||||
return tt(getKey(namespace, key), ...(arg as I18nTranslationRestParameters));
|
||||
};
|
||||
return {
|
||||
...methods,
|
||||
t: tFn,
|
||||
};
|
||||
}
|
||||
|
||||
// Why write this function?
|
||||
// Mainly to configure the vscode i18nn ally plugin. This function is only used for routing and menus. Please use useI18n for other places
|
||||
|
||||
// 为什么要编写此函数?
|
||||
// 主要用于配合vscode i18nn ally插件。此功能仅用于路由和菜单。请在其他地方使用useI18n
|
||||
export const t = (key: string) => key;
|
||||
77
web-vue/packages/core/hooks/web/useLockPage.ts
Normal file
77
web-vue/packages/core/hooks/web/useLockPage.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { computed, onUnmounted, unref, watchEffect } from 'vue';
|
||||
import { useThrottleFn } from '@vueuse/core';
|
||||
|
||||
import { useAppStore } from '@jeesite/core/store/modules/app';
|
||||
import { useLockStore } from '@jeesite/core/store/modules/lock';
|
||||
|
||||
import { useUserStore } from '@jeesite/core/store/modules/user';
|
||||
import { useRootSetting } from '../setting/useRootSetting';
|
||||
|
||||
export function useLockPage() {
|
||||
const { getLockTime } = useRootSetting();
|
||||
const lockStore = useLockStore();
|
||||
const userStore = useUserStore();
|
||||
const appStore = useAppStore();
|
||||
|
||||
let timeId: TimeoutHandle;
|
||||
|
||||
function clear(): void {
|
||||
window.clearTimeout(timeId);
|
||||
}
|
||||
|
||||
function resetCalcLockTimeout(): void {
|
||||
// not login
|
||||
// if (!userStore.getToken) {
|
||||
if (userStore.getSessionTimeout) {
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
const lockTime = appStore.getProjectConfig.lockTime;
|
||||
if (!lockTime || lockTime < 1 || lockTime > 99999) {
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
clear();
|
||||
|
||||
timeId = setTimeout(
|
||||
() => {
|
||||
lockPage();
|
||||
},
|
||||
lockTime * 60 * 1000,
|
||||
);
|
||||
}
|
||||
|
||||
function lockPage(): void {
|
||||
lockStore.setLockInfo({
|
||||
isLock: true,
|
||||
pwd: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
watchEffect((onClean) => {
|
||||
// if (userStore.getToken) {
|
||||
if (!userStore.getSessionTimeout) {
|
||||
resetCalcLockTimeout();
|
||||
} else {
|
||||
clear();
|
||||
}
|
||||
onClean(() => {
|
||||
clear();
|
||||
});
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
clear();
|
||||
});
|
||||
|
||||
const keyupFn = useThrottleFn(resetCalcLockTimeout, 2000);
|
||||
|
||||
return computed(() => {
|
||||
if (unref(getLockTime)) {
|
||||
return { onKeyup: keyupFn, onMousemove: keyupFn };
|
||||
} else {
|
||||
clear();
|
||||
return {};
|
||||
}
|
||||
});
|
||||
}
|
||||
197
web-vue/packages/core/hooks/web/useMessage.tsx
Normal file
197
web-vue/packages/core/hooks/web/useMessage.tsx
Normal file
@@ -0,0 +1,197 @@
|
||||
import type { ModalFunc, ModalFuncProps } from 'ant-design-vue/lib/modal/Modal';
|
||||
|
||||
import { Modal, message as Message, notification } from 'ant-design-vue';
|
||||
import { InfoCircleFilled, CheckCircleFilled, CloseCircleFilled } from '@ant-design/icons-vue';
|
||||
|
||||
import { NotificationArgsProps, ConfigProps } from 'ant-design-vue/lib/notification';
|
||||
import { useI18n } from './useI18n';
|
||||
import { isString } from '@jeesite/core/utils/is';
|
||||
import { Icon } from '@jeesite/core/components/Icon';
|
||||
|
||||
import type { ConfigOnClose, MessageType } from 'ant-design-vue/lib/message';
|
||||
import type { JointContent } from 'ant-design-vue/es/message/interface';
|
||||
|
||||
export interface NotifyApi {
|
||||
info(config: NotificationArgsProps): void;
|
||||
success(config: NotificationArgsProps): void;
|
||||
error(config: NotificationArgsProps): void;
|
||||
warn(config: NotificationArgsProps): void;
|
||||
warning(config: NotificationArgsProps): void;
|
||||
open(args: NotificationArgsProps): void;
|
||||
close(key: string): void;
|
||||
config(options: ConfigProps): void;
|
||||
destroy(): void;
|
||||
}
|
||||
|
||||
export declare type NotificationPlacement = 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight';
|
||||
export declare type IconType = 'success' | 'info' | 'error' | 'warning';
|
||||
export interface ModalOptionsEx extends Omit<ModalFuncProps, 'iconType'> {
|
||||
iconType: 'warning' | 'success' | 'error' | 'info';
|
||||
icon?: any;
|
||||
title?: any;
|
||||
content?: any;
|
||||
}
|
||||
export type ModalOptionsPartial = Partial<ModalOptionsEx> & Pick<ModalOptionsEx, 'content'>;
|
||||
|
||||
interface ConfirmOptions {
|
||||
info: ModalFunc;
|
||||
success: ModalFunc;
|
||||
error: ModalFunc;
|
||||
warn: ModalFunc;
|
||||
warning: ModalFunc;
|
||||
}
|
||||
|
||||
function getIcon(iconType: string) {
|
||||
if (iconType === 'warning') {
|
||||
return <InfoCircleFilled class="modal-icon-warning" />;
|
||||
} else if (iconType === 'success') {
|
||||
return <CheckCircleFilled class="modal-icon-success" />;
|
||||
} else if (iconType === 'info') {
|
||||
return <InfoCircleFilled class="modal-icon-info" />;
|
||||
} else {
|
||||
return <CloseCircleFilled class="modal-icon-error" />;
|
||||
}
|
||||
}
|
||||
|
||||
function renderContent({ content }: Pick<ModalOptionsEx, 'content'>) {
|
||||
if (isString(content)) {
|
||||
return <div innerHTML={`<div>${content as string}</div>`}></div>;
|
||||
} else {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Create confirmation box
|
||||
*/
|
||||
function createConfirm(options: ModalOptionsEx) {
|
||||
const iconType = options.iconType || 'info';
|
||||
Reflect.deleteProperty(options, 'iconType');
|
||||
const opt: ModalFuncProps = {
|
||||
maskClosable: true,
|
||||
centered: true,
|
||||
icon: getIcon(iconType),
|
||||
...options,
|
||||
content: renderContent(options),
|
||||
};
|
||||
return Modal.confirm(opt);
|
||||
}
|
||||
|
||||
const getBaseOptions = () => {
|
||||
const { t } = useI18n();
|
||||
return {
|
||||
okText: t('common.okText'),
|
||||
centered: true,
|
||||
};
|
||||
};
|
||||
|
||||
function createModalOptions(options: ModalOptionsPartial, icon: string): ModalOptionsPartial | any {
|
||||
return {
|
||||
...getBaseOptions(),
|
||||
...options,
|
||||
content: renderContent(options),
|
||||
icon: getIcon(icon),
|
||||
};
|
||||
}
|
||||
|
||||
function createSuccessModal(options: ModalOptionsPartial) {
|
||||
return Modal.success(createModalOptions(options, 'success'));
|
||||
}
|
||||
|
||||
function createErrorModal(options: ModalOptionsPartial) {
|
||||
return Modal.error(createModalOptions(options, 'close'));
|
||||
}
|
||||
|
||||
function createInfoModal(options: ModalOptionsPartial) {
|
||||
return Modal.info(createModalOptions(options, 'info'));
|
||||
}
|
||||
|
||||
function createWarningModal(options: ModalOptionsPartial) {
|
||||
return Modal.warning(createModalOptions(options, 'warning'));
|
||||
}
|
||||
|
||||
function contains(str, searchs) {
|
||||
if (typeof str === 'object' && str.content) {
|
||||
str = str.content;
|
||||
}
|
||||
if (typeof str === 'object' && str.props?.innerHTML) {
|
||||
str = str.props?.innerHTML;
|
||||
}
|
||||
if (typeof str === 'string' && searchs) {
|
||||
const ss = searchs.split(',');
|
||||
for (let i = 0; i < ss.length; i++) {
|
||||
if (str.indexOf(ss[i]) >= 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function showMessageModal(options: ModalOptionsPartial | string, type?: string) {
|
||||
const { t } = useI18n();
|
||||
if (typeof options === 'string') options = { content: options };
|
||||
if (typeof options.content === 'string' && options.content.startsWith('posfull:')) {
|
||||
options.content = '<div class="modal-posfull-content">' + options.content.substring(8) + '</div>';
|
||||
options.width = '80%';
|
||||
}
|
||||
if (type === 'error' || contains(options.content, t('sys.message.error'))) {
|
||||
return Modal.error(createModalOptions(options, 'close'));
|
||||
} else if (type === 'warning' || contains(options.content, t('sys.message.warning'))) {
|
||||
return Modal.warning(createModalOptions(options, 'warning'));
|
||||
} else if (type === 'success' || contains(options.content, t('sys.message.success'))) {
|
||||
return Modal.success(createModalOptions(options, 'success'));
|
||||
} else {
|
||||
return Modal.info(createModalOptions(options, 'info'));
|
||||
}
|
||||
}
|
||||
|
||||
function showMessage(content: JointContent, type?: string, duration?: number, onClose?: ConfigOnClose) {
|
||||
const { t } = useI18n();
|
||||
let messageRemove = () => {};
|
||||
if (typeof content === 'string' && content.startsWith('posfull:')) {
|
||||
content = {
|
||||
content: (
|
||||
<div style="position: relative;" onMouseout={() => setTimeout(messageRemove, 1000)}>
|
||||
<div style="position: absolute; right: -6px; top: -20px;" onClick={() => messageRemove()}>
|
||||
<Icon icon="i-ant-design:close-outlined" color="#555" class="cursor-pointer" />
|
||||
</div>
|
||||
<div class="text-left" innerHTML={content.substring(8)}></div>
|
||||
</div>
|
||||
),
|
||||
duration,
|
||||
};
|
||||
}
|
||||
if (type === 'error' || contains(content, t('sys.message.error'))) {
|
||||
messageRemove = Message.error(content, duration, onClose);
|
||||
} else if (type === 'warning' || contains(content, t('sys.message.warning'))) {
|
||||
messageRemove = Message.warning(content, duration, onClose);
|
||||
} else if (type === 'success' || contains(content, t('sys.message.success'))) {
|
||||
messageRemove = Message.success(content, duration, onClose);
|
||||
} else {
|
||||
messageRemove = Message.info(content, duration, onClose);
|
||||
}
|
||||
return messageRemove;
|
||||
}
|
||||
|
||||
notification.config({
|
||||
placement: 'topRight',
|
||||
duration: 3,
|
||||
});
|
||||
|
||||
/**
|
||||
* @description: message
|
||||
*/
|
||||
export function useMessage() {
|
||||
return {
|
||||
createMessage: Message,
|
||||
notification: notification as NotifyApi,
|
||||
createConfirm: createConfirm,
|
||||
createSuccessModal,
|
||||
createErrorModal,
|
||||
createInfoModal,
|
||||
createWarningModal,
|
||||
showMessageModal,
|
||||
showMessage,
|
||||
};
|
||||
}
|
||||
74
web-vue/packages/core/hooks/web/usePage.ts
Normal file
74
web-vue/packages/core/hooks/web/usePage.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import type { RouteLocationRaw, Router } from 'vue-router';
|
||||
|
||||
import { PageEnum } from '@jeesite/core/enums/pageEnum';
|
||||
import { isString } from '@jeesite/core/utils/is';
|
||||
import { computed, unref } from 'vue';
|
||||
|
||||
import { useRouter } from 'vue-router';
|
||||
import { REDIRECT_NAME } from '@jeesite/core/router/constant';
|
||||
|
||||
export type RouteLocationRawEx = Omit<RouteLocationRaw, 'path'> & {
|
||||
path: PageEnum | string;
|
||||
query?: object;
|
||||
};
|
||||
|
||||
function handleError(e: Error) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
// page switch
|
||||
export function useGo(_router?: Router) {
|
||||
let router: any;
|
||||
if (!_router) {
|
||||
router = useRouter();
|
||||
}
|
||||
const { push, replace } = _router || router;
|
||||
async function go(opt: PageEnum | RouteLocationRawEx | string = PageEnum.BASE_HOME, isReplace = false) {
|
||||
if (!opt) {
|
||||
return;
|
||||
}
|
||||
if (isString(opt)) {
|
||||
isReplace ? await replace(opt).catch(handleError) : await push(opt).catch(handleError);
|
||||
} else {
|
||||
const o = opt as RouteLocationRaw;
|
||||
isReplace ? await replace(o).catch(handleError) : await push(o).catch(handleError);
|
||||
}
|
||||
}
|
||||
return go;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: redo current page
|
||||
*/
|
||||
export const useRedo = (_router?: Router) => {
|
||||
const { push, currentRoute } = _router || useRouter();
|
||||
const { query, params = {}, name, fullPath } = unref(currentRoute.value);
|
||||
function redo(): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
if (name === REDIRECT_NAME) {
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
if (name && Object.keys(params).length > 0) {
|
||||
params['_redirect_type'] = 'name';
|
||||
params['path'] = String(name);
|
||||
} else {
|
||||
params['_redirect_type'] = 'path';
|
||||
params['path'] = fullPath;
|
||||
}
|
||||
push({ name: REDIRECT_NAME, params, query }).then(() => resolve(true));
|
||||
});
|
||||
}
|
||||
return redo;
|
||||
};
|
||||
|
||||
export function useQuery(_router?: Router) {
|
||||
let router;
|
||||
if (!_router) {
|
||||
router = useRouter();
|
||||
}
|
||||
const query = computed(() => {
|
||||
return unref(router.currentRoute).query;
|
||||
});
|
||||
return query;
|
||||
}
|
||||
34
web-vue/packages/core/hooks/web/usePagination.ts
Normal file
34
web-vue/packages/core/hooks/web/usePagination.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import type { Ref } from 'vue';
|
||||
import { ref, unref, computed } from 'vue';
|
||||
|
||||
function pagination<T = any>(list: T[], pageNo: number, pageSize: number): T[] {
|
||||
const offset = (pageNo - 1) * Number(pageSize);
|
||||
const ret =
|
||||
offset + Number(pageSize) >= list.length
|
||||
? list.slice(offset, list.length)
|
||||
: list.slice(offset, offset + Number(pageSize));
|
||||
return ret;
|
||||
}
|
||||
|
||||
export function usePagination<T = any>(list: Ref<T[]>, pageSize: number) {
|
||||
const currentPage = ref(1);
|
||||
const pageSizeRef = ref(pageSize);
|
||||
|
||||
const getPaginationList = computed(() => {
|
||||
return pagination(unref(list), unref(currentPage), unref(pageSizeRef));
|
||||
});
|
||||
|
||||
const getTotal = computed(() => {
|
||||
return unref(list).length;
|
||||
});
|
||||
|
||||
function setCurrentPage(page: number) {
|
||||
currentPage.value = page;
|
||||
}
|
||||
|
||||
function setPageSize(pageSize: number) {
|
||||
pageSizeRef.value = pageSize;
|
||||
}
|
||||
|
||||
return { setCurrentPage, getTotal, setPageSize, getPaginationList };
|
||||
}
|
||||
139
web-vue/packages/core/hooks/web/usePermission.ts
Normal file
139
web-vue/packages/core/hooks/web/usePermission.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||
* No deletion without permission, or be held responsible to law.
|
||||
* @author Vben、ThinkGem
|
||||
*/
|
||||
import type { RouteRecordRaw } from 'vue-router';
|
||||
|
||||
import { useAppStore } from '@jeesite/core/store/modules/app';
|
||||
import { usePermissionStore } from '@jeesite/core/store/modules/permission';
|
||||
import { useUserStore } from '@jeesite/core/store/modules/user';
|
||||
|
||||
import { useTabs } from './useTabs';
|
||||
|
||||
import { router, resetRouter } from '@jeesite/core/router';
|
||||
// import { RootRoute } from '@jeesite/core/router/routes';
|
||||
|
||||
import projectSetting from '@jeesite/core/settings/projectSetting';
|
||||
import { PermissionModeEnum } from '@jeesite/core/enums/appEnum';
|
||||
import { RoleEnum } from '@jeesite/core/enums/roleEnum';
|
||||
|
||||
import { intersection } from 'lodash-es';
|
||||
import { isArray } from '@jeesite/core/utils/is';
|
||||
import { useMultipleTabStore } from '@jeesite/core/store/modules/multipleTab';
|
||||
|
||||
// User permissions related operations
|
||||
export function usePermission() {
|
||||
const userStore = useUserStore();
|
||||
const appStore = useAppStore();
|
||||
const permissionStore = usePermissionStore();
|
||||
const { closeAll } = useTabs(router);
|
||||
|
||||
/**
|
||||
* Change permission mode
|
||||
*/
|
||||
async function togglePermissionMode() {
|
||||
appStore.setProjectConfig({
|
||||
permissionMode:
|
||||
projectSetting.permissionMode === PermissionModeEnum.BACK
|
||||
? PermissionModeEnum.ROUTE_MAPPING
|
||||
: PermissionModeEnum.BACK,
|
||||
});
|
||||
location.reload();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset and regain authority resource information
|
||||
* @param id
|
||||
*/
|
||||
async function resume() {
|
||||
const tabStore = useMultipleTabStore();
|
||||
tabStore.clearCacheTabs();
|
||||
resetRouter();
|
||||
const routes = await permissionStore.buildRoutesAction();
|
||||
routes.forEach((route) => {
|
||||
router.addRoute(route as unknown as RouteRecordRaw);
|
||||
});
|
||||
permissionStore.setLastBuildMenuTime();
|
||||
closeAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether there is permission
|
||||
*/
|
||||
function hasPermission(value?: RoleEnum | RoleEnum[] | string | string[], def = true): boolean {
|
||||
// Open by default
|
||||
if (!value) {
|
||||
return def;
|
||||
}
|
||||
|
||||
const permMode = projectSetting.permissionMode;
|
||||
|
||||
if ([PermissionModeEnum.ROUTE_MAPPING, PermissionModeEnum.ROLE].includes(permMode)) {
|
||||
if (!isArray(value)) {
|
||||
return userStore.getRoleList?.includes(value as RoleEnum);
|
||||
}
|
||||
return (intersection(value, userStore.getRoleList) as RoleEnum[]).length > 0;
|
||||
}
|
||||
|
||||
if (PermissionModeEnum.BACK === permMode) {
|
||||
const permiCodeList = permissionStore.getPermCodeList;
|
||||
|
||||
// if (!isArray(value)) {
|
||||
// return permiCodeList.includes(value);
|
||||
// }
|
||||
// return (intersection(value, permiCodeList) as string[]).length > 0;
|
||||
|
||||
if (value) {
|
||||
const values = !isArray(value) ? [value] : value;
|
||||
for (const val of values) {
|
||||
if (val && val !== '') {
|
||||
const currPermi = val.split(':');
|
||||
for (const permi of permiCodeList) {
|
||||
if (isPermitted(permi, currPermi)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function isPermitted(permi: string[], currPermi: string[]) {
|
||||
for (const i in permi) {
|
||||
if (permi[i] !== currPermi[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change roles
|
||||
* @param roles
|
||||
*/
|
||||
async function changeRole(roles: RoleEnum | RoleEnum[]): Promise<void> {
|
||||
if (projectSetting.permissionMode !== PermissionModeEnum.ROUTE_MAPPING) {
|
||||
throw new Error('Please switch PermissionModeEnum to ROUTE_MAPPING mode in the configuration to operate!');
|
||||
}
|
||||
|
||||
if (!isArray(roles)) {
|
||||
roles = [roles];
|
||||
}
|
||||
userStore.setRoleList(roles);
|
||||
await resume();
|
||||
}
|
||||
|
||||
/**
|
||||
* refresh menu data
|
||||
*/
|
||||
async function refreshMenu() {
|
||||
resume();
|
||||
}
|
||||
|
||||
return { changeRole, hasPermission, togglePermissionMode, refreshMenu };
|
||||
}
|
||||
46
web-vue/packages/core/hooks/web/useScript.ts
Normal file
46
web-vue/packages/core/hooks/web/useScript.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { onMounted, onUnmounted, ref } from 'vue';
|
||||
|
||||
interface ScriptOptions {
|
||||
src: string;
|
||||
}
|
||||
|
||||
export function useScript(opts: ScriptOptions) {
|
||||
const isLoading = ref(false);
|
||||
const error = ref(false);
|
||||
const success = ref(false);
|
||||
let script: HTMLScriptElement;
|
||||
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
onMounted(() => {
|
||||
script = document.createElement('script');
|
||||
script.type = 'text/javascript';
|
||||
script.onload = function () {
|
||||
isLoading.value = false;
|
||||
success.value = true;
|
||||
error.value = false;
|
||||
resolve('');
|
||||
};
|
||||
|
||||
script.onerror = function (err) {
|
||||
isLoading.value = false;
|
||||
success.value = false;
|
||||
error.value = true;
|
||||
reject(err);
|
||||
};
|
||||
|
||||
script.src = opts.src;
|
||||
document.head.appendChild(script);
|
||||
});
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
script && script.remove();
|
||||
});
|
||||
|
||||
return {
|
||||
isLoading,
|
||||
error,
|
||||
success,
|
||||
toPromise: () => promise,
|
||||
};
|
||||
}
|
||||
21
web-vue/packages/core/hooks/web/useSortable.ts
Normal file
21
web-vue/packages/core/hooks/web/useSortable.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { nextTick, unref } from 'vue';
|
||||
import type { Ref } from 'vue';
|
||||
import type { Options } from 'sortablejs';
|
||||
|
||||
export function useSortable(el: HTMLElement | Ref<HTMLElement>, options?: Options) {
|
||||
function initSortable() {
|
||||
nextTick(async () => {
|
||||
if (!el) return;
|
||||
|
||||
const Sortable = (await import('sortablejs')).default;
|
||||
Sortable.create(unref(el), {
|
||||
animation: 500,
|
||||
delay: 400,
|
||||
delayOnTouchOnly: true,
|
||||
...options,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return { initSortable };
|
||||
}
|
||||
106
web-vue/packages/core/hooks/web/useTabs.ts
Normal file
106
web-vue/packages/core/hooks/web/useTabs.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import type { RouteLocationNormalized, Router } from 'vue-router';
|
||||
|
||||
import { useRouter } from 'vue-router';
|
||||
import { unref } from 'vue';
|
||||
|
||||
import { useMultipleTabStore } from '@jeesite/core/store/modules/multipleTab';
|
||||
import { useAppStore } from '@jeesite/core/store/modules/app';
|
||||
|
||||
enum TableActionEnum {
|
||||
REFRESH,
|
||||
CLOSE_ALL,
|
||||
CLOSE_LEFT,
|
||||
CLOSE_RIGHT,
|
||||
CLOSE_OTHER,
|
||||
CLOSE_CURRENT,
|
||||
CLOSE,
|
||||
}
|
||||
|
||||
export function useTabs(_router?: Router) {
|
||||
const appStore = useAppStore();
|
||||
|
||||
function canIUseTabs(): boolean {
|
||||
const { show } = appStore.getMultiTabsSetting;
|
||||
if (!show) {
|
||||
throw new Error('The multi-tab page is currently not open, please open it in the settings!');
|
||||
}
|
||||
return !!show;
|
||||
}
|
||||
|
||||
const tabStore = useMultipleTabStore();
|
||||
const router = _router || useRouter();
|
||||
|
||||
const { currentRoute } = router;
|
||||
|
||||
function getCurrentTab() {
|
||||
const route = unref(currentRoute);
|
||||
return tabStore.getTabList.find((item) => {
|
||||
return (item.fullPath || item.path) === route.fullPath;
|
||||
})!;
|
||||
}
|
||||
|
||||
async function updateTabTitle(title: string, tab?: RouteLocationNormalized) {
|
||||
const canIUse = canIUseTabs;
|
||||
if (!canIUse) {
|
||||
return;
|
||||
}
|
||||
const targetTab = tab || getCurrentTab();
|
||||
await tabStore.setTabTitle(title, targetTab);
|
||||
}
|
||||
|
||||
async function updateTabPath(path: string, tab?: RouteLocationNormalized) {
|
||||
const canIUse = canIUseTabs;
|
||||
if (!canIUse) {
|
||||
return;
|
||||
}
|
||||
const targetTab = tab || getCurrentTab();
|
||||
await tabStore.updateTabPath(path, targetTab);
|
||||
}
|
||||
|
||||
async function handleTabAction(action: TableActionEnum, tab?: RouteLocationNormalized) {
|
||||
const canIUse = canIUseTabs;
|
||||
if (!canIUse) {
|
||||
return;
|
||||
}
|
||||
const currentTab = getCurrentTab();
|
||||
switch (action) {
|
||||
case TableActionEnum.REFRESH:
|
||||
await tabStore.refreshPage(router);
|
||||
break;
|
||||
|
||||
case TableActionEnum.CLOSE_ALL:
|
||||
await tabStore.closeAllTab(router);
|
||||
break;
|
||||
|
||||
case TableActionEnum.CLOSE_LEFT:
|
||||
await tabStore.closeLeftTabs(currentTab, router);
|
||||
break;
|
||||
|
||||
case TableActionEnum.CLOSE_RIGHT:
|
||||
await tabStore.closeRightTabs(currentTab, router);
|
||||
break;
|
||||
|
||||
case TableActionEnum.CLOSE_OTHER:
|
||||
await tabStore.closeOtherTabs(currentTab, router);
|
||||
break;
|
||||
|
||||
case TableActionEnum.CLOSE_CURRENT:
|
||||
case TableActionEnum.CLOSE:
|
||||
await tabStore.closeTab(tab || currentTab, router);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
tabStore,
|
||||
refreshPage: () => handleTabAction(TableActionEnum.REFRESH),
|
||||
closeAll: () => handleTabAction(TableActionEnum.CLOSE_ALL),
|
||||
closeLeft: () => handleTabAction(TableActionEnum.CLOSE_LEFT),
|
||||
closeRight: () => handleTabAction(TableActionEnum.CLOSE_RIGHT),
|
||||
closeOther: () => handleTabAction(TableActionEnum.CLOSE_OTHER),
|
||||
closeCurrent: () => handleTabAction(TableActionEnum.CLOSE_CURRENT),
|
||||
close: (tab?: RouteLocationNormalized) => handleTabAction(TableActionEnum.CLOSE, tab),
|
||||
setTitle: (title: string, tab?: RouteLocationNormalized) => updateTabTitle(title, tab),
|
||||
updatePath: (fullPath: string, tab?: RouteLocationNormalized) => updateTabPath(fullPath, tab),
|
||||
};
|
||||
}
|
||||
38
web-vue/packages/core/hooks/web/useTitle.ts
Normal file
38
web-vue/packages/core/hooks/web/useTitle.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||
* No deletion without permission, or be held responsible to law.
|
||||
* @author ThinkGem
|
||||
*/
|
||||
import { watch, unref } from 'vue';
|
||||
import { useI18n } from '@jeesite/core/hooks/web/useI18n';
|
||||
import { useTitle as usePageTitle } from '@vueuse/core';
|
||||
import { useGlobSetting } from '@jeesite/core/hooks/setting';
|
||||
import { useRouter } from 'vue-router';
|
||||
|
||||
import { REDIRECT_NAME } from '@jeesite/core/router/constant';
|
||||
|
||||
/**
|
||||
* Listening to page changes and dynamically changing site titles
|
||||
*/
|
||||
export function useTitle() {
|
||||
const { title } = useGlobSetting();
|
||||
const { t } = useI18n();
|
||||
const { currentRoute } = useRouter();
|
||||
|
||||
const pageTitle = usePageTitle();
|
||||
|
||||
watch(
|
||||
() => currentRoute.value.path,
|
||||
() => {
|
||||
const route = unref(currentRoute);
|
||||
|
||||
if (route.name === REDIRECT_NAME) {
|
||||
return;
|
||||
}
|
||||
|
||||
const tTitle = t(route?.meta?.title as string);
|
||||
pageTitle.value = tTitle ? ` ${tTitle} - ${title} ` : `${title}`;
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
}
|
||||
205
web-vue/packages/core/hooks/web/useWatermark.ts
Normal file
205
web-vue/packages/core/hooks/web/useWatermark.ts
Normal file
@@ -0,0 +1,205 @@
|
||||
import { getCurrentInstance, onBeforeUnmount, ref, Ref, shallowRef, unref } from 'vue';
|
||||
import { useRafThrottle } from '@jeesite/core/utils/domUtils';
|
||||
import { addResizeListener, removeResizeListener } from '@jeesite/core/utils/event';
|
||||
import { isDef } from '@jeesite/core/utils/is';
|
||||
|
||||
const watermarkSymbol = 'watermark-dom';
|
||||
const updateWatermarkText = ref<string | null>(null);
|
||||
|
||||
type UseWatermarkRes = {
|
||||
setWatermark: (str: string) => void;
|
||||
clear: () => void;
|
||||
clearAll: () => void;
|
||||
waterMarkOptions?: waterMarkOptionsType;
|
||||
obInstance?: MutationObserver;
|
||||
targetElement?: HTMLElement;
|
||||
parentElement?: HTMLElement;
|
||||
};
|
||||
|
||||
type waterMarkOptionsType = {
|
||||
// 自定义水印的文字大小
|
||||
fontSize?: number;
|
||||
// 自定义水印的文字颜色
|
||||
fontColor?: string;
|
||||
// 自定义水印的文字字体
|
||||
fontFamily?: string;
|
||||
// 自定义水印的文字对齐方式
|
||||
textAlign?: CanvasTextAlign;
|
||||
// 自定义水印的文字基线
|
||||
textBaseline?: CanvasTextBaseline;
|
||||
// 自定义水印的文字倾斜角度
|
||||
rotate?: number;
|
||||
};
|
||||
|
||||
const sourceMap = new Map<symbol, Omit<UseWatermarkRes, 'clearAll'>>();
|
||||
|
||||
function findTargetNode(el) {
|
||||
return Array.from(sourceMap.values()).find((item) => item.targetElement === el);
|
||||
}
|
||||
|
||||
function createBase64(str: string, waterMarkOptions: waterMarkOptionsType) {
|
||||
const can = document.createElement('canvas');
|
||||
const width = 300;
|
||||
const height = 240;
|
||||
Object.assign(can, { width, height });
|
||||
|
||||
const cans = can.getContext('2d');
|
||||
if (cans) {
|
||||
const fontFamily = waterMarkOptions?.fontFamily || 'Vedana';
|
||||
const fontSize = waterMarkOptions?.fontSize || 15;
|
||||
const fontColor = waterMarkOptions?.fontColor || 'rgba(0, 0, 0, 0.15)';
|
||||
const textAlign = waterMarkOptions?.textAlign || 'left';
|
||||
const textBaseline = waterMarkOptions?.textBaseline || 'middle';
|
||||
const rotate = waterMarkOptions?.rotate || 20;
|
||||
cans.rotate((-rotate * Math.PI) / 180);
|
||||
cans.font = `${fontSize}px ${fontFamily}`;
|
||||
cans.fillStyle = fontColor;
|
||||
cans.textAlign = textAlign;
|
||||
cans.textBaseline = textBaseline;
|
||||
cans.fillText(str, width / 20, height);
|
||||
}
|
||||
return can.toDataURL('image/png');
|
||||
}
|
||||
const resetWatermarkStyle = (element: HTMLElement, watermarkText: string, waterMarkOptions: waterMarkOptionsType) => {
|
||||
element.className = '__' + watermarkSymbol;
|
||||
element.style.pointerEvents = 'none';
|
||||
element.style.display = 'block';
|
||||
element.style.visibility = 'visible';
|
||||
element.style.top = '0px';
|
||||
element.style.left = '0px';
|
||||
element.style.position = 'absolute';
|
||||
element.style.zIndex = '100000';
|
||||
element.style.height = '100%';
|
||||
element.style.width = '100%';
|
||||
element.style.background = `url(${createBase64(
|
||||
unref(updateWatermarkText) || watermarkText,
|
||||
waterMarkOptions,
|
||||
)}) left top repeat`;
|
||||
};
|
||||
|
||||
const obFn = () => {
|
||||
const obInstance = new MutationObserver((mutationRecords) => {
|
||||
for (const mutation of mutationRecords) {
|
||||
for (const node of Array.from(mutation.removedNodes)) {
|
||||
const target = findTargetNode(node);
|
||||
if (!target) return;
|
||||
const { targetElement, parentElement } = target;
|
||||
// 父元素的子元素水印如果被删除 重新插入被删除的水印(防篡改,插入通过控制台删除的水印)
|
||||
if (!parentElement?.contains(targetElement as Node | null)) {
|
||||
target?.parentElement?.appendChild(node as HTMLElement);
|
||||
}
|
||||
}
|
||||
if (mutation.type === 'attributes' && mutation.target) {
|
||||
// 修复控制台可以”Hide element” 的问题
|
||||
const _target = mutation.target as HTMLElement;
|
||||
const target = findTargetNode(_target);
|
||||
if (target) {
|
||||
// 禁止改属性 包括class 修改以后 mutation.type 也等于 'attributes'
|
||||
// 先解除监听 再加一下
|
||||
clearAll();
|
||||
target.setWatermark(target.targetElement?.['data-watermark-text']);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return obInstance;
|
||||
};
|
||||
|
||||
export function useWatermark(
|
||||
appendEl: Ref<HTMLElement | null> = ref(document.body) as Ref<HTMLElement>,
|
||||
waterMarkOptions: waterMarkOptionsType = {},
|
||||
): UseWatermarkRes {
|
||||
const domSymbol = Symbol(watermarkSymbol);
|
||||
const appendElRaw = unref(appendEl);
|
||||
if (appendElRaw && sourceMap.has(domSymbol)) {
|
||||
const { setWatermark, clear } = sourceMap.get(domSymbol) as UseWatermarkRes;
|
||||
return { setWatermark, clear, clearAll };
|
||||
}
|
||||
const func = useRafThrottle(function () {
|
||||
const el = unref(appendEl);
|
||||
if (!el) return;
|
||||
const { clientHeight: height, clientWidth: width } = el;
|
||||
updateWatermark({ height, width });
|
||||
});
|
||||
const watermarkEl = shallowRef<HTMLElement>();
|
||||
const clear = () => {
|
||||
const domId = unref(watermarkEl);
|
||||
watermarkEl.value = undefined;
|
||||
const el = unref(appendEl);
|
||||
sourceMap.has(domSymbol) && sourceMap.get(domSymbol)?.obInstance?.disconnect();
|
||||
sourceMap.delete(domSymbol);
|
||||
if (!el) return;
|
||||
domId && el.removeChild(domId);
|
||||
removeResizeListener(el, func);
|
||||
};
|
||||
|
||||
function updateWatermark(
|
||||
options: {
|
||||
width?: number;
|
||||
height?: number;
|
||||
str?: string;
|
||||
} = {},
|
||||
) {
|
||||
const el = unref(watermarkEl);
|
||||
if (!el) return;
|
||||
if (isDef(options.width)) {
|
||||
el.style.width = `${options.width}px`;
|
||||
}
|
||||
if (isDef(options.height)) {
|
||||
el.style.height = `${options.height}px`;
|
||||
}
|
||||
if (isDef(options.str)) {
|
||||
el.style.background = `url(${createBase64(options.str, waterMarkOptions)}) left top repeat`;
|
||||
}
|
||||
}
|
||||
|
||||
const createWatermark = (str: string) => {
|
||||
if (unref(watermarkEl) && sourceMap.has(domSymbol)) {
|
||||
updateWatermarkText.value = str;
|
||||
updateWatermark({ str });
|
||||
return;
|
||||
}
|
||||
const div = document.createElement('div');
|
||||
div['data-watermark-text'] = str; //自定义属性 用于恢复水印
|
||||
updateWatermarkText.value = str;
|
||||
watermarkEl.value = div;
|
||||
resetWatermarkStyle(div, str, waterMarkOptions);
|
||||
const el = unref(appendEl);
|
||||
if (!el) return;
|
||||
const { clientHeight: height, clientWidth: width } = el;
|
||||
updateWatermark({ str, width, height });
|
||||
el.appendChild(div);
|
||||
sourceMap.set(domSymbol, {
|
||||
setWatermark,
|
||||
clear,
|
||||
parentElement: el,
|
||||
targetElement: div,
|
||||
obInstance: obFn(),
|
||||
waterMarkOptions,
|
||||
});
|
||||
sourceMap.get(domSymbol)?.obInstance?.observe(el, {
|
||||
childList: true, // 子节点的变动(指新增,删除或者更改)
|
||||
subtree: true, // 该观察器应用于该节点的所有后代节点
|
||||
attributes: true, // 属性的变动
|
||||
});
|
||||
};
|
||||
|
||||
function setWatermark(str: string) {
|
||||
createWatermark(str);
|
||||
addResizeListener(document.documentElement, func);
|
||||
const instance = getCurrentInstance();
|
||||
if (instance) {
|
||||
onBeforeUnmount(() => {
|
||||
clear();
|
||||
});
|
||||
}
|
||||
}
|
||||
return { setWatermark, clear, clearAll };
|
||||
}
|
||||
|
||||
function clearAll() {
|
||||
Array.from(sourceMap.values()).forEach((item) => {
|
||||
item?.obInstance?.disconnect();
|
||||
item.clear();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user