@likec4/core 1.10.0 → 1.11.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.
@@ -1 +1,2 @@
1
- export { aq as AndOperator, A as AsFqn, aP as AutoLayoutDirection, bl as BBox, aU as BasicElementView, aT as BasicView, o as BorderStyle, B as BorderStyles, e as Color, aE as ColorLiteral, bi as ComputedDynamicView, f as ComputedEdge, bh as ComputedElementView, g as ComputedLikeC4Model, b as ComputedNode, C as ComputedView, m as CustomColor, b4 as CustomColorDefinitions, G as CustomElementExpr, ad as CustomRelationExpr, aA as DefaultArrowType, q as DefaultElementShape, az as DefaultLineStyle, aB as DefaultRelationshipColor, D as DefaultThemeColor, bo as DiagramEdge, bn as DiagramNode, bp as DiagramView, b2 as DynamicView, a$ as DynamicViewIncludeRule, aZ as DynamicViewParallelSteps, b1 as DynamicViewRule, aY as DynamicViewStep, a_ as DynamicViewStepOrParallel, E as EdgeId, h as Element, U as ElementExpression, c as ElementKind, M as ElementKindExpr, v as ElementKindSpecification, u as ElementKindSpecificationStyle, bs as ElementNotation, $ as ElementPredicateExpression, w as ElementRefExpr, d as ElementShape, p as ElementShapes, r as ElementStyle, Q as ElementTagExpr, aG as ElementThemeColorValues, aH as ElementThemeColors, aX as ElementView, Z as ElementWhereExpr, aj as EqualOperator, y as ExpandedElementExpr, l as Expr, ah as Expression, aW as ExtendsElementView, F as Fqn, H as HexColorLiteral, I as IconUrl, a3 as InOutExpr, a5 as IncomingExpr, am as KindEqual, aK as LikeC4Theme, b5 as LikeC4View, t as Link, N as NodeId, k as NonEmptyArray, ao as NotOperator, as as OrOperator, a7 as OutgoingExpr, aw as OverviewGraph, ai as ParsedLikeC4Model, n as Point, i as Relation, a1 as RelationExpr, a9 as RelationExpression, R as RelationID, af as RelationPredicateExpression, ab as RelationWhereExpr, ay as RelationshipArrowType, j as RelationshipKind, aC as RelationshipKindSpecification, ax as RelationshipLineType, aI as RelationshipThemeColorValues, aJ as RelationshipThemeColors, aV as ScopedElementView, bb as StepEdgeId, ba as StepEdgeIdLiteral, a as Tag, ak as TagEqual, s as TagSpec, aD as ThemeColor, T as ThemeColorValues, br as ViewChange, V as ViewID, bq as ViewManualLayout, aS as ViewRule, aQ as ViewRuleAutoLayout, aL as ViewRulePredicate, aN as ViewRuleStyle, bf as ViewWithHash, bg as ViewWithNotation, au as WhereOperator, W as WildcardExpr, X as XYPoint, bd as extractStep, bm as getBBoxCenter, be as getParallelStepsPrefix, ar as isAndOperator, bj as isComputedDynamicView, bk as isComputedElementView, J as isCustomElement, ae as isCustomRelationExpr, b6 as isDynamicView, b0 as isDynamicViewIncludeRule, b3 as isDynamicViewParallelSteps, Y as isElement, O as isElementKindExpr, a0 as isElementPredicateExpr, x as isElementRef, S as isElementTagExpr, b7 as isElementView, _ as isElementWhere, z as isExpandedElementExpr, b8 as isExtendsElementView, a4 as isInOut, a6 as isIncoming, an as isKindEqual, ap as isNotOperator, at as isOrOperator, a8 as isOutgoing, a2 as isRelation, aa as isRelationExpression, ag as isRelationPredicateExpr, ac as isRelationWhere, b9 as isScopedElementView, bc as isStepEdgeId, al as isTagEqual, aF as isThemeColor, aR as isViewRuleAutoLayout, aM as isViewRulePredicate, aO as isViewRuleStyle, K as isWildcard, av as whereOperatorAsPredicate } from '../shared/core.CpR2fq5B.mjs';
1
+ export { at as AndOperator, A as AsFqn, aS as AutoLayoutDirection, bo as BBox, aX as BasicElementView, aW as BasicView, q as BorderStyle, B as BorderStyles, e as Color, aH as ColorLiteral, bl as ComputedDynamicView, f as ComputedEdge, bk as ComputedElementView, g as ComputedLikeC4Model, b as ComputedNode, C as ComputedView, p as CustomColor, b7 as CustomColorDefinitions, M as CustomElementExpr, ag as CustomRelationExpr, aD as DefaultArrowType, t as DefaultElementShape, aC as DefaultLineStyle, aE as DefaultRelationshipColor, s as DefaultThemeColor, l as DiagramEdge, k as DiagramNode, D as DiagramView, b5 as DynamicView, b2 as DynamicViewIncludeRule, b0 as DynamicViewParallelSteps, b4 as DynamicViewRule, a$ as DynamicViewStep, b1 as DynamicViewStepOrParallel, E as EdgeId, h as Element, _ as ElementExpression, c as ElementKind, S as ElementKindExpr, y as ElementKindSpecification, x as ElementKindSpecificationStyle, bs as ElementNotation, a2 as ElementPredicateExpression, z as ElementRefExpr, d as ElementShape, r as ElementShapes, u as ElementStyle, Y as ElementTagExpr, aJ as ElementThemeColorValues, aK as ElementThemeColors, a_ as ElementView, a0 as ElementWhereExpr, am as EqualOperator, J as ExpandedElementExpr, ak as Expression, aZ as ExtendsElementView, F as Fqn, H as HexColorLiteral, I as IconUrl, a6 as InOutExpr, a8 as IncomingExpr, ap as KindEqual, L as LayoutedLikeC4Model, aN as LikeC4Theme, b8 as LikeC4View, w as Link, N as NodeId, m as NonEmptyArray, o as NonEmptyReadonlyArray, ar as NotOperator, av as OrOperator, aa as OutgoingExpr, az as OverviewGraph, al as ParsedLikeC4Model, P as Point, i as Relation, a4 as RelationExpr, ac as RelationExpression, R as RelationID, ai as RelationPredicateExpression, ae as RelationWhereExpr, aB as RelationshipArrowType, j as RelationshipKind, aF as RelationshipKindSpecification, aA as RelationshipLineType, aL as RelationshipThemeColorValues, aM as RelationshipThemeColors, aY as ScopedElementView, be as StepEdgeId, bd as StepEdgeIdLiteral, a as Tag, an as TagEqual, v as TagSpec, aG as ThemeColor, T as ThemeColorValues, br as ViewChange, V as ViewID, bq as ViewManualLayout, aV as ViewRule, aT as ViewRuleAutoLayout, aO as ViewRulePredicate, aQ as ViewRuleStyle, bi as ViewWithHash, bj as ViewWithNotation, ax as WhereOperator, W as WildcardExpr, X as XYPoint, bg as extractStep, bp as getBBoxCenter, bh as getParallelStepsPrefix, au as isAndOperator, bm as isComputedDynamicView, bn as isComputedElementView, O as isCustomElement, ah as isCustomRelationExpr, b9 as isDynamicView, b3 as isDynamicViewIncludeRule, b6 as isDynamicViewParallelSteps, $ as isElement, U as isElementKindExpr, a3 as isElementPredicateExpr, G as isElementRef, Z as isElementTagExpr, ba as isElementView, a1 as isElementWhere, K as isExpandedElementExpr, bb as isExtendsElementView, a7 as isInOut, a9 as isIncoming, aq as isKindEqual, as as isNotOperator, aw as isOrOperator, ab as isOutgoing, a5 as isRelation, ad as isRelationExpression, aj as isRelationPredicateExpr, af as isRelationWhere, bc as isScopedElementView, bf as isStepEdgeId, ao as isTagEqual, aI as isThemeColor, aU as isViewRuleAutoLayout, aP as isViewRulePredicate, aR as isViewRuleStyle, Q as isWildcard, ay as whereOperatorAsPredicate } from '../shared/core.aVt8heZL.mjs';
2
+ import 'type-fest';
@@ -1 +1,2 @@
1
- export { aq as AndOperator, A as AsFqn, aP as AutoLayoutDirection, bl as BBox, aU as BasicElementView, aT as BasicView, o as BorderStyle, B as BorderStyles, e as Color, aE as ColorLiteral, bi as ComputedDynamicView, f as ComputedEdge, bh as ComputedElementView, g as ComputedLikeC4Model, b as ComputedNode, C as ComputedView, m as CustomColor, b4 as CustomColorDefinitions, G as CustomElementExpr, ad as CustomRelationExpr, aA as DefaultArrowType, q as DefaultElementShape, az as DefaultLineStyle, aB as DefaultRelationshipColor, D as DefaultThemeColor, bo as DiagramEdge, bn as DiagramNode, bp as DiagramView, b2 as DynamicView, a$ as DynamicViewIncludeRule, aZ as DynamicViewParallelSteps, b1 as DynamicViewRule, aY as DynamicViewStep, a_ as DynamicViewStepOrParallel, E as EdgeId, h as Element, U as ElementExpression, c as ElementKind, M as ElementKindExpr, v as ElementKindSpecification, u as ElementKindSpecificationStyle, bs as ElementNotation, $ as ElementPredicateExpression, w as ElementRefExpr, d as ElementShape, p as ElementShapes, r as ElementStyle, Q as ElementTagExpr, aG as ElementThemeColorValues, aH as ElementThemeColors, aX as ElementView, Z as ElementWhereExpr, aj as EqualOperator, y as ExpandedElementExpr, l as Expr, ah as Expression, aW as ExtendsElementView, F as Fqn, H as HexColorLiteral, I as IconUrl, a3 as InOutExpr, a5 as IncomingExpr, am as KindEqual, aK as LikeC4Theme, b5 as LikeC4View, t as Link, N as NodeId, k as NonEmptyArray, ao as NotOperator, as as OrOperator, a7 as OutgoingExpr, aw as OverviewGraph, ai as ParsedLikeC4Model, n as Point, i as Relation, a1 as RelationExpr, a9 as RelationExpression, R as RelationID, af as RelationPredicateExpression, ab as RelationWhereExpr, ay as RelationshipArrowType, j as RelationshipKind, aC as RelationshipKindSpecification, ax as RelationshipLineType, aI as RelationshipThemeColorValues, aJ as RelationshipThemeColors, aV as ScopedElementView, bb as StepEdgeId, ba as StepEdgeIdLiteral, a as Tag, ak as TagEqual, s as TagSpec, aD as ThemeColor, T as ThemeColorValues, br as ViewChange, V as ViewID, bq as ViewManualLayout, aS as ViewRule, aQ as ViewRuleAutoLayout, aL as ViewRulePredicate, aN as ViewRuleStyle, bf as ViewWithHash, bg as ViewWithNotation, au as WhereOperator, W as WildcardExpr, X as XYPoint, bd as extractStep, bm as getBBoxCenter, be as getParallelStepsPrefix, ar as isAndOperator, bj as isComputedDynamicView, bk as isComputedElementView, J as isCustomElement, ae as isCustomRelationExpr, b6 as isDynamicView, b0 as isDynamicViewIncludeRule, b3 as isDynamicViewParallelSteps, Y as isElement, O as isElementKindExpr, a0 as isElementPredicateExpr, x as isElementRef, S as isElementTagExpr, b7 as isElementView, _ as isElementWhere, z as isExpandedElementExpr, b8 as isExtendsElementView, a4 as isInOut, a6 as isIncoming, an as isKindEqual, ap as isNotOperator, at as isOrOperator, a8 as isOutgoing, a2 as isRelation, aa as isRelationExpression, ag as isRelationPredicateExpr, ac as isRelationWhere, b9 as isScopedElementView, bc as isStepEdgeId, al as isTagEqual, aF as isThemeColor, aR as isViewRuleAutoLayout, aM as isViewRulePredicate, aO as isViewRuleStyle, K as isWildcard, av as whereOperatorAsPredicate } from '../shared/core.CpR2fq5B.js';
1
+ export { at as AndOperator, A as AsFqn, aS as AutoLayoutDirection, bo as BBox, aX as BasicElementView, aW as BasicView, q as BorderStyle, B as BorderStyles, e as Color, aH as ColorLiteral, bl as ComputedDynamicView, f as ComputedEdge, bk as ComputedElementView, g as ComputedLikeC4Model, b as ComputedNode, C as ComputedView, p as CustomColor, b7 as CustomColorDefinitions, M as CustomElementExpr, ag as CustomRelationExpr, aD as DefaultArrowType, t as DefaultElementShape, aC as DefaultLineStyle, aE as DefaultRelationshipColor, s as DefaultThemeColor, l as DiagramEdge, k as DiagramNode, D as DiagramView, b5 as DynamicView, b2 as DynamicViewIncludeRule, b0 as DynamicViewParallelSteps, b4 as DynamicViewRule, a$ as DynamicViewStep, b1 as DynamicViewStepOrParallel, E as EdgeId, h as Element, _ as ElementExpression, c as ElementKind, S as ElementKindExpr, y as ElementKindSpecification, x as ElementKindSpecificationStyle, bs as ElementNotation, a2 as ElementPredicateExpression, z as ElementRefExpr, d as ElementShape, r as ElementShapes, u as ElementStyle, Y as ElementTagExpr, aJ as ElementThemeColorValues, aK as ElementThemeColors, a_ as ElementView, a0 as ElementWhereExpr, am as EqualOperator, J as ExpandedElementExpr, ak as Expression, aZ as ExtendsElementView, F as Fqn, H as HexColorLiteral, I as IconUrl, a6 as InOutExpr, a8 as IncomingExpr, ap as KindEqual, L as LayoutedLikeC4Model, aN as LikeC4Theme, b8 as LikeC4View, w as Link, N as NodeId, m as NonEmptyArray, o as NonEmptyReadonlyArray, ar as NotOperator, av as OrOperator, aa as OutgoingExpr, az as OverviewGraph, al as ParsedLikeC4Model, P as Point, i as Relation, a4 as RelationExpr, ac as RelationExpression, R as RelationID, ai as RelationPredicateExpression, ae as RelationWhereExpr, aB as RelationshipArrowType, j as RelationshipKind, aF as RelationshipKindSpecification, aA as RelationshipLineType, aL as RelationshipThemeColorValues, aM as RelationshipThemeColors, aY as ScopedElementView, be as StepEdgeId, bd as StepEdgeIdLiteral, a as Tag, an as TagEqual, v as TagSpec, aG as ThemeColor, T as ThemeColorValues, br as ViewChange, V as ViewID, bq as ViewManualLayout, aV as ViewRule, aT as ViewRuleAutoLayout, aO as ViewRulePredicate, aQ as ViewRuleStyle, bi as ViewWithHash, bj as ViewWithNotation, ax as WhereOperator, W as WildcardExpr, X as XYPoint, bg as extractStep, bp as getBBoxCenter, bh as getParallelStepsPrefix, au as isAndOperator, bm as isComputedDynamicView, bn as isComputedElementView, O as isCustomElement, ah as isCustomRelationExpr, b9 as isDynamicView, b3 as isDynamicViewIncludeRule, b6 as isDynamicViewParallelSteps, $ as isElement, U as isElementKindExpr, a3 as isElementPredicateExpr, G as isElementRef, Z as isElementTagExpr, ba as isElementView, a1 as isElementWhere, K as isExpandedElementExpr, bb as isExtendsElementView, a7 as isInOut, a9 as isIncoming, aq as isKindEqual, as as isNotOperator, aw as isOrOperator, ab as isOutgoing, a5 as isRelation, ad as isRelationExpression, aj as isRelationPredicateExpr, af as isRelationWhere, bc as isScopedElementView, bf as isStepEdgeId, ao as isTagEqual, aI as isThemeColor, aU as isViewRuleAutoLayout, aP as isViewRulePredicate, aR as isViewRuleStyle, Q as isWildcard, ay as whereOperatorAsPredicate } from '../shared/core.aVt8heZL.js';
2
+ import 'type-fest';
@@ -1 +1 @@
1
- export { A as AsFqn, B as BorderStyles, J as DefaultArrowType, D as DefaultElementShape, I as DefaultLineStyle, K as DefaultRelationshipColor, a as DefaultThemeColor, E as ElementShapes, c as Expr, V as StepEdgeId, X as extractStep, $ as getBBoxCenter, Y as getParallelStepsPrefix, F as isAndOperator, Z as isComputedDynamicView, _ as isComputedElementView, g as isCustomElement, w as isCustomRelationExpr, R as isDynamicView, P as isDynamicViewIncludeRule, Q as isDynamicViewParallelSteps, l as isElement, j as isElementKindExpr, o as isElementPredicateExpr, d as isElementRef, k as isElementTagExpr, S as isElementView, m as isElementWhere, f as isExpandedElementExpr, T as isExtendsElementView, q as isInOut, r as isIncoming, z as isKindEqual, C as isNotOperator, G as isOrOperator, s as isOutgoing, p as isRelation, t as isRelationExpression, x as isRelationPredicateExpr, v as isRelationWhere, U as isScopedElementView, W as isStepEdgeId, y as isTagEqual, L as isThemeColor, O as isViewRuleAutoLayout, M as isViewRulePredicate, N as isViewRuleStyle, h as isWildcard, H as whereOperatorAsPredicate } from '../shared/core.DeifT5lC.mjs';
1
+ export { A as AsFqn, B as BorderStyles, K as DefaultArrowType, a as DefaultElementShape, J as DefaultLineStyle, D as DefaultRelationshipColor, b as DefaultThemeColor, E as ElementShapes, V as StepEdgeId, X as extractStep, $ as getBBoxCenter, Y as getParallelStepsPrefix, G as isAndOperator, Z as isComputedDynamicView, _ as isComputedElementView, h as isCustomElement, x as isCustomRelationExpr, R as isDynamicView, P as isDynamicViewIncludeRule, Q as isDynamicViewParallelSteps, m as isElement, k as isElementKindExpr, p as isElementPredicateExpr, f as isElementRef, l as isElementTagExpr, S as isElementView, o as isElementWhere, g as isExpandedElementExpr, T as isExtendsElementView, r as isInOut, s as isIncoming, C as isKindEqual, F as isNotOperator, H as isOrOperator, t as isOutgoing, q as isRelation, v as isRelationExpression, y as isRelationPredicateExpr, w as isRelationWhere, U as isScopedElementView, W as isStepEdgeId, z as isTagEqual, L as isThemeColor, O as isViewRuleAutoLayout, M as isViewRulePredicate, N as isViewRuleStyle, j as isWildcard, I as whereOperatorAsPredicate } from '../shared/core.CUlHblBQ.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@likec4/core",
3
- "version": "1.10.0",
3
+ "version": "1.11.0",
4
4
  "license": "MIT",
