@likec4/core 1.13.0 → 1.15.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/src/types/view.ts CHANGED
@@ -1,8 +1,17 @@
1
1
  import { isArray, isNullish } from 'remeda'
2
2
  import type { Tagged } from 'type-fest'
3
3
  import type { IconUrl, NonEmptyArray, Point, XYPoint } from './_common'
4
- import type { ElementKind, ElementShape, ElementStyle, Fqn, Link, Tag } from './element'
4
+ import {
5
+ type BorderStyle,
6
+ ElementKind,
7
+ type ElementShape,
8
+ type ElementStyle,
9
+ type Fqn,
10
+ type Link,
11
+ type Tag
12
+ } from './element'
5
13
  import type { ElementExpression, ElementPredicateExpression, Expression } from './expression'
14
+ import type { GlobalStyleID } from './global'
6
15
  import type { RelationID, RelationshipArrowType, RelationshipKind, RelationshipLineType } from './relation'
7
16
  import type { Color, ThemeColorValues } from './theme'
8
17
  import type { ElementNotation } from './view-notation'
@@ -38,6 +47,15 @@ export function isViewRuleStyle(rule: ViewRule): rule is ViewRuleStyle {
38
47
  return 'style' in rule && 'targets' in rule
39
48
  }
40
49
 
50
+ export interface ViewRuleGlobalStyle {
51
+ styleId: GlobalStyleID
52
+ }
53
+ export function isViewRuleGlobalStyle(rule: ViewRule): rule is ViewRuleGlobalStyle {
54
+ return 'styleId' in rule
55
+ }
56
+
57
+ export type ViewRuleStyleOrGlobalRef = ViewRuleStyle | ViewRuleGlobalStyle
58
+
41
59
  export type AutoLayoutDirection = 'TB' | 'BT' | 'LR' | 'RL'
42
60
  export function isAutoLayoutDirection(autoLayout: unknown): autoLayout is AutoLayoutDirection {
43
61
  return autoLayout === 'TB' || autoLayout === 'BT' || autoLayout === 'LR' || autoLayout === 'RL'
@@ -52,7 +70,20 @@ export function isViewRuleAutoLayout(rule: ViewRule): rule is ViewRuleAutoLayout
52
70
  return 'direction' in rule
53
71
  }
54
72
 
55
- export type ViewRule = ViewRulePredicate | ViewRuleStyle | ViewRuleAutoLayout
73
+ export interface ViewRuleGroup {
74
+ groupRules: Array<ViewRulePredicate | ViewRuleGroup>
75
+ title: string | null
76
+ color?: Color
77
+ border?: BorderStyle
78
+ // 0-100
79
+ opacity?: number
80
+ }
81
+
82
+ export function isViewRuleGroup(rule: ViewRule): rule is ViewRuleGroup {
83
+ return 'title' in rule && 'groupRules' in rule && Array.isArray(rule.groupRules)
84
+ }
85
+
86
+ export type ViewRule = ViewRulePredicate | ViewRuleGroup | ViewRuleStyle | ViewRuleGlobalStyle | ViewRuleAutoLayout
56
87
 
57
88
  export interface BasicView<
58
89
  ViewType extends 'element' | 'dynamic',
@@ -147,7 +178,7 @@ export function isDynamicViewIncludeRule(rule: DynamicViewRule): rule is Dynamic
147
178
  return 'include' in rule && Array.isArray(rule.include)
148
179
  }
149
180
 
150
- export type DynamicViewRule = DynamicViewIncludeRule | ViewRuleStyle | ViewRuleAutoLayout
181
+ export type DynamicViewRule = DynamicViewIncludeRule | ViewRuleStyle | ViewRuleGlobalStyle | ViewRuleAutoLayout
151
182
  export interface DynamicView<
152
183
  ViewIDs extends string = string,
153
184
  Tags extends string = string
@@ -247,6 +278,14 @@ export interface ComputedNode {
247
278
  */
248
279
  isCustomized?: boolean
249
280
  }
281
+ export namespace ComputedNode {
282
+ /**
283
+ * Nodes group is a special kind of node, exisiting only in view
284
+ */
285
+ export function isNodesGroup(node: ComputedNode): boolean {
286
+ return node.kind === ElementKind.Group
287
+ }
288
+ }
250
289
 
251
290
  export interface ComputedEdge {
252
291
  id: EdgeId
@@ -360,6 +399,15 @@ export interface DiagramNode extends ComputedNode {
360
399
  labelBBox: BBox
361
400
  }
362
401
 
402
+ export namespace DiagramNode {
403
+ /**
404
+ * Nodes group is a special kind of node, exisiting only in view
405
+ */
406
+ export function isNodesGroup(node: DiagramNode): boolean {
407
+ return node.kind === ElementKind.Group
408
+ }
409
+ }
410
+
363
411
  export interface DiagramEdge extends ComputedEdge {
364
412
  // Bezier points
365
413
  points: NonEmptyArray<Point>
@@ -0,0 +1,20 @@
1
+ import { map, pipe, takeWhile, zip } from 'remeda'
2
+
3
+ /**
4
+ * Common head of two arrays
5
+ */
6
+ export function commonHead<T>(
7
+ sources: ReadonlyArray<T>,
8
+ targets: ReadonlyArray<T>,
9
+ equals?: (a: T, b: T) => boolean
10
+ ): T[] {
11
+ if (sources.length === 0 || targets.length === 0) {
12
+ return []
13
+ }
14
+ equals ??= Object.is
15
+ return pipe(
16
+ zip(sources, targets),
17
+ takeWhile(([source, target]) => equals(source, target)),
18
+ map(([source, _]) => source)
19
+ )
20
+ }
@@ -1,3 +1,4 @@
1
+ export * from './commonHead'
1
2
  export * from './compare-natural'
2
3
  export * from './fqn'
3
4
  export * from './guards'