@likec4/core 0.32.0 → 0.33.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.
Files changed (106) hide show
  1. package/dist/colors.d.ts +76 -9
  2. package/dist/colors.js +116 -93
  3. package/dist/colors.mjs +112 -0
  4. package/dist/compute-view/EdgeBuilder.d.ts +0 -1
  5. package/dist/compute-view/EdgeBuilder.js +37 -34
  6. package/dist/compute-view/EdgeBuilder.mjs +31 -0
  7. package/dist/compute-view/compute-ctx.d.ts +0 -1
  8. package/dist/compute-view/compute-ctx.js +33 -24
  9. package/dist/compute-view/compute-ctx.mjs +42 -0
  10. package/dist/compute-view/compute-element-view.d.ts +2 -3
  11. package/dist/compute-view/compute-element-view.js +137 -525
  12. package/dist/compute-view/compute-element-view.mjs +145 -0
  13. package/dist/compute-view/compute-predicates.d.ts +16 -0
  14. package/dist/compute-view/compute-predicates.js +358 -0
  15. package/dist/compute-view/compute-predicates.mjs +324 -0
  16. package/dist/compute-view/compute.d.ts +8 -9
  17. package/dist/compute-view/compute.js +49 -39
  18. package/dist/compute-view/compute.mjs +39 -0
  19. package/dist/compute-view/index.d.ts +1 -1
  20. package/dist/compute-view/index.js +24 -2
  21. package/dist/compute-view/index.mjs +1 -0
  22. package/dist/compute-view/utils/anyPossibleRelations.d.ts +0 -1
  23. package/dist/compute-view/utils/anyPossibleRelations.js +17 -12
  24. package/dist/compute-view/utils/anyPossibleRelations.mjs +12 -0
  25. package/dist/compute-view/utils/applyViewRuleStyles.d.ts +3 -0
  26. package/dist/compute-view/utils/applyViewRuleStyles.js +59 -0
  27. package/dist/compute-view/utils/applyViewRuleStyles.mjs +50 -0
  28. package/dist/compute-view/utils/sortNodes.d.ts +0 -1
  29. package/dist/compute-view/utils/sortNodes.js +47 -42
  30. package/dist/compute-view/utils/sortNodes.mjs +40 -0
  31. package/dist/errors/_base.d.ts +0 -1
  32. package/dist/errors/_base.js +14 -11
  33. package/dist/errors/_base.mjs +4 -0
  34. package/dist/errors/index.d.ts +1 -1
  35. package/dist/errors/index.js +60 -5
  36. package/dist/errors/index.mjs +5 -0
  37. package/dist/errors/invariant.d.ts +0 -2
  38. package/dist/errors/invariant.js +16 -21
  39. package/dist/errors/invariant.mjs +8 -0
  40. package/dist/errors/model-index.d.ts +0 -1
  41. package/dist/errors/model-index.js +16 -14
  42. package/dist/errors/model-index.mjs +8 -0
  43. package/dist/errors/non-nullable.d.ts +2 -0
  44. package/dist/errors/non-nullable.js +16 -0
  45. package/dist/errors/non-nullable.mjs +8 -0
  46. package/dist/errors/nonexhaustive.d.ts +0 -1
  47. package/dist/errors/nonexhaustive.js +13 -6
  48. package/dist/errors/nonexhaustive.mjs +5 -0
  49. package/dist/index.d.ts +6 -3
  50. package/dist/index.js +71 -7
  51. package/dist/index.mjs +6 -0
  52. package/dist/model-index/ModelIndex.d.ts +0 -1
  53. package/dist/model-index/ModelIndex.js +125 -122
  54. package/dist/model-index/ModelIndex.mjs +128 -0
  55. package/dist/model-index/index.d.ts +0 -1
  56. package/dist/model-index/index.js +13 -2
  57. package/dist/model-index/index.mjs +1 -0
  58. package/dist/types/_common.d.ts +0 -1
  59. package/dist/types/_common.js +1 -2
  60. package/dist/types/_common.mjs +0 -0
  61. package/dist/types/computed-view.d.ts +7 -6
  62. package/dist/types/computed-view.js +1 -2
  63. package/dist/types/computed-view.mjs +0 -0
  64. package/dist/types/diagram.d.ts +5 -2
  65. package/dist/types/diagram.js +1 -2
  66. package/dist/types/diagram.mjs +0 -0
  67. package/dist/types/element.d.ts +3 -7
  68. package/dist/types/element.js +13 -5
  69. package/dist/types/element.mjs +5 -0
  70. package/dist/types/expression.d.ts +0 -1
  71. package/dist/types/expression.js +45 -31
  72. package/dist/types/expression.mjs +30 -0
  73. package/dist/types/index.d.ts +1 -1
  74. package/dist/types/index.js +48 -4
  75. package/dist/types/index.mjs +3 -0
  76. package/dist/types/model.d.ts +0 -1
  77. package/dist/types/model.js +1 -2
  78. package/dist/types/model.mjs +0 -0
  79. package/dist/types/opaque.d.ts +0 -1
  80. package/dist/types/opaque.js +1 -2
  81. package/dist/types/opaque.mjs +0 -0
  82. package/dist/types/relation.d.ts +0 -1
  83. package/dist/types/relation.js +1 -2
  84. package/dist/types/relation.mjs +0 -0
  85. package/dist/types/theme.d.ts +22 -0
  86. package/dist/types/theme.js +1 -0
  87. package/dist/types/theme.mjs +0 -0
  88. package/dist/types/view.d.ts +2 -2
  89. package/dist/types/view.js +15 -8
  90. package/dist/types/view.mjs +9 -0
  91. package/dist/utils/compute-node-levels.d.ts +0 -1
  92. package/dist/utils/compute-node-levels.js +45 -29
  93. package/dist/utils/compute-node-levels.mjs +31 -0
  94. package/dist/utils/fqn.d.ts +0 -1
  95. package/dist/utils/fqn.js +71 -71
  96. package/dist/utils/fqn.mjs +66 -0
  97. package/dist/utils/guards.d.ts +0 -1
  98. package/dist/utils/guards.js +19 -12
  99. package/dist/utils/guards.mjs +13 -0
  100. package/dist/utils/index.d.ts +0 -1
  101. package/dist/utils/index.js +49 -5
  102. package/dist/utils/index.mjs +4 -0
  103. package/dist/utils/relations.d.ts +0 -1
  104. package/dist/utils/relations.js +56 -49
  105. package/dist/utils/relations.mjs +42 -0
  106. package/package.json +20 -55
