chore: 调整组件导出方式

This commit is contained in:
dap 2024-09-12 08:31:47 +08:00
parent f2c627b51e
commit 4d67c99f2b
6 changed files with 6 additions and 206 deletions

View File

@ -1,8 +1,4 @@
import { withInstall } from '#/utils';
import cropperImage from './src/cropper.vue';
import avatarCropper from './src/cropper-avatar.vue';
export { default as CropperImage } from './src/cropper.vue';
export { default as CropperAvatar } from './src/cropper-avatar.vue';
export type { Cropper } from './src/typing';
export const CropperImage = withInstall(cropperImage);
export const CropperAvatar = withInstall(avatarCropper);

View File

@ -1,7 +1,3 @@
import { withInstall } from '#/utils';
import description from './src/description.vue';
export { default as Description } from './src/description.vue';
export * from './src/typing';
export { useDescription } from './src/useDescription';
export const Description = withInstall(description);

View File

@ -1,5 +1 @@
import { withInstall } from '#/utils';
import dictTag from './src/index.vue';
export const DictTag = withInstall(dictTag);
export { default as DictTag } from './src/index.vue';

View File

@ -1,5 +1 @@
import { withInstall } from '#/utils';
import tenantToggle from './src/index.vue';
export const TenantToggle = withInstall(tenantToggle);
export { default as TenantToggle } from './src/index.vue';

View File

@ -1,5 +1 @@
import { withInstall } from '#/utils';
import tinymce from './src/editor.vue';
export const Tinymce = withInstall(tinymce);
export { default as Tinymce } from './src/editor.vue';

View File

@ -1,180 +0,0 @@
import type {
RouteLocationNormalized,
RouteRecordNormalized,
} from 'vue-router';
import type { App, Component } from 'vue';
import { unref } from 'vue';
import {
intersectionWith,
isArray,
isEqual,
isObject,
mergeWith,
unionWith,
} from 'lodash-es';
export const noop = () => {};
/**
* @description: Set ui mount node
*/
export function getPopupContainer(node?: HTMLElement): HTMLElement {
return (node?.parentNode as HTMLElement) ?? document.body;
}
/**
* Add the object as a parameter to the URL
* @param baseUrl url
* @param obj
* @returns {string}
* eg:
* let obj = {a: '3', b: '4'}
* setObjToUrlParams('www.baidu.com', obj)
* ==>www.baidu.com?a=3&b=4
*/
export function setObjToUrlParams(baseUrl: string, obj: any): string {
let parameters = '';
for (const key in obj) {
parameters += `${key}=${encodeURIComponent(obj[key])}&`;
}
parameters = parameters.replace(/&$/, '');
return /\?$/.test(baseUrl)
? baseUrl + parameters
: baseUrl.replace(/\/?$/, '?') + parameters;
}
/**
* Recursively merge two objects.
*
*
* @param source The source object to merge from.
* @param target The target object to merge into.
* @param mergeArrays How to merge arrays. Default is "replace".
* replace
* - "union": Union the arrays.
* - "intersection": Intersect the arrays.
* - "concat": Concatenate the arrays.
* - "replace": Replace the source array with the target array.
* @returns The merged object.
*/
export function deepMerge<
T extends null | object | undefined,
U extends null | object | undefined,
>(
source: T,
target: U,
mergeArrays: 'concat' | 'intersection' | 'replace' | 'union' = 'replace',
): T & U {
if (!target) {
return source as T & U;
}
if (!source) {
return target as T & U;
}
return mergeWith({}, source, target, (sourceValue, targetValue) => {
if (isArray(targetValue) && isArray(sourceValue)) {
switch (mergeArrays) {
case 'concat': {
return [...sourceValue, ...targetValue];
}
case 'intersection': {
return intersectionWith(sourceValue, targetValue, isEqual);
}
case 'replace': {
return targetValue;
}
case 'union': {
return unionWith(sourceValue, targetValue, isEqual);
}
default: {
throw new Error(
`Unknown merge array strategy: ${mergeArrays as string}`,
);
}
}
}
if (isObject(targetValue) && isObject(sourceValue)) {
return deepMerge(sourceValue, targetValue, mergeArrays);
}
});
}
export function openWindow(
url: string,
opt?: {
noopener?: boolean;
noreferrer?: boolean;
target?: '_blank' | '_self' | string;
},
) {
const { noopener = true, noreferrer = true, target = '__blank' } = opt || {};
const feature: string[] = [];
noopener && feature.push('noopener=yes');
noreferrer && feature.push('noreferrer=yes');
window.open(url, target, feature.join(','));
}
// dynamic use hook props
export function getDynamicProps<T extends Record<string, unknown>, U>(
props: T,
): Partial<U> {
const ret: Record<string, any> = {};
Object.keys(props).forEach((key) => {
ret[key] = unref((props as Record<string, any>)[key]);
});
return ret as Partial<U>;
}
export function getRawRoute(
route: RouteLocationNormalized,
): RouteLocationNormalized {
if (!route) return route;
const { matched, ...opt } = route;
return {
...opt,
matched: (matched
? matched.map((item) => ({
meta: item.meta,
name: item.name,
path: item.path,
}))
: undefined) as RouteRecordNormalized[],
};
}
// https://github.com/vant-ui/vant/issues/8302
interface EventShim {
new (...args: any[]): {
$props: {
onClick?: (...args: any[]) => void;
};
};
}
export type WithInstall<T> = {
install(app: App): void;
} & EventShim &
T;
export type CustomComponent = { displayName?: string } & Component;
export const withInstall = <T extends CustomComponent>(
component: T,
alias?: string,
) => {
(component as Record<string, unknown>).install = (app: App) => {
const compName = component.name || component.displayName;
if (!compName) return;
app.component(compName, component);
if (alias) {
app.config.globalProperties[alias] = component;
}
};
return component as WithInstall<T>;
};