UNPKG

6.81 kBJavaScriptView Raw
1/**
2* Sutton SignWriting Core Module v1.4.2 (https://github.com/sutton-signwriting/core)
3* Author: Steve Slevinski (https://SteveSlevinski.me)
4* style.mjs is released under the MIT License.
5*/
6
7/**
8 * Object of regular expressions for style strings
9 *
10 * @alias style.re
11 * @type {object}
12 * @property {string} colorize - regular expression for colorize section
13 * @property {string} colorhex - regular expression for color hex values with 3 or 6 characters
14 * @property {string} colorname - regular expression for css color name
15 * @property {string} padding - regular expression for padding section
16 * @property {string} zoom - regular expression for zoom section
17 * @property {string} classbase - regular expression for class name definition
18 * @property {string} id - regular expression for id definition
19 * @property {string} colorbase - regular expression for color hex or color name
20 * @property {string} color - regular expression for single color entry
21 * @property {string} colors - regular expression for double color entry
22 * @property {string} background - regular expression for background section
23 * @property {string} detail - regular expression for color details for line and optional fill
24 * @property {string} detailsym - regular expression for color details for individual symbols
25 * @property {string} classes - regular expression for one or more class names
26 * @property {string} full - full regular expression for style string
27 */
28let re = {
29 'colorize': 'C',
30 'colorhex': '(?:[0-9a-fA-F]{3}){1,2}',
31 'colorname': '[a-zA-Z]+',
32 'padding': 'P[0-9]{2}',
33 'zoom': 'Z(?:[0-9]+(?:\\.[0-9]+)?|x)',
34 'classbase': '-?[_a-zA-Z][_a-zA-Z0-9-]{0,100}',
35 'id': '[a-zA-Z][_a-zA-Z0-9-]{0,100}'
36};
37re.colorbase = `(?:${re.colorhex}|${re.colorname})`;
38re.color = `_${re.colorbase}_`;
39re.colors = `_${re.colorbase}(?:,${re.colorbase})?_`;
40re.background = `G${re.color}`;
41re.detail = `D${re.colors}`;
42re.detailsym = `D[0-9]{2}${re.colors}`;
43re.classes = `${re.classbase}(?: ${re.classbase})*`;
44re.full = `-(${re.colorize})?(${re.padding})?(${re.background})?(${re.detail})?(${re.zoom})?(?:-((?:${re.detailsym})*))?(?:-(${re.classes})?!(?:(${re.id})!)?)?`;
45
46const prefixColor = color => {
47 const regex = new RegExp(`^${re.colorhex}$`);
48 return (regex.test(color) ? '#' : '') + color;
49};
50
51const definedProps = obj => Object.fromEntries(Object.entries(obj).filter(([k, v]) => v !== undefined));
52/**
53 * Function to parse style string to object
54 * @function style.parse
55 * @param {string} styleString - a style string
56 * @returns {StyleObject} elements of style string
57 * @example
58 * style.parse('-CP10G_blue_D_red,Cyan_')
59 *
60 * return {
61 * 'colorize': true,
62 * 'padding': 10,
63 * 'background': 'blue',
64 * 'detail': ['red', 'Cyan']
65 * }
66 */
67
68
69const parse = styleString => {
70 const regex = `^${re.full}`;
71 const m = (typeof styleString === 'string' ? styleString.match(new RegExp(regex)) : []) || [];
72 return definedProps({
73 'colorize': !m[1] ? undefined : !!m[1],
74 'padding': !m[2] ? undefined : parseInt(m[2].slice(1)),
75 'background': !m[3] ? undefined : prefixColor(m[3].slice(2, -1)),
76 'detail': !m[4] ? undefined : m[4].slice(2, -1).split(',').map(prefixColor),
77 'zoom': !m[5] ? undefined : m[5] === 'Zx' ? 'x' : parseFloat(m[5].slice(1)),
78 'detailsym': !m[6] ? undefined : m[6].match(new RegExp(re.detailsym, 'g')).map(val => {
79 const parts = val.split('_');
80 const detail = parts[1].split(',').map(prefixColor);
81 return {
82 'index': parseInt(parts[0].slice(1)),
83 'detail': detail
84 };
85 }),
86 'classes': !m[7] ? undefined : m[7],
87 'id': !m[8] ? undefined : m[8]
88 });
89};
90
91/**
92 * Function to compose style string from object
93 * @function style.compose
94 * @param {StyleObject} styleObject - an object of style options
95 * @returns {string} style string
96 * @example
97 * style.compose({
98 * 'colorize': true,
99 * 'padding': 10,
100 * 'background': 'blue',
101 * 'detail': ['red', 'Cyan'],
102 * 'zoom': 1.1,
103 * 'detailsym': [
104 * {
105 * 'index': 1,
106 * 'detail': ['#ff00ff']
107 * },
108 * {
109 * 'index': 2,
110 * 'detail': ['yellow', 'green']
111 * }
112 * ],
113 * 'classes': 'primary blinking',
114 * 'id': 'cursor'
115 * })
116 *
117 * return '-CP10G_blue_D_red,Cyan_Z1.1-D01_ff00ff_D02_yellow,green_-primary blinking!cursor!'
118 */
119
120const compose = styleObject => {
121 if (typeof styleObject !== 'object' || styleObject === null) return undefined; // three sections
122
123 let style1 = '-';
124 style1 += !styleObject.colorize ? '' : 'C';
125 const padding = parseInt(styleObject.padding);
126 style1 += !padding || padding <= 0 || padding > 99 ? '' : 'P' + (padding > 9 ? padding : '0' + padding);
127 const background = !styleObject.background || !(typeof styleObject.background === 'string') ? undefined : styleObject.background.match(re.colorbase)[0];
128 style1 += !background ? '' : 'G_' + background + '_';
129 const detail1 = !styleObject.detail || !styleObject.detail[0] || !(typeof styleObject.detail[0] === 'string') ? undefined : styleObject.detail[0].match(re.colorbase)[0];
130 const detail2 = !styleObject.detail || !styleObject.detail[1] || !(typeof styleObject.detail[1] === 'string') ? undefined : styleObject.detail[1].match(re.colorbase)[0];
131
132 if (detail1) {
133 style1 += 'D_' + detail1;
134
135 if (detail2) {
136 style1 += ',' + detail2;
137 }
138
139 style1 += '_';
140 }
141
142 const zoom = styleObject.zoom === 'x' ? 'x' : parseFloat(styleObject.zoom);
143 style1 += !zoom || zoom <= 0 ? '' : 'Z' + zoom;
144 let style2 = '';
145 const detailsym = !styleObject.detailsym || !Array.isArray(styleObject.detailsym) ? [] : styleObject.detailsym.map(styleObject => {
146 const index = parseInt(styleObject.index);
147 if (!index || index <= 0 || index > 99) return '';
148 let style = 'D' + (index > 9 ? index : '0' + index);
149 const detail1 = !styleObject.detail || !styleObject.detail[0] ? undefined : styleObject.detail[0].match(re.colorbase)[0];
150 const detail2 = !styleObject.detail || !styleObject.detail[1] ? undefined : styleObject.detail[1].match(re.colorbase)[0];
151
152 if (detail1) {
153 style += '_' + detail1;
154
155 if (detail2) {
156 style += ',' + detail2;
157 }
158
159 style += '_';
160 }
161
162 return style;
163 });
164 style2 += detailsym.join('');
165 let style3 = '';
166 const classes = !styleObject.classes || !(typeof styleObject.classes === 'string') ? undefined : styleObject.classes.match(re.classes)[0];
167 style3 += !classes ? '' : classes;
168 const id = !styleObject.id || !(typeof styleObject.id === 'string') ? undefined : styleObject.id.match(re.id)[0];
169 style3 += classes || id ? '!' : '';
170 style3 += !id ? '' : id + '!';
171 return style1 + (style2 || style3 ? '-' + style2 : '') + (style3 ? '-' + style3 : '');
172};
173
174export { compose, parse, re };
175
176/* support ongoing development on https://patreon.com/signwriting */