Skip to content

Commit 8ba4b7d

Browse files
committed
umd
1 parent 4696775 commit 8ba4b7d

File tree

7 files changed

+136
-9
lines changed

7 files changed

+136
-9
lines changed

package-lock.json

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"scripts": {
2020
"dev": "npm run storybook",
2121
"build": "npm run clean && npm run build:graph && npm run build:react",
22-
"build:graph": "npm run clean:graph && rollup -c --config-package graph && tsc --project packages/graph/tsconfig.json --emitDeclarationOnly",
22+
"build:graph": "npm run clean:graph && rollup -c --config-package graph && tsc --project packages/graph/tsconfig.json --emitDeclarationOnly && npm run build:graph:umd",
23+
"build:graph:umd": "rollup -c rollup.config.umd.mjs",
2324
"build:react": "npm run clean:react && rollup -c --config-package react && tsc --project packages/react/tsconfig.json --emitDeclarationOnly",
2425
"watch": "rollup -c --watch",
2526
"typecheck": "tsc -b",
@@ -67,6 +68,7 @@
6768
"@monaco-editor/react": "^4.6.0",
6869
"@rollup/plugin-commonjs": "^28.0.8",
6970
"@rollup/plugin-node-resolve": "^16.0.3",
71+
"@rollup/plugin-terser": "^0.4.4",
7072
"@rollup/plugin-typescript": "^12.1.4",
7173
"@size-limit/preset-big-lib": "^11.2.0",
7274
"@storybook/addon-docs": "^9.1.2",

packages/graph/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"description": "Modern graph editor component - core library with Canvas rendering, state management, and plugins",
55
"main": "./dist/index.js",
66
"module": "./dist/index.js",
7+
"umd": "./dist/graph.umd.js",
8+
"umd:min": "./dist/graph.umd.min.js",
79
"types": "./dist/index.d.ts",
810
"type": "module",
911
"keywords": [
@@ -22,6 +24,7 @@
2224
"exports": {
2325
".": {
2426
"types": "./dist/index.d.ts",
27+
"umd": "./dist/graph.umd.js",
2528
"default": "./dist/index.js"
2629
},
2730
"./utils": {

packages/graph/src/index.umd.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// UMD-specific exports - only essential classes and enums
2+
export { ESchedulerPriority } from "./lib/Scheduler";
3+
export { ECanChangeBlockGeometry } from "./store/settings";
4+
export { ECameraScaleLevel } from "./services/camera/CameraService";
5+
export { EAnchorType } from "./store/anchor/Anchor";
6+
export { ESelectionStrategy } from "./services/selection/types";
7+
export { GraphState } from "./graph";
8+
9+
export { Anchor, type TAnchor, type TAnchorProps } from "./components/canvas/anchors";
10+
export { BaseConnection } from "./components/canvas/connections";
11+
export { BlockConnection } from "./components/canvas/connections";
12+
export { BlockGroups } from "./components/canvas/groups";
13+
export { Block as CanvasBlock, type TBlock } from "./components/canvas/blocks/Block";
14+
export { Component } from "./lib/Component";
15+
export { ConnectionArrow } from "./components/canvas/connections";
16+
export { ConnectionLayer } from "./components/canvas/layers/connectionLayer/ConnectionLayer";
17+
export { Graph } from "./graph";
18+
export { GraphComponent } from "./components/canvas/GraphComponent";
19+
export { Group } from "./components/canvas/groups";
20+
export { Layer } from "./services/Layer";
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import { useMemo } from "react";
22

3-
import { Graph, TBlock } from "@gravity-ui/graph";
3+
import { Graph } from "@gravity-ui/graph";
4+
import type { BlockState, CanvasBlock, TBlock } from "@gravity-ui/graph";
45
import { computed } from "@preact/signals-core";
56

67
import { useSignal } from "./useSignal";
78

8-
export function useBlockState<T extends TBlock>(graph: Graph, block: T | T["id"]) {
9+
export function useBlockState<T extends TBlock>(graph: Graph, block: T | T["id"]): BlockState<T> | undefined {
910
const signal = useMemo(() => {
10-
return computed(() => graph.rootStore.blocksList.getBlockState(typeof block === "object" ? block.id : block));
11+
return computed(
12+
() =>
13+
graph.rootStore.blocksList.getBlockState(typeof block === "object" ? block.id : block) as
14+
| BlockState<T>
15+
| undefined
16+
);
1117
}, [graph, block]);
1218
return useSignal(signal);
1319
}
1420

15-
export function useBlockViewState<T extends TBlock>(graph: Graph, block: T | T["id"]) {
16-
const blockState = useBlockState(graph, block);
17-
return blockState?.getViewComponent();
21+
export function useBlockViewState<T extends TBlock>(graph: Graph, block: T | T["id"]): CanvasBlock<T> | undefined {
22+
const blockState = useBlockState<T>(graph, block);
23+
return blockState?.getViewComponent() as CanvasBlock<T> | undefined;
1824
}

packages/react/src/hooks/useGraph.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,23 @@ export type HookGraphParams = Pick<TGraphConfig, "settings" | "layers"> & {
2626
};
2727
};
2828

29-
export function useGraph(config: HookGraphParams) {
29+
export type UseGraphResult = {
30+
graph: Graph;
31+
api: Graph["api"];
32+
setSettings: (settings: Partial<TGraphSettingsConfig>) => void;
33+
start: () => void;
34+
stop: () => void;
35+
setViewConfiguration: (viewConfiguration: HookGraphParams["viewConfiguration"]) => void;
36+
addLayer: <T extends Constructor<Layer> = Constructor<Layer>>(
37+
layerCtor: T,
38+
props: LayerPublicProps<T>
39+
) => InstanceType<T>;
40+
setEntities: <B extends TBlock, C extends TConnection>(entities: { blocks?: B[]; connections?: C[] }) => void;
41+
updateEntities: <B extends TBlock, C extends TConnection>(entities: { blocks?: B[]; connections?: C[] }) => void;
42+
zoomTo: (target: TGraphZoomTarget, config?: ZoomConfig) => void;
43+
};
44+
45+
export function useGraph(config: HookGraphParams): UseGraphResult {
3046
const graph = useMemo(() => {
3147
if (config.graph) {
3248
return config.graph;
@@ -98,5 +114,5 @@ export function useGraph(config: HookGraphParams) {
98114
zoomTo: useFn((target: TGraphZoomTarget, config?: ZoomConfig) => {
99115
graph.zoomTo(target, config);
100116
}),
101-
};
117+
} as const;
102118
}

rollup.config.umd.mjs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import commonjs from "@rollup/plugin-commonjs";
2+
import resolve from "@rollup/plugin-node-resolve";
3+
import terser from "@rollup/plugin-terser";
4+
import typescript from "@rollup/plugin-typescript";
5+
import postcss from "rollup-plugin-postcss";
6+
7+
const createConfig = (minify = false) => ({
8+
input: "packages/graph/src/index.umd.ts",
9+
output: {
10+
file: minify ? "packages/graph/dist/graph.umd.min.js" : "packages/graph/dist/graph.umd.js",
11+
format: "umd",
12+
name: "GravityGraph",
13+
sourcemap: true,
14+
},
15+
plugins: [
16+
resolve({
17+
extensions: [".ts", ".tsx", ".js", ".jsx", ".json"],
18+
browser: true,
19+
}),
20+
typescript({
21+
tsconfig: "packages/graph/tsconfig.json",
22+
declaration: false,
23+
}),
24+
commonjs(),
25+
postcss({
26+
extract: false,
27+
inject: true,
28+
modules: false,
29+
}),
30+
...(minify
31+
? [
32+
terser({
33+
compress: {
34+
drop_console: false,
35+
drop_debugger: true,
36+
pure_funcs: [],
37+
ecma: 2015,
38+
},
39+
format: {
40+
// Нужно уточнить, нужно ли Добавить copyright лицензию
41+
comments: false,
42+
},
43+
}),
44+
]
45+
: []),
46+
],
47+
});
48+
49+
export default [createConfig(false), createConfig(true)];

0 commit comments

Comments
 (0)