diff --git a/src/component/toolbox/featureManager.ts b/src/component/toolbox/featureManager.ts index 65b0917bc9..f7d1aa9a3f 100644 --- a/src/component/toolbox/featureManager.ts +++ b/src/component/toolbox/featureManager.ts @@ -102,7 +102,7 @@ export interface UserDefinedToolboxFeature { onclick(): void } -type ToolboxFeatureCtor = { +export type ToolboxFeatureCtor = { new(): ToolboxFeature /** * Static defaultOption property diff --git a/src/export/core.ts b/src/export/core.ts index 7d42fe1abf..455b021536 100644 --- a/src/export/core.ts +++ b/src/export/core.ts @@ -23,6 +23,18 @@ export * from '../core/echarts'; export * from './api'; import { use } from '../extension'; +export type { + EChartsExtension, + EChartsExtensionInstaller, + EChartsExtensionInstallRegisters +} from '../extension'; +export { + ToolboxFeature, + ToolboxFeatureOption, + ToolboxFeatureModel, + ToolboxFeatureCtor +} from '../component/toolbox/featureManager'; + // Import label layout by default. // TODO will be treeshaked. import { installLabelLayout } from '../label/installLabelLayout'; diff --git a/src/extension.ts b/src/extension.ts index d9794d2c2a..3027ef7e5b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -43,6 +43,7 @@ import { registerImpl } from './core/impl'; import { registerPainter } from 'zrender/src/zrender'; import { CustomSeriesRenderItem } from './chart/custom/CustomSeries'; import { registerCustomSeries } from './chart/custom/customSeriesRegister'; +import { registerFeature as registerToolboxFeature } from './component/toolbox/featureManager'; const extensions: (EChartsExtensionInstaller | EChartsExtension)[] = []; @@ -60,6 +61,7 @@ const extensionRegisters = { registerLoading, registerMap, registerImpl, + registerToolboxFeature, PRIORITY, diff --git a/test/types/esm/importPartial.ts b/test/types/esm/importPartial.ts index 57e42f286b..b56c1a4357 100644 --- a/test/types/esm/importPartial.ts +++ b/test/types/esm/importPartial.ts @@ -17,7 +17,14 @@ * under the License. */ -import {init, use, ComposeOption} from 'echarts/core'; +import { + init, + use, + ComposeOption, + EChartsExtensionInstaller, + ToolboxFeature, + ToolboxFeatureOption +} from 'echarts/core'; import { BarChart, BarSeriesOption, @@ -36,6 +43,23 @@ import { use([BarChart, LineChart, GridComponent, DataZoomComponent, CanvasRenderer]); +interface ExportDataFeatureOption extends ToolboxFeatureOption { + filename?: string +} + +class ExportDataFeature extends ToolboxFeature { + onclick(): void { + this.api.getOption(); + this.model.get('filename'); + } +} + +const installExportData: EChartsExtensionInstaller = function (registers) { + registers.registerToolboxFeature('exportData', ExportDataFeature); +}; + +use(installExportData); + type Option = ComposeOption< GridComponentOption | DataZoomComponentOption @@ -61,4 +85,4 @@ const dom = document.createElement('div'); dom.className = 'chart'; const chart = init(dom); -chart.setOption(option); \ No newline at end of file +chart.setOption(option); diff --git a/test/ut/spec/component/toolbox/featureExtension.test.ts b/test/ut/spec/component/toolbox/featureExtension.test.ts new file mode 100644 index 0000000000..e71b573486 --- /dev/null +++ b/test/ut/spec/component/toolbox/featureExtension.test.ts @@ -0,0 +1,46 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ + +import { + EChartsExtensionInstaller, + ToolboxFeature, + ToolboxFeatureOption, + use +} from '@/src/export/core'; +import { getFeature } from '@/src/component/toolbox/featureManager'; + +interface ExportDataFeatureOption extends ToolboxFeatureOption { + filename?: string +} + +class ExportDataFeature extends ToolboxFeature { + onclick(): void {} +} + +describe('toolbox feature extension', function () { + it('registers a toolbox feature through the extension API', function () { + const install: EChartsExtensionInstaller = function (registers) { + registers.registerToolboxFeature('exportData', ExportDataFeature); + }; + + use(install); + + expect(getFeature('exportData')).toBe(ExportDataFeature); + }); +});