@swirly/renderer 0.20.0 → 0.22.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 +1 -1
- package/README.md +4 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/arrow.d.ts +1 -1
- package/dist/arrow.js +29 -26
- package/dist/arrow.js.map +1 -1
- package/dist/decoration/barrier.d.ts +2 -2
- package/dist/decoration/barrier.js +8 -8
- package/dist/decoration/barrier.js.map +1 -1
- package/dist/decoration/index.d.ts +2 -2
- package/dist/decoration/index.js +3 -3
- package/dist/decoration/index.js.map +1 -1
- package/dist/decoration/range.d.ts +2 -2
- package/dist/decoration/range.js +7 -7
- package/dist/decoration/range.js.map +1 -1
- package/dist/index.d.ts +2 -4
- package/dist/index.js +28 -20
- package/dist/index.js.map +1 -1
- package/dist/message/complete.d.ts +2 -2
- package/dist/message/complete.js +10 -10
- package/dist/message/complete.js.map +1 -1
- package/dist/message/error.d.ts +2 -2
- package/dist/message/error.js +12 -12
- package/dist/message/error.js.map +1 -1
- package/dist/message/index.d.ts +2 -2
- package/dist/message/index.js +2 -2
- package/dist/message/index.js.map +1 -1
- package/dist/message/non-stream.d.ts +2 -2
- package/dist/message/non-stream.js +5 -9
- package/dist/message/non-stream.js.map +1 -1
- package/dist/message/scalar.d.ts +2 -2
- package/dist/message/scalar.js +21 -21
- package/dist/message/scalar.js.map +1 -1
- package/dist/message/stream.d.ts +2 -2
- package/dist/message/stream.js +7 -7
- package/dist/message/stream.js.map +1 -1
- package/dist/operator.d.ts +2 -2
- package/dist/operator.js +70 -52
- package/dist/operator.js.map +1 -1
- package/dist/stream/core.d.ts +1 -1
- package/dist/stream/core.js +2 -2
- package/dist/stream/core.js.map +1 -1
- package/dist/stream/factory.d.ts +2 -2
- package/dist/stream/factory.js +18 -24
- package/dist/stream/factory.js.map +1 -1
- package/dist/stream/full.d.ts +2 -2
- package/dist/stream/full.js +14 -14
- package/dist/stream/full.js.map +1 -1
- package/dist/types.d.ts +14 -17
- package/dist/util/degrees-to-radians.js.map +1 -1
- package/dist/util/geometry.d.ts +1 -1
- package/dist/util/geometry.js +6 -6
- package/dist/util/geometry.js.map +1 -1
- package/dist/util/hex-to-dec.js +2 -2
- package/dist/util/hex-to-dec.js.map +1 -1
- package/dist/util/merge-styles.d.ts +1 -1
- package/dist/util/merge-styles.js.map +1 -1
- package/dist/util/notification-kind.js +4 -4
- package/dist/util/notification-kind.js.map +1 -1
- package/dist/util/string-to-color.d.ts +1 -1
- package/dist/util/string-to-color.js +3 -3
- package/dist/util/string-to-color.js.map +1 -1
- package/dist/util/svg-xml.d.ts +1 -1
- package/dist/util/svg-xml.js +8 -8
- package/dist/util/svg-xml.js.map +1 -1
- package/dist/util/transform.js +3 -3
- package/dist/util/transform.js.map +1 -1
- package/package.json +20 -20
- package/src/arrow.ts +43 -40
- package/src/decoration/barrier.ts +20 -24
- package/src/decoration/index.ts +11 -11
- package/src/decoration/range.ts +20 -27
- package/src/index.ts +61 -63
- package/src/message/complete.ts +24 -28
- package/src/message/error.ts +27 -27
- package/src/message/index.ts +8 -12
- package/src/message/non-stream.ts +12 -20
- package/src/message/scalar.ts +44 -52
- package/src/message/stream.ts +27 -40
- package/src/operator.ts +121 -113
- package/src/stream/core.ts +3 -3
- package/src/stream/factory.ts +86 -102
- package/src/stream/full.ts +34 -37
- package/src/types.ts +39 -43
- package/src/util/degrees-to-radians.ts +1 -2
- package/src/util/geometry.ts +29 -42
- package/src/util/hex-to-dec.ts +8 -8
- package/src/util/merge-styles.ts +9 -11
- package/src/util/notification-kind.ts +8 -8
- package/src/util/string-to-color.ts +10 -10
- package/src/util/svg-xml.ts +20 -23
- package/src/util/transform.ts +11 -17
package/src/stream/factory.ts
CHANGED
|
@@ -1,136 +1,120 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ArrowStyles,
|
|
3
|
-
MessageSpecification,
|
|
4
|
-
StreamSpecification
|
|
5
|
-
} from '@swirly/types'
|
|
1
|
+
import { ArrowStyles, MessageSpecification, StreamSpecification } from "@swirly/types";
|
|
6
2
|
|
|
7
|
-
import { renderArrow } from
|
|
8
|
-
import { renderDecoration } from
|
|
3
|
+
import { renderArrow } from "../arrow.js";
|
|
4
|
+
import { renderDecoration } from "../decoration/index.js";
|
|
9
5
|
import {
|
|
10
6
|
MessageRenderer,
|
|
11
7
|
Rectangle,
|
|
12
8
|
RendererContext,
|
|
13
9
|
RendererResult,
|
|
14
|
-
TimeScaler
|
|
15
|
-
} from
|
|
16
|
-
import { degreesToRadians } from
|
|
17
|
-
import { rectangleUnion } from
|
|
18
|
-
import { mergeStyles } from
|
|
19
|
-
import { NotificationKind } from
|
|
20
|
-
import { createSvgElement } from
|
|
21
|
-
import { translate } from
|
|
10
|
+
TimeScaler,
|
|
11
|
+
} from "../types.js";
|
|
12
|
+
import { degreesToRadians } from "../util/degrees-to-radians.js";
|
|
13
|
+
import { rectangleUnion } from "../util/geometry.js";
|
|
14
|
+
import { mergeStyles } from "../util/merge-styles.js";
|
|
15
|
+
import { NotificationKind } from "../util/notification-kind.js";
|
|
16
|
+
import { createSvgElement } from "../util/svg-xml.js";
|
|
17
|
+
import { translate } from "../util/transform.js";
|
|
22
18
|
|
|
23
|
-
const nullScaleTime: TimeScaler = (time) => time
|
|
19
|
+
const nullScaleTime: TimeScaler = (time) => time;
|
|
24
20
|
|
|
25
21
|
const createScaleTime = (higherOrderAngleDegrees: number): TimeScaler => {
|
|
26
|
-
const scale = 1 / Math.cos(degreesToRadians(higherOrderAngleDegrees))
|
|
27
|
-
return (time) => time * scale
|
|
28
|
-
}
|
|
22
|
+
const scale = 1 / Math.cos(degreesToRadians(higherOrderAngleDegrees));
|
|
23
|
+
return (time) => time * scale;
|
|
24
|
+
};
|
|
29
25
|
|
|
30
|
-
const isNotification = ({
|
|
31
|
-
|
|
32
|
-
}: MessageSpecification): boolean => NotificationKind.NEXT.equals(kind)
|
|
26
|
+
const isNotification = ({ notification: { kind } }: MessageSpecification): boolean =>
|
|
27
|
+
NotificationKind.NEXT.equals(kind);
|
|
33
28
|
|
|
34
29
|
const countPriors = (
|
|
35
30
|
message: MessageSpecification,
|
|
36
31
|
index: number,
|
|
37
|
-
messages: readonly MessageSpecification[]
|
|
32
|
+
messages: readonly MessageSpecification[],
|
|
38
33
|
): number => {
|
|
39
|
-
let count = 0
|
|
34
|
+
let count = 0;
|
|
40
35
|
for (let i = 0; i < index; ++i) {
|
|
41
36
|
if (isNotification(messages[i]) && messages[i].frame === message.frame) {
|
|
42
|
-
++count
|
|
37
|
+
++count;
|
|
43
38
|
}
|
|
44
39
|
}
|
|
45
|
-
return count
|
|
46
|
-
}
|
|
40
|
+
return count;
|
|
41
|
+
};
|
|
47
42
|
|
|
48
43
|
export const createRenderStream =
|
|
49
44
|
(renderMessage: MessageRenderer) =>
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
)
|
|
71
|
-
const bboxes: Rectangle[] = []
|
|
72
|
-
|
|
73
|
-
const add = ({ element, bbox }: RendererResult) => {
|
|
74
|
-
$group.appendChild(element)
|
|
75
|
-
bboxes.push(bbox)
|
|
76
|
-
}
|
|
45
|
+
(
|
|
46
|
+
ctx: RendererContext,
|
|
47
|
+
stream: StreamSpecification,
|
|
48
|
+
isHigherOrder: boolean,
|
|
49
|
+
isGhost: boolean,
|
|
50
|
+
): RendererResult => {
|
|
51
|
+
const { document, styles } = ctx;
|
|
52
|
+
|
|
53
|
+
const scaleTime = isHigherOrder ? createScaleTime(styles.higher_order_angle!) : nullScaleTime;
|
|
54
|
+
|
|
55
|
+
const $group = createSvgElement(
|
|
56
|
+
document,
|
|
57
|
+
"g",
|
|
58
|
+
isGhost
|
|
59
|
+
? {
|
|
60
|
+
style: `filter: opacity(${styles.ghost_opacity}%)`,
|
|
61
|
+
}
|
|
62
|
+
: undefined,
|
|
63
|
+
);
|
|
64
|
+
const bboxes: Rectangle[] = [];
|
|
77
65
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
true
|
|
83
|
-
)
|
|
84
|
-
const scaledDuration = scaleTime(stream.duration)
|
|
85
|
-
add(renderArrow(ctx, arrowStyles, styles.arrowhead_angle!, scaledDuration))
|
|
86
|
-
|
|
87
|
-
for (let i = stream.messages.length - 1; i >= 0; --i) {
|
|
88
|
-
const message = stream.messages[i]
|
|
89
|
-
const scaledMessage = {
|
|
90
|
-
...message,
|
|
91
|
-
frame: scaleTime(message.frame)
|
|
92
|
-
}
|
|
66
|
+
const add = ({ element, bbox }: RendererResult) => {
|
|
67
|
+
$group.appendChild(element);
|
|
68
|
+
bboxes.push(bbox);
|
|
69
|
+
};
|
|
93
70
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const dy = priorCount * styles.stacking_height!
|
|
71
|
+
const arrowStyles: ArrowStyles = mergeStyles(styles, stream.styles, "arrow_", true);
|
|
72
|
+
const scaledDuration = scaleTime(stream.duration);
|
|
73
|
+
add(renderArrow(ctx, arrowStyles, styles.arrowhead_angle!, scaledDuration));
|
|
98
74
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
75
|
+
for (let i = stream.messages.length - 1; i >= 0; --i) {
|
|
76
|
+
const message = stream.messages[i];
|
|
77
|
+
const scaledMessage = {
|
|
78
|
+
...message,
|
|
79
|
+
frame: scaleTime(message.frame),
|
|
80
|
+
};
|
|
102
81
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
valueAngle
|
|
106
|
-
}
|
|
82
|
+
const priorCount = isNotification(message) ? countPriors(message, i, stream.messages) : 0;
|
|
83
|
+
const dy = priorCount * styles.stacking_height!;
|
|
107
84
|
|
|
108
|
-
|
|
85
|
+
const valueAngle = isHigherOrder ? styles.higher_order_event_value_angle! : 0;
|
|
109
86
|
|
|
110
|
-
|
|
111
|
-
|
|
87
|
+
const options = {
|
|
88
|
+
verticalOffset: dy,
|
|
89
|
+
valueAngle,
|
|
90
|
+
};
|
|
112
91
|
|
|
113
|
-
|
|
114
|
-
}
|
|
92
|
+
const { element, bbox } = renderMessage(ctx, scaledMessage, options);
|
|
115
93
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
scaleTime,
|
|
119
|
-
bbox: rectangleUnion(bboxes)
|
|
120
|
-
}
|
|
94
|
+
translate(element, 0, dy);
|
|
95
|
+
bbox.y2 += dy;
|
|
121
96
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const { element, bbox } = renderDecoration(innerCtx, decoration)
|
|
125
|
-
$group.appendChild(element)
|
|
126
|
-
if (bbox != null) {
|
|
127
|
-
innerCtx.bbox = rectangleUnion([innerCtx.bbox, bbox])
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
97
|
+
add({ element, bbox });
|
|
98
|
+
}
|
|
131
99
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
100
|
+
const innerCtx = {
|
|
101
|
+
...ctx,
|
|
102
|
+
scaleTime,
|
|
103
|
+
bbox: rectangleUnion(bboxes),
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
if (stream.decorations != null) {
|
|
107
|
+
for (const decoration of stream.decorations) {
|
|
108
|
+
const { element, bbox } = renderDecoration(innerCtx, decoration);
|
|
109
|
+
$group.appendChild(element);
|
|
110
|
+
if (bbox != null) {
|
|
111
|
+
innerCtx.bbox = rectangleUnion([innerCtx.bbox, bbox]);
|
|
112
|
+
}
|
|
135
113
|
}
|
|
136
114
|
}
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
element: $group,
|
|
118
|
+
bbox: innerCtx.bbox,
|
|
119
|
+
};
|
|
120
|
+
};
|
package/src/stream/full.ts
CHANGED
|
@@ -1,62 +1,59 @@
|
|
|
1
|
-
import { StreamSpecification, StreamStyles } from
|
|
1
|
+
import { StreamSpecification, StreamStyles } from "@swirly/types";
|
|
2
2
|
|
|
3
|
-
import { RendererContext, RendererResult } from
|
|
4
|
-
import { mergeStyles } from
|
|
5
|
-
import { createSvgElement } from
|
|
6
|
-
import { translate } from
|
|
7
|
-
import { renderStreamBase } from
|
|
3
|
+
import { RendererContext, RendererResult } from "../types.js";
|
|
4
|
+
import { mergeStyles } from "../util/merge-styles.js";
|
|
5
|
+
import { createSvgElement } from "../util/svg-xml.js";
|
|
6
|
+
import { translate } from "../util/transform.js";
|
|
7
|
+
import { renderStreamBase } from "./core.js";
|
|
8
8
|
|
|
9
|
-
export const renderStream = (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
): RendererResult => {
|
|
13
|
-
const { document, styles, streamHeight, streamTitleEnabled } = ctx
|
|
14
|
-
const s: StreamStyles = mergeStyles(styles, stream.styles, 'stream_')
|
|
9
|
+
export const renderStream = (ctx: RendererContext, stream: StreamSpecification): RendererResult => {
|
|
10
|
+
const { document, styles, streamHeight, streamTitleEnabled } = ctx;
|
|
11
|
+
const s: StreamStyles = mergeStyles(styles, stream.styles, "stream_");
|
|
15
12
|
|
|
16
|
-
const renderStreamResult = renderStreamBase(ctx, stream, false, false)
|
|
13
|
+
const renderStreamResult = renderStreamBase(ctx, stream, false, false);
|
|
17
14
|
|
|
18
|
-
const { element: $streamGroup, bbox } = renderStreamResult
|
|
15
|
+
const { element: $streamGroup, bbox } = renderStreamResult;
|
|
19
16
|
|
|
20
17
|
if (stream.frame != null && stream.frame > 0) {
|
|
21
|
-
const dx = stream.frame * styles.frame_width
|
|
22
|
-
translate($streamGroup, dx, 0)
|
|
23
|
-
bbox.x1 += dx
|
|
24
|
-
bbox.x2 += dx
|
|
18
|
+
const dx = stream.frame * styles.frame_width!;
|
|
19
|
+
translate($streamGroup, dx, 0);
|
|
20
|
+
bbox.x1 += dx;
|
|
21
|
+
bbox.x2 += dx;
|
|
25
22
|
}
|
|
26
23
|
|
|
27
24
|
if (!streamTitleEnabled) {
|
|
28
|
-
return renderStreamResult
|
|
25
|
+
return renderStreamResult;
|
|
29
26
|
}
|
|
30
27
|
|
|
31
|
-
const $outerGroup = createSvgElement(document,
|
|
28
|
+
const $outerGroup = createSvgElement(document, "g");
|
|
32
29
|
|
|
33
|
-
const title = typeof stream.title ===
|
|
30
|
+
const title = typeof stream.title === "string" ? stream.title : "";
|
|
34
31
|
$outerGroup.appendChild(
|
|
35
32
|
createSvgElement(
|
|
36
33
|
document,
|
|
37
|
-
|
|
34
|
+
"text",
|
|
38
35
|
{
|
|
39
36
|
x: 0,
|
|
40
37
|
y: streamHeight / 2,
|
|
41
38
|
fill: s.title_color!,
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
39
|
+
"font-family": s.title_font_family!,
|
|
40
|
+
"font-size": s.title_font_size! + "px",
|
|
41
|
+
"font-weight": s.title_font_weight!,
|
|
42
|
+
"font-style": s.title_font_style!,
|
|
43
|
+
"dominant-baseline": "middle",
|
|
47
44
|
},
|
|
48
|
-
title
|
|
49
|
-
)
|
|
50
|
-
)
|
|
45
|
+
title,
|
|
46
|
+
),
|
|
47
|
+
);
|
|
51
48
|
|
|
52
|
-
$outerGroup.appendChild($streamGroup)
|
|
53
|
-
translate($streamGroup, s.title_width!, 0)
|
|
49
|
+
$outerGroup.appendChild($streamGroup);
|
|
50
|
+
translate($streamGroup, s.title_width!, 0);
|
|
54
51
|
|
|
55
|
-
bbox.x1 = 0
|
|
56
|
-
bbox.x2 += s.title_width
|
|
52
|
+
bbox.x1 = 0;
|
|
53
|
+
bbox.x2 += s.title_width!;
|
|
57
54
|
|
|
58
55
|
return {
|
|
59
56
|
element: $outerGroup,
|
|
60
|
-
bbox
|
|
61
|
-
}
|
|
62
|
-
}
|
|
57
|
+
bbox,
|
|
58
|
+
};
|
|
59
|
+
};
|
package/src/types.ts
CHANGED
|
@@ -2,72 +2,68 @@ import {
|
|
|
2
2
|
DecorationSpecification,
|
|
3
3
|
DiagramStyles,
|
|
4
4
|
MessageSpecification,
|
|
5
|
-
SVGDocument
|
|
6
|
-
} from
|
|
5
|
+
SVGDocument,
|
|
6
|
+
} from "@swirly/types";
|
|
7
7
|
|
|
8
8
|
export type Point = {
|
|
9
|
-
x: number
|
|
10
|
-
y: number
|
|
11
|
-
}
|
|
9
|
+
x: number;
|
|
10
|
+
y: number;
|
|
11
|
+
};
|
|
12
12
|
|
|
13
13
|
export type Rectangle = {
|
|
14
|
-
x1: number
|
|
15
|
-
y1: number
|
|
16
|
-
x2: number
|
|
17
|
-
y2: number
|
|
18
|
-
}
|
|
14
|
+
x1: number;
|
|
15
|
+
y1: number;
|
|
16
|
+
x2: number;
|
|
17
|
+
y2: number;
|
|
18
|
+
};
|
|
19
19
|
|
|
20
|
-
export type TimeScaler = (time: number) => number
|
|
21
|
-
|
|
22
|
-
export type RendererOptions = {
|
|
23
|
-
DOMParser?: typeof DOMParser
|
|
24
|
-
styles?: DiagramStyles
|
|
25
|
-
}
|
|
20
|
+
export type TimeScaler = (time: number) => number;
|
|
26
21
|
|
|
27
22
|
export type RendererContext = {
|
|
28
|
-
DOMParser?: typeof DOMParser
|
|
29
|
-
document: SVGDocument
|
|
30
|
-
styles: DiagramStyles
|
|
31
|
-
streamHeight: number
|
|
32
|
-
streamTitleEnabled: boolean
|
|
33
|
-
}
|
|
23
|
+
DOMParser?: typeof DOMParser;
|
|
24
|
+
document: SVGDocument;
|
|
25
|
+
styles: DiagramStyles;
|
|
26
|
+
streamHeight: number;
|
|
27
|
+
streamTitleEnabled: boolean;
|
|
28
|
+
};
|
|
34
29
|
|
|
35
30
|
export type RendererResult = {
|
|
36
|
-
element: SVGElement
|
|
37
|
-
bbox: Rectangle
|
|
38
|
-
}
|
|
31
|
+
element: SVGElement;
|
|
32
|
+
bbox: Rectangle;
|
|
33
|
+
};
|
|
39
34
|
|
|
40
35
|
export type PostRenderUpdateContext = {
|
|
41
|
-
width: number
|
|
42
|
-
height: number
|
|
43
|
-
|
|
36
|
+
width: number;
|
|
37
|
+
height: number;
|
|
38
|
+
dx: number;
|
|
39
|
+
};
|
|
44
40
|
|
|
45
41
|
export type UpdatableRendererResult = RendererResult & {
|
|
46
|
-
update?: (ctx: PostRenderUpdateContext) => void
|
|
47
|
-
}
|
|
42
|
+
update?: (ctx: PostRenderUpdateContext) => void;
|
|
43
|
+
};
|
|
48
44
|
|
|
49
45
|
export type MessageRendererOptions = {
|
|
50
|
-
verticalOffset: number
|
|
51
|
-
valueAngle: number
|
|
52
|
-
}
|
|
46
|
+
verticalOffset: number;
|
|
47
|
+
valueAngle: number;
|
|
48
|
+
};
|
|
53
49
|
|
|
54
50
|
export type MessageRenderer = (
|
|
55
51
|
ctx: RendererContext,
|
|
56
52
|
message: MessageSpecification,
|
|
57
|
-
options: MessageRendererOptions
|
|
58
|
-
) => RendererResult
|
|
53
|
+
options: MessageRendererOptions,
|
|
54
|
+
) => RendererResult;
|
|
59
55
|
|
|
60
56
|
export type DecorationRendererContext = RendererContext & {
|
|
61
|
-
bbox: Rectangle
|
|
62
|
-
scaleTime: TimeScaler
|
|
63
|
-
}
|
|
57
|
+
bbox: Rectangle;
|
|
58
|
+
scaleTime: TimeScaler;
|
|
59
|
+
};
|
|
64
60
|
|
|
65
61
|
export type DecorationRendererResult = {
|
|
66
|
-
element: SVGElement
|
|
67
|
-
bbox?: Rectangle
|
|
68
|
-
}
|
|
62
|
+
element: SVGElement;
|
|
63
|
+
bbox?: Rectangle;
|
|
64
|
+
};
|
|
69
65
|
|
|
70
66
|
export type DecorationRenderer = (
|
|
71
67
|
ctx: DecorationRendererContext,
|
|
72
|
-
decoration: DecorationSpecification
|
|
73
|
-
) => DecorationRendererResult
|
|
68
|
+
decoration: DecorationSpecification,
|
|
69
|
+
) => DecorationRendererResult;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export const degreesToRadians = (degrees: number): number =>
|
|
2
|
-
(degrees / 180) * Math.PI
|
|
1
|
+
export const degreesToRadians = (degrees: number): number => (degrees / 180) * Math.PI;
|
package/src/util/geometry.ts
CHANGED
|
@@ -1,74 +1,61 @@
|
|
|
1
|
-
import { Point, Rectangle } from
|
|
1
|
+
import { Point, Rectangle } from "../types.js";
|
|
2
2
|
|
|
3
|
-
export const translatePoint = (
|
|
4
|
-
x: number,
|
|
5
|
-
y: number,
|
|
6
|
-
dx: number,
|
|
7
|
-
dy: number
|
|
8
|
-
): Point => ({
|
|
3
|
+
export const translatePoint = (x: number, y: number, dx: number, dy: number): Point => ({
|
|
9
4
|
x: x + dx,
|
|
10
|
-
y: y + dy
|
|
11
|
-
})
|
|
5
|
+
y: y + dy,
|
|
6
|
+
});
|
|
12
7
|
|
|
13
8
|
export const translateRectangle = (
|
|
14
9
|
{ x1, y1, x2, y2 }: Rectangle,
|
|
15
10
|
dx: number,
|
|
16
|
-
dy: number
|
|
11
|
+
dy: number,
|
|
17
12
|
): Rectangle => {
|
|
18
|
-
const p1 = translatePoint(x1, y1, dx, dy)
|
|
19
|
-
const p2 = translatePoint(x2, y2, dx, dy)
|
|
13
|
+
const p1 = translatePoint(x1, y1, dx, dy);
|
|
14
|
+
const p2 = translatePoint(x2, y2, dx, dy);
|
|
20
15
|
return {
|
|
21
16
|
x1: p1.x,
|
|
22
17
|
y1: p1.y,
|
|
23
18
|
x2: p2.x,
|
|
24
|
-
y2: p2.y
|
|
25
|
-
}
|
|
26
|
-
}
|
|
19
|
+
y2: p2.y,
|
|
20
|
+
};
|
|
21
|
+
};
|
|
27
22
|
|
|
28
|
-
export const rotatePoint = (
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
y0: number
|
|
34
|
-
): Point => {
|
|
35
|
-
const s = Math.sin(angle)
|
|
36
|
-
const c = Math.cos(angle)
|
|
37
|
-
x -= x0
|
|
38
|
-
y -= y0
|
|
23
|
+
export const rotatePoint = (x: number, y: number, angle: number, x0: number, y0: number): Point => {
|
|
24
|
+
const s = Math.sin(angle);
|
|
25
|
+
const c = Math.cos(angle);
|
|
26
|
+
x -= x0;
|
|
27
|
+
y -= y0;
|
|
39
28
|
return {
|
|
40
29
|
x: x * c - y * s + x0,
|
|
41
|
-
y: x * s + y * c + y0
|
|
42
|
-
}
|
|
43
|
-
}
|
|
30
|
+
y: x * s + y * c + y0,
|
|
31
|
+
};
|
|
32
|
+
};
|
|
44
33
|
|
|
45
34
|
export const rotateRectangle = (
|
|
46
35
|
{ x1, y1, x2, y2 }: Rectangle,
|
|
47
36
|
angle: number,
|
|
48
37
|
x0: number,
|
|
49
|
-
y0: number
|
|
38
|
+
y0: number,
|
|
50
39
|
): Rectangle => {
|
|
51
|
-
const p1 = rotatePoint(x1, y1, angle, x0, y0)
|
|
52
|
-
const p2 = rotatePoint(x2, y2, angle, x0, y0)
|
|
40
|
+
const p1 = rotatePoint(x1, y1, angle, x0, y0);
|
|
41
|
+
const p2 = rotatePoint(x2, y2, angle, x0, y0);
|
|
53
42
|
return {
|
|
54
43
|
x1: p1.x,
|
|
55
44
|
y1: p1.y,
|
|
56
45
|
x2: p2.x,
|
|
57
|
-
y2: p2.y
|
|
58
|
-
}
|
|
59
|
-
}
|
|
46
|
+
y2: p2.y,
|
|
47
|
+
};
|
|
48
|
+
};
|
|
60
49
|
|
|
61
50
|
export const rectangleUnion = (rects: readonly Rectangle[]): Rectangle => {
|
|
62
|
-
const nonEmptyRects = rects.filter(
|
|
63
|
-
({ x1, y1, x2, y2 }) => x2 - x1 !== 0 && y2 - y1 !== 0
|
|
64
|
-
)
|
|
51
|
+
const nonEmptyRects = rects.filter(({ x1, y1, x2, y2 }) => x2 - x1 !== 0 && y2 - y1 !== 0);
|
|
65
52
|
if (nonEmptyRects.length === 0) {
|
|
66
|
-
throw new Error(
|
|
53
|
+
throw new Error("All rectangles empty");
|
|
67
54
|
}
|
|
68
55
|
return {
|
|
69
56
|
x1: Math.min(...nonEmptyRects.map((rect) => rect.x1)),
|
|
70
57
|
y1: Math.min(...nonEmptyRects.map((rect) => rect.y1)),
|
|
71
58
|
x2: Math.max(...nonEmptyRects.map((rect) => rect.x2)),
|
|
72
|
-
y2: Math.max(...nonEmptyRects.map((rect) => rect.y2))
|
|
73
|
-
}
|
|
74
|
-
}
|
|
59
|
+
y2: Math.max(...nonEmptyRects.map((rect) => rect.y2)),
|
|
60
|
+
};
|
|
61
|
+
};
|
package/src/util/hex-to-dec.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
const A =
|
|
2
|
-
const ZERO =
|
|
1
|
+
const A = "A".charCodeAt(0);
|
|
2
|
+
const ZERO = "0".charCodeAt(0);
|
|
3
3
|
|
|
4
4
|
export const hexToDec = (hex: string): number => {
|
|
5
|
-
let dec = 0
|
|
6
|
-
hex = hex.toUpperCase()
|
|
5
|
+
let dec = 0;
|
|
6
|
+
hex = hex.toUpperCase();
|
|
7
7
|
for (let i = 0; i < hex.length; ++i) {
|
|
8
|
-
const c = hex.charCodeAt(i)
|
|
9
|
-
dec = (dec << 4) + (c >= A ? c - A + 10 : c - ZERO)
|
|
8
|
+
const c = hex.charCodeAt(i);
|
|
9
|
+
dec = (dec << 4) + (c >= A ? c - A + 10 : c - ZERO);
|
|
10
10
|
}
|
|
11
|
-
return dec
|
|
12
|
-
}
|
|
11
|
+
return dec;
|
|
12
|
+
};
|
package/src/util/merge-styles.ts
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
import { FreeformStyles } from
|
|
1
|
+
import { FreeformStyles } from "@swirly/types";
|
|
2
2
|
|
|
3
3
|
export const mergeStyles = (
|
|
4
4
|
globalStyles: FreeformStyles,
|
|
5
5
|
localStyles: FreeformStyles | null | undefined,
|
|
6
6
|
globalPrefix: string,
|
|
7
|
-
localStylesPrefixed: boolean = false
|
|
7
|
+
localStylesPrefixed: boolean = false,
|
|
8
8
|
): FreeformStyles => {
|
|
9
|
-
const result: FreeformStyles = {}
|
|
9
|
+
const result: FreeformStyles = {};
|
|
10
10
|
|
|
11
|
-
const globalKeys = Object.keys(globalStyles).filter((key) =>
|
|
12
|
-
key.startsWith(globalPrefix)
|
|
13
|
-
)
|
|
11
|
+
const globalKeys = Object.keys(globalStyles).filter((key) => key.startsWith(globalPrefix));
|
|
14
12
|
for (const globalKey of globalKeys) {
|
|
15
|
-
const simpleKey = globalKey.substring(globalPrefix.length)
|
|
16
|
-
const localKey = localStylesPrefixed ? globalKey : simpleKey
|
|
13
|
+
const simpleKey = globalKey.substring(globalPrefix.length);
|
|
14
|
+
const localKey = localStylesPrefixed ? globalKey : simpleKey;
|
|
17
15
|
result[simpleKey] =
|
|
18
16
|
localStyles != null && localStyles[localKey] != null
|
|
19
17
|
? localStyles[localKey]
|
|
20
|
-
: globalStyles[globalKey]
|
|
18
|
+
: globalStyles[globalKey];
|
|
21
19
|
}
|
|
22
20
|
|
|
23
|
-
return result
|
|
24
|
-
}
|
|
21
|
+
return result;
|
|
22
|
+
};
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
export class NotificationKind {
|
|
2
|
-
static NEXT = new NotificationKind(
|
|
3
|
-
static COMPLETE = new NotificationKind(
|
|
4
|
-
static ERROR = new NotificationKind(
|
|
2
|
+
static NEXT = new NotificationKind("N");
|
|
3
|
+
static COMPLETE = new NotificationKind("C");
|
|
4
|
+
static ERROR = new NotificationKind("E");
|
|
5
5
|
|
|
6
|
-
#char: string
|
|
6
|
+
#char: string;
|
|
7
7
|
|
|
8
|
-
private constructor
|
|
9
|
-
this.#char = char
|
|
8
|
+
private constructor(char: string) {
|
|
9
|
+
this.#char = char;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
equals
|
|
13
|
-
return typeof x ===
|
|
12
|
+
equals(x: any): boolean {
|
|
13
|
+
return typeof x === "string" && x.charAt(0).toUpperCase() === this.#char;
|
|
14
14
|
}
|
|
15
15
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
// @ts-ignore
|
|
2
|
-
import { sync as sha1 } from
|
|
2
|
+
import { sync as sha1 } from "simple-sha1";
|
|
3
3
|
|
|
4
|
-
import { hexToDec } from
|
|
4
|
+
import { hexToDec } from "./hex-to-dec.js";
|
|
5
5
|
|
|
6
|
-
export const stringToColor = (str: string, mode:
|
|
7
|
-
const hashHex = sha1(str)
|
|
8
|
-
const hash = hexToDec(hashHex.substring(4, 8))
|
|
9
|
-
const hue = Math.round((hash / (1 << 16)) * 360)
|
|
10
|
-
const saturation = 60
|
|
11
|
-
const lightness = mode ===
|
|
12
|
-
return `hsl(${hue}, ${saturation}%, ${lightness}%)
|
|
13
|
-
}
|
|
6
|
+
export const stringToColor = (str: string, mode: "light" | "dark"): string => {
|
|
7
|
+
const hashHex = sha1(str);
|
|
8
|
+
const hash = hexToDec(hashHex.substring(4, 8));
|
|
9
|
+
const hue = Math.round((hash / (1 << 16)) * 360);
|
|
10
|
+
const saturation = 60;
|
|
11
|
+
const lightness = mode === "dark" ? 20 : 80;
|
|
12
|
+
return `hsl(${hue}, ${saturation}%, ${lightness}%)`;
|
|
13
|
+
};
|