-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSvgUtils.ts
98 lines (91 loc) · 3.39 KB
/
SvgUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright 2023 Inclusive Design Research Centre, OCAD University
* All rights reserved.
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this License.
*
* You may obtain a copy of the License at
* https://github.com/inclusive-design/adaptive-palette/blob/main/LICENSE
*/
import { BlissSVGBuilder } from "bliss-svg-builder";
import { BciAvIdType } from "./index.d";
import { adaptivePaletteGlobals } from "./GlobalData";
/**
* Convert the given `BciAvIdType` to a SVG builder code string. If the
* `BciAvIdType`argument is an array of BCI-AV-IDs and punctuation, concatenate
* the array into a string of builder code strings and punctuation marks.
* @param {BciAvIdType} bciAvId - The BciAvIdType to convert.
* @return {String} - The concatenation of the builder codes and punctuation,
* e.g., "B106/B12".
*/
export function bciAvIdToString (bciAvId: BciAvIdType) {
let finalCode = "";
if (typeof bciAvId === "number") {
const { blissSvgBuilderCode } = bciToBlissaryId(bciAvId);
finalCode = blissSvgBuilderCode;
}
// `bicAvId` is an array
else {
bciAvId.forEach((item) => {
if (typeof item === "number") {
const { blissSvgBuilderCode } = bciToBlissaryId(item);
finalCode = `${finalCode}${blissSvgBuilderCode}`;
} else {
finalCode = `${finalCode}${item}`;
}
});
}
return finalCode;
}
/**
* Create and return the builder from a string based on the given BciAvIdType.
* If the BciAvIdType is invalid, `null` is returned.
* @param {BciAvIdType} bciAvId - A single BCI-AV-ID (a number) or an array of
* such ids and characters, e.g.
* `[ 12335, "/", 8499 ]`
* @return {BlissSVGBuilder} - The corresponding SVG markup, or `null`.
*/
function getSvgBuilder (bciAvId: BciAvIdType) {
let builder;
try {
const svgBuilderArgument = bciAvIdToString(bciAvId);
builder = new BlissSVGBuilder(svgBuilderArgument);
}
catch (err) {
console.error(err);
console.error(`Unknown bci-av-id = ${bciAvId}`);
builder = null;
}
return builder;
}
/**
* Get the SVG markup as a string based on the given single BCI-AV-ID.
* or an array of BCI-AV-IDs and other characters
*
* @param {BciAvIdType} bciAvId - A single BCI-AV-ID (a number) or an array of
* such ids and characters, e.g.
* `[ 12335, "/", 8499 ]`
* @return {String} - The corresponding SVG markup, or `undefined`.
*/
export function getSvgMarkupString (bciAvId: BciAvIdType) {
const builder = getSvgBuilder(bciAvId);
return ( builder ? builder.svgCode : undefined );
}
/**
* Get the SVG markup as a DOM element based on the given single BCI-AV-ID.
* or an array of BCI-AV-IDs and other characters
*
* @param {BciAvIdType} bciAvId - A single BCI-AV-ID (a number) or an array of
* such ids and characters, e.g.
* `[ 12335, "/", 8499 ]`
* @return {Element} - The corresponding SVG markup, or `undefined`.
*/
export function getSvgElement (bciAvId: BciAvIdType) {
const builder = getSvgBuilder(bciAvId);
return ( builder ? builder.svgElement : undefined );
}
export function bciToBlissaryId (bciAvId: number) {
const { blissaryIdMap } = adaptivePaletteGlobals;
return blissaryIdMap.find((entry) => entry.bciAvId === bciAvId);
}