@briklab/lib 1.1.9 → 1.2.0-test
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/dist/cli-john/index.js +459 -15
- package/dist/cli-john/index.js.map +1 -1
- package/dist/color/index.js +205 -7
- package/dist/index.d.ts +1 -1
- package/dist/index.js +5 -1
- package/dist/jstc/index.js +97 -5
- package/dist/stylesheet/index.js +251 -14
- package/dist/warner/index.d.ts +4 -0
- package/dist/warner/index.js +169 -4
- package/dist/warner/index.js.map +1 -1
- package/package.json +2 -3
package/dist/color/index.js
CHANGED
|
@@ -1,7 +1,205 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Easy way to use colors
|
|
3
|
+
*/
|
|
4
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
5
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
6
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
7
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
8
|
+
};
|
|
9
|
+
var _Color_instances, _Color_rgbToAnsi256Index, _Color_clamp, _Color_toHex, _Color_parseString, _Color_hslToRgb, _Color_rgbToHsl;
|
|
10
|
+
import { warner } from "../warner/index.js";
|
|
11
|
+
const NAMED_COLORS = {
|
|
12
|
+
red: "#ff0000",
|
|
13
|
+
blue: "#0000ff",
|
|
14
|
+
green: "#00ff00",
|
|
15
|
+
yellow: "#ffff00",
|
|
16
|
+
orange: "#ffa500",
|
|
17
|
+
black: "#000000",
|
|
18
|
+
white: "#ffffff",
|
|
19
|
+
gray: "#808080",
|
|
20
|
+
};
|
|
21
|
+
class Color {
|
|
22
|
+
constructor(input) {
|
|
23
|
+
_Color_instances.add(this);
|
|
24
|
+
this.r = 0;
|
|
25
|
+
this.g = 0;
|
|
26
|
+
this.b = 0;
|
|
27
|
+
this.a = 1;
|
|
28
|
+
if (typeof input === "string") {
|
|
29
|
+
__classPrivateFieldGet(this, _Color_instances, "m", _Color_parseString).call(this, input);
|
|
30
|
+
}
|
|
31
|
+
else if ("r" in input && "g" in input && "b" in input) {
|
|
32
|
+
this.r = __classPrivateFieldGet(this, _Color_instances, "m", _Color_clamp).call(this, input.r);
|
|
33
|
+
this.g = __classPrivateFieldGet(this, _Color_instances, "m", _Color_clamp).call(this, input.g);
|
|
34
|
+
this.b = __classPrivateFieldGet(this, _Color_instances, "m", _Color_clamp).call(this, input.b);
|
|
35
|
+
this.a = input.a ?? 1;
|
|
36
|
+
}
|
|
37
|
+
else if ("h" in input && "s" in input && "l" in input) {
|
|
38
|
+
const { r, g, b } = __classPrivateFieldGet(this, _Color_instances, "m", _Color_hslToRgb).call(this, input.h, input.s, input.l);
|
|
39
|
+
this.r = r;
|
|
40
|
+
this.g = g;
|
|
41
|
+
this.b = b;
|
|
42
|
+
this.a = input.a ?? 1;
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
warner.warn({ message: `[Color.constructor] Invalid first argument!
|
|
46
|
+
Hint: The first argument must be a valid color array
|
|
47
|
+
Using black as fallback.` });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// -----------------------
|
|
51
|
+
// Public Methods
|
|
52
|
+
// -----------------------
|
|
53
|
+
hex() {
|
|
54
|
+
return `#${__classPrivateFieldGet(this, _Color_instances, "m", _Color_toHex).call(this, this.r)}${__classPrivateFieldGet(this, _Color_instances, "m", _Color_toHex).call(this, this.g)}${__classPrivateFieldGet(this, _Color_instances, "m", _Color_toHex).call(this, this.b)}`;
|
|
55
|
+
}
|
|
56
|
+
rgb() {
|
|
57
|
+
return `rgb(${this.r}, ${this.g}, ${this.b})`;
|
|
58
|
+
}
|
|
59
|
+
rgba() {
|
|
60
|
+
return `rgba(${this.r}, ${this.g}, ${this.b}, ${this.a})`;
|
|
61
|
+
}
|
|
62
|
+
hsl() {
|
|
63
|
+
const { h, s, l } = __classPrivateFieldGet(this, _Color_instances, "m", _Color_rgbToHsl).call(this, this.r, this.g, this.b);
|
|
64
|
+
return `hsl(${h}, ${s}%, ${l}%)`;
|
|
65
|
+
}
|
|
66
|
+
hsla() {
|
|
67
|
+
const { h, s, l } = __classPrivateFieldGet(this, _Color_instances, "m", _Color_rgbToHsl).call(this, this.r, this.g, this.b);
|
|
68
|
+
return `hsla(${h}, ${s}%, ${l}%, ${this.a})`;
|
|
69
|
+
}
|
|
70
|
+
css() {
|
|
71
|
+
return this.a === 1 ? this.hex() : this.rgba();
|
|
72
|
+
}
|
|
73
|
+
/** Return a 24-bit (truecolor) ANSI sequence for this color (foreground) */
|
|
74
|
+
ansiTruecolor() {
|
|
75
|
+
return `\x1b[38;2;${this.r};${this.g};${this.b}m`;
|
|
76
|
+
}
|
|
77
|
+
/** Return a 24-bit (truecolor) ANSI sequence for background */
|
|
78
|
+
ansiTruecolorBg() {
|
|
79
|
+
return `\x1b[48;2;${this.r};${this.g};${this.b}m`;
|
|
80
|
+
}
|
|
81
|
+
/** Return a 256-color ANSI sequence for this color (foreground) */
|
|
82
|
+
ansi256() {
|
|
83
|
+
const idx = __classPrivateFieldGet(this, _Color_instances, "m", _Color_rgbToAnsi256Index).call(this, this.r, this.g, this.b);
|
|
84
|
+
return `\x1b[38;5;${idx}m`;
|
|
85
|
+
}
|
|
86
|
+
/** Return a 256-color ANSI sequence for background */
|
|
87
|
+
ansi256Bg() {
|
|
88
|
+
const idx = __classPrivateFieldGet(this, _Color_instances, "m", _Color_rgbToAnsi256Index).call(this, this.r, this.g, this.b);
|
|
89
|
+
return `\x1b[48;5;${idx}m`;
|
|
90
|
+
}
|
|
91
|
+
/** Wrap text with this color (truecolor by default). Options: {background?: boolean, use256?: boolean, bold?: boolean, underline?: boolean} */
|
|
92
|
+
wrapAnsi(text, opts = {}) {
|
|
93
|
+
const use256 = Boolean(opts.use256);
|
|
94
|
+
const seq = opts.background
|
|
95
|
+
? use256
|
|
96
|
+
? this.ansi256Bg()
|
|
97
|
+
: this.ansiTruecolorBg()
|
|
98
|
+
: use256
|
|
99
|
+
? this.ansi256()
|
|
100
|
+
: this.ansiTruecolor();
|
|
101
|
+
const mods = `${opts.bold ? Color.BOLD : ""}${opts.underline ? Color.UNDERLINE : ""}`;
|
|
102
|
+
return `${mods}${seq}${text}${Color.RESET}`;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
_Color_instances = new WeakSet(), _Color_rgbToAnsi256Index = function _Color_rgbToAnsi256Index(r, g, b) {
|
|
106
|
+
// grayscale range
|
|
107
|
+
if (r === g && g === b) {
|
|
108
|
+
if (r < 8)
|
|
109
|
+
return 16;
|
|
110
|
+
if (r > 248)
|
|
111
|
+
return 231;
|
|
112
|
+
return Math.round(((r - 8) / 247) * 24) + 232;
|
|
113
|
+
}
|
|
114
|
+
const to6 = (v) => Math.round((v / 255) * 5);
|
|
115
|
+
const ri = to6(r);
|
|
116
|
+
const gi = to6(g);
|
|
117
|
+
const bi = to6(b);
|
|
118
|
+
return 16 + 36 * ri + 6 * gi + bi;
|
|
119
|
+
}, _Color_clamp = function _Color_clamp(value) {
|
|
120
|
+
return Math.max(0, Math.min(255, value));
|
|
121
|
+
}, _Color_toHex = function _Color_toHex(value) {
|
|
122
|
+
return value.toString(16).padStart(2, "0");
|
|
123
|
+
}, _Color_parseString = function _Color_parseString(str) {
|
|
124
|
+
str = str.trim().toLowerCase();
|
|
125
|
+
if (NAMED_COLORS[str]) {
|
|
126
|
+
str = NAMED_COLORS[str];
|
|
127
|
+
}
|
|
128
|
+
if (str.startsWith("#")) {
|
|
129
|
+
const hex = str.slice(1);
|
|
130
|
+
if (hex.length === 3) {
|
|
131
|
+
this.r = parseInt(hex[0] + hex[0], 16);
|
|
132
|
+
this.g = parseInt(hex[1] + hex[1], 16);
|
|
133
|
+
this.b = parseInt(hex[2] + hex[2], 16);
|
|
134
|
+
}
|
|
135
|
+
else if (hex.length === 6) {
|
|
136
|
+
this.r = parseInt(hex.slice(0, 2), 16);
|
|
137
|
+
this.g = parseInt(hex.slice(2, 4), 16);
|
|
138
|
+
this.b = parseInt(hex.slice(4, 6), 16);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
warner.warn({ message: `[Color class] @briklab/lib/color: Invalid hex!
|
|
142
|
+
Hint: You must pass a valid hex color string!
|
|
143
|
+
Using black as fallback.` });
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
else if (str.startsWith("rgb")) {
|
|
147
|
+
const vals = str.match(/[\d.]+/g)?.map(Number);
|
|
148
|
+
if (vals && vals.length >= 3) {
|
|
149
|
+
[this.r, this.g, this.b] = vals;
|
|
150
|
+
this.a = vals[3] ?? 1;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else if (str.startsWith("hsl")) {
|
|
154
|
+
const vals = str.match(/[\d.]+/g)?.map(Number);
|
|
155
|
+
if (vals && vals.length >= 3) {
|
|
156
|
+
const { r, g, b } = __classPrivateFieldGet(this, _Color_instances, "m", _Color_hslToRgb).call(this, vals[0], vals[1], vals[2]);
|
|
157
|
+
this.r = r;
|
|
158
|
+
this.g = g;
|
|
159
|
+
this.b = b;
|
|
160
|
+
this.a = vals[3] ?? 1;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
warner.warn({ message: `[Color class] @briklab/lib/color: Unknown color string "${str}"!
|
|
165
|
+
Hint: The argument must be a valid color string!
|
|
166
|
+
Using black as fallback.` });
|
|
167
|
+
}
|
|
168
|
+
}, _Color_hslToRgb = function _Color_hslToRgb(h, s, l) {
|
|
169
|
+
s /= 100;
|
|
170
|
+
l /= 100;
|
|
171
|
+
const k = (n) => (n + h / 30) % 12;
|
|
172
|
+
const a = s * Math.min(l, 1 - l);
|
|
173
|
+
const f = (n) => l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));
|
|
174
|
+
return { r: Math.round(f(0) * 255), g: Math.round(f(8) * 255), b: Math.round(f(4) * 255) };
|
|
175
|
+
}, _Color_rgbToHsl = function _Color_rgbToHsl(r, g, b) {
|
|
176
|
+
r /= 255;
|
|
177
|
+
g /= 255;
|
|
178
|
+
b /= 255;
|
|
179
|
+
const max = Math.max(r, g, b), min = Math.min(r, g, b);
|
|
180
|
+
let h = 0, s = 0, l = (max + min) / 2;
|
|
181
|
+
if (max !== min) {
|
|
182
|
+
const d = max - min;
|
|
183
|
+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
184
|
+
switch (max) {
|
|
185
|
+
case r:
|
|
186
|
+
h = (g - b) / d + (g < b ? 6 : 0);
|
|
187
|
+
break;
|
|
188
|
+
case g:
|
|
189
|
+
h = (b - r) / d + 2;
|
|
190
|
+
break;
|
|
191
|
+
case b:
|
|
192
|
+
h = (r - g) / d + 4;
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
h *= 60;
|
|
196
|
+
}
|
|
197
|
+
return { h: Math.round(h), s: Math.round(s * 100), l: Math.round(l * 100) };
|
|
198
|
+
};
|
|
199
|
+
/**
|
|
200
|
+
* ANSI / Terminal color helpers
|
|
201
|
+
*/
|
|
202
|
+
Color.RESET = "\x1b[0m";
|
|
203
|
+
Color.BOLD = "\x1b[1m";
|
|
204
|
+
Color.UNDERLINE = "\x1b[4m";
|
|
205
|
+
export default Color;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/jstc/index.js
CHANGED
|
@@ -1,5 +1,97 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/**
|
|
2
|
+
* # JSTC
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
* Runtime JS Type Checker
|
|
5
|
+
* @module JSTC
|
|
6
|
+
*/
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _JSTypeChecker___CustomHandler;
|
|
13
|
+
import { warner } from "../warner/index.js";
|
|
14
|
+
/**
|
|
15
|
+
* # JSTypeChecker
|
|
16
|
+
* A JS Type Checker. Add type checking to your javascript files as well
|
|
17
|
+
*/
|
|
18
|
+
export class JSTypeChecker {
|
|
19
|
+
constructor() {
|
|
20
|
+
_JSTypeChecker___CustomHandler.set(this, {
|
|
21
|
+
Array: (value) => Array.isArray(value),
|
|
22
|
+
"string[]": (value) => Array.isArray(value) && value.every((v) => typeof v === "string"),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* ### JSTypeChecker.for
|
|
27
|
+
* check if specific arguments are of a specific type, class, etc.
|
|
28
|
+
* @param {unknown[]} args
|
|
29
|
+
*/
|
|
30
|
+
for(args) {
|
|
31
|
+
if (!Array.isArray(args)) {
|
|
32
|
+
warner.warn({ message: `[JSTC.for] @briklab/lib/jstc: Invalid first argument!
|
|
33
|
+
Hint: The first argument must be a array.
|
|
34
|
+
Using [givenValue] as fallback` });
|
|
35
|
+
args = [args];
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
/**
|
|
39
|
+
* ### JSTypeChecker.for().check
|
|
40
|
+
* Check the given arguments with corresponding given types
|
|
41
|
+
*/
|
|
42
|
+
check: (types) => {
|
|
43
|
+
if (args.length < types.length)
|
|
44
|
+
return false;
|
|
45
|
+
for (let i = 0; i < types.length; i++) {
|
|
46
|
+
const value = args[i];
|
|
47
|
+
const expected = types[i];
|
|
48
|
+
const expectedTypes = Array.isArray(expected) ? expected : [expected];
|
|
49
|
+
let matched = false;
|
|
50
|
+
for (const tRaw of expectedTypes) {
|
|
51
|
+
const unionTypes = typeof tRaw === "string" ? tRaw.split("|") : [tRaw];
|
|
52
|
+
for (const t of unionTypes) {
|
|
53
|
+
if (typeof t === "function") {
|
|
54
|
+
if (value instanceof t) {
|
|
55
|
+
matched = true;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else if (typeof t === "string" && __classPrivateFieldGet(this, _JSTypeChecker___CustomHandler, "f")[t]) {
|
|
60
|
+
if (__classPrivateFieldGet(this, _JSTypeChecker___CustomHandler, "f")[t](value)) {
|
|
61
|
+
matched = true;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else if (typeof value === t) {
|
|
66
|
+
matched = true;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (matched)
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
if (!matched)
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* ### JSTypeChecker.addCustomHandler
|
|
82
|
+
* Create a custom handler for checking types.
|
|
83
|
+
*/
|
|
84
|
+
addCustomHandler(name, handler) {
|
|
85
|
+
if (!(typeof name === "string" && typeof handler === "function")) {
|
|
86
|
+
warner.warn({ message: `[JSTC.addCustomHandler] @briklab/lib/jstc: Invalid Arguments!
|
|
87
|
+
Hint: The first argument must be a string, and the second argument must be a function
|
|
88
|
+
Using String(argument1) and ()=>false as fallbacks` });
|
|
89
|
+
name = String(name);
|
|
90
|
+
handler = () => false;
|
|
91
|
+
}
|
|
92
|
+
__classPrivateFieldGet(this, _JSTypeChecker___CustomHandler, "f")[name] = handler;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
_JSTypeChecker___CustomHandler = new WeakMap();
|
|
96
|
+
const JSTC = new JSTypeChecker();
|
|
97
|
+
export default JSTC;
|
package/dist/stylesheet/index.js
CHANGED
|
@@ -1,14 +1,251 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
/**
|
|
2
|
+
* # @briklab/lib/stylesheet
|
|
3
|
+
* Create inline styles in JS/TS
|
|
4
|
+
*/
|
|
5
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
6
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
7
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
8
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
9
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
10
|
+
};
|
|
11
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
12
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
13
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
14
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
15
|
+
};
|
|
16
|
+
var _InlineStyle_instances, _InlineStyle_cssStyleDec, _InlineStyle_convertFieldToHyphenCase, _InlineStyle_convertKeysToValidCSS, _InlineStyle_styleObject, _StyleSheet_styles;
|
|
17
|
+
import JSTC from "../jstc/index.js";
|
|
18
|
+
import { warner } from "../warner/index.js";
|
|
19
|
+
import { CSSStyleDeclaration as UUIII } from "cssom";
|
|
20
|
+
import Color from "../color/index.js";
|
|
21
|
+
/**
|
|
22
|
+
* # InlineStyle
|
|
23
|
+
* @classdesc Create a CSS Inline style.
|
|
24
|
+
* @class
|
|
25
|
+
*/
|
|
26
|
+
class InlineStyle {
|
|
27
|
+
/**
|
|
28
|
+
* ## constructor
|
|
29
|
+
* construct a InlineStyle
|
|
30
|
+
*/
|
|
31
|
+
constructor(styleObject) {
|
|
32
|
+
_InlineStyle_instances.add(this);
|
|
33
|
+
_InlineStyle_cssStyleDec.set(this, void 0);
|
|
34
|
+
_InlineStyle_styleObject.set(this, void 0);
|
|
35
|
+
if (!JSTC.for([styleObject]).check(["object|undefined"])) {
|
|
36
|
+
warner.warn({ message: `[InlineStyle class] @briklab/lib/stylesheet: Invalid first argument!
|
|
37
|
+
Hint: The first argument must be a valid style object or not be given!
|
|
38
|
+
Using {"imeMode":${styleObject}} as fallback` });
|
|
39
|
+
styleObject = { imeMode: `${styleObject}` };
|
|
40
|
+
}
|
|
41
|
+
__classPrivateFieldSet(this, _InlineStyle_styleObject, styleObject, "f");
|
|
42
|
+
__classPrivateFieldSet(this, _InlineStyle_cssStyleDec, new UUIII(), "f");
|
|
43
|
+
}
|
|
44
|
+
generate() {
|
|
45
|
+
let a = __classPrivateFieldGet(this, _InlineStyle_cssStyleDec, "f");
|
|
46
|
+
let b = __classPrivateFieldGet(this, _InlineStyle_styleObject, "f");
|
|
47
|
+
let c = Object.keys(b);
|
|
48
|
+
let d = Object.values(b);
|
|
49
|
+
for (let i = 0; i < c.length; i++) {
|
|
50
|
+
const prop = c[i];
|
|
51
|
+
let val = d[i];
|
|
52
|
+
if (val == null) {
|
|
53
|
+
warner.warn({ message: `[InlineStyle.generate] @briklab/lib/stylesheet: Skipping property "${prop}" with ${String(val)} value. Hint: avoid null/undefined style values.` });
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (typeof val !== "string") {
|
|
57
|
+
warner.warn({ message: `[InlineStyle.generate] @briklab/lib/stylesheet: Non-string style value for "${prop}" (type=${typeof val}). Coercing to string.` });
|
|
58
|
+
val = String(val);
|
|
59
|
+
}
|
|
60
|
+
a.setProperty(prop, val);
|
|
61
|
+
}
|
|
62
|
+
return a.cssText;
|
|
63
|
+
}
|
|
64
|
+
get text() {
|
|
65
|
+
return this.generate();
|
|
66
|
+
}
|
|
67
|
+
get ansi() {
|
|
68
|
+
const s = __classPrivateFieldGet(this, _InlineStyle_styleObject, "f") || {};
|
|
69
|
+
let parts = [];
|
|
70
|
+
if (s["font-weight"] === "bold" || s.fontWeight === "bold")
|
|
71
|
+
parts.push(Color.BOLD);
|
|
72
|
+
if ((s["text-decoration"] || s.textDecoration || "").includes("underline"))
|
|
73
|
+
parts.push(Color.UNDERLINE);
|
|
74
|
+
const colorVal = s.color || s["color"];
|
|
75
|
+
if (colorVal) {
|
|
76
|
+
try {
|
|
77
|
+
const c = new Color(String(colorVal));
|
|
78
|
+
parts.push(c.ansiTruecolor());
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
warner.warn({ message: `[InlineStyle.ansi] @briklab/lib/stylesheet: Invalid color value "${String(colorVal)}" — ignoring. Hint: use a valid hex, rgb(), hsl() or named color.` });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const bgVal = s["background-color"] || s.backgroundColor;
|
|
85
|
+
if (bgVal) {
|
|
86
|
+
try {
|
|
87
|
+
const c = new Color(String(bgVal));
|
|
88
|
+
parts.push(c.ansiTruecolorBg());
|
|
89
|
+
}
|
|
90
|
+
catch (e) {
|
|
91
|
+
warner.warn({ message: `[InlineStyle.ansi] @briklab/lib/stylesheet: Invalid background-color value "${String(bgVal)}" — ignoring. Hint: use a valid hex, rgb(), hsl() or named color.` });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return parts.join("");
|
|
95
|
+
}
|
|
96
|
+
addStyleWithObject(styleObject) {
|
|
97
|
+
if (!JSTC.for([styleObject]).check(["object"])) {
|
|
98
|
+
warner.warn({ message: `[InlineStyle.addStyleWithObject] @briklab/lib/stylesheet: Invalid first argument!\n` +
|
|
99
|
+
`Hint: expected a plain object with CSS properties. Received: ${String(styleObject)}\n` +
|
|
100
|
+
`Returned with no operations.` });
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
__classPrivateFieldSet(this, _InlineStyle_styleObject, { ...__classPrivateFieldGet(this, _InlineStyle_styleObject, "f"), ...styleObject }, "f");
|
|
104
|
+
this.generate();
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
addStyleWithInlineCSS(inlineCSS) {
|
|
108
|
+
if (!JSTC.for([inlineCSS]).check(["string"])) {
|
|
109
|
+
warner.warn({ message: `[InlineStyle.addStyleWithInlineCSS] @briklab/lib/stylesheet: Invalid first argument!
|
|
110
|
+
Hint: The first argument must be a valid inline css string!
|
|
111
|
+
Returned with no operations.` });
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
let s = new UUIII();
|
|
115
|
+
s.cssText = __classPrivateFieldGet(this, _InlineStyle_instances, "m", _InlineStyle_convertKeysToValidCSS).call(this, inlineCSS);
|
|
116
|
+
let o = {};
|
|
117
|
+
for (let i = 0; i < s.length; i++) {
|
|
118
|
+
const a = s[i];
|
|
119
|
+
const v = s.getPropertyValue(a);
|
|
120
|
+
o[a] = v;
|
|
121
|
+
}
|
|
122
|
+
this.addStyleWithObject(o);
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
removeStyle(styles) {
|
|
126
|
+
if (!JSTC.for([styles]).check(["string[]|string"])) {
|
|
127
|
+
warner.warn({ message: `[InlineStyle.removeStyle] @briklab/lib/stylesheet: Invalid first argument!\n` +
|
|
128
|
+
`Hint: expected a string or array of strings. Returned with no operations. Received: ${String(styles)}` });
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
if (typeof styles === "string") {
|
|
132
|
+
styles = [styles];
|
|
133
|
+
}
|
|
134
|
+
for (let i = 0; i < styles.length; i++) {
|
|
135
|
+
const prop = styles[i];
|
|
136
|
+
if (typeof prop !== "string") {
|
|
137
|
+
warner.warn({ message: `[InlineStyle.removeStyle] @briklab/lib/stylesheet: Ignoring non-string style name at index ${i}: ${String(prop)}` });
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
delete __classPrivateFieldGet(this, _InlineStyle_styleObject, "f")[prop];
|
|
141
|
+
}
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
applyTo(element) {
|
|
145
|
+
if (!JSTC.for([element]).check(["object"])) {
|
|
146
|
+
warner.warn({ message: `[InlineStyle.applyTo] @briklab/lib/stylesheet: Invalid first argument!\n` +
|
|
147
|
+
`Hint: expected an HTMLElement. No operation was performed.` });
|
|
148
|
+
return this;
|
|
149
|
+
}
|
|
150
|
+
if (!element || typeof element.style !== "object") {
|
|
151
|
+
warner.warn({ message: `[InlineStyle.applyTo] @briklab/lib/stylesheet: Given object does not look like an HTMLElement (missing .style). No operation was performed.` });
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
element.style.cssText = this.generate();
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
_InlineStyle_cssStyleDec = new WeakMap(), _InlineStyle_styleObject = new WeakMap(), _InlineStyle_instances = new WeakSet(), _InlineStyle_convertFieldToHyphenCase = function _InlineStyle_convertFieldToHyphenCase(string) {
|
|
159
|
+
return string.replace(/([A-Z])/g, (match) => `-${match.toLowerCase()}`);
|
|
160
|
+
}, _InlineStyle_convertKeysToValidCSS = function _InlineStyle_convertKeysToValidCSS(string) {
|
|
161
|
+
const parts = String(string).split(";");
|
|
162
|
+
let out = "";
|
|
163
|
+
for (let i = 0; i < parts.length; i++) {
|
|
164
|
+
const raw = parts[i].trim();
|
|
165
|
+
if (!raw)
|
|
166
|
+
continue;
|
|
167
|
+
const kv = raw.split(":");
|
|
168
|
+
if (kv.length < 2) {
|
|
169
|
+
warner.warn({ message: `[InlineStyle.#convertKeysToValidCSS] @briklab/lib/stylesheet: Skipping malformed rule: "${raw}". ` +
|
|
170
|
+
`Hint: expected "property: value" pairs separated by ";"` });
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const k = kv[0].trim();
|
|
174
|
+
const v = kv.slice(1).join(":").trim();
|
|
175
|
+
if (!k || !v) {
|
|
176
|
+
warner.warn({ message: `[InlineStyle.#convertKeysToValidCSS] @briklab/lib/stylesheet: Skipping empty property or value in rule: "${raw}".` });
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
out += `${__classPrivateFieldGet(this, _InlineStyle_instances, "m", _InlineStyle_convertFieldToHyphenCase).call(this, k)}:${v};`;
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
};
|
|
183
|
+
export default InlineStyle;
|
|
184
|
+
export class StyleSheet {
|
|
185
|
+
constructor() {
|
|
186
|
+
_StyleSheet_styles.set(this, void 0);
|
|
187
|
+
__classPrivateFieldSet(this, _StyleSheet_styles, {}, "f");
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Add or update a rule in the stylesheet.
|
|
191
|
+
* @param name The rule name or selector (string).
|
|
192
|
+
* @param style An InlineStyle instance.
|
|
193
|
+
*/
|
|
194
|
+
set(name, style) {
|
|
195
|
+
if (!JSTC.for([name, style]).check(["string", "object"])) {
|
|
196
|
+
warner.warn({ message: `[StyleSheet.set] @briklab/lib/stylesheet: Invalid arguments!\n` +
|
|
197
|
+
`Hint: call .set("ruleName", new InlineStyle({...})). Received name=${String(name)}, style=${String(style)}. Returned with no operations.` });
|
|
198
|
+
return this;
|
|
199
|
+
}
|
|
200
|
+
if (!(style instanceof InlineStyle)) {
|
|
201
|
+
warner.warn({ message: `[StyleSheet.set] @briklab/lib/stylesheet: Provided style is not an InlineStyle instance!\n` +
|
|
202
|
+
`Hint: create the style with new InlineStyle({...}). Received: ${String(style)}. Returned with no operations.` });
|
|
203
|
+
return this;
|
|
204
|
+
}
|
|
205
|
+
__classPrivateFieldGet(this, _StyleSheet_styles, "f")[name] = style;
|
|
206
|
+
return this;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Get a rule by name.
|
|
210
|
+
*/
|
|
211
|
+
get(name) {
|
|
212
|
+
if (!JSTC.for([name]).check(["string"])) {
|
|
213
|
+
warner.warn({ message: `[StyleSheet.get] @briklab/lib/stylesheet: Invalid argument!\n` +
|
|
214
|
+
`Hint: name must be a string. Received: ${String(name)}. Returned undefined.` });
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
return __classPrivateFieldGet(this, _StyleSheet_styles, "f")[name];
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Remove a rule by name.
|
|
221
|
+
*/
|
|
222
|
+
remove(name) {
|
|
223
|
+
if (!JSTC.for([name]).check(["string"])) {
|
|
224
|
+
warner.warn({ message: `[StyleSheet.remove] @briklab/lib/stylesheet: Invalid argument!\n` +
|
|
225
|
+
`Hint: name must be a string. Received: ${String(name)}. No-op.` });
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
delete __classPrivateFieldGet(this, _StyleSheet_styles, "f")[name];
|
|
229
|
+
return this;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Generate CSS text for the whole stylesheet.
|
|
233
|
+
*/
|
|
234
|
+
generate() {
|
|
235
|
+
let css = "";
|
|
236
|
+
for (const key in __classPrivateFieldGet(this, _StyleSheet_styles, "f")) {
|
|
237
|
+
const style = __classPrivateFieldGet(this, _StyleSheet_styles, "f")[key];
|
|
238
|
+
if (style) {
|
|
239
|
+
css += `${key} { ${style.text} }\n`;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return css.trim();
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Export as a string for inline style usage or injection.
|
|
246
|
+
*/
|
|
247
|
+
toString() {
|
|
248
|
+
return this.generate();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
_StyleSheet_styles = new WeakMap();
|
package/dist/warner/index.d.ts
CHANGED