5
5
  "homepage": "https://likec4.dev",
6
6
  "author": "Denis Davydkov <denis@davydkov.com>",
@@ -16,11 +16,13 @@
16
16
  "lint": "run -T eslint src/ --fix",
17
17
  "test": "vitest run --no-isolate",
18
18
  "test:watch": "vitest",
19
- "clean": "run -T rimraf dist"
19
+ "clean": "rm -r -f dist"
20
20
  },
21
21
  "files": [
22
22
  "dist",
23
23
  "src",
24
+ "!**/*.spec.ts",
25
+ "!**/__tests__/*",
24
26
  "!**/*.map"
25
27
  ],
26
28
  "type": "module",
@@ -73,19 +75,21 @@
73
75
  }
74
76
  }
75
77
  },
78
+ "dependencies": {
79
+ "type-fest": "4.26.1"
80
+ },
76
81
  "devDependencies": {
77
- "@likec4/tsconfig": "1.10.0",
82
+ "@likec4/tsconfig": "1.11.0",
78
83
  "@mantine/colors-generator": "^7.12.2",
79
- "@types/natural-compare-lite": "^1",
80
- "chroma-js": "^3.0.0",
84
+ "@types/natural-compare-lite": "^1.4.2",
85
+ "chroma-js": "^3.1.1",
81
86
  "execa": "^9.3.1",
82
87
  "natural-compare-lite": "^1.4.0",
83
- "remeda": "^2.11.0",
88
+ "remeda": "^2.14.0",
84
89
  "turbo": "^2.1.1",
85
- "type-fest": "^4.26.0",
86
- "typescript": "^5.5.4",
90
+ "typescript": "^5.6.2",
87
91
  "unbuild": "^3.0.0-rc.7",
88
- "vitest": "~2.0.5"
92
+ "vitest": "^2.1.1"
89
93
  },
