loading函数式调用

This commit is contained in:
目棃
2024-11-15 16:38:39 +08:00
parent 36b0d198a9
commit c87ec77543
4 changed files with 294 additions and 38 deletions

View File

@@ -0,0 +1,73 @@
/**
* @file component/func/loading.ts
* @description loading 组件封装,函数式调用
* @since Beta v0.6.3
*/
import type { ComponentInternalInstance, VNode } from "vue";
import { h, render } from "vue";
import loading from "./loading.vue";
const loadingId = "tg-func-loading";
export type LoadingParams = { show: boolean; title: string; subtitle?: string; empty?: boolean };
/**
* @description 自定义 loading 组件
* @since Beta v0.6.3
* @return LoadingInstance
*/
interface LoadingInstance extends ComponentInternalInstance {
exposeProxy: {
displayBox: (props: LoadingParams) => void;
};
}
function renderBox(props: LoadingParams): VNode {
const container = document.createElement("div");
container.id = loadingId;
const boxVNode: VNode = h(loading, props);
render(boxVNode, container);
document.body.appendChild(container);
return boxVNode;
}
let loadingInstance: VNode;
function showLoadingFull(show: boolean, title: string, sub: string, empty: boolean): void {
const params: LoadingParams = { show: show, title: title, subtitle: sub, empty: empty };
if (loadingInstance !== undefined) {
const boxVue = <LoadingInstance>loadingInstance.component;
return boxVue.exposeProxy.displayBox(params);
} else {
loadingInstance = renderBox(params);
return showLoadingFull(show, title, sub, empty);
}
}
function showLoadingStart(title: string, sub?: string): void {
showLoadingFull(true, title, sub ?? "", false);
}
function showLoadingUpdate(title: string, sub?: string, empty?: boolean): void {
showLoadingFull(true, title, sub ?? "", empty ?? false);
}
function showLoadingEmpty(title: string, sub?: string): void {
showLoadingFull(true, title, sub ?? "", true);
}
function showLoadingEnd(): void {
showLoadingFull(false, "", "", false);
}
const showLoading = {
_: showLoadingFull,
start: showLoadingStart,
update: showLoadingUpdate,
empty: showLoadingEmpty,
end: showLoadingEnd,
};
export default showLoading;