@3plate/graph-react 0.1.6 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +34 -1
- package/dist/index.d.ts +34 -1
- package/dist/index.js +162 -0
- package/dist/index.mjs +125 -0
- package/package.json +11 -4
package/dist/index.d.mts
CHANGED
|
@@ -1,2 +1,35 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Update, IngestionConfig, APIArguments, PlaygroundOptions } from '@3plate/graph-core';
|
|
1
3
|
|
|
2
|
-
|
|
4
|
+
type GraphProps<N, E> = {
|
|
5
|
+
/** Initial nodes */
|
|
6
|
+
nodes?: N[];
|
|
7
|
+
/** Initial edges */
|
|
8
|
+
edges?: E[];
|
|
9
|
+
/** Initial history */
|
|
10
|
+
history?: Update<N, E>[];
|
|
11
|
+
/** Ingestion source configuration (alternative to nodes/edges/history) */
|
|
12
|
+
ingestion?: IngestionConfig;
|
|
13
|
+
/** Options */
|
|
14
|
+
options?: APIArguments<N, E>['options'];
|
|
15
|
+
/** Events */
|
|
16
|
+
events?: APIArguments<N, E>['events'];
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Graph component - renders a graph visualization
|
|
20
|
+
* Intelligently handles prop changes by diffing nodes, edges, and history
|
|
21
|
+
*/
|
|
22
|
+
declare function Graph<N, E>(props: GraphProps<N, E>): React.JSX.Element;
|
|
23
|
+
|
|
24
|
+
type PlaygroundProps = {
|
|
25
|
+
/** Examples to display */
|
|
26
|
+
examples: PlaygroundOptions['examples'];
|
|
27
|
+
/** Default example key */
|
|
28
|
+
defaultExample?: string;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Playground component - renders the interactive playground with examples
|
|
32
|
+
*/
|
|
33
|
+
declare function Playground(props: PlaygroundProps): React.JSX.Element;
|
|
34
|
+
|
|
35
|
+
export { Graph, type GraphProps, Playground, type PlaygroundProps };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,35 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Update, IngestionConfig, APIArguments, PlaygroundOptions } from '@3plate/graph-core';
|
|
1
3
|
|
|
2
|
-
|
|
4
|
+
type GraphProps<N, E> = {
|
|
5
|
+
/** Initial nodes */
|
|
6
|
+
nodes?: N[];
|
|
7
|
+
/** Initial edges */
|
|
8
|
+
edges?: E[];
|
|
9
|
+
/** Initial history */
|
|
10
|
+
history?: Update<N, E>[];
|
|
11
|
+
/** Ingestion source configuration (alternative to nodes/edges/history) */
|
|
12
|
+
ingestion?: IngestionConfig;
|
|
13
|
+
/** Options */
|
|
14
|
+
options?: APIArguments<N, E>['options'];
|
|
15
|
+
/** Events */
|
|
16
|
+
events?: APIArguments<N, E>['events'];
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Graph component - renders a graph visualization
|
|
20
|
+
* Intelligently handles prop changes by diffing nodes, edges, and history
|
|
21
|
+
*/
|
|
22
|
+
declare function Graph<N, E>(props: GraphProps<N, E>): React.JSX.Element;
|
|
23
|
+
|
|
24
|
+
type PlaygroundProps = {
|
|
25
|
+
/** Examples to display */
|
|
26
|
+
examples: PlaygroundOptions['examples'];
|
|
27
|
+
/** Default example key */
|
|
28
|
+
defaultExample?: string;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Playground component - renders the interactive playground with examples
|
|
32
|
+
*/
|
|
33
|
+
declare function Playground(props: PlaygroundProps): React.JSX.Element;
|
|
34
|
+
|
|
35
|
+
export { Graph, type GraphProps, Playground, type PlaygroundProps };
|
package/dist/index.js
CHANGED
|
@@ -1 +1,163 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
Graph: () => Graph,
|
|
34
|
+
Playground: () => Playground
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
|
|
38
|
+
// src/Graph.tsx
|
|
39
|
+
var import_react = __toESM(require("react"));
|
|
40
|
+
var import_graph_core = require("@3plate/graph-core");
|
|
41
|
+
function Graph(props) {
|
|
42
|
+
const rootRef = (0, import_react.useRef)(null);
|
|
43
|
+
const apiRef = (0, import_react.useRef)(null);
|
|
44
|
+
const rootIdRef = (0, import_react.useRef)(`graph-${Math.random().toString(36).slice(2, 11)}`);
|
|
45
|
+
(0, import_react.useEffect)(() => {
|
|
46
|
+
if (!rootRef.current || apiRef.current) return;
|
|
47
|
+
rootRef.current.id = rootIdRef.current;
|
|
48
|
+
(0, import_graph_core.graph)({
|
|
49
|
+
root: rootIdRef.current,
|
|
50
|
+
nodes: props.nodes,
|
|
51
|
+
edges: props.edges,
|
|
52
|
+
history: props.history,
|
|
53
|
+
ingestion: props.ingestion,
|
|
54
|
+
options: props.options,
|
|
55
|
+
events: props.events
|
|
56
|
+
}).then((api) => {
|
|
57
|
+
apiRef.current = api;
|
|
58
|
+
});
|
|
59
|
+
return () => {
|
|
60
|
+
if (apiRef.current) {
|
|
61
|
+
apiRef.current.destroy();
|
|
62
|
+
apiRef.current = null;
|
|
63
|
+
}
|
|
64
|
+
if (rootRef.current) {
|
|
65
|
+
const canvas = rootRef.current.querySelector("canvas, svg");
|
|
66
|
+
if (canvas) {
|
|
67
|
+
canvas.remove();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}, []);
|
|
72
|
+
(0, import_react.useEffect)(() => {
|
|
73
|
+
if (!apiRef.current) return;
|
|
74
|
+
apiRef.current.applyProps(props);
|
|
75
|
+
}, [props.nodes, props.edges, props.history, props.options]);
|
|
76
|
+
return /* @__PURE__ */ import_react.default.createElement("div", { ref: rootRef, style: { width: "100%", height: "100%" } });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/Playground.tsx
|
|
80
|
+
var import_react2 = __toESM(require("react"));
|
|
81
|
+
var import_graph_core2 = require("@3plate/graph-core");
|
|
82
|
+
function Playground(props) {
|
|
83
|
+
const rootRef = (0, import_react2.useRef)(null);
|
|
84
|
+
const playgroundRef = (0, import_react2.useRef)(null);
|
|
85
|
+
const rootIdRef = (0, import_react2.useRef)(`playground-${Math.random().toString(36).slice(2, 11)}`);
|
|
86
|
+
const prevExamplesRef = (0, import_react2.useRef)({});
|
|
87
|
+
(0, import_react2.useEffect)(() => {
|
|
88
|
+
if (!rootRef.current || playgroundRef.current) return;
|
|
89
|
+
rootRef.current.id = rootIdRef.current;
|
|
90
|
+
const playground = new import_graph_core2.Playground({
|
|
91
|
+
root: rootIdRef.current,
|
|
92
|
+
examples: props.examples,
|
|
93
|
+
defaultExample: props.defaultExample
|
|
94
|
+
});
|
|
95
|
+
playgroundRef.current = playground;
|
|
96
|
+
prevExamplesRef.current = { ...props.examples };
|
|
97
|
+
playground.init();
|
|
98
|
+
return () => {
|
|
99
|
+
playgroundRef.current = null;
|
|
100
|
+
};
|
|
101
|
+
}, []);
|
|
102
|
+
(0, import_react2.useEffect)(() => {
|
|
103
|
+
if (!playgroundRef.current) return;
|
|
104
|
+
const playground = playgroundRef.current;
|
|
105
|
+
const prev = prevExamplesRef.current;
|
|
106
|
+
const current = props.examples;
|
|
107
|
+
const allKeys = /* @__PURE__ */ new Set([...Object.keys(prev), ...Object.keys(current)]);
|
|
108
|
+
for (const key of allKeys) {
|
|
109
|
+
const prevExample = prev[key];
|
|
110
|
+
const currentExample = current[key];
|
|
111
|
+
if (!prevExample && currentExample) {
|
|
112
|
+
playground.addExample(key, currentExample);
|
|
113
|
+
} else if (prevExample && !currentExample) {
|
|
114
|
+
playground.removeExample(key);
|
|
115
|
+
} else if (prevExample && currentExample && !shallowEqualExample(prevExample, currentExample)) {
|
|
116
|
+
playground.addExample(key, currentExample);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
prevExamplesRef.current = { ...current };
|
|
120
|
+
}, [props.examples]);
|
|
121
|
+
return /* @__PURE__ */ import_react2.default.createElement("div", { ref: rootRef, style: { width: "100%", height: "100%" } });
|
|
122
|
+
}
|
|
123
|
+
function shallowEqualExample(a, b) {
|
|
124
|
+
if (a === b) return true;
|
|
125
|
+
if (a.name !== b.name) return false;
|
|
126
|
+
if (a.description !== b.description) return false;
|
|
127
|
+
if (!shallowEqualArray(a.nodes, b.nodes)) return false;
|
|
128
|
+
if (!shallowEqualArray(a.edges, b.edges)) return false;
|
|
129
|
+
if (!shallowEqualOptions(a.options, b.options)) return false;
|
|
130
|
+
if (!shallowEqualSource(a.source, b.source)) return false;
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
function shallowEqualArray(a, b) {
|
|
134
|
+
if (a === b) return true;
|
|
135
|
+
if (!a || !b) return false;
|
|
136
|
+
if (a.length !== b.length) return false;
|
|
137
|
+
for (let i = 0; i < a.length; i++) {
|
|
138
|
+
if (a[i] !== b[i]) return false;
|
|
139
|
+
}
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
function shallowEqualOptions(a, b) {
|
|
143
|
+
if (a === b) return true;
|
|
144
|
+
if (!a || !b) return false;
|
|
145
|
+
return a === b;
|
|
146
|
+
}
|
|
147
|
+
function shallowEqualSource(a, b) {
|
|
148
|
+
if (a === b) return true;
|
|
149
|
+
if (!a || !b) return false;
|
|
150
|
+
if (a.type !== b.type) return false;
|
|
151
|
+
if (a.type === "file" && b.type === "file") {
|
|
152
|
+
return a.path === b.path;
|
|
153
|
+
}
|
|
154
|
+
if (a.type === "websocket" && b.type === "websocket") {
|
|
155
|
+
return a.url === b.url;
|
|
156
|
+
}
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
160
|
+
0 && (module.exports = {
|
|
161
|
+
Graph,
|
|
162
|
+
Playground
|
|
163
|
+
});
|
package/dist/index.mjs
CHANGED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// src/Graph.tsx
|
|
2
|
+
import React, { useEffect, useRef } from "react";
|
|
3
|
+
import { graph } from "@3plate/graph-core";
|
|
4
|
+
function Graph(props) {
|
|
5
|
+
const rootRef = useRef(null);
|
|
6
|
+
const apiRef = useRef(null);
|
|
7
|
+
const rootIdRef = useRef(`graph-${Math.random().toString(36).slice(2, 11)}`);
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
if (!rootRef.current || apiRef.current) return;
|
|
10
|
+
rootRef.current.id = rootIdRef.current;
|
|
11
|
+
graph({
|
|
12
|
+
root: rootIdRef.current,
|
|
13
|
+
nodes: props.nodes,
|
|
14
|
+
edges: props.edges,
|
|
15
|
+
history: props.history,
|
|
16
|
+
ingestion: props.ingestion,
|
|
17
|
+
options: props.options,
|
|
18
|
+
events: props.events
|
|
19
|
+
}).then((api) => {
|
|
20
|
+
apiRef.current = api;
|
|
21
|
+
});
|
|
22
|
+
return () => {
|
|
23
|
+
if (apiRef.current) {
|
|
24
|
+
apiRef.current.destroy();
|
|
25
|
+
apiRef.current = null;
|
|
26
|
+
}
|
|
27
|
+
if (rootRef.current) {
|
|
28
|
+
const canvas = rootRef.current.querySelector("canvas, svg");
|
|
29
|
+
if (canvas) {
|
|
30
|
+
canvas.remove();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}, []);
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (!apiRef.current) return;
|
|
37
|
+
apiRef.current.applyProps(props);
|
|
38
|
+
}, [props.nodes, props.edges, props.history, props.options]);
|
|
39
|
+
return /* @__PURE__ */ React.createElement("div", { ref: rootRef, style: { width: "100%", height: "100%" } });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/Playground.tsx
|
|
43
|
+
import React2, { useEffect as useEffect2, useRef as useRef2 } from "react";
|
|
44
|
+
import { Playground as PlaygroundClass } from "@3plate/graph-core";
|
|
45
|
+
function Playground(props) {
|
|
46
|
+
const rootRef = useRef2(null);
|
|
47
|
+
const playgroundRef = useRef2(null);
|
|
48
|
+
const rootIdRef = useRef2(`playground-${Math.random().toString(36).slice(2, 11)}`);
|
|
49
|
+
const prevExamplesRef = useRef2({});
|
|
50
|
+
useEffect2(() => {
|
|
51
|
+
if (!rootRef.current || playgroundRef.current) return;
|
|
52
|
+
rootRef.current.id = rootIdRef.current;
|
|
53
|
+
const playground = new PlaygroundClass({
|
|
54
|
+
root: rootIdRef.current,
|
|
55
|
+
examples: props.examples,
|
|
56
|
+
defaultExample: props.defaultExample
|
|
57
|
+
});
|
|
58
|
+
playgroundRef.current = playground;
|
|
59
|
+
prevExamplesRef.current = { ...props.examples };
|
|
60
|
+
playground.init();
|
|
61
|
+
return () => {
|
|
62
|
+
playgroundRef.current = null;
|
|
63
|
+
};
|
|
64
|
+
}, []);
|
|
65
|
+
useEffect2(() => {
|
|
66
|
+
if (!playgroundRef.current) return;
|
|
67
|
+
const playground = playgroundRef.current;
|
|
68
|
+
const prev = prevExamplesRef.current;
|
|
69
|
+
const current = props.examples;
|
|
70
|
+
const allKeys = /* @__PURE__ */ new Set([...Object.keys(prev), ...Object.keys(current)]);
|
|
71
|
+
for (const key of allKeys) {
|
|
72
|
+
const prevExample = prev[key];
|
|
73
|
+
const currentExample = current[key];
|
|
74
|
+
if (!prevExample && currentExample) {
|
|
75
|
+
playground.addExample(key, currentExample);
|
|
76
|
+
} else if (prevExample && !currentExample) {
|
|
77
|
+
playground.removeExample(key);
|
|
78
|
+
} else if (prevExample && currentExample && !shallowEqualExample(prevExample, currentExample)) {
|
|
79
|
+
playground.addExample(key, currentExample);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
prevExamplesRef.current = { ...current };
|
|
83
|
+
}, [props.examples]);
|
|
84
|
+
return /* @__PURE__ */ React2.createElement("div", { ref: rootRef, style: { width: "100%", height: "100%" } });
|
|
85
|
+
}
|
|
86
|
+
function shallowEqualExample(a, b) {
|
|
87
|
+
if (a === b) return true;
|
|
88
|
+
if (a.name !== b.name) return false;
|
|
89
|
+
if (a.description !== b.description) return false;
|
|
90
|
+
if (!shallowEqualArray(a.nodes, b.nodes)) return false;
|
|
91
|
+
if (!shallowEqualArray(a.edges, b.edges)) return false;
|
|
92
|
+
if (!shallowEqualOptions(a.options, b.options)) return false;
|
|
93
|
+
if (!shallowEqualSource(a.source, b.source)) return false;
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
function shallowEqualArray(a, b) {
|
|
97
|
+
if (a === b) return true;
|
|
98
|
+
if (!a || !b) return false;
|
|
99
|
+
if (a.length !== b.length) return false;
|
|
100
|
+
for (let i = 0; i < a.length; i++) {
|
|
101
|
+
if (a[i] !== b[i]) return false;
|
|
102
|
+
}
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
function shallowEqualOptions(a, b) {
|
|
106
|
+
if (a === b) return true;
|
|
107
|
+
if (!a || !b) return false;
|
|
108
|
+
return a === b;
|
|
109
|
+
}
|
|
110
|
+
function shallowEqualSource(a, b) {
|
|
111
|
+
if (a === b) return true;
|
|
112
|
+
if (!a || !b) return false;
|
|
113
|
+
if (a.type !== b.type) return false;
|
|
114
|
+
if (a.type === "file" && b.type === "file") {
|
|
115
|
+
return a.path === b.path;
|
|
116
|
+
}
|
|
117
|
+
if (a.type === "websocket" && b.type === "websocket") {
|
|
118
|
+
return a.url === b.url;
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
export {
|
|
123
|
+
Graph,
|
|
124
|
+
Playground
|
|
125
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@3plate/graph-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"license": "GPL-3.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -21,16 +21,23 @@
|
|
|
21
21
|
"react-dom": "^18.0.0 || ^19.0.0"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@3plate/graph-core": "0.1.
|
|
24
|
+
"@3plate/graph-core": "0.1.9"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
28
|
+
"@testing-library/react": "^16.1.0",
|
|
29
|
+
"@testing-library/user-event": "^14.5.2",
|
|
27
30
|
"@types/react": "^19.2.7",
|
|
28
31
|
"@types/react-dom": "^19.2.3",
|
|
32
|
+
"jsdom": "^25.0.1",
|
|
29
33
|
"tsup": "^8.5.1",
|
|
30
|
-
"typescript": "^5.9.3"
|
|
34
|
+
"typescript": "^5.9.3",
|
|
35
|
+
"vitest": "^2.1.8"
|
|
31
36
|
},
|
|
32
37
|
"scripts": {
|
|
33
38
|
"build": "tsup src/index.ts --format cjs,esm --dts --external react --external react-dom",
|
|
34
|
-
"dev": "tsup src/index.ts --format cjs,esm --dts --watch --external react --external react-dom"
|
|
39
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch --external react --external react-dom",
|
|
40
|
+
"test": "vitest run",
|
|
41
|
+
"test:watch": "vitest"
|
|
35
42
|
}
|
|
36
43
|
}
|