@@ -1,527 +1,139 @@
1
- import { anyPass, both, either, filter, isNil, uniq } from 'rambdax';
2
- import { allPass, find, isDefined } from 'remeda';
3
- import { invariant, nonexhaustive } from '../errors';
4
- import { DefaultElementShape, DefaultThemeColor } from '../types';
5
- import * as Expr from '../types/expression';
6
- import { isViewRuleAutoLayout, isViewRuleExpression, isViewRuleStyle } from '../types/view';
7
- import { Relations, commonAncestor, compareByFqnHierarchically, failUnexpected, isAncestor, isSameHierarchy, parentFqn } from '../utils';
8
- import { compareRelations, isAnyInOut, isBetween, isIncoming, isInside, isOutgoing } from '../utils/relations';
9
- import { EdgeBuilder } from './EdgeBuilder';
10
- import { ComputeCtx } from './compute-ctx';
11
- import { sortNodes } from './utils/sortNodes';
12
- function transformToNodes(elementsIterator) {
13
- return Array.from(elementsIterator)
14
- .sort(compareByFqnHierarchically)
15
- .reduce((map, { id, color, shape, ...el }) => {
16
- let parent = parentFqn(id);
17
- while (parent) {
18
- if (map.has(parent)) {
19
- break;
20
- }
21
- parent = parentFqn(parent);
22
- }
23
- if (parent) {
24
- const parentNd = map.get(parent);
25
- invariant(parentNd, `parent node ${parent} not found`);
26
- parentNd.children.push(id);
27
- }
28
- const node = {
29
- ...el,
30
- id,
31
- parent,
32
- color: color ?? DefaultThemeColor,
33
- shape: shape ?? DefaultElementShape,
34
- children: []
35
- };
36
- map.set(id, node);
37
- return map;
38
- }, new Map());
39
- }
40
- function applyViewRuleStyles(rules, nodes) {
41
- for (const rule of rules) {
42
- const predicates = [];
43
- if (!rule.style.color && !rule.style.shape) {
44
- // skip empty
45
- continue;
46
- }
47
- for (const target of rule.targets) {
48
- if (Expr.isWildcard(target)) {
49
- predicates.push(() => true);
50
- break;
51
- }
52
- if (Expr.isElementKindExpr(target)) {
53
- predicates.push(target.isEqual ? n => n.kind === target.elementKind : n => n.kind !== target.elementKind);
54
- continue;
55
- }
56
- if (Expr.isElementTagExpr(target)) {
57
- predicates.push(target.isEqual
58
- ? ({ tags }) => !!tags && tags.includes(target.elementTag)
59
- : ({ tags }) => isNil(tags) || !tags.includes(target.elementTag));
60
- continue;
61
- }
62
- if (Expr.isElementRef(target)) {
63
- const { element, isDescedants } = target;
64
- predicates.push(isDescedants ? n => n.id.startsWith(element + '.') : n => n.id === element);
65
- continue;
66
- }
67
- failUnexpected(target);
68
- }
69
- filter(anyPass(predicates), nodes).forEach(n => {
70
- n.shape = rule.style.shape ?? n.shape;
71
- n.color = rule.style.color ?? n.color;
72
- if (isDefined.strict(rule.style.icon)) {
73
- n.icon = rule.style.icon;
74
- }
75
- });
76
- }
77
- return nodes;
78
- }
79
- const includeElementRef = (ctx, expr) => {
80
- const elements = expr.isDescedants
81
- ? ctx.index.children(expr.element)
82
- : [ctx.index.find(expr.element)];
83
- const filters = [];
84
- if (expr.isDescedants) {
85
- elements.forEach(child => {
86
- filters.push(both(Relations.isInside(expr.element), Relations.isAnyInOut(child.id)));
87
- });
88
- }
89
- const ctxElements = uniq([...ctx.elements, ...ctx.implicits]);
90
- const excludeImplicits = [];
91
- for (const el of elements) {
92
- if (ctx.root && ctx.root !== el.id && !isAncestor(el.id, ctx.root)) {
93
- excludeImplicits.push(...[...ctx.implicits].filter(impl => isAncestor(el.id, impl.id)));
94
- }
95
- for (const ctxEl of ctxElements) {
96
- if (!isSameHierarchy(el, ctxEl)) {
97
- filters.push(Relations.isAnyBetween(el.id, ctxEl.id));
98
- }
99
- }
100
- }
101
- const relations = filters.length ? ctx.index.filterRelations(anyPass(filters)) : [];
102
- if (excludeImplicits.length == 0) {
103
- return ctx.include({ elements, relations });
104
- }
105
- return ctx.include({ elements, relations }).exclude({ implicits: excludeImplicits });
106
- };
107
- const excludeElementRef = (ctx, expr) => {
108
- const elements = expr.isDescedants
109
- ? ctx.index.children(expr.element)
110
- : [ctx.index.find(expr.element)];
111
- const elementsIds = elements.map(e => e.id);
112
- const relations = [...ctx.relations].filter(r => elementsIds.includes(r.source) || elementsIds.includes(r.target));
113
- return ctx.exclude({ elements, relations });
114
- };
115
- const asElementPredicate = (expr) => {
116
- if (expr.isEqual) {
117
- if (Expr.isElementKindExpr(expr)) {
118
- return e => e.kind === expr.elementKind;
119
- }
120
- else {
121
- return ({ tags }) => !!tags && tags.includes(expr.elementTag);
122
- }
123
- }
124
- else {
125
- if (Expr.isElementKindExpr(expr)) {
126
- return e => e.kind !== expr.elementKind;
127
- }
128
- else {
129
- return ({ tags }) => isNil(tags) || tags.length === 0 || !tags.includes(expr.elementTag);
130
- }
131
- }
132
- };
133
- const includeElementKindOrTag = (ctx, expr) => {
134
- const elements = ctx.index.elements.filter(asElementPredicate(expr));
135
- return elements.reduce((accCtx, el) => includeElementRef(accCtx, {
136
- element: el.id,
137
- isDescedants: false
138
- }), ctx);
139
- };
140
- const excludeElementKindOrTag = (ctx, expr) => {
141
- const predicate = asElementPredicate(expr);
142
- const elements = [...ctx.elements].filter(predicate);
143
- const implicits = [...ctx.implicits].filter(predicate);
144
- return ctx.exclude({ elements, implicits });
145
- };
146
- const includeWildcardRef = (ctx, _expr) => {
147
- const { root } = ctx;
148
- if (root) {
149
- const elements = [ctx.index.find(root), ...ctx.index.children(root)];
150
- // All neighbors that may have relations with root or its children
151
- const allImplicits = [
152
- ...ctx.index.siblings(root),
153
- ...ctx.index.ancestors(root).flatMap(a => [...ctx.index.siblings(a.id)])
154
- ];
155
- const inOut = ctx.index.filterRelations(isAnyInOut(root));
156
- // Neighbors that have relations with root or its children
157
- const implicits = [];
158
- for (const implicit of allImplicits) {
159
- if (inOut.some(isAnyInOut(implicit.id))) {
160
- implicits.push(implicit);
161
- }
162
- }
163
- return ctx.include({
164
- elements,
165
- relations: [...ctx.index.filterRelations(isInside(root)), ...inOut],
166
- implicits: implicits
167
- });
168
- }
169
- else {
170
- // Take root elements
171
- const elements = ctx.index.rootElements();
172
- if (elements.length <= 1) {
173
- return new ComputeCtx(ctx.index, ctx.root, new Set(elements));
174
- }
175
- // Only relations without common ancenstors, i.e. between hierarchies
176
- const relations = ctx.index.filterRelations(rel => isNil(commonAncestor(rel.source, rel.target)));
177
- return ctx.include({
178
- elements,
179
- relations
180
- });
181
- }
182
- };
183
- const excludeWildcardRef = (ctx, _expr) => {
184
- const { root } = ctx;
185
- if (root) {
186
- const relations = [...ctx.relations].filter(either(isInside(root), isAnyInOut(root)));
187
- return ctx.exclude({
188
- elements: [...ctx.elements].filter(e => e.id === root || isAncestor(root, e.id)),
189
- relations,
190
- implicits: [...ctx.implicits].filter(e => relations.some(isAnyInOut(e.id)))
191
- });
192
- }
193
- else {
194
- return new ComputeCtx(ctx.index, ctx.root);
195
- }
196
- };
197
- const resolveElements = (ctx, expr) => {
198
- if (Expr.isWildcard(expr)) {
199
- if (ctx.root) {
200
- return [ctx.index.find(ctx.root)];
201
- }
202
- else {
203
- return ctx.index.rootElements();
204
- }
205
- }
206
- if (Expr.isElementKindExpr(expr)) {
207
- return ctx.index.elements.filter(el => {
208
- if (expr.isEqual) {
209
- return el.kind === expr.elementKind;
210
- }
211
- return el.kind !== expr.elementKind;
212
- });
213
- }
214
- if (Expr.isElementTagExpr(expr)) {
215
- return ctx.index.elements.filter(el => {
216
- const tags = el.tags;
217
- if (expr.isEqual) {
218
- return !!tags && tags.includes(expr.elementTag);
219
- }
220
- return isNil(tags) || tags.length === 0 || !tags.includes(expr.elementTag);
221
- });
222
- }
223
- if (expr.isDescedants) {
224
- return ctx.index.children(expr.element);
225
- }
226
- else {
227
- return [ctx.index.find(expr.element)];
228
- }
229
- };
230
- const includeIncomingExpr = (ctx, expr) => {
231
- if (Expr.isWildcard(expr.incoming) && !ctx.root) {
232
- return ctx;
233
- }
234
- const elements = resolveElements(ctx, expr.incoming);
235
- if (elements.length === 0) {
236
- return ctx;
237
- }
238
- // '-> element.*' should include only "outside" relations.
239
- // From the outside to the element descendants,
240
- let allrelations = ctx.index.relations;
241
- if (Expr.isElementRef(expr.incoming)) {
242
- allrelations = allrelations.filter(isIncoming(expr.incoming.element));
243
- }
244
- const implicits = [];
245
- const relations = [];
246
- for (const el of elements) {
247
- const elRelations = allrelations.filter(isIncoming(el.id));
248
- if (elRelations.length > 0) {
249
- relations.push(...elRelations);
250
- implicits.push(el);
251
- }
252
- }
253
- return ctx.include({
254
- relations,
255
- implicits
256
- });
257
- };
258
- const excludeIncomingExpr = (ctx, expr) => {
259
- if (Expr.isWildcard(expr.incoming) && !ctx.root) {
260
- return ctx;
261
- }
262
- const elements = resolveElements(ctx, expr.incoming);
263
- if (elements.length === 0) {
264
- return ctx;
265
- }
266
- const isIncomings = anyPass(elements.map(el => isIncoming(el.id)));
267
- const excluded = [...ctx.relations].filter(isIncomings);
268
- if (excluded.length > 0) {
269
- return ctx.exclude({
270
- relations: excluded
271
- });
272
- }
273
- return ctx;
274
- };
275
- const includeOutgoingExpr = (ctx, expr) => {
276
- if (Expr.isWildcard(expr.outgoing) && !ctx.root) {
277
- return ctx;
278
- }
279
- const elements = resolveElements(ctx, expr.outgoing);
280
- if (elements.length === 0) {
281
- return ctx;
282
- }
283
- // 'element.* -> ' should include only "outside" relations.
284
- // From element descendants to outside of 'element.*'
285
- let allrelations = ctx.index.relations;
286
- if (Expr.isElementRef(expr.outgoing)) {
287
- allrelations = allrelations.filter(isOutgoing(expr.outgoing.element));
288
- }
289
- const implicits = [];
290
- const relations = [];
291
- for (const el of elements) {
292
- const elRelations = allrelations.filter(isOutgoing(el.id));
293
- if (elRelations.length > 0) {
294
- relations.push(...elRelations);
295
- implicits.push(el);
296
- }
297
- }
298
- return ctx.include({
299
- relations,
300
- implicits
301
- });
302
- };
303
- const excludeOutgoingExpr = (ctx, expr) => {
304
- if (Expr.isWildcard(expr.outgoing) && !ctx.root) {
305
- return ctx;
306
- }
307
- const elements = resolveElements(ctx, expr.outgoing);
308
- if (elements.length === 0) {
309
- return ctx;
310
- }
311
- const isOutgoings = anyPass(elements.map(el => isOutgoing(el.id)));
312
- const excluded = [...ctx.relations].filter(isOutgoings);
313
- if (excluded.length > 0) {
314
- return ctx.exclude({
315
- relations: excluded
316
- });
317
- }
318
- return ctx;
319
- };
320
- const includeInOutExpr = (ctx, expr) => {
321
- if (Expr.isWildcard(expr.inout) && !ctx.root) {
322
- return ctx;
323
- }
324
- const targets = resolveElements(ctx, expr.inout);
325
- if (targets.length === 0) {
326
- return ctx;
327
- }
328
- // '-> element.* -> ' should include only "outside" relations.
329
- let allrelations = ctx.index.relations;
330
- if (Expr.isElementRef(expr.inout)) {
331
- allrelations = allrelations.filter(isAnyInOut(expr.inout.element));
332
- }
333
- const implicits = [];
334
- const relations = [];
335
- for (const target of targets) {
336
- const foundRelations = allrelations.filter(isAnyInOut(target.id));
337
- if (foundRelations.length > 0) {
338
- relations.push(...foundRelations);
339
- implicits.push(target);
340
- }
341
- }
342
- return ctx.include({
343
- relations,
344
- implicits
345
- });
346
- };
347
- const excludeInOutExpr = (ctx, expr) => {
348
- if (Expr.isWildcard(expr.inout) && !ctx.root) {
349
- return ctx;
350
- }
351
- const targets = resolveElements(ctx, expr.inout);
352
- if (targets.length === 0) {
353
- return ctx;
354
- }
355
- const excluded = [...ctx.relations].filter(anyPass(targets.map(t => isAnyInOut(t.id))));
356
- if (excluded.length > 0) {
357
- return ctx.exclude({
358
- relations: excluded
359
- });
360
- }
361
- return ctx;
362
- };
363
- const includeRelationExpr = (ctx, expr) => {
364
- const sources = resolveElements(ctx, expr.source);
365
- const targets = resolveElements(ctx, expr.target);
366
- if (sources.length === 0 || targets.length === 0) {
367
- return ctx;
368
- }
369
- const implicits = [];
370
- const relations = [];
371
- for (const source of sources) {
372
- for (const target of targets) {
373
- if (isSameHierarchy(source, target)) {
374
- continue;
375
- }
376
- const foundRelations = ctx.index.filterRelations(isBetween(source.id, target.id));
377
- if (foundRelations.length > 0) {
378
- relations.push(...foundRelations);
379
- implicits.push(source, target);
380
- }
381
- }
382
- }
383
- return ctx.include({
384
- relations,
385
- implicits
386
- });
387
- };
388
- const excludeRelationExpr = (ctx, expr) => {
389
- const sources = resolveElements(ctx, expr.source);
390
- const targets = resolveElements(ctx, expr.target);
391
- const filters = [];
392
- for (const source of sources) {
393
- for (const target of targets) {
394
- if (isSameHierarchy(source, target)) {
395
- continue;
396
- }
397
- filters.push(isBetween(source.id, target.id));
398
- }
399
- }
400
- if (filters.length == 0) {
401
- return ctx;
402
- }
403
- const excluded = [...ctx.relations].filter(anyPass(filters));
404
- if (excluded.length == 0) {
405
- return ctx;
406
- }
407
- return ctx.exclude({
408
- relations: excluded
409
- });
410
- };
411
- export function computeElementView(view, index) {
412
- const rootElement = view.viewOf ?? null;
413
- let ctx = new ComputeCtx(index, rootElement);
414
- const rulesInclude = view.rules.filter(isViewRuleExpression);
415
- if (rootElement && rulesInclude.length == 0) {
416
- ctx = ctx.include({
417
- elements: [index.find(rootElement)]
418
- });
419
- }
420
- for (const { isInclude, exprs } of rulesInclude) {
421
- for (const expr of exprs) {
422
- if (Expr.isElementKindExpr(expr) || Expr.isElementTagExpr(expr)) {
423
- ctx = isInclude ? includeElementKindOrTag(ctx, expr) : excludeElementKindOrTag(ctx, expr);
424
- continue;
425
- }
426
- if (Expr.isElementRef(expr)) {
427
- ctx = isInclude ? includeElementRef(ctx, expr) : excludeElementRef(ctx, expr);
428
- continue;
429
- }
430
- if (Expr.isWildcard(expr)) {
431
- ctx = isInclude ? includeWildcardRef(ctx, expr) : excludeWildcardRef(ctx, expr);
432
- continue;
433
- }
434
- if (Expr.isIncoming(expr)) {
435
- ctx = isInclude ? includeIncomingExpr(ctx, expr) : excludeIncomingExpr(ctx, expr);
436
- continue;
437
- }
438
- if (Expr.isOutgoing(expr)) {
439
- ctx = isInclude ? includeOutgoingExpr(ctx, expr) : excludeOutgoingExpr(ctx, expr);
440
- continue;
441
- }
442
- if (Expr.isInOut(expr)) {
443
- ctx = isInclude ? includeInOutExpr(ctx, expr) : excludeInOutExpr(ctx, expr);
444
- continue;
445
- }
446
- if (Expr.isRelation(expr)) {
447
- ctx = isInclude ? includeRelationExpr(ctx, expr) : excludeRelationExpr(ctx, expr);
448
- continue;
449
- }
450
- nonexhaustive(expr);
451
- }
452
- }
453
- // All "predicated" elements (including implicit ones)
454
- const allElements = [...ctx.elements, ...ctx.implicits].sort(compareByFqnHierarchically).reverse();
455
- // Filtered "allElements", only with relations
456
- const elementsWithRelations = new Set();
457
- const edgeBuilder = new EdgeBuilder();
458
- const anscestorOf = (id) => (e) => e.id === id || isAncestor(e.id, id);
459
- // Step 1: Add explicit relations
460
- const sortedRelations = [...ctx.relations].sort(compareRelations);
461
- for (const rel of sortedRelations) {
462
- const source = find(allElements, anscestorOf(rel.source));
463
- if (!source) {
464
- continue;
465
- }
466
- const target = find(allElements, allPass([anscestorOf(rel.target), e => !isSameHierarchy(e, source)]));
467
- if (!target) {
468
- continue;
469
- }
470
- elementsWithRelations.add(source);
471
- elementsWithRelations.add(target);
472
- edgeBuilder.add(source.id, target.id, rel);
473
- }
474
- const elements = new Set([...elementsWithRelations, ...ctx.elements]);
475
- // Step 2: Add implicit relations
476
- // if (relationsWithImplicits.length > 0) {
477
- // elmnts = [...ctx.elements, ...ctx.implicits].sort(compareByFqnHierarchically).reverse()
478
- // for (const rel of relationsWithImplicits) {
479
- // const source = find(elmnts, anscestorOf(rel.source))
480
- // if (!source) {
481
- // continue
482
- // }
483
- // const target = find(elmnts, anscestorOf(rel.target))
484
- // if (!target || isSameHierarchy(source, target)) {
485
- // continue
486
- // }
487
- // elements.add(source)
488
- // elements.add(target)
489
- // edgeBuilder.add(source.id, target.id, rel)
490
- // }
491
- // }
492
- // for (const [source, target] of anyPossibleRelations([...elements, ...ctx.implicits])) {
493
- // if (!elements.has(source) && !elements.has(target)) {
494
- // continue
495
- // }
496
- // const findPredicate = Relations.isBetween(source.id, target.id)
497
- // let idx = resolvedRelations.findIndex(findPredicate)
498
- // while (idx >= 0) {
499
- // const [rel] = resolvedRelations.splice(idx, 1)
500
- // if (rel) {
501
- // elements.add(source)
502
- // elements.add(target)
503
- // edgeBuilder.add(source.id, target.id, rel)
504
- // }
505
- // idx = resolvedRelations.findIndex(findPredicate)
506
- // }
507
- // }
508
- const nodesreg = transformToNodes(elements);
509
- const edges = edgeBuilder.build().map(edge => {
510
- while (edge.parent) {
511
- if (nodesreg.has(edge.parent)) {
512
- break;
513
- }
514
- edge.parent = parentFqn(edge.parent);
515
- }
516
- return edge;
517
- });
518
- const nodes = applyViewRuleStyles(view.rules.filter(isViewRuleStyle), sortNodes(nodesreg, edges));
519
- const autoLayoutRule = view.rules.find(isViewRuleAutoLayout);
520
- return {
521
- ...view,
522
- autoLayout: autoLayoutRule?.autoLayout ?? 'TB',
523
- nodes,
524
- edges
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.computeElementView = computeElementView;
7
+ var _remeda = require("remeda");
8
+ var _errors = require("../errors");
9
+ var _types = require("../types");
10
+ var Expr = _interopRequireWildcard(require("../types/expression"));
11
+ var _view = require("../types/view");
12
+ var _utils = require("../utils");
13
+ var _EdgeBuilder = require("./EdgeBuilder");
14
+ var _computeCtx = require("./compute-ctx");
15
+ var _computePredicates = require("./compute-predicates");
16
+ var _applyViewRuleStyles = require("./utils/applyViewRuleStyles");
17
+ var _sortNodes = require("./utils/sortNodes");
18
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
19
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
20
+ function reduceToMap(elementsIterator) {
21
+ return Array.from(elementsIterator).sort(_utils.compareByFqnHierarchically).reduce((map, {
22
+ id,
23
+ color,
24
+ shape,
25
+ ...el
26
+ }) => {
27
+ let parent = (0, _utils.parentFqn)(id);
28
+ while (parent) {
29
+ if (map.has(parent)) {
30
+ break;
31
+ }
32
+ parent = (0, _utils.parentFqn)(parent);
33
+ }
34
+ if (parent) {
35
+ const parentNd = (0, _errors.nonNullable)(map.get(parent));
36
+ parentNd.children.push(id);
37
+ }
38
+ const node = {
39
+ ...el,
40
+ id,
41
+ parent,
42
+ color: color ?? _types.DefaultThemeColor,
43
+ shape: shape ?? _types.DefaultElementShape,
44
+ children: [],
45
+ inEdges: [],
46
+ outEdges: []
525
47
  };
48
+ map.set(id, node);
49
+ return map;
50
+ }, /* @__PURE__ */new Map());
526
51
  }
527
- //# sourceMappingURL=compute-element-view.js.map
52
+ function computeElementView(view, index) {
53
+ const rootElement = view.viewOf ?? null;
54
+ let ctx = new _computeCtx.ComputeCtx(index, rootElement);
55
+ const rulesInclude = view.rules.filter(_view.isViewRuleExpression);
56
+ if (rootElement && rulesInclude.length == 0) {
57
+ ctx = ctx.include({
58
+ elements: [index.find(rootElement)]
59
+ });
60
+ }
61
+ for (const {
62
+ isInclude,
63
+ exprs
64
+ } of rulesInclude) {
65
+ for (const expr of exprs) {
66
+ if (Expr.isElementKindExpr(expr) || Expr.isElementTagExpr(expr)) {
67
+ ctx = isInclude ? (0, _computePredicates.includeElementKindOrTag)(ctx, expr) : (0, _computePredicates.excludeElementKindOrTag)(ctx, expr);
68
+ continue;
69
+ }
70
+ if (Expr.isElementRef(expr)) {
71
+ ctx = isInclude ? (0, _computePredicates.includeElementRef)(ctx, expr) : (0, _computePredicates.excludeElementRef)(ctx, expr);
72
+ continue;
73
+ }
74
+ if (Expr.isWildcard(expr)) {
75
+ ctx = isInclude ? (0, _computePredicates.includeWildcardRef)(ctx, expr) : (0, _computePredicates.excludeWildcardRef)(ctx, expr);
76
+ continue;
77
+ }
78
+ if (Expr.isIncoming(expr)) {
79
+ ctx = isInclude ? (0, _computePredicates.includeIncomingExpr)(ctx, expr) : (0, _computePredicates.excludeIncomingExpr)(ctx, expr);
80
+ continue;
81
+ }
82
+ if (Expr.isOutgoing(expr)) {
83
+ ctx = isInclude ? (0, _computePredicates.includeOutgoingExpr)(ctx, expr) : (0, _computePredicates.excludeOutgoingExpr)(ctx, expr);
84
+ continue;
85
+ }
86
+ if (Expr.isInOut(expr)) {
87
+ ctx = isInclude ? (0, _computePredicates.includeInOutExpr)(ctx, expr) : (0, _computePredicates.excludeInOutExpr)(ctx, expr);
88
+ continue;
89
+ }
90
+ if (Expr.isRelation(expr)) {
91
+ ctx = isInclude ? (0, _computePredicates.includeRelationExpr)(ctx, expr) : (0, _computePredicates.excludeRelationExpr)(ctx, expr);
92
+ continue;
93
+ }
94
+ (0, _errors.nonexhaustive)(expr);
95
+ }
96
+ }
97
+ const allElements = [...ctx.elements, ...ctx.implicits].sort(_utils.compareByFqnHierarchically).reverse();
98
+ const elementsWithRelations = /* @__PURE__ */new Set();
99
+ const edgeBuilder = new _EdgeBuilder.EdgeBuilder();
100
+ const anscestorOf = id => e => e.id === id || (0, _utils.isAncestor)(e.id, id);
101
+ const sortedRelations = [...ctx.relations];
102
+ for (const rel of sortedRelations) {
103
+ const source = (0, _remeda.find)(allElements, anscestorOf(rel.source));
104
+ if (!source) {
105
+ continue;
106
+ }
107
+ const target = (0, _remeda.find)(allElements, (0, _remeda.allPass)([anscestorOf(rel.target), e => !(0, _utils.isSameHierarchy)(e, source)]));
108
+ if (!target) {
109
+ continue;
110
+ }
111
+ elementsWithRelations.add(source);
112
+ elementsWithRelations.add(target);
113
+ edgeBuilder.add(source.id, target.id, rel);
114
+ }
115
+ const elements = /* @__PURE__ */new Set([...ctx.elements, ...elementsWithRelations]);
116
+ const nodesMap = reduceToMap(elements);
117
+ const edges = edgeBuilder.build().map(edge => {
118
+ while (edge.parent) {
119
+ if (nodesMap.has(edge.parent)) {
120
+ break;
121
+ }
122
+ edge.parent = (0, _utils.parentFqn)(edge.parent);
123
+ }
124
+ (0, _errors.nonNullable)(nodesMap.get(edge.source)).outEdges.push(edge.id);
125
+ (0, _errors.nonNullable)(nodesMap.get(edge.target)).inEdges.push(edge.id);
126
+ return edge;
127
+ });
128
+ const nodes = (0, _applyViewRuleStyles.applyViewRuleStyles)(view.rules.filter(_view.isViewRuleStyle), (0, _sortNodes.sortNodes)(nodesMap, edges)
129
+ // map(e => nodesMap.get(e.id)!, Array.from(elements))
130
+ );
131
+
132
+ const autoLayoutRule = view.rules.find(_view.isViewRuleAutoLayout);
133
+ return {
134
+ ...view,
135
+ autoLayout: autoLayoutRule?.autoLayout ?? "TB",
136
+ nodes,
137
+ edges
138
+ };
139
+ }