mirror of
https://github.com/ecomfe/vue-echarts.git
synced 2025-08-16 12:52:48 +08:00

* build: generate d.ts * fix: preserve PublicMethods * fix: avoid exposing types of attrs * refactor: use existing setoption type * fix: expose root and chart * feat: use symbol as injection key * chore: add comment for the type casting of the exposed
51 lines
1.1 KiB
TypeScript
51 lines
1.1 KiB
TypeScript
import type { Ref } from "vue";
|
|
import type { EChartsType } from "../types";
|
|
|
|
const METHOD_NAMES = [
|
|
"getWidth",
|
|
"getHeight",
|
|
"getDom",
|
|
"getOption",
|
|
"resize",
|
|
"dispatchAction",
|
|
"convertToPixel",
|
|
"convertFromPixel",
|
|
"containPixel",
|
|
"getDataURL",
|
|
"getConnectedDataURL",
|
|
"appendData",
|
|
"clear",
|
|
"isDisposed",
|
|
"dispose",
|
|
] as const;
|
|
|
|
type MethodName = (typeof METHOD_NAMES)[number];
|
|
|
|
export type PublicMethods = Pick<EChartsType, MethodName>;
|
|
|
|
export function usePublicAPI(
|
|
chart: Ref<EChartsType | undefined>,
|
|
): PublicMethods {
|
|
function makePublicMethod<T extends MethodName>(
|
|
name: T,
|
|
): (...args: Parameters<EChartsType[T]>) => ReturnType<EChartsType[T]> {
|
|
return (...args) => {
|
|
if (!chart.value) {
|
|
throw new Error("ECharts is not initialized yet.");
|
|
}
|
|
return (chart.value[name] as any).apply(chart.value, args);
|
|
};
|
|
}
|
|
|
|
function makePublicMethods(): PublicMethods {
|
|
const methods = Object.create(null);
|
|
METHOD_NAMES.forEach((name) => {
|
|
methods[name] = makePublicMethod(name);
|
|
});
|
|
|
|
return methods as PublicMethods;
|
|
}
|
|
|
|
return makePublicMethods();
|
|
}
|