cx-diagrams 22.3.0

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/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2022 Codaxy d.o.o. Banja Luka
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,42 @@
1
+ <br />
2
+
3
+ <p align="center">
4
+ <a href="https://diagrams.cxjs.io">
5
+ <img src="../../docs/assets/img/logo.svg" height="142" alt="logo">
6
+ </a>
7
+ </p>
8
+
9
+ <p align="center" style="font-weight: bold; font-size: 24px">
10
+ CxJS Diagrams
11
+ </p>
12
+
13
+ <p align="center">
14
+ <img src="https://img.shields.io/npm/v/cx-diagrams" alt="version" />
15
+ </p>
16
+
17
+ <br />
18
+
19
+ This is a simple library that allows you to create diagrams within CxJS applications.
20
+
21
+ ```js
22
+ import { Diagram, Flow, Cell, Shape } from "cx-diagrams";
23
+
24
+ <Svg class="w-auto h-full bg-white">
25
+ <Diagram unitSize={32} showGrid center>
26
+ <Flow gap={1}>
27
+ <Cell width={2}>
28
+ <Shape stroke="red" fill="white" text="Red" />
29
+ </Cell>
30
+ <Cell width={2}>
31
+ <Shape stroke="blue" fill="white" text="Blue" />
32
+ </Cell>
33
+ </Flow>
34
+ </Diagram>
35
+ </Svg>;
36
+ ```
37
+
38
+ Please refer to [the documentation](https://diagrams.cxjs.io) and [the GitHub repository](https://github.com/codaxy/cx-diagrams) for more information and usage examples.
39
+
40
+ ## License
41
+
42
+ This package is available under [the MIT license](./LICENSE.md).
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "cx-diagrams",
3
+ "version": "22.3.0",
4
+ "description": "A library for creating diagrams within CxJS applications",
5
+ "main": "src/index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [
10
+ "diagrams",
11
+ "cxjs"
12
+ ],
13
+ "author": "Codaxy d.o.o.",
14
+ "license": "MIT",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git@github.com:codaxy/cx-diagrams.git"
18
+ },
19
+ "peerDependencies": {
20
+ "cx": "^22.1.0"
21
+ }
22
+ }
package/src/Cell.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { NumberProp, Widget } from "cx/src/core";
2
+ import { NodeProps } from "./Node";
3
+
4
+ export interface CellProps extends NodeProps {
5
+ /**Width of the cell. */
6
+ width?: NumberProp;
7
+
8
+ /**Width of the cell. */
9
+ w?: NumberProp;
10
+
11
+ /**Height of the cell. */
12
+ height?: NumberProp;
13
+
14
+ /**Height of the cell. */
15
+ h?: NumberProp;
16
+ }
17
+
18
+ export class Cell extends Widget<CellProps> {}
package/src/Cell.js ADDED
@@ -0,0 +1,37 @@
1
+ import { isDefined } from "cx/util";
2
+ import { Node } from "./Node";
3
+
4
+ export class Cell extends Node {
5
+ init() {
6
+ if (isDefined(this.w)) this.width = this.w;
7
+ if (isDefined(this.h)) this.height = this.h;
8
+ super.init();
9
+ }
10
+ declareData(...args) {
11
+ super.declareData(...args, {
12
+ width: undefined,
13
+ height: undefined,
14
+ });
15
+ }
16
+
17
+ explore(context, instance) {
18
+ instance.diagram = context.diagram;
19
+ let { data } = instance;
20
+ instance.box = {
21
+ row: 0,
22
+ col: 0,
23
+ width: data.width,
24
+ height: data.height,
25
+ ml: data.ml,
26
+ mr: data.mr,
27
+ mt: data.mt,
28
+ mb: data.mb,
29
+ ms: data.ms,
30
+ me: data.me,
31
+ };
32
+ super.explore(context, instance);
33
+ }
34
+ }
35
+
36
+ Cell.prototype.width = 1;
37
+ Cell.prototype.height = 1;
@@ -0,0 +1,32 @@
1
+ import { BooleanProp, NumberProp, Widget } from "cx/src/core";
2
+ import { BoundedObjectProps } from "cx/src/svg/BoundedObject";
3
+
4
+ export interface DiagramProps extends BoundedObjectProps {
5
+ /** Zoom level. */
6
+ zoom?: NumberProp;
7
+
8
+ /** Horizontal offset (pan). */
9
+ offsetX?: NumberProp;
10
+
11
+ /** Vertical offset (pan). */
12
+ offsetY?: NumberProp;
13
+
14
+ /** Unit size in pixels. */
15
+ unitSize?: NumberProp;
16
+
17
+ /** Set to true to center the content both horizontally and vertically. */
18
+ center?: boolean;
19
+
20
+ /** Set to true to center the content horizontally. */
21
+ centerX?: boolean;
22
+
23
+ /** Set to true to center the content vertically. */
24
+ centerY?: boolean;
25
+
26
+ showGrid?: BooleanProp;
27
+
28
+ /** Set to true to disable zooming and panning. */
29
+ fixed?: BooleanProp;
30
+ }
31
+
32
+ export class Diagram extends Widget<DiagramProps> {}
package/src/Diagram.js ADDED
@@ -0,0 +1,312 @@
1
+ import { VDOM } from "cx/ui";
2
+ import { BoundedObject } from "cx/svg";
3
+ import { DiagramState } from "./DiagramState";
4
+ import { addEventListenerWithOptions, debounce } from "cx/util";
5
+ import { getCursorPos, captureMouseOrTouch } from "cx/widgets";
6
+
7
+ export class Diagram extends BoundedObject {
8
+ init() {
9
+ if (this.center) {
10
+ this.centerX = true;
11
+ this.centerY = true;
12
+ }
13
+ super.init();
14
+ }
15
+
16
+ declareData(...args) {
17
+ super.declareData(...args, {
18
+ offsetX: undefined,
19
+ offsetY: undefined,
20
+ zoom: undefined,
21
+ unitSize: undefined,
22
+ showGrid: undefined,
23
+ fixed: undefined,
24
+ });
25
+ }
26
+
27
+ prepareData(context, instance) {
28
+ let { data } = instance;
29
+ data.stateMods = {
30
+ ...data.stateMods,
31
+ pannable: !data.fixed,
32
+ };
33
+ super.prepareData(context, instance);
34
+ }
35
+
36
+ explore(context, instance) {
37
+ let { data } = instance;
38
+ let ds = (instance.diagramState = new DiagramState());
39
+ ds.unitSize = data.unitSize;
40
+ context.push("diagram", ds);
41
+ super.explore(context, instance);
42
+ }
43
+
44
+ exploreCleanup(context, instance) {
45
+ context.pop("diagram");
46
+ }
47
+
48
+ prepare(context, instance) {
49
+ let r2 = 0;
50
+ let c2 = 0;
51
+ let r1 = 0;
52
+ let c1 = 0;
53
+
54
+ let { children } = instance;
55
+ if (this.centerX || this.centerY) {
56
+ for (let { box } of children) {
57
+ if (!box) continue;
58
+ if (box.row < r1) r1 = box.row;
59
+ if (box.row + box.height > r2) r2 = box.row + box.height;
60
+ if (box.col < c1) c1 = box.col;
61
+ if (box.col + box.width > c2) c2 = box.col + box.width;
62
+ }
63
+
64
+ let dc = (c1 - c2) / 2;
65
+ let dr = (r1 - r2) / 2;
66
+
67
+ for (let { box } of children) {
68
+ if (!box) continue;
69
+ box.row += dr;
70
+ box.col += dc;
71
+ }
72
+ }
73
+
74
+ super.prepare(context, instance);
75
+ context.push("diagram", instance.diagramState);
76
+ }
77
+
78
+ prepareCleanup(context, instance) {
79
+ context.pop("diagram");
80
+ super.prepareCleanup(context, instance);
81
+ }
82
+
83
+ render(context, instance, key) {
84
+ let { data } = instance;
85
+ return (
86
+ <DiagramComponent key={key} data={data} instance={instance}>
87
+ {this.renderChildren(context, instance)}
88
+ </DiagramComponent>
89
+ );
90
+ }
91
+ }
92
+
93
+ Diagram.prototype.offsetX = 0;
94
+ Diagram.prototype.offsetY = 0;
95
+ Diagram.prototype.zoom = 1;
96
+ Diagram.prototype.anchors = "0 1 1 0";
97
+ Diagram.prototype.baseClass = "diagram";
98
+ Diagram.prototype.styled = true;
99
+ Diagram.prototype.unitSize = 32;
100
+ Diagram.prototype.centerX = false;
101
+ Diagram.prototype.centerY = false;
102
+ Diagram.prototype.center = false;
103
+ Diagram.prototype.showGrid = false;
104
+ Diagram.prototype.fixed = false;
105
+
106
+ const defaultZoomStep = 0.05;
107
+ const minZoom = 0.25;
108
+ const maxZoom = 4;
109
+
110
+ class DiagramComponent extends VDOM.Component {
111
+ constructor(props) {
112
+ super(props);
113
+ this.state = {
114
+ zoom: props.data.zoom,
115
+ offsetX: props.data.offsetX,
116
+ offsetY: props.data.offsetY,
117
+ };
118
+ this.touchOperation = 0;
119
+
120
+ this.saveState = debounce(() => {
121
+ let { instance } = this.props;
122
+ let { zoom, offsetX, offsetY } = this.state;
123
+ instance.set("zoom", zoom);
124
+ instance.set("offsetX", offsetX);
125
+ instance.set("offsetY", offsetY);
126
+ }, 100);
127
+
128
+ this.handleInitialMouseMove = this.handleInitialMouseMove.bind(this);
129
+ this.elRef = (el) => {
130
+ this.el = el;
131
+ };
132
+ }
133
+
134
+ componentWillReceiveProps(props) {
135
+ this.setState({
136
+ zoom: props.data.zoom,
137
+ offsetX: props.data.offsetX,
138
+ offsetY: props.data.offsetY,
139
+ });
140
+ }
141
+
142
+ render() {
143
+ let { offsetX, offsetY, zoom } = this.state;
144
+ let { data, children } = this.props;
145
+ let { bounds } = data;
146
+
147
+ let cx = (bounds.r - bounds.l) / 2 + offsetX;
148
+ let cy = (bounds.b - bounds.t) / 2 + offsetY;
149
+
150
+ let path = null;
151
+ if (data.showGrid) {
152
+ let p = "";
153
+ let d = data.unitSize * zoom;
154
+
155
+ let fromX = Math.ceil((bounds.l - cx) / d);
156
+ let toX = Math.floor((bounds.r - cx) / d);
157
+
158
+ for (let x = fromX; x <= toX; x++) p += `M ${cx + x * d} ${bounds.t} L ${cx + x * d} ${bounds.b}`;
159
+
160
+ let fromY = Math.ceil((bounds.t - cy) / d);
161
+ let toY = Math.floor((bounds.b - cy) / d);
162
+
163
+ for (let y = fromY; y <= toY; y++) p += `M ${bounds.l} ${cy + y * d} L ${bounds.r} ${cy + y * d}`;
164
+
165
+ path = <path style={data.style} d={p} stroke="lightgray" strokeWidth={0.5} />;
166
+ }
167
+
168
+ return (
169
+ <g className={data.classNames} ref={this.elRef} onMouseMove={this.handleInitialMouseMove}>
170
+ <rect
171
+ x={data.bounds.l}
172
+ y={data.bounds.t}
173
+ width={data.bounds.width()}
174
+ height={data.bounds.height()}
175
+ fill="transparent"
176
+ stroke="transparent"
177
+ />
178
+ {path}
179
+ <g transform={`translate(${cx}, ${cy}) scale(${zoom}, ${zoom})`}>{children}</g>
180
+ </g>
181
+ );
182
+ }
183
+
184
+ componentDidMount() {
185
+ this.offWheel = addEventListenerWithOptions(this.el, "wheel", (e) => this.handleWheel(e));
186
+ }
187
+
188
+ componentWillUnmount() {
189
+ this.offWheel();
190
+ }
191
+
192
+ handleWheel(ev) {
193
+ if (this.props.data.fixed) return;
194
+ if (ev.deltaY > 0) this.zoomOut(ev, false);
195
+ else this.zoomIn(ev, false);
196
+
197
+ ev.stopPropagation();
198
+ ev.preventDefault();
199
+ }
200
+
201
+ zoom(e, factor, center = true, zoomStep = defaultZoomStep, pinchPoint) {
202
+ let nzoom = (1 + factor * zoomStep) * this.state.zoom;
203
+ this.zoomTo(e, nzoom, center, pinchPoint);
204
+ }
205
+
206
+ zoomTo(e, zoom, center, pinchPoint) {
207
+ let mx, my;
208
+
209
+ if (pinchPoint) {
210
+ let el = e.currentTarget;
211
+ let bounds = el.getBoundingClientRect();
212
+ mx = pinchPoint.x - bounds.left;
213
+ my = pinchPoint.y - bounds.top;
214
+ } else if (!center) {
215
+ let el = e.currentTarget;
216
+ let bounds = this.el.firstElementChild.getBoundingClientRect();
217
+ let cursor = getCursorPos(e);
218
+ mx = cursor.clientX - (bounds.left + bounds.right) / 2;
219
+ my = cursor.clientY - (bounds.top + bounds.bottom) / 2;
220
+ }
221
+
222
+ zoom = Math.max(minZoom, Math.min(maxZoom, zoom));
223
+
224
+ let offsetX = mx - (zoom / this.state.zoom) * (mx - this.state.offsetX);
225
+ let offsetY = my - (zoom / this.state.zoom) * (my - this.state.offsetY);
226
+
227
+ this.setState(
228
+ {
229
+ zoom,
230
+ offsetX,
231
+ offsetY,
232
+ },
233
+ this.saveState
234
+ );
235
+ }
236
+
237
+ zoomIn(e, center) {
238
+ this.zoom(e, 1, center);
239
+ }
240
+
241
+ zoomOut(e, center) {
242
+ this.zoom(e, -1, center);
243
+ }
244
+
245
+ handleInitialMouseMove(e) {
246
+ if (e.buttons != 1) return;
247
+ if (this.props.data.fixed) return;
248
+ let cursor = getCursorPos(e);
249
+ let mode = e.touches && e.touches.length >= 2 ? "zoom" : "pan";
250
+ let captureData = {
251
+ mode,
252
+ touchOperation: ++this.touchOperation,
253
+ };
254
+
255
+ if (mode == "pan") {
256
+ captureData.dx = this.state.offsetX - cursor.clientX;
257
+ captureData.dy = this.state.offsetY - cursor.clientY;
258
+ } else {
259
+ captureData.cx = (e.touches[0].clientX + e.touches[1].clientX) / 2;
260
+ captureData.cy = (e.touches[0].clientY + e.touches[1].clientY) / 2;
261
+ captureData.originalZoom = view.zoom;
262
+ captureData.pointsDistance = Math.sqrt(
263
+ Math.pow(e.touches[0].clientX - e.touches[1].clientX, 2) +
264
+ Math.pow(e.touches[0].clientY - e.touches[1].clientY, 2)
265
+ );
266
+ }
267
+
268
+ captureMouseOrTouch(
269
+ e,
270
+ (e, captureData) => {
271
+ this.handleMouseMove(e, captureData);
272
+ },
273
+ () => {},
274
+ captureData,
275
+ "grabbing"
276
+ );
277
+
278
+ e.stopPropagation();
279
+ //e.preventDefault(); //prevents clicks
280
+ document.activeElement.blur(); //hide the context menu
281
+ }
282
+
283
+ handleMouseMove(e, captureData) {
284
+ if (captureData.touchOperation != this.touchOperation) return;
285
+
286
+ if (captureData.mode == "pan") {
287
+ let cursor = getCursorPos(e);
288
+ let offsetX = cursor.clientX + captureData.dx;
289
+ let offsetY = cursor.clientY + captureData.dy;
290
+ if (offsetX != NaN && offsetY != NaN) {
291
+ this.setState(
292
+ {
293
+ offsetX: offsetX,
294
+ offsetY: offsetY,
295
+ },
296
+ this.saveState
297
+ );
298
+ }
299
+ } else {
300
+ let pointsDistance = Math.sqrt(
301
+ Math.pow(e.touches[0].clientX - e.touches[1].clientX, 2) +
302
+ Math.pow(e.touches[0].clientY - e.touches[1].clientY, 2)
303
+ );
304
+ //console.log(captureData, e.touches);
305
+ let newZoom = (pointsDistance / captureData.pointsDistance) * captureData.originalZoom;
306
+ this.zoomTo(e, newZoom, false, { x: captureData.cx, y: captureData.cy });
307
+ //console.log('ZOOM');
308
+ e.stopPropagation();
309
+ e.preventDefault();
310
+ }
311
+ }
312
+ }
@@ -0,0 +1,13 @@
1
+ .cxb-diagram {
2
+ fill: none;
3
+ stroke: none;
4
+ stroke-width: 1;
5
+ }
6
+
7
+ .cxb-diagram.cxs-pannable {
8
+ cursor: grab;
9
+
10
+ &:active {
11
+ cursor: grabbing;
12
+ }
13
+ }
@@ -0,0 +1,24 @@
1
+ export class DiagramState {
2
+ unitSize = 16;
3
+ shapes = {};
4
+
5
+ map(x, y) {
6
+ return { x: x * this.unitSize, y: y * this.unitSize };
7
+ }
8
+
9
+ registerShapeBounds(id, shape, bounds) {
10
+ let data = this.shapes[id];
11
+ if (!data) data = this.shapes[id] = {};
12
+ data.bounds = bounds;
13
+ data.shape = shape;
14
+ }
15
+
16
+ getShape(id) {
17
+ let data = this.shapes[id];
18
+ if (!data || !data.bounds)
19
+ throw new Error(
20
+ `Shape ${id} has no registered bounds. Please make sure that shapes have correct ids and connections come afterwards.`
21
+ );
22
+ return data;
23
+ }
24
+ }
package/src/Flow.d.ts ADDED
@@ -0,0 +1,39 @@
1
+ import { NumberProp, Widget } from "cx/src/core";
2
+ import { NodeProps } from "./Node";
3
+
4
+ export interface FlowProps extends NodeProps {
5
+ /** Gap between items. */
6
+ gap?: NumberProp;
7
+
8
+ /** Padding. */
9
+ p?: NumberProp;
10
+
11
+ /** Padding. */
12
+ padding?: NumberProp;
13
+
14
+ /** Left padding. */
15
+ pl?: NumberProp;
16
+
17
+ /** Right padding. */
18
+ pr?: NumberProp;
19
+
20
+ /** Top paddding. */
21
+ pt?: NumberProp;
22
+
23
+ /** Bottom padding. */
24
+ pb?: NumberProp;
25
+
26
+ /** Horizontal padding. */
27
+ px?: NumberProp;
28
+
29
+ /** Vertical padding. */
30
+ py?: NumberProp;
31
+
32
+ /** Set to true to disable rotation. */
33
+ fixed?: boolean;
34
+
35
+ /** Direction of the flow. Default value is `right`. */
36
+ direction?: "right" | "left" | "up" | "down";
37
+ }
38
+
39
+ export class Flow extends Widget<FlowProps> {}