@swirly/renderer 0.21.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 +14 -14
- 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 +1 -1
- package/dist/index.js +15 -15
- 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 +40 -40
- 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 +13 -13
- 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 +34 -35
- 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 +55 -68
- 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 +104 -120
- 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 -39
- 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/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,68 +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
|
|
20
|
+
export type TimeScaler = (time: number) => number;
|
|
21
21
|
|
|
22
22
|
export type RendererContext = {
|
|
23
|
-
DOMParser?: typeof DOMParser
|
|
24
|
-
document: SVGDocument
|
|
25
|
-
styles: DiagramStyles
|
|
26
|
-
streamHeight: number
|
|
27
|
-
streamTitleEnabled: boolean
|
|
28
|
-
}
|
|
23
|
+
DOMParser?: typeof DOMParser;
|
|
24
|
+
document: SVGDocument;
|
|
25
|
+
styles: DiagramStyles;
|
|
26
|
+
streamHeight: number;
|
|
27
|
+
streamTitleEnabled: boolean;
|
|
28
|
+
};
|
|
29
29
|
|
|
30
30
|
export type RendererResult = {
|
|
31
|
-
element: SVGElement
|
|
32
|
-
bbox: Rectangle
|
|
33
|
-
}
|
|
31
|
+
element: SVGElement;
|
|
32
|
+
bbox: Rectangle;
|
|
33
|
+
};
|
|
34
34
|
|
|
35
35
|
export type PostRenderUpdateContext = {
|
|
36
|
-
width: number
|
|
37
|
-
height: number
|
|
38
|
-
dx: number
|
|
39
|
-
}
|
|
36
|
+
width: number;
|
|
37
|
+
height: number;
|
|
38
|
+
dx: number;
|
|
39
|
+
};
|
|
40
40
|
|
|
41
41
|
export type UpdatableRendererResult = RendererResult & {
|
|
42
|
-
update?: (ctx: PostRenderUpdateContext) => void
|
|
43
|
-
}
|
|
42
|
+
update?: (ctx: PostRenderUpdateContext) => void;
|
|
43
|
+
};
|
|
44
44
|
|
|
45
45
|
export type MessageRendererOptions = {
|
|
46
|
-
verticalOffset: number
|
|
47
|
-
valueAngle: number
|
|
48
|
-
}
|
|
46
|
+
verticalOffset: number;
|
|
47
|
+
valueAngle: number;
|
|
48
|
+
};
|
|
49
49
|
|
|
50
50
|
export type MessageRenderer = (
|
|
51
51
|
ctx: RendererContext,
|
|
52
52
|
message: MessageSpecification,
|
|
53
|
-
options: MessageRendererOptions
|
|
54
|
-
) => RendererResult
|
|
53
|
+
options: MessageRendererOptions,
|
|
54
|
+
) => RendererResult;
|
|
55
55
|
|
|
56
56
|
export type DecorationRendererContext = RendererContext & {
|
|
57
|
-
bbox: Rectangle
|
|
58
|
-
scaleTime: TimeScaler
|
|
59
|
-
}
|
|
57
|
+
bbox: Rectangle;
|
|
58
|
+
scaleTime: TimeScaler;
|
|
59
|
+
};
|
|
60
60
|
|
|
61
61
|
export type DecorationRendererResult = {
|
|
62
|
-
element: SVGElement
|
|
63
|
-
bbox?: Rectangle
|
|
64
|
-
}
|
|
62
|
+
element: SVGElement;
|
|
63
|
+
bbox?: Rectangle;
|
|
64
|
+
};
|
|
65
65
|
|
|
66
66
|
export type DecorationRenderer = (
|
|
67
67
|
ctx: DecorationRendererContext,
|
|
68
|
-
decoration: DecorationSpecification
|
|
69
|
-
) => 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
|
+
};
|
package/src/util/svg-xml.ts
CHANGED
|
@@ -1,44 +1,41 @@
|
|
|
1
|
-
import { SVGDocument } from
|
|
1
|
+
import { SVGDocument } from "@swirly/types";
|
|
2
2
|
|
|
3
|
-
export const SVG_NS =
|
|
4
|
-
export const SVG_MIME_TYPE =
|
|
3
|
+
export const SVG_NS = "http://www.w3.org/2000/svg";
|
|
4
|
+
export const SVG_MIME_TYPE = "image/svg+xml";
|
|
5
5
|
|
|
6
|
-
export const XHTML_NS =
|
|
6
|
+
export const XHTML_NS = "http://www.w3.org/1999/xhtml";
|
|
7
7
|
|
|
8
|
-
const EMPTY_SVG_XML = `<svg xmlns="${SVG_NS}"
|
|
8
|
+
const EMPTY_SVG_XML = `<svg xmlns="${SVG_NS}"/>`;
|
|
9
9
|
|
|
10
|
-
const parseXml = (
|
|
11
|
-
xml
|
|
12
|
-
mimeType: string,
|
|
13
|
-
DOMParserImpl: typeof DOMParser = DOMParser
|
|
14
|
-
) => new DOMParserImpl().parseFromString(xml, mimeType as any) as XMLDocument
|
|
10
|
+
const parseXml = (xml: string, mimeType: string, DOMParserImpl: typeof DOMParser = DOMParser) =>
|
|
11
|
+
new DOMParserImpl().parseFromString(xml, mimeType as any) as XMLDocument;
|
|
15
12
|
|
|
16
13
|
export const createSvgDocument = (DOMParserImpl?: typeof DOMParser) =>
|
|
17
|
-
parseXml(EMPTY_SVG_XML, SVG_MIME_TYPE, DOMParserImpl) as SVGDocument
|
|
14
|
+
parseXml(EMPTY_SVG_XML, SVG_MIME_TYPE, DOMParserImpl) as SVGDocument;
|
|
18
15
|
|
|
19
16
|
export const createSvgElement = (
|
|
20
17
|
document: SVGDocument,
|
|
21
18
|
name: string,
|
|
22
19
|
attrs: Record<string, any> = {},
|
|
23
|
-
text: string =
|
|
20
|
+
text: string = "",
|
|
24
21
|
): SVGElement => {
|
|
25
|
-
const $el = document.createElementNS(SVG_NS, name)
|
|
22
|
+
const $el = document.createElementNS(SVG_NS, name);
|
|
26
23
|
for (const [name, value] of Object.entries(attrs)) {
|
|
27
|
-
$el.setAttribute(name, String(value))
|
|
24
|
+
$el.setAttribute(name, String(value));
|
|
28
25
|
}
|
|
29
|
-
if (text !==
|
|
30
|
-
$el.textContent = text
|
|
26
|
+
if (text !== "") {
|
|
27
|
+
$el.textContent = text;
|
|
31
28
|
}
|
|
32
|
-
return $el
|
|
33
|
-
}
|
|
29
|
+
return $el;
|
|
30
|
+
};
|
|
34
31
|
|
|
35
32
|
export const setSvgDimensions = (
|
|
36
33
|
$svg: SVGSVGElement,
|
|
37
34
|
width: number,
|
|
38
35
|
height: number,
|
|
39
|
-
scale: number = 1
|
|
36
|
+
scale: number = 1,
|
|
40
37
|
) => {
|
|
41
|
-
$svg.setAttribute(
|
|
42
|
-
$svg.setAttribute(
|
|
43
|
-
$svg.setAttribute(
|
|
44
|
-
}
|
|
38
|
+
$svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
|
|
39
|
+
$svg.setAttribute("width", String(width * scale));
|
|
40
|
+
$svg.setAttribute("height", String(height * scale));
|
|
41
|
+
};
|
package/src/util/transform.ts
CHANGED
|
@@ -1,25 +1,19 @@
|
|
|
1
1
|
export const transform = ($element: SVGElement, value: string) => {
|
|
2
|
-
const existing = $element.getAttribute(
|
|
3
|
-
const updated =
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
2
|
+
const existing = $element.getAttribute("transform");
|
|
3
|
+
const updated = (existing != null && existing !== "" ? existing + " " : "") + value;
|
|
4
|
+
$element.setAttribute("transform", updated);
|
|
5
|
+
};
|
|
7
6
|
|
|
8
7
|
export const translate = ($element: SVGElement, x: number, y: number) => {
|
|
9
8
|
if (x === 0 && y === 0) {
|
|
10
|
-
return
|
|
9
|
+
return;
|
|
11
10
|
}
|
|
12
|
-
transform($element, `translate(${x} ${y})`)
|
|
13
|
-
}
|
|
11
|
+
transform($element, `translate(${x} ${y})`);
|
|
12
|
+
};
|
|
14
13
|
|
|
15
|
-
export const rotate = (
|
|
16
|
-
$element: SVGElement,
|
|
17
|
-
angle: number,
|
|
18
|
-
x: number = 0,
|
|
19
|
-
y: number = 0
|
|
20
|
-
) => {
|
|
14
|
+
export const rotate = ($element: SVGElement, angle: number, x: number = 0, y: number = 0) => {
|
|
21
15
|
if (angle === 0) {
|
|
22
|
-
return
|
|
16
|
+
return;
|
|
23
17
|
}
|
|
24
|
-
transform($element, `rotate(${angle} ${x} ${y})`)
|
|
25
|
-
}
|
|
18
|
+
transform($element, `rotate(${angle} ${x} ${y})`);
|
|
19
|
+
};
|