@dxos/graph 0.8.4-main.406dc2a → 0.8.4-main.59c2e9b
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/lib/browser/index.mjs +377 -201
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +377 -202
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/Graph.d.ts +64 -0
- package/dist/types/src/Graph.d.ts.map +1 -0
- package/dist/types/src/GraphModel.d.ts +135 -0
- package/dist/types/src/GraphModel.d.ts.map +1 -0
- package/dist/types/src/GraphModel.test.d.ts +2 -0
- package/dist/types/src/GraphModel.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/selection.d.ts +24 -6
- package/dist/types/src/selection.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +17 -15
- package/src/Graph.ts +81 -0
- package/src/GraphModel.test.ts +265 -0
- package/src/GraphModel.ts +452 -0
- package/src/index.ts +3 -3
- package/src/selection.ts +62 -22
- package/dist/types/src/model.d.ts +0 -102
- package/dist/types/src/model.d.ts.map +0 -1
- package/dist/types/src/model.test.d.ts +0 -2
- package/dist/types/src/model.test.d.ts.map +0 -1
- package/dist/types/src/types.d.ts +0 -58
- package/dist/types/src/types.d.ts.map +0 -1
- package/dist/types/src/util.d.ts +0 -9
- package/dist/types/src/util.d.ts.map +0 -1
- package/src/model.test.ts +0 -188
- package/src/model.ts +0 -317
- package/src/types.ts +0 -72
- package/src/util.ts +0 -22
|
@@ -1,27 +1,157 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import { isTruthy, removeBy } from "@dxos/util";
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
7
6
|
|
|
8
|
-
// src/
|
|
7
|
+
// src/selection.ts
|
|
8
|
+
import { Atom, Registry } from "@effect-atom/atom-react";
|
|
9
9
|
import { invariant } from "@dxos/invariant";
|
|
10
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/common/graph/src/
|
|
10
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/common/graph/src/selection.ts";
|
|
11
|
+
var SelectionModel = class {
|
|
12
|
+
_singleSelect;
|
|
13
|
+
_registry;
|
|
14
|
+
_selected;
|
|
15
|
+
constructor(_singleSelect = false) {
|
|
16
|
+
this._singleSelect = _singleSelect;
|
|
17
|
+
this._registry = Registry.make();
|
|
18
|
+
this._selected = Atom.make(/* @__PURE__ */ new Set());
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Returns the selected IDs atom for subscription.
|
|
22
|
+
*/
|
|
23
|
+
get selected() {
|
|
24
|
+
return this._selected;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Gets the current selected IDs as a Set.
|
|
28
|
+
*/
|
|
29
|
+
getSelected() {
|
|
30
|
+
return this._registry.get(this._selected);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Gets the current selected IDs as an array.
|
|
34
|
+
*/
|
|
35
|
+
getSelectedIds() {
|
|
36
|
+
return Array.from(this._registry.get(this._selected).values());
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Subscribe to selection changes.
|
|
40
|
+
*/
|
|
41
|
+
subscribe(cb) {
|
|
42
|
+
this._registry.get(this._selected);
|
|
43
|
+
return this._registry.subscribe(this._selected, () => {
|
|
44
|
+
cb(this._registry.get(this._selected));
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
toJSON() {
|
|
48
|
+
return {
|
|
49
|
+
selected: this.getSelectedIds()
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Gets the current selection size.
|
|
54
|
+
*/
|
|
55
|
+
getSize() {
|
|
56
|
+
return this._registry.get(this._selected).size;
|
|
57
|
+
}
|
|
58
|
+
contains(id) {
|
|
59
|
+
return this._registry.get(this._selected).has(id);
|
|
60
|
+
}
|
|
61
|
+
clear() {
|
|
62
|
+
this._registry.set(this._selected, /* @__PURE__ */ new Set());
|
|
63
|
+
}
|
|
64
|
+
add(id) {
|
|
65
|
+
invariant(id, void 0, {
|
|
66
|
+
F: __dxlog_file,
|
|
67
|
+
L: 76,
|
|
68
|
+
S: this,
|
|
69
|
+
A: [
|
|
70
|
+
"id",
|
|
71
|
+
""
|
|
72
|
+
]
|
|
73
|
+
});
|
|
74
|
+
const current = this._registry.get(this._selected);
|
|
75
|
+
this._registry.set(this._selected, new Set(this._singleSelect ? [
|
|
76
|
+
id
|
|
77
|
+
] : [
|
|
78
|
+
...Array.from(current.values()),
|
|
79
|
+
id
|
|
80
|
+
]));
|
|
81
|
+
}
|
|
82
|
+
remove(id) {
|
|
83
|
+
invariant(id, void 0, {
|
|
84
|
+
F: __dxlog_file,
|
|
85
|
+
L: 85,
|
|
86
|
+
S: this,
|
|
87
|
+
A: [
|
|
88
|
+
"id",
|
|
89
|
+
""
|
|
90
|
+
]
|
|
91
|
+
});
|
|
92
|
+
const current = this._registry.get(this._selected);
|
|
93
|
+
this._registry.set(this._selected, new Set(Array.from(current.values()).filter((_id) => _id !== id)));
|
|
94
|
+
}
|
|
95
|
+
// TODO(burdon): Handle single select.
|
|
96
|
+
setSelected(ids, subtract = false) {
|
|
97
|
+
const current = this._registry.get(this._selected);
|
|
98
|
+
this._registry.set(this._selected, /* @__PURE__ */ new Set([
|
|
99
|
+
...subtract ? Array.from(current.values()) : [],
|
|
100
|
+
...ids
|
|
101
|
+
]));
|
|
102
|
+
}
|
|
103
|
+
toggleSelected(ids, subtract = false) {
|
|
104
|
+
const current = this._registry.get(this._selected);
|
|
105
|
+
const set = new Set(subtract ? Array.from(current.values()) : void 0);
|
|
106
|
+
ids.forEach((id) => {
|
|
107
|
+
if (current.has(id)) {
|
|
108
|
+
set.delete(id);
|
|
109
|
+
} else {
|
|
110
|
+
set.add(id);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
this._registry.set(this._selected, set);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// src/Graph.ts
|
|
118
|
+
var Graph_exports = {};
|
|
119
|
+
__export(Graph_exports, {
|
|
120
|
+
Edge: () => Edge,
|
|
121
|
+
Graph: () => Graph,
|
|
122
|
+
Node: () => Node,
|
|
123
|
+
createEdgeId: () => createEdgeId,
|
|
124
|
+
parseEdgeId: () => parseEdgeId
|
|
125
|
+
});
|
|
126
|
+
import * as Schema from "effect/Schema";
|
|
127
|
+
import { invariant as invariant2 } from "@dxos/invariant";
|
|
128
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/common/graph/src/Graph.ts";
|
|
129
|
+
var Node = Schema.Struct({
|
|
130
|
+
id: Schema.String,
|
|
131
|
+
type: Schema.optional(Schema.String),
|
|
132
|
+
data: Schema.optional(Schema.Any)
|
|
133
|
+
});
|
|
134
|
+
var Edge = Schema.Struct({
|
|
135
|
+
id: Schema.String,
|
|
136
|
+
type: Schema.optional(Schema.String),
|
|
137
|
+
source: Schema.String,
|
|
138
|
+
target: Schema.String,
|
|
139
|
+
data: Schema.optional(Schema.Any)
|
|
140
|
+
});
|
|
11
141
|
var KEY_REGEX = /\w+/;
|
|
12
142
|
var createEdgeId = ({ source, target, relation }) => {
|
|
13
|
-
|
|
14
|
-
F:
|
|
15
|
-
L:
|
|
143
|
+
invariant2(source.match(KEY_REGEX), `invalid source: ${source}`, {
|
|
144
|
+
F: __dxlog_file2,
|
|
145
|
+
L: 52,
|
|
16
146
|
S: void 0,
|
|
17
147
|
A: [
|
|
18
148
|
"source.match(KEY_REGEX)",
|
|
19
149
|
"`invalid source: ${source}`"
|
|
20
150
|
]
|
|
21
151
|
});
|
|
22
|
-
|
|
23
|
-
F:
|
|
24
|
-
L:
|
|
152
|
+
invariant2(target.match(KEY_REGEX), `invalid target: ${target}`, {
|
|
153
|
+
F: __dxlog_file2,
|
|
154
|
+
L: 53,
|
|
25
155
|
S: void 0,
|
|
26
156
|
A: [
|
|
27
157
|
"target.match(KEY_REGEX)",
|
|
@@ -36,9 +166,9 @@ var createEdgeId = ({ source, target, relation }) => {
|
|
|
36
166
|
};
|
|
37
167
|
var parseEdgeId = (id) => {
|
|
38
168
|
const [source, relation, target] = id.split("_");
|
|
39
|
-
|
|
40
|
-
F:
|
|
41
|
-
L:
|
|
169
|
+
invariant2(source.length && target.length, void 0, {
|
|
170
|
+
F: __dxlog_file2,
|
|
171
|
+
L: 59,
|
|
42
172
|
S: void 0,
|
|
43
173
|
A: [
|
|
44
174
|
"source.length && target.length",
|
|
@@ -51,25 +181,47 @@ var parseEdgeId = (id) => {
|
|
|
51
181
|
target
|
|
52
182
|
};
|
|
53
183
|
};
|
|
184
|
+
var Graph = Schema.Struct({
|
|
185
|
+
id: Schema.optional(Schema.String),
|
|
186
|
+
nodes: Schema.mutable(Schema.Array(Node)),
|
|
187
|
+
edges: Schema.mutable(Schema.Array(Edge))
|
|
188
|
+
});
|
|
54
189
|
|
|
55
|
-
// src/
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
var
|
|
70
|
-
var _inspectCustom = inspectCustom;
|
|
190
|
+
// src/GraphModel.ts
|
|
191
|
+
var GraphModel_exports = {};
|
|
192
|
+
__export(GraphModel_exports, {
|
|
193
|
+
AbstractBuilder: () => AbstractBuilder,
|
|
194
|
+
AbstractGraphModel: () => AbstractGraphModel,
|
|
195
|
+
Builder: () => Builder,
|
|
196
|
+
GraphModel: () => GraphModel,
|
|
197
|
+
ReactiveGraphModel: () => ReactiveGraphModel,
|
|
198
|
+
ReadonlyGraphModel: () => ReadonlyGraphModel
|
|
199
|
+
});
|
|
200
|
+
import { Atom as Atom2 } from "@effect-atom/atom-react";
|
|
201
|
+
import { inspectCustom } from "@dxos/debug";
|
|
202
|
+
import { failedInvariant, invariant as invariant3 } from "@dxos/invariant";
|
|
203
|
+
import { isTruthy, removeBy } from "@dxos/util";
|
|
204
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/common/graph/src/GraphModel.ts";
|
|
71
205
|
var ReadonlyGraphModel = class {
|
|
72
|
-
|
|
206
|
+
_graph;
|
|
207
|
+
/**
|
|
208
|
+
* Optional function to wrap mutations.
|
|
209
|
+
* When set, all graph mutations are wrapped in this function.
|
|
210
|
+
*/
|
|
211
|
+
_change;
|
|
212
|
+
/**
|
|
213
|
+
* NOTE: Pass in simple Graph or Live.
|
|
214
|
+
* @param graph - The graph data.
|
|
215
|
+
* @param change - Optional function to wrap mutations (e.g., Obj.change for ECHO objects).
|
|
216
|
+
*/
|
|
217
|
+
constructor(graph, change) {
|
|
218
|
+
this._graph = graph ?? {
|
|
219
|
+
nodes: [],
|
|
220
|
+
edges: []
|
|
221
|
+
};
|
|
222
|
+
this._change = change;
|
|
223
|
+
}
|
|
224
|
+
[inspectCustom]() {
|
|
73
225
|
return this.toJSON();
|
|
74
226
|
}
|
|
75
227
|
/**
|
|
@@ -133,21 +285,24 @@ var ReadonlyGraphModel = class {
|
|
|
133
285
|
...targets.flatMap((target) => this._traverse(target, visited))
|
|
134
286
|
];
|
|
135
287
|
}
|
|
288
|
+
};
|
|
289
|
+
var AbstractGraphModel = class extends ReadonlyGraphModel {
|
|
136
290
|
/**
|
|
137
|
-
*
|
|
291
|
+
* Execute a mutation on the graph.
|
|
292
|
+
* If a change function is set, wraps the mutation in it.
|
|
138
293
|
*/
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
294
|
+
_mutate(fn) {
|
|
295
|
+
if (this._change != null) {
|
|
296
|
+
this._change(() => fn(this._graph));
|
|
297
|
+
} else {
|
|
298
|
+
fn(this._graph);
|
|
299
|
+
}
|
|
145
300
|
}
|
|
146
|
-
};
|
|
147
|
-
var AbstractGraphModel = class extends ReadonlyGraphModel {
|
|
148
301
|
clear() {
|
|
149
|
-
this.
|
|
150
|
-
|
|
302
|
+
this._mutate((graph) => {
|
|
303
|
+
graph.nodes.length = 0;
|
|
304
|
+
graph.edges.length = 0;
|
|
305
|
+
});
|
|
151
306
|
return this;
|
|
152
307
|
}
|
|
153
308
|
addGraph(graph) {
|
|
@@ -163,43 +318,45 @@ var AbstractGraphModel = class extends ReadonlyGraphModel {
|
|
|
163
318
|
return this;
|
|
164
319
|
}
|
|
165
320
|
addNode(node) {
|
|
166
|
-
|
|
167
|
-
F:
|
|
168
|
-
L:
|
|
321
|
+
invariant3(node.id, "ID is required", {
|
|
322
|
+
F: __dxlog_file3,
|
|
323
|
+
L: 182,
|
|
169
324
|
S: this,
|
|
170
325
|
A: [
|
|
171
326
|
"node.id",
|
|
172
327
|
"'ID is required'"
|
|
173
328
|
]
|
|
174
329
|
});
|
|
175
|
-
|
|
176
|
-
F:
|
|
177
|
-
L:
|
|
330
|
+
invariant3(!this.findNode(node.id), `node already exists: ${node.id}`, {
|
|
331
|
+
F: __dxlog_file3,
|
|
332
|
+
L: 183,
|
|
178
333
|
S: this,
|
|
179
334
|
A: [
|
|
180
335
|
"!this.findNode(node.id)",
|
|
181
336
|
"`node already exists: ${node.id}`"
|
|
182
337
|
]
|
|
183
338
|
});
|
|
184
|
-
this.
|
|
339
|
+
this._mutate((graph) => {
|
|
340
|
+
graph.nodes.push(node);
|
|
341
|
+
});
|
|
185
342
|
return node;
|
|
186
343
|
}
|
|
187
344
|
addNodes(nodes) {
|
|
188
345
|
return nodes.map((node) => this.addNode(node));
|
|
189
346
|
}
|
|
190
347
|
addEdge(edge) {
|
|
191
|
-
|
|
192
|
-
F:
|
|
193
|
-
L:
|
|
348
|
+
invariant3(edge.source, void 0, {
|
|
349
|
+
F: __dxlog_file3,
|
|
350
|
+
L: 195,
|
|
194
351
|
S: this,
|
|
195
352
|
A: [
|
|
196
353
|
"edge.source",
|
|
197
354
|
""
|
|
198
355
|
]
|
|
199
356
|
});
|
|
200
|
-
|
|
201
|
-
F:
|
|
202
|
-
L:
|
|
357
|
+
invariant3(edge.target, void 0, {
|
|
358
|
+
F: __dxlog_file3,
|
|
359
|
+
L: 196,
|
|
203
360
|
S: this,
|
|
204
361
|
A: [
|
|
205
362
|
"edge.target",
|
|
@@ -212,27 +369,33 @@ var AbstractGraphModel = class extends ReadonlyGraphModel {
|
|
|
212
369
|
...edge
|
|
213
370
|
};
|
|
214
371
|
}
|
|
215
|
-
|
|
216
|
-
F:
|
|
217
|
-
L:
|
|
372
|
+
invariant3(!this.findNode(edge.id), void 0, {
|
|
373
|
+
F: __dxlog_file3,
|
|
374
|
+
L: 201,
|
|
218
375
|
S: this,
|
|
219
376
|
A: [
|
|
220
377
|
"!this.findNode(edge.id!)",
|
|
221
378
|
""
|
|
222
379
|
]
|
|
223
380
|
});
|
|
224
|
-
this.
|
|
381
|
+
this._mutate((graph) => {
|
|
382
|
+
graph.edges.push(edge);
|
|
383
|
+
});
|
|
225
384
|
return edge;
|
|
226
385
|
}
|
|
227
386
|
addEdges(edges) {
|
|
228
387
|
return edges.map((edge) => this.addEdge(edge));
|
|
229
388
|
}
|
|
230
389
|
removeNode(id) {
|
|
231
|
-
|
|
232
|
-
|
|
390
|
+
let removedEdges = [];
|
|
391
|
+
let removedNodes = [];
|
|
392
|
+
this._mutate((graph) => {
|
|
393
|
+
removedEdges = removeBy(graph.edges, (edge) => edge.source === id || edge.target === id);
|
|
394
|
+
removedNodes = removeBy(graph.nodes, (node) => node.id === id);
|
|
395
|
+
});
|
|
233
396
|
return this.copy({
|
|
234
|
-
nodes,
|
|
235
|
-
edges
|
|
397
|
+
nodes: removedNodes,
|
|
398
|
+
edges: removedEdges
|
|
236
399
|
});
|
|
237
400
|
}
|
|
238
401
|
removeNodes(ids) {
|
|
@@ -240,10 +403,13 @@ var AbstractGraphModel = class extends ReadonlyGraphModel {
|
|
|
240
403
|
return this.copy().addGraphs(graphs);
|
|
241
404
|
}
|
|
242
405
|
removeEdge(id) {
|
|
243
|
-
|
|
406
|
+
let removedEdges = [];
|
|
407
|
+
this._mutate((graph) => {
|
|
408
|
+
removedEdges = removeBy(graph.edges, (edge) => edge.id === id);
|
|
409
|
+
});
|
|
244
410
|
return this.copy({
|
|
245
411
|
nodes: [],
|
|
246
|
-
edges
|
|
412
|
+
edges: removedEdges
|
|
247
413
|
});
|
|
248
414
|
}
|
|
249
415
|
removeEdges(ids) {
|
|
@@ -251,7 +417,11 @@ var AbstractGraphModel = class extends ReadonlyGraphModel {
|
|
|
251
417
|
return this.copy().addGraphs(graphs);
|
|
252
418
|
}
|
|
253
419
|
};
|
|
254
|
-
var
|
|
420
|
+
var AbstractBuilder = class {
|
|
421
|
+
_model;
|
|
422
|
+
constructor(_model) {
|
|
423
|
+
this._model = _model;
|
|
424
|
+
}
|
|
255
425
|
get model() {
|
|
256
426
|
return this._model;
|
|
257
427
|
}
|
|
@@ -278,175 +448,181 @@ var AbstractGraphBuilder = class {
|
|
|
278
448
|
this._model.addEdges(edges);
|
|
279
449
|
return this;
|
|
280
450
|
}
|
|
281
|
-
constructor(_model) {
|
|
282
|
-
_define_property(this, "_model", void 0);
|
|
283
|
-
this._model = _model;
|
|
284
|
-
}
|
|
285
451
|
};
|
|
286
452
|
var GraphModel = class _GraphModel extends AbstractGraphModel {
|
|
287
453
|
get builder() {
|
|
288
|
-
return new
|
|
454
|
+
return new Builder(this);
|
|
289
455
|
}
|
|
290
456
|
copy(graph) {
|
|
291
|
-
return new _GraphModel(
|
|
292
|
-
nodes: graph?.nodes ?? [],
|
|
293
|
-
edges: graph?.edges ?? []
|
|
294
|
-
});
|
|
457
|
+
return new _GraphModel(graph);
|
|
295
458
|
}
|
|
296
459
|
};
|
|
297
|
-
var subscribe = (model, cb, fire = false) => {
|
|
298
|
-
if (fire) {
|
|
299
|
-
cb(model, model.graph);
|
|
300
|
-
}
|
|
301
|
-
return effect(() => {
|
|
302
|
-
cb(model, model.graph);
|
|
303
|
-
});
|
|
304
|
-
};
|
|
305
460
|
var ReactiveGraphModel = class _ReactiveGraphModel extends GraphModel {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
return subscribe(this, cb, fire);
|
|
311
|
-
}
|
|
312
|
-
constructor(graph) {
|
|
313
|
-
super(live({
|
|
461
|
+
_registry;
|
|
462
|
+
_graphAtom;
|
|
463
|
+
constructor(_registry, graph) {
|
|
464
|
+
const initialGraph = {
|
|
314
465
|
nodes: graph?.nodes ?? [],
|
|
315
466
|
edges: graph?.edges ?? []
|
|
316
|
-
}
|
|
467
|
+
};
|
|
468
|
+
super(initialGraph), this._registry = _registry;
|
|
469
|
+
this._graphAtom = Atom2.make(initialGraph);
|
|
317
470
|
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
call(cb) {
|
|
321
|
-
cb(this);
|
|
322
|
-
return this;
|
|
471
|
+
get registry() {
|
|
472
|
+
return this._registry;
|
|
323
473
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
function _define_property2(obj, key, value) {
|
|
330
|
-
if (key in obj) {
|
|
331
|
-
Object.defineProperty(obj, key, {
|
|
332
|
-
value,
|
|
333
|
-
enumerable: true,
|
|
334
|
-
configurable: true,
|
|
335
|
-
writable: true
|
|
336
|
-
});
|
|
337
|
-
} else {
|
|
338
|
-
obj[key] = value;
|
|
474
|
+
/**
|
|
475
|
+
* Get the graph atom for reactive subscriptions.
|
|
476
|
+
*/
|
|
477
|
+
get graphAtom() {
|
|
478
|
+
return this._graphAtom;
|
|
339
479
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
var __dxlog_file3 = "/__w/dxos/dxos/packages/common/graph/src/selection.ts";
|
|
343
|
-
var SelectionModel = class {
|
|
344
|
-
toJSON() {
|
|
345
|
-
return {
|
|
346
|
-
selected: Array.from(this._selected.value.values())
|
|
347
|
-
};
|
|
480
|
+
get graph() {
|
|
481
|
+
return this._registry.get(this._graphAtom);
|
|
348
482
|
}
|
|
349
|
-
get
|
|
350
|
-
return this.
|
|
483
|
+
get nodes() {
|
|
484
|
+
return this.graph.nodes;
|
|
351
485
|
}
|
|
352
|
-
get
|
|
353
|
-
return this.
|
|
486
|
+
get edges() {
|
|
487
|
+
return this.graph.edges;
|
|
354
488
|
}
|
|
355
|
-
|
|
356
|
-
return this.
|
|
489
|
+
copy(graph) {
|
|
490
|
+
return new _ReactiveGraphModel(this._registry, graph);
|
|
357
491
|
}
|
|
358
492
|
clear() {
|
|
359
|
-
this.
|
|
493
|
+
this._registry.set(this._graphAtom, {
|
|
494
|
+
nodes: [],
|
|
495
|
+
edges: []
|
|
496
|
+
});
|
|
497
|
+
return this;
|
|
360
498
|
}
|
|
361
|
-
|
|
362
|
-
|
|
499
|
+
/**
|
|
500
|
+
* Set the entire graph at once, triggering a single notification.
|
|
501
|
+
*/
|
|
502
|
+
setGraph(graph) {
|
|
503
|
+
this._registry.set(this._graphAtom, graph);
|
|
504
|
+
return this;
|
|
505
|
+
}
|
|
506
|
+
addNode(node) {
|
|
507
|
+
invariant3(node.id, "ID is required", {
|
|
363
508
|
F: __dxlog_file3,
|
|
364
|
-
L:
|
|
509
|
+
L: 374,
|
|
365
510
|
S: this,
|
|
366
511
|
A: [
|
|
367
|
-
"id",
|
|
368
|
-
""
|
|
512
|
+
"node.id",
|
|
513
|
+
"'ID is required'"
|
|
369
514
|
]
|
|
370
515
|
});
|
|
371
|
-
this.
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
516
|
+
invariant3(!this.findNode(node.id), `node already exists: ${node.id}`, {
|
|
517
|
+
F: __dxlog_file3,
|
|
518
|
+
L: 375,
|
|
519
|
+
S: this,
|
|
520
|
+
A: [
|
|
521
|
+
"!this.findNode(node.id)",
|
|
522
|
+
"`node already exists: ${node.id}`"
|
|
523
|
+
]
|
|
524
|
+
});
|
|
525
|
+
const current = this._registry.get(this._graphAtom);
|
|
526
|
+
this._registry.set(this._graphAtom, {
|
|
527
|
+
...current,
|
|
528
|
+
nodes: [
|
|
529
|
+
...current.nodes,
|
|
530
|
+
node
|
|
531
|
+
]
|
|
532
|
+
});
|
|
533
|
+
return node;
|
|
377
534
|
}
|
|
378
|
-
|
|
379
|
-
invariant3(
|
|
535
|
+
addEdge(edge) {
|
|
536
|
+
invariant3(edge.source, void 0, {
|
|
380
537
|
F: __dxlog_file3,
|
|
381
|
-
L:
|
|
538
|
+
L: 385,
|
|
382
539
|
S: this,
|
|
383
540
|
A: [
|
|
384
|
-
"
|
|
541
|
+
"edge.source",
|
|
385
542
|
""
|
|
386
543
|
]
|
|
387
544
|
});
|
|
388
|
-
|
|
545
|
+
invariant3(edge.target, void 0, {
|
|
546
|
+
F: __dxlog_file3,
|
|
547
|
+
L: 386,
|
|
548
|
+
S: this,
|
|
549
|
+
A: [
|
|
550
|
+
"edge.target",
|
|
551
|
+
""
|
|
552
|
+
]
|
|
553
|
+
});
|
|
554
|
+
if (!edge.id) {
|
|
555
|
+
edge = {
|
|
556
|
+
id: createEdgeId(edge),
|
|
557
|
+
...edge
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
invariant3(!this.findNode(edge.id), void 0, {
|
|
561
|
+
F: __dxlog_file3,
|
|
562
|
+
L: 390,
|
|
563
|
+
S: this,
|
|
564
|
+
A: [
|
|
565
|
+
"!this.findNode(edge.id!)",
|
|
566
|
+
""
|
|
567
|
+
]
|
|
568
|
+
});
|
|
569
|
+
const current = this._registry.get(this._graphAtom);
|
|
570
|
+
this._registry.set(this._graphAtom, {
|
|
571
|
+
...current,
|
|
572
|
+
edges: [
|
|
573
|
+
...current.edges,
|
|
574
|
+
edge
|
|
575
|
+
]
|
|
576
|
+
});
|
|
577
|
+
return edge;
|
|
389
578
|
}
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
579
|
+
removeNode(id) {
|
|
580
|
+
const current = this._registry.get(this._graphAtom);
|
|
581
|
+
const removedEdges = current.edges.filter((edge) => edge.source === id || edge.target === id);
|
|
582
|
+
const removedNodes = current.nodes.filter((node) => node.id === id);
|
|
583
|
+
this._registry.set(this._graphAtom, {
|
|
584
|
+
nodes: current.nodes.filter((node) => node.id !== id),
|
|
585
|
+
edges: current.edges.filter((edge) => edge.source !== id && edge.target !== id)
|
|
586
|
+
});
|
|
587
|
+
return this.copy({
|
|
588
|
+
nodes: removedNodes,
|
|
589
|
+
edges: removedEdges
|
|
590
|
+
});
|
|
396
591
|
}
|
|
397
|
-
|
|
398
|
-
const
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
592
|
+
removeEdge(id) {
|
|
593
|
+
const current = this._registry.get(this._graphAtom);
|
|
594
|
+
const removedEdges = current.edges.filter((edge) => edge.id === id);
|
|
595
|
+
this._registry.set(this._graphAtom, {
|
|
596
|
+
...current,
|
|
597
|
+
edges: current.edges.filter((edge) => edge.id !== id)
|
|
598
|
+
});
|
|
599
|
+
return this.copy({
|
|
600
|
+
nodes: [],
|
|
601
|
+
edges: removedEdges
|
|
405
602
|
});
|
|
406
|
-
this._selected.value = set;
|
|
407
603
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
604
|
+
/**
|
|
605
|
+
* Subscribe to graph changes.
|
|
606
|
+
*/
|
|
607
|
+
subscribe(cb, fire = false) {
|
|
608
|
+
if (fire) {
|
|
609
|
+
cb(this, this.graph);
|
|
610
|
+
}
|
|
611
|
+
this._registry.get(this._graphAtom);
|
|
612
|
+
return this._registry.subscribe(this._graphAtom, () => {
|
|
613
|
+
cb(this, this.graph);
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
var Builder = class extends AbstractBuilder {
|
|
618
|
+
call(cb) {
|
|
619
|
+
cb(this);
|
|
620
|
+
return this;
|
|
415
621
|
}
|
|
416
622
|
};
|
|
417
|
-
|
|
418
|
-
// src/types.ts
|
|
419
|
-
import * as Schema from "effect/Schema";
|
|
420
|
-
var BaseGraphNode = Schema.Struct({
|
|
421
|
-
id: Schema.String,
|
|
422
|
-
type: Schema.optional(Schema.String),
|
|
423
|
-
data: Schema.optional(Schema.Any)
|
|
424
|
-
});
|
|
425
|
-
var BaseGraphEdge = Schema.Struct({
|
|
426
|
-
id: Schema.String,
|
|
427
|
-
type: Schema.optional(Schema.String),
|
|
428
|
-
source: Schema.String,
|
|
429
|
-
target: Schema.String,
|
|
430
|
-
data: Schema.optional(Schema.Any)
|
|
431
|
-
});
|
|
432
|
-
var Graph = Schema.Struct({
|
|
433
|
-
id: Schema.optional(Schema.String),
|
|
434
|
-
nodes: Schema.mutable(Schema.Array(BaseGraphNode)),
|
|
435
|
-
edges: Schema.mutable(Schema.Array(BaseGraphEdge))
|
|
436
|
-
});
|
|
437
623
|
export {
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
BaseGraphNode,
|
|
442
|
-
Graph,
|
|
443
|
-
GraphBuilder,
|
|
444
|
-
GraphModel,
|
|
445
|
-
ReactiveGraphModel,
|
|
446
|
-
ReadonlyGraphModel,
|
|
447
|
-
SelectionModel,
|
|
448
|
-
createEdgeId,
|
|
449
|
-
parseEdgeId,
|
|
450
|
-
subscribe
|
|
624
|
+
Graph_exports as Graph,
|
|
625
|
+
GraphModel_exports as GraphModel,
|
|
626
|
+
SelectionModel
|
|
451
627
|
};
|
|
452
628
|
//# sourceMappingURL=index.mjs.map
|