Skip to content

Commit cab2a9a

Browse files
committed
further split
1 parent 78d95ea commit cab2a9a

File tree

20 files changed

+127
-70
lines changed

20 files changed

+127
-70
lines changed

chartlets.js/package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,18 @@
2828
"license": "MIT",
2929
"types": "./dist/index.d.ts",
3030
"module": "./dist/chartlets.js",
31-
"main": "./dist/chartlets.umd.cjs",
3231
"exports": {
3332
".": {
3433
"types": "./dist/index.d.ts",
35-
"module": "./dist/chartlets.js",
36-
"require": "./dist/chartlets.umd.cjs"
34+
"module": "./dist/chartlets.js"
35+
},
36+
"./plugins/mui": {
37+
"types": "./dist/mui-plugin.d.ts",
38+
"module": "./dist/mui-plugin.js"
39+
},
40+
"./plugins/vega": {
41+
"types": "./dist/vega-plugin.d.ts",
42+
"module": "./dist/vega-plugin.js"
3743
}
3844
},
3945
"scripts": {
@@ -53,6 +59,7 @@
5359
"react": ">=18",
5460
"react-dom": ">=18",
5561
"react-vega": "^7.6.0",
62+
"vega-themes": "^2.15.0",
5663
"zustand": "^5.0.0"
5764
},
5865
"peerDependencies": {

chartlets.js/src/demo/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import PanelsControl from "./components/PanelsControl";
1212
import PanelsRow from "./components/PanelsRow";
1313

1414
initializeContributions({
15-
plugins: [mui, vega],
15+
plugins: [mui(), vega()],
1616
hostStore: {
1717
// Let Chartlets listen to changes in application state.
1818
subscribe: (listener: () => void) => appStore.subscribe(listener),
Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { store } from "@/lib/store";
2-
import type { FrameworkOptions } from "@/lib/types/state/options";
3-
import { configureLogging } from "@/lib/actions/helpers/configureLogging";
2+
import type {
3+
ComponentRegistration,
4+
FrameworkOptions,
5+
PluginLike,
6+
} from "@/lib/types/state/options";
7+
import { registry } from "@/lib/component/Registry";
8+
import { isPromise } from "@/lib/utils/isPromise";
9+
import { isFunction } from "@/lib/utils/isFunction";
10+
import { isObject } from "@/lib/utils/isObject";
411
import { handleHostStoreChange } from "./handleHostStoreChange";
12+
import { configureLogging } from "./helpers/configureLogging";
513

614
export function configureFramework(options: FrameworkOptions) {
715
if (options.logging) {
@@ -14,8 +22,20 @@ export function configureFramework(options: FrameworkOptions) {
1422
configuration: { ...options } as FrameworkOptions,
1523
});
1624
if (options.plugins) {
17-
options.plugins.forEach((initializePlugin) => {
18-
initializePlugin();
19-
});
25+
options.plugins.forEach(resolvePlugin);
26+
}
27+
}
28+
29+
function resolvePlugin(plugin: PluginLike) {
30+
if (isPromise<PluginLike>(plugin)) {
31+
plugin.then(resolvePlugin);
32+
} else if (isFunction(plugin)) {
33+
resolvePlugin(plugin());
34+
} else if (isObject(plugin) && plugin.components) {
35+
(plugin.components as ComponentRegistration[]).forEach(
36+
([name, component]) => {
37+
registry.register(name, component);
38+
},
39+
);
2040
}
2141
}

chartlets.js/src/lib/component/Registry.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { FC } from "react";
1+
import type { ComponentType } from "react";
2+
23
import type { ComponentProps } from "@/lib/component/Component";
34

45
/**
@@ -22,14 +23,14 @@ export interface Registry {
2223
* @param type The Chartlets component's unique type name.
2324
* @param component A functional React component.
2425
*/
25-
register(type: string, component: FC<ComponentProps>): () => void;
26+
register(type: string, component: ComponentType<ComponentProps>): () => void;
2627

2728
/**
2829
* Lookup the component of the provided type.
2930
*
3031
* @param type The Chartlets component's type name.
3132
*/
32-
lookup(type: string): FC<ComponentProps> | undefined;
33+
lookup(type: string): ComponentType<ComponentProps> | undefined;
3334

3435
/**
3536
* Get the type names of all registered components.
@@ -39,9 +40,9 @@ export interface Registry {
3940

4041
// export for testing only
4142
export class RegistryImpl implements Registry {
42-
private components = new Map<string, FC<ComponentProps>>();
43+
private components = new Map<string, ComponentType<ComponentProps>>();
4344

44-
register(type: string, component: FC<ComponentProps>): () => void {
45+
register(type: string, component: ComponentType<ComponentProps>): () => void {
4546
const oldComponent = this.components.get(type);
4647
this.components.set(type, component);
4748
return () => {
@@ -53,7 +54,7 @@ export class RegistryImpl implements Registry {
5354
};
5455
}
5556

56-
lookup(type: string): FC<ComponentProps> | undefined {
57+
lookup(type: string): ComponentType<ComponentProps> | undefined {
5758
return this.components.get(type);
5859
}
5960

@@ -63,7 +64,7 @@ export class RegistryImpl implements Registry {
6364
}
6465

6566
/**
66-
* The Chartly component registry.
67+
* The Chartlets component registry.
6768
*
6869
* Use `registry.register("C", C)` to register your own component `C`.
6970
*

chartlets.js/src/lib/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ export { initializeContributions } from "@/lib/actions/initializeContributions";
1414
export { handleComponentChange } from "@/lib/actions/handleComponentChange";
1515
export { updateContributionContainer } from "@/lib/actions/updateContributionContainer";
1616
// Component registry
17-
export {
18-
type Registry,
19-
registry as componentRegistry,
20-
} from "@/lib/component/Registry";
17+
export type { Registry } from "@/lib/component/Registry";
2118
// React components
22-
export { Component } from "@/lib/component/Component";
19+
export { Component, type ComponentProps } from "@/lib/component/Component";
2320
export { Children } from "@/lib/component/Children";
2421
// React hooks
2522
export {
@@ -37,4 +34,8 @@ export type {
3734
HostStore,
3835
MutableHostStore,
3936
Plugin,
37+
PluginLike,
4038
} from "@/lib/types/state/options";
39+
// Some common utilities
40+
export { isObject } from "@/lib/utils/isObject";
41+
export { isString } from "@/lib/utils/isString";

chartlets.js/src/lib/plugins/mui/Box.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
import type { ElementType } from "react";
12
import MuiBox from "@mui/material/Box";
23

3-
import type { ElementType } from "react";
4-
import type { ComponentState } from "@/lib/types/state/component";
5-
import type { ComponentProps } from "@/lib/component/Component";
6-
import { Children } from "@/lib/component/Children";
4+
import type { ComponentState, ComponentProps } from "@/lib";
5+
import { Children } from "@/lib";
76

87
interface BoxState extends ComponentState {
98
component?: ElementType<Element>;

chartlets.js/src/lib/plugins/mui/Button.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { type MouseEvent } from "react";
22
import MuiButton from "@mui/material/Button";
33
import MuiIcon from "@mui/material/Icon";
44

5-
import { type ComponentState } from "@/lib/types/state/component";
6-
import type { ComponentProps } from "@/lib/component/Component";
5+
import type { ComponentState, ComponentProps } from "@/lib";
76

87
interface ButtonState extends ComponentState {
98
text?: string;

chartlets.js/src/lib/plugins/mui/Checkbox.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import MuiCheckbox from "@mui/material/Checkbox";
33
import MuiFormControl from "@mui/material/FormControl";
44
import MuiFormControlLabel from "@mui/material/FormControlLabel";
55

6-
import { type ComponentState } from "@/lib/types/state/component";
7-
import type { ComponentProps } from "@/lib/component/Component";
6+
import type { ComponentState, ComponentProps } from "@/lib";
87

98
interface CheckboxState extends ComponentState {
109
label?: string;

chartlets.js/src/lib/plugins/mui/CircularProgress.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import MuiCircularProgress from "@mui/material/CircularProgress";
22

3-
import { type ComponentState } from "@/lib/types/state/component";
4-
import type { ComponentProps } from "@/lib/component/Component";
3+
import type { ComponentState, ComponentProps } from "@/lib";
54

65
interface CircularProgressState extends ComponentState {
76
size?: number | string;

chartlets.js/src/lib/plugins/mui/IconButton.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { type MouseEvent } from "react";
22
import MuiIconButton from "@mui/material/IconButton";
33
import MuiIcon from "@mui/material/Icon";
44

5-
import { type ComponentState } from "@/lib/types/state/component";
6-
import type { ComponentProps } from "@/lib/component/Component";
5+
import type { ComponentState, ComponentProps } from "@/lib";
76

87
interface IconButtonState extends ComponentState {
98
icon?: string;

0 commit comments

Comments
 (0)