@webstudio-is/css-engine 0.1.0 → 0.2.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/lib/core/create-css-engine.js +3 -7
- package/lib/core/css-engine.d.ts +1 -0
- package/lib/core/css-engine.d.ts.map +1 -1
- package/lib/core/css-engine.js +13 -14
- package/lib/core/css-engine.stories.js +7 -11
- package/lib/core/css-engine.test.js +2 -4
- package/lib/core/index.js +3 -21
- package/lib/core/rules.js +7 -17
- package/lib/core/style-element.js +1 -5
- package/lib/core/style-sheet.js +1 -5
- package/lib/core/to-value.js +5 -9
- package/lib/core/to-value.test.js +9 -11
- package/lib/index.js +1 -17
- package/package.json +7 -5
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const css_engine_1 = require("./css-engine");
|
|
5
|
-
const createCssEngine = () => {
|
|
6
|
-
return new css_engine_1.CssEngine();
|
|
1
|
+
import { CssEngine } from "./css-engine";
|
|
2
|
+
export const createCssEngine = () => {
|
|
3
|
+
return new CssEngine();
|
|
7
4
|
};
|
|
8
|
-
exports.createCssEngine = createCssEngine;
|
package/lib/core/css-engine.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"css-engine.d.ts","sourceRoot":"","sources":["../../src/core/css-engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAEL,SAAS,EACT,aAAa,EACb,SAAS,EACT,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACtB,MAAM,SAAS,CAAC;AAMjB,qBAAa,SAAS;;;IAapB,YAAY,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB;IASnD,YAAY,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO;IAahD,gBAAgB,CAAC,OAAO,EAAE,MAAM;IAKhC,eAAe,CAAC,OAAO,EAAE,eAAe;IAGxC,KAAK;IAML,MAAM;IAKN,IAAI,OAAO,WAiBV;CAKF"}
|
|
1
|
+
{"version":3,"file":"css-engine.d.ts","sourceRoot":"","sources":["../../src/core/css-engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAEL,SAAS,EACT,aAAa,EACb,SAAS,EACT,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACtB,MAAM,SAAS,CAAC;AAMjB,qBAAa,SAAS;;;IAapB,YAAY,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB;IASnD,YAAY,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO;IAahD,gBAAgB,CAAC,OAAO,EAAE,MAAM;IAKhC,eAAe,CAAC,OAAO,EAAE,eAAe;IAGxC,KAAK;IAML,MAAM;IAKN,OAAO;IAGP,IAAI,OAAO,WAiBV;CAKF"}
|
package/lib/core/css-engine.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
2
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
3
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
@@ -11,13 +10,11 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
11
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
11
|
};
|
|
13
12
|
var _CssEngine_element, _CssEngine_mediaRules, _CssEngine_plainRules, _CssEngine_fontFaceRules, _CssEngine_sheet, _CssEngine_isDirty, _CssEngine_cssText, _CssEngine_onChangeRule;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const style_element_1 = require("./style-element");
|
|
18
|
-
const style_sheet_1 = require("./style-sheet");
|
|
13
|
+
import { FontFaceRule, MediaRule, PlaintextRule, StyleRule, } from "./rules";
|
|
14
|
+
import { StyleElement } from "./style-element";
|
|
15
|
+
import { StyleSheet } from "./style-sheet";
|
|
19
16
|
const defaultMediaRuleId = "__default-media-rule__";
|
|
20
|
-
class CssEngine {
|
|
17
|
+
export class CssEngine {
|
|
21
18
|
constructor() {
|
|
22
19
|
_CssEngine_element.set(this, void 0);
|
|
23
20
|
_CssEngine_mediaRules.set(this, new Map());
|
|
@@ -29,14 +26,14 @@ class CssEngine {
|
|
|
29
26
|
_CssEngine_onChangeRule.set(this, () => {
|
|
30
27
|
__classPrivateFieldSet(this, _CssEngine_isDirty, true, "f");
|
|
31
28
|
});
|
|
32
|
-
__classPrivateFieldSet(this, _CssEngine_element, new
|
|
33
|
-
__classPrivateFieldSet(this, _CssEngine_sheet, new
|
|
29
|
+
__classPrivateFieldSet(this, _CssEngine_element, new StyleElement(), "f");
|
|
30
|
+
__classPrivateFieldSet(this, _CssEngine_sheet, new StyleSheet(__classPrivateFieldGet(this, _CssEngine_element, "f")), "f");
|
|
34
31
|
this.addMediaRule(defaultMediaRuleId);
|
|
35
32
|
}
|
|
36
33
|
addMediaRule(id, options) {
|
|
37
34
|
let mediaRule = __classPrivateFieldGet(this, _CssEngine_mediaRules, "f").get(id);
|
|
38
35
|
if (mediaRule === undefined) {
|
|
39
|
-
mediaRule = new
|
|
36
|
+
mediaRule = new MediaRule(options);
|
|
40
37
|
__classPrivateFieldGet(this, _CssEngine_mediaRules, "f").set(id, mediaRule);
|
|
41
38
|
__classPrivateFieldSet(this, _CssEngine_isDirty, true, "f");
|
|
42
39
|
}
|
|
@@ -46,7 +43,7 @@ class CssEngine {
|
|
|
46
43
|
const mediaRule = __classPrivateFieldGet(this, _CssEngine_mediaRules, "f").get(rule.breakpoint) ??
|
|
47
44
|
__classPrivateFieldGet(this, _CssEngine_mediaRules, "f").get(defaultMediaRuleId);
|
|
48
45
|
__classPrivateFieldSet(this, _CssEngine_isDirty, true, "f");
|
|
49
|
-
const styleRule = new
|
|
46
|
+
const styleRule = new StyleRule(selectorText, rule.style);
|
|
50
47
|
styleRule.onChange = __classPrivateFieldGet(this, _CssEngine_onChangeRule, "f");
|
|
51
48
|
if (mediaRule === undefined) {
|
|
52
49
|
// Should be impossible to reach.
|
|
@@ -58,10 +55,10 @@ class CssEngine {
|
|
|
58
55
|
const rule = __classPrivateFieldGet(this, _CssEngine_plainRules, "f").get(cssText);
|
|
59
56
|
if (rule !== undefined)
|
|
60
57
|
return rule;
|
|
61
|
-
return __classPrivateFieldGet(this, _CssEngine_plainRules, "f").set(cssText, new
|
|
58
|
+
return __classPrivateFieldGet(this, _CssEngine_plainRules, "f").set(cssText, new PlaintextRule(cssText));
|
|
62
59
|
}
|
|
63
60
|
addFontFaceRule(options) {
|
|
64
|
-
return __classPrivateFieldGet(this, _CssEngine_fontFaceRules, "f").push(new
|
|
61
|
+
return __classPrivateFieldGet(this, _CssEngine_fontFaceRules, "f").push(new FontFaceRule(options));
|
|
65
62
|
}
|
|
66
63
|
clear() {
|
|
67
64
|
__classPrivateFieldGet(this, _CssEngine_mediaRules, "f").clear();
|
|
@@ -74,6 +71,9 @@ class CssEngine {
|
|
|
74
71
|
// This isn't going to do anything if the `cssText` hasn't changed.
|
|
75
72
|
__classPrivateFieldGet(this, _CssEngine_sheet, "f").replaceSync(this.cssText);
|
|
76
73
|
}
|
|
74
|
+
unmount() {
|
|
75
|
+
__classPrivateFieldGet(this, _CssEngine_element, "f").unmount();
|
|
76
|
+
}
|
|
77
77
|
get cssText() {
|
|
78
78
|
if (__classPrivateFieldGet(this, _CssEngine_isDirty, "f") === false) {
|
|
79
79
|
return __classPrivateFieldGet(this, _CssEngine_cssText, "f");
|
|
@@ -93,5 +93,4 @@ class CssEngine {
|
|
|
93
93
|
return __classPrivateFieldGet(this, _CssEngine_cssText, "f");
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
|
-
exports.CssEngine = CssEngine;
|
|
97
96
|
_CssEngine_element = new WeakMap(), _CssEngine_mediaRules = new WeakMap(), _CssEngine_plainRules = new WeakMap(), _CssEngine_fontFaceRules = new WeakMap(), _CssEngine_sheet = new WeakMap(), _CssEngine_isDirty = new WeakMap(), _CssEngine_cssText = new WeakMap(), _CssEngine_onChangeRule = new WeakMap();
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
-
const css_engine_1 = require("./css-engine");
|
|
6
|
-
exports.default = {
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { CssEngine } from "./css-engine";
|
|
3
|
+
export default {
|
|
7
4
|
component: "CssEngine",
|
|
8
5
|
};
|
|
9
6
|
const style0 = {
|
|
@@ -11,18 +8,18 @@ const style0 = {
|
|
|
11
8
|
};
|
|
12
9
|
const mediaRuleOptions0 = { minWidth: 0 };
|
|
13
10
|
const mediaId = "0";
|
|
14
|
-
const Basic = () => {
|
|
15
|
-
const engine = new
|
|
11
|
+
export const Basic = () => {
|
|
12
|
+
const engine = new CssEngine();
|
|
16
13
|
engine.addMediaRule(mediaId, mediaRuleOptions0);
|
|
17
14
|
const rule = engine.addStyleRule(".test", {
|
|
18
15
|
style: style0,
|
|
19
16
|
breakpoint: "0",
|
|
20
17
|
});
|
|
21
18
|
engine.render();
|
|
22
|
-
return ((
|
|
19
|
+
return (_jsxs(_Fragment, { children: [_jsx("div", { className: "test", children: "Should be red" }), _jsx("button", { onClick: () => {
|
|
23
20
|
rule.styleMap.set("color", { type: "keyword", value: "green" });
|
|
24
21
|
engine.render();
|
|
25
|
-
}, children: "Make it green" }), (
|
|
22
|
+
}, children: "Make it green" }), _jsx("button", { onClick: () => {
|
|
26
23
|
engine.addStyleRule(".test", {
|
|
27
24
|
style: {
|
|
28
25
|
background: { type: "keyword", value: "yellow" },
|
|
@@ -32,4 +29,3 @@ const Basic = () => {
|
|
|
32
29
|
engine.render();
|
|
33
30
|
}, children: "Add rule with yellow background" })] }));
|
|
34
31
|
};
|
|
35
|
-
exports.Basic = Basic;
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const css_engine_1 = require("./css-engine");
|
|
1
|
+
import { CssEngine } from "./css-engine";
|
|
4
2
|
const style0 = {
|
|
5
3
|
display: { type: "keyword", value: "block" },
|
|
6
4
|
};
|
|
@@ -9,7 +7,7 @@ const mediaId = "0";
|
|
|
9
7
|
describe("CssEngine", () => {
|
|
10
8
|
let engine;
|
|
11
9
|
beforeEach(() => {
|
|
12
|
-
engine = new
|
|
10
|
+
engine = new CssEngine();
|
|
13
11
|
});
|
|
14
12
|
test("use default media rule when there is no matching one registrered", () => {
|
|
15
13
|
engine.addStyleRule(".c", {
|
package/lib/core/index.js
CHANGED
|
@@ -1,21 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.CssEngine = void 0;
|
|
18
|
-
var css_engine_1 = require("./css-engine");
|
|
19
|
-
Object.defineProperty(exports, "CssEngine", { enumerable: true, get: function () { return css_engine_1.CssEngine; } });
|
|
20
|
-
__exportStar(require("./create-css-engine"), exports);
|
|
21
|
-
__exportStar(require("./to-value"), exports);
|
|
1
|
+
export { CssEngine } from "./css-engine";
|
|
2
|
+
export * from "./create-css-engine";
|
|
3
|
+
export * from "./to-value";
|
package/lib/core/rules.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
2
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
3
|
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,14 +9,9 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
10
9
|
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");
|
|
11
10
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
11
|
};
|
|
13
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
-
};
|
|
16
12
|
var _StylePropertyMap_styleMap, _StylePropertyMap_isDirty, _StylePropertyMap_string, _StyleRule_onChange, _MediaRule_options, _MediaRule_mediaType, _FontFaceRule_options;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const hyphenate_style_name_1 = __importDefault(require("hyphenate-style-name"));
|
|
20
|
-
const to_value_1 = require("./to-value");
|
|
13
|
+
import hyphenate from "hyphenate-style-name";
|
|
14
|
+
import { toValue } from "./to-value";
|
|
21
15
|
class StylePropertyMap {
|
|
22
16
|
constructor() {
|
|
23
17
|
_StylePropertyMap_styleMap.set(this, new Map());
|
|
@@ -37,7 +31,7 @@ class StylePropertyMap {
|
|
|
37
31
|
for (const [property, value] of __classPrivateFieldGet(this, _StylePropertyMap_styleMap, "f")) {
|
|
38
32
|
if (value === undefined)
|
|
39
33
|
continue;
|
|
40
|
-
block.push(`${(
|
|
34
|
+
block.push(`${hyphenate(property)}: ${toValue(value)}`);
|
|
41
35
|
}
|
|
42
36
|
__classPrivateFieldSet(this, _StylePropertyMap_string, block.join("; "), "f");
|
|
43
37
|
__classPrivateFieldSet(this, _StylePropertyMap_isDirty, false, "f");
|
|
@@ -45,7 +39,7 @@ class StylePropertyMap {
|
|
|
45
39
|
}
|
|
46
40
|
}
|
|
47
41
|
_StylePropertyMap_styleMap = new WeakMap(), _StylePropertyMap_isDirty = new WeakMap(), _StylePropertyMap_string = new WeakMap();
|
|
48
|
-
class StyleRule {
|
|
42
|
+
export class StyleRule {
|
|
49
43
|
constructor(selectorText, style) {
|
|
50
44
|
_StyleRule_onChange.set(this, () => {
|
|
51
45
|
this.onChange?.();
|
|
@@ -62,9 +56,8 @@ class StyleRule {
|
|
|
62
56
|
return `${this.selectorText} { ${this.styleMap} }`;
|
|
63
57
|
}
|
|
64
58
|
}
|
|
65
|
-
exports.StyleRule = StyleRule;
|
|
66
59
|
_StyleRule_onChange = new WeakMap();
|
|
67
|
-
class MediaRule {
|
|
60
|
+
export class MediaRule {
|
|
68
61
|
constructor(options = {}) {
|
|
69
62
|
_MediaRule_options.set(this, void 0);
|
|
70
63
|
this.rules = [];
|
|
@@ -94,16 +87,14 @@ class MediaRule {
|
|
|
94
87
|
return `@media ${__classPrivateFieldGet(this, _MediaRule_mediaType, "f")} ${conditionText}{\n${rules.join("\n")}\n}`;
|
|
95
88
|
}
|
|
96
89
|
}
|
|
97
|
-
exports.MediaRule = MediaRule;
|
|
98
90
|
_MediaRule_options = new WeakMap(), _MediaRule_mediaType = new WeakMap();
|
|
99
|
-
class PlaintextRule {
|
|
91
|
+
export class PlaintextRule {
|
|
100
92
|
constructor(cssText) {
|
|
101
93
|
this.styleMap = new Map();
|
|
102
94
|
this.cssText = cssText;
|
|
103
95
|
}
|
|
104
96
|
}
|
|
105
|
-
|
|
106
|
-
class FontFaceRule {
|
|
97
|
+
export class FontFaceRule {
|
|
107
98
|
constructor(options) {
|
|
108
99
|
_FontFaceRule_options.set(this, void 0);
|
|
109
100
|
__classPrivateFieldSet(this, _FontFaceRule_options, options, "f");
|
|
@@ -113,5 +104,4 @@ class FontFaceRule {
|
|
|
113
104
|
return `@font-face {\n font-family: ${fontFamily}; font-style: ${fontStyle}; font-weight: ${fontWeight}; font-display: ${fontDisplay}; src: ${src};\n}`;
|
|
114
105
|
}
|
|
115
106
|
}
|
|
116
|
-
exports.FontFaceRule = FontFaceRule;
|
|
117
107
|
_FontFaceRule_options = new WeakMap();
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
2
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
3
|
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");
|
|
@@ -11,9 +10,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
11
10
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
11
|
};
|
|
13
12
|
var _StyleElement_element;
|
|
14
|
-
|
|
15
|
-
exports.StyleElement = void 0;
|
|
16
|
-
class StyleElement {
|
|
13
|
+
export class StyleElement {
|
|
17
14
|
constructor() {
|
|
18
15
|
_StyleElement_element.set(this, void 0);
|
|
19
16
|
}
|
|
@@ -39,5 +36,4 @@ class StyleElement {
|
|
|
39
36
|
}
|
|
40
37
|
}
|
|
41
38
|
}
|
|
42
|
-
exports.StyleElement = StyleElement;
|
|
43
39
|
_StyleElement_element = new WeakMap();
|
package/lib/core/style-sheet.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
2
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
3
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
@@ -11,9 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
11
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
11
|
};
|
|
13
12
|
var _StyleSheet_cssText, _StyleSheet_element;
|
|
14
|
-
|
|
15
|
-
exports.StyleSheet = void 0;
|
|
16
|
-
class StyleSheet {
|
|
13
|
+
export class StyleSheet {
|
|
17
14
|
constructor(element) {
|
|
18
15
|
_StyleSheet_cssText.set(this, "");
|
|
19
16
|
_StyleSheet_element.set(this, void 0);
|
|
@@ -26,5 +23,4 @@ class StyleSheet {
|
|
|
26
23
|
}
|
|
27
24
|
}
|
|
28
25
|
}
|
|
29
|
-
exports.StyleSheet = StyleSheet;
|
|
30
26
|
_StyleSheet_cssText = new WeakMap(), _StyleSheet_element = new WeakMap();
|
package/lib/core/to-value.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toValue = void 0;
|
|
4
|
-
const fonts_1 = require("@webstudio-is/fonts");
|
|
1
|
+
import { DEFAULT_FONT_FALLBACK, SYSTEM_FONTS } from "@webstudio-is/fonts";
|
|
5
2
|
const defaultOptions = {
|
|
6
3
|
withFallback: true,
|
|
7
4
|
};
|
|
8
|
-
const toValue = (value, options = defaultOptions) => {
|
|
5
|
+
export const toValue = (value, options = defaultOptions) => {
|
|
9
6
|
if (value === undefined)
|
|
10
7
|
return "";
|
|
11
8
|
if (value.type === "unit") {
|
|
@@ -16,20 +13,19 @@ const toValue = (value, options = defaultOptions) => {
|
|
|
16
13
|
return value.value[0];
|
|
17
14
|
}
|
|
18
15
|
const family = value.value[0];
|
|
19
|
-
const fallbacks =
|
|
16
|
+
const fallbacks = SYSTEM_FONTS.get(family);
|
|
20
17
|
if (Array.isArray(fallbacks)) {
|
|
21
18
|
return [...value.value, ...fallbacks].join(", ");
|
|
22
19
|
}
|
|
23
|
-
return [...value.value,
|
|
20
|
+
return [...value.value, DEFAULT_FONT_FALLBACK].join(", ");
|
|
24
21
|
}
|
|
25
22
|
if (value.type === "var") {
|
|
26
23
|
const fallbacks = [];
|
|
27
24
|
for (const fallback of value.fallbacks) {
|
|
28
|
-
fallbacks.push(
|
|
25
|
+
fallbacks.push(toValue(fallback, options));
|
|
29
26
|
}
|
|
30
27
|
const fallbacksString = fallbacks.length > 0 ? `, ${fallbacks.join(", ")}` : "";
|
|
31
28
|
return `var(--${value.value}${fallbacksString})`;
|
|
32
29
|
}
|
|
33
30
|
return value.value;
|
|
34
31
|
};
|
|
35
|
-
exports.toValue = toValue;
|
|
@@ -1,29 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const to_value_1 = require("./to-value");
|
|
1
|
+
import { toValue } from "./to-value";
|
|
4
2
|
describe("Convert WS CSS Values to native CSS strings", () => {
|
|
5
3
|
test("keyword", () => {
|
|
6
|
-
const value =
|
|
4
|
+
const value = toValue({ type: "keyword", value: "red" });
|
|
7
5
|
expect(value).toBe("red");
|
|
8
6
|
});
|
|
9
7
|
test("unit", () => {
|
|
10
|
-
const value =
|
|
8
|
+
const value = toValue({ type: "unit", value: 10, unit: "px" });
|
|
11
9
|
expect(value).toBe("10px");
|
|
12
10
|
});
|
|
13
11
|
test("invalid", () => {
|
|
14
|
-
const value =
|
|
12
|
+
const value = toValue({ type: "invalid", value: "bad" });
|
|
15
13
|
expect(value).toBe("bad");
|
|
16
14
|
});
|
|
17
15
|
test("unset", () => {
|
|
18
|
-
const value =
|
|
16
|
+
const value = toValue({ type: "unset", value: "" });
|
|
19
17
|
expect(value).toBe("");
|
|
20
18
|
});
|
|
21
19
|
test("var", () => {
|
|
22
|
-
const value =
|
|
20
|
+
const value = toValue({ type: "var", value: "namespace", fallbacks: [] });
|
|
23
21
|
expect(value).toBe("var(--namespace)");
|
|
24
22
|
});
|
|
25
23
|
test("var with fallbacks", () => {
|
|
26
|
-
const value =
|
|
24
|
+
const value = toValue({
|
|
27
25
|
type: "var",
|
|
28
26
|
value: "namespace",
|
|
29
27
|
fallbacks: [
|
|
@@ -41,14 +39,14 @@ describe("Convert WS CSS Values to native CSS strings", () => {
|
|
|
41
39
|
expect(value).toBe("var(--namespace, normal, 10px)");
|
|
42
40
|
});
|
|
43
41
|
test("fontFamily", () => {
|
|
44
|
-
const value =
|
|
42
|
+
const value = toValue({
|
|
45
43
|
type: "fontFamily",
|
|
46
44
|
value: ["Courier New"],
|
|
47
45
|
});
|
|
48
46
|
expect(value).toBe("Courier New, monospace");
|
|
49
47
|
});
|
|
50
48
|
test("withFallback=false", () => {
|
|
51
|
-
const value =
|
|
49
|
+
const value = toValue({
|
|
52
50
|
type: "fontFamily",
|
|
53
51
|
value: ["Courier New"],
|
|
54
52
|
}, { withFallback: false });
|
package/lib/index.js
CHANGED
|
@@ -1,17 +1 @@
|
|
|
1
|
-
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./core"), exports);
|
|
1
|
+
export * from "./core";
|
package/package.json
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webstudio-is/css-engine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "CSS Renderer for Webstudio",
|
|
5
5
|
"author": "Webstudio <github@webstudio.is>",
|
|
6
6
|
"homepage": "https://webstudio.is",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"scripts": {
|
|
8
9
|
"typecheck": "tsc --noEmit",
|
|
9
10
|
"checks": "yarn typecheck && yarn lint && yarn test",
|
|
10
11
|
"dev": "tsup --watch",
|
|
11
12
|
"build": "rm -fr lib tsconfig.tsbuildinfo && tsc",
|
|
12
|
-
"test": "jest",
|
|
13
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
|
|
13
14
|
"lint": "eslint ./src --ext .ts,.tsx --max-warnings 0",
|
|
14
15
|
"storybook:run": "start-storybook -p 6006",
|
|
15
|
-
"storybook:build": "build-storybook"
|
|
16
|
+
"storybook:build": "build-storybook",
|
|
17
|
+
"publish-to-npm": "bash ../../bin/publish-to-npm.sh"
|
|
16
18
|
},
|
|
17
19
|
"dependencies": {
|
|
18
20
|
"hyphenate-style-name": "^1.0.4",
|
|
@@ -37,7 +39,7 @@
|
|
|
37
39
|
"@types/react": "^17.0.24",
|
|
38
40
|
"@types/react-dom": "^17.0.9"
|
|
39
41
|
},
|
|
40
|
-
"
|
|
42
|
+
"exports": "./lib/index.js",
|
|
41
43
|
"types": "lib/index.d.ts",
|
|
42
44
|
"files": [
|
|
43
45
|
"lib/*",
|
|
@@ -51,7 +53,7 @@
|
|
|
51
53
|
"entry": [
|
|
52
54
|
"src/index.ts"
|
|
53
55
|
],
|
|
54
|
-
"format": "
|
|
56
|
+
"format": "esm",
|
|
55
57
|
"outDir": "lib"
|
|
56
58
|
}
|
|
57
59
|
}
|