90
- "packageManager": "yarn@4.3.1"
94
+ "packageManager": "yarn@4.4.1"
91
95
  }
package/src/index.ts CHANGED
@@ -7,3 +7,5 @@ export * from './utils/fqn'
7
7
  export * from './utils/guards'
8
8
  export * from './utils/promises'
9
9
  export { compareRelations } from './utils/relations'
10
+
11
+ export * as Expr from './types/expression'
@@ -0,0 +1,338 @@
1
+ import { isNullish } from 'remeda'
2
+ import { nonNullable } from '../errors'
3
+ import {
4
+ type ElementKind as C4ElementKind,
5
+ type ElementShape as C4ElementShape,
6
+ type Tag as C4Tag
7
+ } from '../types/element'
8
+ import { DefaultRelationshipColor } from '../types/relation'
9
+ import type { Color as C4Color } from '../types/theme'
10
+ import type { ComputedElementView, DiagramEdge, DiagramNode, DiagramView } from '../types/view'
11
+ import type { LikeC4Model } from './LikeC4Model'
12
+ import { type EdgeId, type ElementOrFqn, type Fqn, getId, type IncomingFilter, type OutgoingFilter } from './types'
13
+
14
+ /**
15
+ * All methods are view-scoped, i.e. only return elements and connections in the view.
16
+ */
17
+ export class LikeC4DiagramModel {
18
+ private _rootElements = new Set<LikeC4DiagramModel.Element>()
19
+
20
+ private _elements = new Map<Fqn, LikeC4DiagramModel.Element>()
21
+
22
+ private _connections: Map<EdgeId, LikeC4DiagramModel.Connection>
23
+
24
+ constructor(
25
+ public readonly view: DiagramView,
26
+ public readonly model: LikeC4Model.Layouted
27
+ ) {
28
+ for (const node of view.nodes) {
29
+ const el = new LikeC4DiagramModel.Element(node, this)
30
+ this._elements.set(node.id, el)
31
+ if (isNullish(node.parent)) {
32
+ this._rootElements.add(el)
33
+ }
34
+ }
35
+ this._connections = new Map(view.edges.map(e => [e.id, new LikeC4DiagramModel.Connection(e, this)]))
36
+ }
37
+
38
+ get isDynamic() {
39
+ return this.view.__ === 'dynamic'
40
+ }
41
+
42
+ get id() {
43
+ return this.view.id
44
+ }
45
+
46
+ get title() {
47
+ return this.view.title
48
+ }
49
+
50
+ get viewOf() {
51
+ if (isNullish(this.view.__) || this.view.__ === 'element') {
52
+ const computedView = this.view as ComputedElementView
53
+ return computedView.viewOf ? this.model.element(computedView.viewOf) : null
54
+ }
55
+ return null
56
+ }
57
+
58
+ get tags(): C4Tag[] {
59
+ return this.view.tags ?? []
60
+ }
61
+
62
+ public roots(): ReadonlyArray<LikeC4DiagramModel.Element> {
63
+ return [...this._rootElements]
64
+ }
65
+
66
+ public elements(): ReadonlyArray<LikeC4DiagramModel.Element> {
67
+ return [...this._elements.values()]
68
+ }
69
+
70
+ public element(id: Fqn): LikeC4DiagramModel.Element {
71
+ return nonNullable(this._elements.get(id), `LikeC4DiagramModel.Element ${id} in view ${this.view.id} not found`)
72
+ }
73
+
74
+ public hasElement(id: Fqn): boolean {
75
+ return this._elements.has(id)
76
+ }
77
+
78
+ public connections(): ReadonlyArray<LikeC4DiagramModel.Connection> {
79
+ return [...this._connections.values()]
80
+ }
81
+
82
+ public connection(id: EdgeId): LikeC4DiagramModel.Connection {
83
+ return nonNullable(this._connections.get(id), `Connection ${id} in view ${this.view.id} not found`)
84
+ }
85
+
86
+ public findConnections(
87
+ source: ElementOrFqn,
88
+ target: ElementOrFqn,
89
+ direction: 'both' | 'direct' = 'direct'
90
+ ): ReadonlyArray<LikeC4DiagramModel.Connection> {
91
+ const sourceId = getId(source)
92
+ const targetId = getId(target)
93
+ return this.connections().filter(c =>
94
+ (c.source.id === sourceId && c.target.id === targetId)
95
+ || (direction === 'both' && c.source.id === targetId && c.target.id === sourceId)
96
+ )
97
+ }
98
+
99
+ public parent(element: ElementOrFqn): LikeC4DiagramModel.Element | null {
100
+ const el = this.element(getId(element))
101
+ return el.node.parent ? this.element(el.node.parent) : null
102
+ }
103
+
104
+ public children(element: ElementOrFqn): ReadonlyArray<LikeC4DiagramModel.Element> {
105
+ const el = this.element(getId(element))
106
+ return el.node.children.map(c => this.element(c))
107
+ }
108
+
109
+ // Get all sibling (i.e. same parent)
110
+ public siblings(element: ElementOrFqn): ReadonlyArray<LikeC4DiagramModel.Element> {
111
+ const id = getId(element)
112
+ const parent = this.parent(id)
113
+ const siblings = parent ? this.children(parent) : this.roots()
114
+ return siblings.filter(e => e.id !== id)
115
+ }
116
+
117
+ /**
118
+ * Get all ancestor elements (i.e. parent, parent’s parent, etc.)
119
+ * (from closest to root)
120
+ */
121
+ public ancestors(element: ElementOrFqn): ReadonlyArray<LikeC4DiagramModel.Element> {
122
+ let id = getId(element)
123
+ const result = [] as LikeC4DiagramModel.Element[]
124
+ let parent
125
+ while (parent = this.parent(id)) {
126
+ result.push(parent)
127
+ id = parent.id
128
+ }
129
+ return result
130
+ }
131
+
132
+ public descendants(element: ElementOrFqn): ReadonlyArray<LikeC4DiagramModel.Element> {
133
+ const children = this.children(element)
134
+ return children.flatMap(c => [c, ...this.descendants(c.id)])
135
+ }
136
+
137
+ public incoming(
138
+ element: ElementOrFqn,
139
+ filter: IncomingFilter = 'all'
140
+ ): ReadonlyArray<LikeC4DiagramModel.Connection> {
141
+ const el = this.element(getId(element))
142
+ const edges = el.node.inEdges.map(e => this.connection(e))
143
+
144
+ if (filter === 'all' || edges.length === 0) {
145
+ return edges
146
+ }
147
+
148
+ return edges.filter(rel => {
149
+ if (filter === 'direct') {
150
+ return rel.target.id === el.id
151
+ }
152
+ return rel.target.id !== el.id
153
+ })
154
+ }
155
+
156
+ public incomers(
157
+ element: ElementOrFqn,
158
+ filter: IncomingFilter = 'all'
159
+ ): ReadonlyArray<LikeC4DiagramModel.Element> {
160
+ return this.incoming(element, filter).map(r => r.source)
161
+ }
162
+
163
+ /**
164
+ * Outgoing relationships from the element and its descendants
165
+ */
166
+ public outgoing(
167
+ element: ElementOrFqn,
168
+ filter: OutgoingFilter = 'all'
169
+ ): ReadonlyArray<LikeC4DiagramModel.Connection> {
170
+ const el = this.element(getId(element))
171
+ const edges = el.node.outEdges.map(e => this.connection(e))
172
+
173
+ if (filter === 'all' || edges.length === 0) {
174
+ return edges
175
+ }
176
+
177
+ return edges.filter(rel => {
178
+ if (filter === 'direct') {
179
+ return rel.source.id === el.id
180
+ }
181
+ return rel.source.id !== el.id
182
+ })
183
+ }
184
+
185
+ public outgoers(
186
+ element: ElementOrFqn,
187
+ filter: OutgoingFilter = 'all'
188
+ ): ReadonlyArray<LikeC4DiagramModel.Element> {
189
+ return this.outgoing(element, filter).map(r => r.target)
190
+ }
191
+ }
192
+
193
+ export namespace LikeC4DiagramModel {
194
+ /**
195
+ * Represents an element in the view. (Diagram node)
196
+ * All methods are view-scoped, i.e. `children` returns only children of the element in the view.
197
+ */
198
+ export class Element {
199
+ constructor(
200
+ public readonly node: DiagramNode,
201
+ private view: LikeC4DiagramModel
202
+ ) {
203
+ }
204
+
205
+ get id() {
206
+ return this.node.id
207
+ }
208
+
209
+ get title() {
210
+ return this.node.title
211
+ }
212
+
213
+ get kind(): C4ElementKind {
214
+ return this.node.kind
215
+ }
216
+
217
+ get isRoot(): boolean {
218
+ return isNullish(this.node.parent)
219
+ }
220
+
221
+ get hasNested(): boolean {
222
+ return this.node.children.length > 0
223
+ }
224
+
225
+ get shape(): C4ElementShape {
226
+ return this.node.shape
227
+ }
228
+
229
+ get color(): C4Color {
230
+ return this.node.color
231
+ }
232
+
233
+ get tags(): C4Tag[] {
234
+ return this.node.tags ?? []
235
+ }
236
+
237
+ get level(): number {
238
+ return this.node.level
239
+ }
240
+
241
+ get depth(): number {
242
+ return this.node.depth ?? 0
243
+ }
244
+
245
+ public model(): LikeC4Model.ElementModel<LikeC4DiagramModel> {
246
+ return this.view.model.element(this.id)
247
+ }
248
+
249
+ public parent(): LikeC4DiagramModel.Element | null {
250
+ return this.node.parent ? this.view.element(this.node.parent) : null
251
+ }
252
+
253
+ public metadata(key: string): string | undefined
254
+ public metadata(key: string, defaultValue: string): string
255
+ public metadata(key: string, defaultValue?: string): string | undefined {
256
+ return this.model().metadata(key) ?? defaultValue
257
+ }
258
+
259
+ public hasMetadata(key: string): boolean {
260
+ return this.model().hasMetadata(key)
261
+ }
262
+
263
+ public ancestors(): ReadonlyArray<LikeC4DiagramModel.Element> {
264
+ return this.view.ancestors(this)
265
+ }
266
+
267
+ public siblings(): ReadonlyArray<LikeC4DiagramModel.Element> {
268
+ return this.view.siblings(this)
269
+ }
270
+
271
+ public descendants(): ReadonlyArray<LikeC4DiagramModel.Element> {
272
+ return this.view.descendants(this)
273
+ }
274
+
275
+ public children(): ReadonlyArray<LikeC4DiagramModel.Element> {
276
+ return this.view.children(this)
277
+ }
278
+
279
+ public incoming(filter: IncomingFilter = 'all'): ReadonlyArray<LikeC4DiagramModel.Connection> {
280
+ return this.view.incoming(this, filter)
281
+ }
282
+
283
+ public incomers(filter: IncomingFilter = 'all'): ReadonlyArray<LikeC4DiagramModel.Element> {
284
+ return this.view.incomers(this, filter)
285
+ }
286
+
287
+ public outgoing(filter: OutgoingFilter = 'all'): ReadonlyArray<LikeC4DiagramModel.Connection> {
288
+ return this.view.outgoing(this, filter)
289
+ }
290
+
291
+ public outgoers(filter: OutgoingFilter = 'all'): ReadonlyArray<LikeC4DiagramModel.Element> {
292
+ return this.view.outgoers(this, filter)
293
+ }
294
+
295
+ public connectionsTo(target: Fqn | LikeC4DiagramModel.Element): ReadonlyArray<LikeC4DiagramModel.Connection> {
296
+ return this.view.findConnections(this, target)
297
+ }
298
+ }
299
+
300
+ /**
301
+ * Represents a connection between two elements.
302
+ * May be source from multiple model relationships.
303
+ */
304
+ export class Connection {
305
+ constructor(
306
+ public readonly edge: DiagramEdge,
307
+ private view: LikeC4DiagramModel
308
+ ) {
309
+ }
310
+
311
+ get id() {
312
+ return this.edge.id
313
+ }
314
+
315
+ get source(): LikeC4DiagramModel.Element {
316
+ return this.view.element(this.edge.source)
317
+ }
318
+
319
+ get target(): LikeC4DiagramModel.Element {
320
+ return this.view.element(this.edge.target)
321
+ }
322
+
323
+ get tags(): C4Tag[] {
324
+ return this.edge.tags ?? []
325
+ }
326
+
327
+ get color(): C4Color {
328
+ return this.edge.color ?? DefaultRelationshipColor
329
+ }
330
+
331
+ /**
332
+ * Model relationships
333
+ */
334
+ relationships(): ReadonlyArray<LikeC4Model.Relationship<LikeC4DiagramModel>> {
335
+ return this.edge.relations.map(r => nonNullable(this.view.model.relationship(r)))
336
+ }
337
+ }
338
+ }