@webstudio-is/css-engine 0.81.0 → 0.83.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 +661 -21
- package/lib/cjs/core/css-engine.js +40 -67
- package/lib/cjs/core/rules.js +42 -58
- package/lib/cjs/core/style-element.js +15 -36
- package/lib/cjs/core/style-sheet.js +6 -27
- package/lib/cjs/core/to-value.js +2 -5
- package/lib/core/css-engine.js +40 -67
- package/lib/core/rules.js +42 -58
- package/lib/core/style-element.js +15 -36
- package/lib/core/style-sheet.js +6 -27
- package/lib/core/to-value.js +2 -5
- package/lib/types/core/css-engine.stories.d.ts +1 -2
- package/lib/types/core/rules.d.ts +2 -1
- package/package.json +11 -10
- package/src/core/rules.ts +3 -0
- package/src/core/to-value.ts +2 -10
|
@@ -16,24 +16,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var __accessCheck = (obj, member, msg) => {
|
|
20
|
-
if (!member.has(obj))
|
|
21
|
-
throw TypeError("Cannot " + msg);
|
|
22
|
-
};
|
|
23
|
-
var __privateGet = (obj, member, getter) => {
|
|
24
|
-
__accessCheck(obj, member, "read from private field");
|
|
25
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
26
|
-
};
|
|
27
|
-
var __privateAdd = (obj, member, value) => {
|
|
28
|
-
if (member.has(obj))
|
|
29
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
30
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
31
|
-
};
|
|
32
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
33
|
-
__accessCheck(obj, member, "write to private field");
|
|
34
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
35
|
-
return value;
|
|
36
|
-
};
|
|
37
19
|
var css_engine_exports = {};
|
|
38
20
|
__export(css_engine_exports, {
|
|
39
21
|
CssEngine: () => CssEngine
|
|
@@ -43,42 +25,38 @@ var import_rules = require("./rules");
|
|
|
43
25
|
var import_compare_media = require("./compare-media");
|
|
44
26
|
var import_style_element = require("./style-element");
|
|
45
27
|
var import_style_sheet = require("./style-sheet");
|
|
46
|
-
var _element, _mediaRules, _plainRules, _fontFaceRules, _sheet, _isDirty, _cssText, _onChangeRule;
|
|
47
28
|
const defaultMediaRuleId = "__default-media-rule__";
|
|
48
29
|
class CssEngine {
|
|
30
|
+
#element;
|
|
31
|
+
#mediaRules = /* @__PURE__ */ new Map();
|
|
32
|
+
#plainRules = /* @__PURE__ */ new Map();
|
|
33
|
+
#fontFaceRules = [];
|
|
34
|
+
#sheet;
|
|
35
|
+
#isDirty = false;
|
|
36
|
+
#cssText = "";
|
|
49
37
|
constructor({ name }) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
__privateAdd(this, _plainRules, /* @__PURE__ */ new Map());
|
|
53
|
-
__privateAdd(this, _fontFaceRules, []);
|
|
54
|
-
__privateAdd(this, _sheet, void 0);
|
|
55
|
-
__privateAdd(this, _isDirty, false);
|
|
56
|
-
__privateAdd(this, _cssText, "");
|
|
57
|
-
__privateAdd(this, _onChangeRule, () => {
|
|
58
|
-
__privateSet(this, _isDirty, true);
|
|
59
|
-
});
|
|
60
|
-
__privateSet(this, _element, new import_style_element.StyleElement(name));
|
|
61
|
-
__privateSet(this, _sheet, new import_style_sheet.StyleSheet(__privateGet(this, _element)));
|
|
38
|
+
this.#element = new import_style_element.StyleElement(name);
|
|
39
|
+
this.#sheet = new import_style_sheet.StyleSheet(this.#element);
|
|
62
40
|
}
|
|
63
41
|
addMediaRule(id, options) {
|
|
64
|
-
let mediaRule =
|
|
42
|
+
let mediaRule = this.#mediaRules.get(id);
|
|
65
43
|
if (mediaRule === void 0) {
|
|
66
44
|
mediaRule = new import_rules.MediaRule(options);
|
|
67
|
-
|
|
68
|
-
|
|
45
|
+
this.#mediaRules.set(id, mediaRule);
|
|
46
|
+
this.#isDirty = true;
|
|
69
47
|
return mediaRule;
|
|
70
48
|
}
|
|
71
49
|
if (options) {
|
|
72
50
|
mediaRule.options = options;
|
|
73
|
-
|
|
51
|
+
this.#isDirty = true;
|
|
74
52
|
}
|
|
75
53
|
return mediaRule;
|
|
76
54
|
}
|
|
77
55
|
addStyleRule(selectorText, rule, transformValue) {
|
|
78
56
|
const mediaRule = this.addMediaRule(rule.breakpoint || defaultMediaRuleId);
|
|
79
|
-
|
|
57
|
+
this.#isDirty = true;
|
|
80
58
|
const styleRule = new import_rules.StyleRule(selectorText, rule.style, transformValue);
|
|
81
|
-
styleRule.onChange =
|
|
59
|
+
styleRule.onChange = this.#onChangeRule;
|
|
82
60
|
if (mediaRule === void 0) {
|
|
83
61
|
throw new Error("No media rule found");
|
|
84
62
|
}
|
|
@@ -86,47 +64,47 @@ class CssEngine {
|
|
|
86
64
|
return styleRule;
|
|
87
65
|
}
|
|
88
66
|
addPlaintextRule(cssText) {
|
|
89
|
-
const rule =
|
|
67
|
+
const rule = this.#plainRules.get(cssText);
|
|
90
68
|
if (rule !== void 0) {
|
|
91
69
|
return rule;
|
|
92
70
|
}
|
|
93
|
-
|
|
94
|
-
return
|
|
71
|
+
this.#isDirty = true;
|
|
72
|
+
return this.#plainRules.set(cssText, new import_rules.PlaintextRule(cssText));
|
|
95
73
|
}
|
|
96
74
|
addFontFaceRule(options) {
|
|
97
|
-
|
|
98
|
-
return
|
|
75
|
+
this.#isDirty = true;
|
|
76
|
+
return this.#fontFaceRules.push(new import_rules.FontFaceRule(options));
|
|
99
77
|
}
|
|
100
78
|
clear() {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
79
|
+
this.#mediaRules.clear();
|
|
80
|
+
this.#plainRules.clear();
|
|
81
|
+
this.#fontFaceRules = [];
|
|
82
|
+
this.#isDirty = true;
|
|
105
83
|
}
|
|
106
84
|
render() {
|
|
107
|
-
|
|
108
|
-
|
|
85
|
+
this.#element.mount();
|
|
86
|
+
this.#sheet.replaceSync(this.cssText);
|
|
109
87
|
}
|
|
110
88
|
unmount() {
|
|
111
|
-
|
|
89
|
+
this.#element.unmount();
|
|
112
90
|
}
|
|
113
91
|
setAttribute(name, value) {
|
|
114
|
-
|
|
92
|
+
this.#element.setAttribute(name, value);
|
|
115
93
|
}
|
|
116
94
|
getAttribute(name) {
|
|
117
|
-
return
|
|
95
|
+
return this.#element.getAttribute(name);
|
|
118
96
|
}
|
|
119
97
|
get cssText() {
|
|
120
|
-
if (
|
|
121
|
-
return
|
|
98
|
+
if (this.#isDirty === false) {
|
|
99
|
+
return this.#cssText;
|
|
122
100
|
}
|
|
123
|
-
|
|
101
|
+
this.#isDirty = false;
|
|
124
102
|
const css = [];
|
|
125
|
-
css.push(...
|
|
126
|
-
for (const plaintextRule of
|
|
103
|
+
css.push(...this.#fontFaceRules.map((rule) => rule.cssText));
|
|
104
|
+
for (const plaintextRule of this.#plainRules.values()) {
|
|
127
105
|
css.push(plaintextRule.cssText);
|
|
128
106
|
}
|
|
129
|
-
const sortedMediaRules = Array.from(
|
|
107
|
+
const sortedMediaRules = Array.from(this.#mediaRules.values()).sort(
|
|
130
108
|
(ruleA, ruleB) => (0, import_compare_media.compareMedia)(ruleA.options, ruleB.options)
|
|
131
109
|
);
|
|
132
110
|
for (const mediaRule of sortedMediaRules) {
|
|
@@ -135,15 +113,10 @@ class CssEngine {
|
|
|
135
113
|
css.push(cssText);
|
|
136
114
|
}
|
|
137
115
|
}
|
|
138
|
-
|
|
139
|
-
return
|
|
116
|
+
this.#cssText = css.join("\n");
|
|
117
|
+
return this.#cssText;
|
|
140
118
|
}
|
|
119
|
+
#onChangeRule = () => {
|
|
120
|
+
this.#isDirty = true;
|
|
121
|
+
};
|
|
141
122
|
}
|
|
142
|
-
_element = new WeakMap();
|
|
143
|
-
_mediaRules = new WeakMap();
|
|
144
|
-
_plainRules = new WeakMap();
|
|
145
|
-
_fontFaceRules = new WeakMap();
|
|
146
|
-
_sheet = new WeakMap();
|
|
147
|
-
_isDirty = new WeakMap();
|
|
148
|
-
_cssText = new WeakMap();
|
|
149
|
-
_onChangeRule = new WeakMap();
|
package/lib/cjs/core/rules.js
CHANGED
|
@@ -16,24 +16,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var __accessCheck = (obj, member, msg) => {
|
|
20
|
-
if (!member.has(obj))
|
|
21
|
-
throw TypeError("Cannot " + msg);
|
|
22
|
-
};
|
|
23
|
-
var __privateGet = (obj, member, getter) => {
|
|
24
|
-
__accessCheck(obj, member, "read from private field");
|
|
25
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
26
|
-
};
|
|
27
|
-
var __privateAdd = (obj, member, value) => {
|
|
28
|
-
if (member.has(obj))
|
|
29
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
30
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
31
|
-
};
|
|
32
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
33
|
-
__accessCheck(obj, member, "write to private field");
|
|
34
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
35
|
-
return value;
|
|
36
|
-
};
|
|
37
19
|
var rules_exports = {};
|
|
38
20
|
__export(rules_exports, {
|
|
39
21
|
FontFaceRule: () => FontFaceRule,
|
|
@@ -44,81 +26,82 @@ __export(rules_exports, {
|
|
|
44
26
|
module.exports = __toCommonJS(rules_exports);
|
|
45
27
|
var import_to_value = require("./to-value");
|
|
46
28
|
var import_to_property = require("./to-property");
|
|
47
|
-
var _styleMap, _isDirty, _string, _indent, _transformValue, _onChange, _mediaType;
|
|
48
29
|
class StylePropertyMap {
|
|
30
|
+
#styleMap = /* @__PURE__ */ new Map();
|
|
31
|
+
#isDirty = false;
|
|
32
|
+
#string = "";
|
|
33
|
+
#indent = 0;
|
|
34
|
+
#transformValue;
|
|
35
|
+
onChange;
|
|
49
36
|
constructor(transformValue) {
|
|
50
|
-
|
|
51
|
-
__privateAdd(this, _isDirty, false);
|
|
52
|
-
__privateAdd(this, _string, "");
|
|
53
|
-
__privateAdd(this, _indent, 0);
|
|
54
|
-
__privateAdd(this, _transformValue, void 0);
|
|
55
|
-
__privateSet(this, _transformValue, transformValue);
|
|
37
|
+
this.#transformValue = transformValue;
|
|
56
38
|
}
|
|
57
39
|
setTransformer(transformValue) {
|
|
58
|
-
|
|
40
|
+
this.#transformValue = transformValue;
|
|
59
41
|
}
|
|
60
42
|
set(property, value) {
|
|
61
|
-
|
|
62
|
-
|
|
43
|
+
this.#styleMap.set(property, value);
|
|
44
|
+
this.#isDirty = true;
|
|
63
45
|
this.onChange?.();
|
|
64
46
|
}
|
|
65
47
|
has(property) {
|
|
66
|
-
return
|
|
48
|
+
return this.#styleMap.has(property);
|
|
49
|
+
}
|
|
50
|
+
get size() {
|
|
51
|
+
return this.#styleMap.size;
|
|
67
52
|
}
|
|
68
53
|
keys() {
|
|
69
|
-
return
|
|
54
|
+
return this.#styleMap.keys();
|
|
70
55
|
}
|
|
71
56
|
delete(property) {
|
|
72
|
-
|
|
73
|
-
|
|
57
|
+
this.#styleMap.delete(property);
|
|
58
|
+
this.#isDirty = true;
|
|
74
59
|
this.onChange?.();
|
|
75
60
|
}
|
|
76
61
|
clear() {
|
|
77
|
-
|
|
78
|
-
|
|
62
|
+
this.#styleMap.clear();
|
|
63
|
+
this.#isDirty = true;
|
|
79
64
|
this.onChange?.();
|
|
80
65
|
}
|
|
81
66
|
toString({ indent = 0 } = {}) {
|
|
82
|
-
if (
|
|
83
|
-
return
|
|
67
|
+
if (this.#isDirty === false && indent === this.#indent) {
|
|
68
|
+
return this.#string;
|
|
84
69
|
}
|
|
85
|
-
|
|
70
|
+
this.#indent = indent;
|
|
86
71
|
const block = [];
|
|
87
72
|
const spaces = " ".repeat(indent);
|
|
88
|
-
for (const [property, value] of
|
|
73
|
+
for (const [property, value] of this.#styleMap) {
|
|
89
74
|
if (value === void 0) {
|
|
90
75
|
continue;
|
|
91
76
|
}
|
|
92
77
|
block.push(
|
|
93
78
|
`${spaces}${(0, import_to_property.toProperty)(property)}: ${(0, import_to_value.toValue)(
|
|
94
79
|
value,
|
|
95
|
-
|
|
80
|
+
this.#transformValue
|
|
96
81
|
)}`
|
|
97
82
|
);
|
|
98
83
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
return
|
|
84
|
+
this.#string = block.join(";\n");
|
|
85
|
+
this.#isDirty = false;
|
|
86
|
+
return this.#string;
|
|
102
87
|
}
|
|
103
88
|
}
|
|
104
|
-
_styleMap = new WeakMap();
|
|
105
|
-
_isDirty = new WeakMap();
|
|
106
|
-
_string = new WeakMap();
|
|
107
|
-
_indent = new WeakMap();
|
|
108
|
-
_transformValue = new WeakMap();
|
|
109
89
|
class StyleRule {
|
|
90
|
+
styleMap;
|
|
91
|
+
selectorText;
|
|
92
|
+
onChange;
|
|
110
93
|
constructor(selectorText, style, transformValue) {
|
|
111
|
-
__privateAdd(this, _onChange, () => {
|
|
112
|
-
this.onChange?.();
|
|
113
|
-
});
|
|
114
94
|
this.styleMap = new StylePropertyMap(transformValue);
|
|
115
95
|
this.selectorText = selectorText;
|
|
116
96
|
let property;
|
|
117
97
|
for (property in style) {
|
|
118
98
|
this.styleMap.set(property, style[property]);
|
|
119
99
|
}
|
|
120
|
-
this.styleMap.onChange =
|
|
100
|
+
this.styleMap.onChange = this.#onChange;
|
|
121
101
|
}
|
|
102
|
+
#onChange = () => {
|
|
103
|
+
this.onChange?.();
|
|
104
|
+
};
|
|
122
105
|
get cssText() {
|
|
123
106
|
return this.toString();
|
|
124
107
|
}
|
|
@@ -131,13 +114,13 @@ ${this.styleMap.toString({
|
|
|
131
114
|
${spaces}}`;
|
|
132
115
|
}
|
|
133
116
|
}
|
|
134
|
-
_onChange = new WeakMap();
|
|
135
117
|
class MediaRule {
|
|
118
|
+
options;
|
|
119
|
+
rules = [];
|
|
120
|
+
#mediaType;
|
|
136
121
|
constructor(options = {}) {
|
|
137
|
-
this.rules = [];
|
|
138
|
-
__privateAdd(this, _mediaType, void 0);
|
|
139
122
|
this.options = options;
|
|
140
|
-
|
|
123
|
+
this.#mediaType = options.mediaType ?? "all";
|
|
141
124
|
}
|
|
142
125
|
insertRule(rule) {
|
|
143
126
|
this.rules.push(rule);
|
|
@@ -162,17 +145,17 @@ class MediaRule {
|
|
|
162
145
|
if (maxWidth !== void 0) {
|
|
163
146
|
conditionText += ` and (max-width: ${maxWidth}px)`;
|
|
164
147
|
}
|
|
165
|
-
return `@media ${
|
|
148
|
+
return `@media ${this.#mediaType}${conditionText} {
|
|
166
149
|
${rules.join(
|
|
167
150
|
"\n"
|
|
168
151
|
)}
|
|
169
152
|
}`;
|
|
170
153
|
}
|
|
171
154
|
}
|
|
172
|
-
_mediaType = new WeakMap();
|
|
173
155
|
class PlaintextRule {
|
|
156
|
+
cssText;
|
|
157
|
+
styleMap = new StylePropertyMap();
|
|
174
158
|
constructor(cssText) {
|
|
175
|
-
this.styleMap = new StylePropertyMap();
|
|
176
159
|
this.cssText = cssText;
|
|
177
160
|
}
|
|
178
161
|
toString() {
|
|
@@ -180,6 +163,7 @@ class PlaintextRule {
|
|
|
180
163
|
}
|
|
181
164
|
}
|
|
182
165
|
class FontFaceRule {
|
|
166
|
+
options;
|
|
183
167
|
constructor(options) {
|
|
184
168
|
this.options = options;
|
|
185
169
|
}
|
|
@@ -16,67 +16,46 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var __accessCheck = (obj, member, msg) => {
|
|
20
|
-
if (!member.has(obj))
|
|
21
|
-
throw TypeError("Cannot " + msg);
|
|
22
|
-
};
|
|
23
|
-
var __privateGet = (obj, member, getter) => {
|
|
24
|
-
__accessCheck(obj, member, "read from private field");
|
|
25
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
26
|
-
};
|
|
27
|
-
var __privateAdd = (obj, member, value) => {
|
|
28
|
-
if (member.has(obj))
|
|
29
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
30
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
31
|
-
};
|
|
32
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
33
|
-
__accessCheck(obj, member, "write to private field");
|
|
34
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
35
|
-
return value;
|
|
36
|
-
};
|
|
37
19
|
var style_element_exports = {};
|
|
38
20
|
__export(style_element_exports, {
|
|
39
21
|
StyleElement: () => StyleElement
|
|
40
22
|
});
|
|
41
23
|
module.exports = __toCommonJS(style_element_exports);
|
|
42
|
-
var _element, _name;
|
|
43
24
|
class StyleElement {
|
|
25
|
+
#element;
|
|
26
|
+
#name;
|
|
44
27
|
constructor(name = "") {
|
|
45
|
-
|
|
46
|
-
__privateAdd(this, _name, void 0);
|
|
47
|
-
__privateSet(this, _name, name);
|
|
28
|
+
this.#name = name;
|
|
48
29
|
}
|
|
49
30
|
get isMounted() {
|
|
50
|
-
return
|
|
31
|
+
return this.#element?.parentElement != null;
|
|
51
32
|
}
|
|
52
33
|
mount() {
|
|
53
34
|
if (this.isMounted === false) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
document.head.appendChild(
|
|
35
|
+
this.#element = document.createElement("style");
|
|
36
|
+
this.#element.setAttribute("data-webstudio", this.#name);
|
|
37
|
+
document.head.appendChild(this.#element);
|
|
57
38
|
}
|
|
58
39
|
}
|
|
59
40
|
unmount() {
|
|
60
41
|
if (this.isMounted) {
|
|
61
|
-
|
|
62
|
-
|
|
42
|
+
this.#element?.parentElement?.removeChild(this.#element);
|
|
43
|
+
this.#element = void 0;
|
|
63
44
|
}
|
|
64
45
|
}
|
|
65
46
|
render(cssText) {
|
|
66
|
-
if (
|
|
67
|
-
|
|
47
|
+
if (this.#element) {
|
|
48
|
+
this.#element.textContent = cssText;
|
|
68
49
|
}
|
|
69
50
|
}
|
|
70
51
|
setAttribute(name, value) {
|
|
71
|
-
if (
|
|
72
|
-
|
|
52
|
+
if (this.#element) {
|
|
53
|
+
this.#element.setAttribute(name, value);
|
|
73
54
|
}
|
|
74
55
|
}
|
|
75
56
|
getAttribute(name) {
|
|
76
|
-
if (
|
|
77
|
-
return
|
|
57
|
+
if (this.#element) {
|
|
58
|
+
return this.#element.getAttribute(name);
|
|
78
59
|
}
|
|
79
60
|
}
|
|
80
61
|
}
|
|
81
|
-
_element = new WeakMap();
|
|
82
|
-
_name = new WeakMap();
|
|
@@ -16,42 +16,21 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var __accessCheck = (obj, member, msg) => {
|
|
20
|
-
if (!member.has(obj))
|
|
21
|
-
throw TypeError("Cannot " + msg);
|
|
22
|
-
};
|
|
23
|
-
var __privateGet = (obj, member, getter) => {
|
|
24
|
-
__accessCheck(obj, member, "read from private field");
|
|
25
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
26
|
-
};
|
|
27
|
-
var __privateAdd = (obj, member, value) => {
|
|
28
|
-
if (member.has(obj))
|
|
29
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
30
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
31
|
-
};
|
|
32
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
33
|
-
__accessCheck(obj, member, "write to private field");
|
|
34
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
35
|
-
return value;
|
|
36
|
-
};
|
|
37
19
|
var style_sheet_exports = {};
|
|
38
20
|
__export(style_sheet_exports, {
|
|
39
21
|
StyleSheet: () => StyleSheet
|
|
40
22
|
});
|
|
41
23
|
module.exports = __toCommonJS(style_sheet_exports);
|
|
42
|
-
var _cssText, _element;
|
|
43
24
|
class StyleSheet {
|
|
25
|
+
#cssText = "";
|
|
26
|
+
#element;
|
|
44
27
|
constructor(element) {
|
|
45
|
-
|
|
46
|
-
__privateAdd(this, _element, void 0);
|
|
47
|
-
__privateSet(this, _element, element);
|
|
28
|
+
this.#element = element;
|
|
48
29
|
}
|
|
49
30
|
replaceSync(cssText) {
|
|
50
|
-
if (cssText !==
|
|
51
|
-
|
|
52
|
-
|
|
31
|
+
if (cssText !== this.#cssText) {
|
|
32
|
+
this.#cssText = cssText;
|
|
33
|
+
this.#element.render(cssText);
|
|
53
34
|
}
|
|
54
35
|
}
|
|
55
36
|
}
|
|
56
|
-
_cssText = new WeakMap();
|
|
57
|
-
_element = new WeakMap();
|
package/lib/cjs/core/to-value.js
CHANGED
|
@@ -21,10 +21,8 @@ __export(to_value_exports, {
|
|
|
21
21
|
toValue: () => toValue
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(to_value_exports);
|
|
24
|
+
var import_error_utils = require("@webstudio-is/error-utils");
|
|
24
25
|
var import_fonts = require("@webstudio-is/fonts");
|
|
25
|
-
const assertUnreachable = (_arg, errorMessage) => {
|
|
26
|
-
throw new Error(errorMessage);
|
|
27
|
-
};
|
|
28
26
|
const fallbackTransform = (styleValue) => {
|
|
29
27
|
if (styleValue.type === "fontFamily") {
|
|
30
28
|
const firstFontFamily = styleValue.value[0];
|
|
@@ -96,6 +94,5 @@ const toValue = (styleValue, transformValue) => {
|
|
|
96
94
|
if (value.type === "tuple") {
|
|
97
95
|
return value.value.map((value2) => toValue(value2, transformValue)).join(" ");
|
|
98
96
|
}
|
|
99
|
-
|
|
100
|
-
throw new Error("Unknown value type");
|
|
97
|
+
return (0, import_error_utils.captureError)(new Error("Unknown value type"), value);
|
|
101
98
|
};
|
package/lib/core/css-engine.js
CHANGED
|
@@ -1,22 +1,3 @@
|
|
|
1
|
-
var __accessCheck = (obj, member, msg) => {
|
|
2
|
-
if (!member.has(obj))
|
|
3
|
-
throw TypeError("Cannot " + msg);
|
|
4
|
-
};
|
|
5
|
-
var __privateGet = (obj, member, getter) => {
|
|
6
|
-
__accessCheck(obj, member, "read from private field");
|
|
7
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
8
|
-
};
|
|
9
|
-
var __privateAdd = (obj, member, value) => {
|
|
10
|
-
if (member.has(obj))
|
|
11
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
12
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
13
|
-
};
|
|
14
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
15
|
-
__accessCheck(obj, member, "write to private field");
|
|
16
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
17
|
-
return value;
|
|
18
|
-
};
|
|
19
|
-
var _element, _mediaRules, _plainRules, _fontFaceRules, _sheet, _isDirty, _cssText, _onChangeRule;
|
|
20
1
|
import {
|
|
21
2
|
FontFaceRule,
|
|
22
3
|
MediaRule,
|
|
@@ -28,39 +9,36 @@ import { StyleElement } from "./style-element";
|
|
|
28
9
|
import { StyleSheet } from "./style-sheet";
|
|
29
10
|
const defaultMediaRuleId = "__default-media-rule__";
|
|
30
11
|
class CssEngine {
|
|
12
|
+
#element;
|
|
13
|
+
#mediaRules = /* @__PURE__ */ new Map();
|
|
14
|
+
#plainRules = /* @__PURE__ */ new Map();
|
|
15
|
+
#fontFaceRules = [];
|
|
16
|
+
#sheet;
|
|
17
|
+
#isDirty = false;
|
|
18
|
+
#cssText = "";
|
|
31
19
|
constructor({ name }) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
__privateAdd(this, _plainRules, /* @__PURE__ */ new Map());
|
|
35
|
-
__privateAdd(this, _fontFaceRules, []);
|
|
36
|
-
__privateAdd(this, _sheet, void 0);
|
|
37
|
-
__privateAdd(this, _isDirty, false);
|
|
38
|
-
__privateAdd(this, _cssText, "");
|
|
39
|
-
__privateAdd(this, _onChangeRule, () => {
|
|
40
|
-
__privateSet(this, _isDirty, true);
|
|
41
|
-
});
|
|
42
|
-
__privateSet(this, _element, new StyleElement(name));
|
|
43
|
-
__privateSet(this, _sheet, new StyleSheet(__privateGet(this, _element)));
|
|
20
|
+
this.#element = new StyleElement(name);
|
|
21
|
+
this.#sheet = new StyleSheet(this.#element);
|
|
44
22
|
}
|
|
45
23
|
addMediaRule(id, options) {
|
|
46
|
-
let mediaRule =
|
|
24
|
+
let mediaRule = this.#mediaRules.get(id);
|
|
47
25
|
if (mediaRule === void 0) {
|
|
48
26
|
mediaRule = new MediaRule(options);
|
|
49
|
-
|
|
50
|
-
|
|
27
|
+
this.#mediaRules.set(id, mediaRule);
|
|
28
|
+
this.#isDirty = true;
|
|
51
29
|
return mediaRule;
|
|
52
30
|
}
|
|
53
31
|
if (options) {
|
|
54
32
|
mediaRule.options = options;
|
|
55
|
-
|
|
33
|
+
this.#isDirty = true;
|
|
56
34
|
}
|
|
57
35
|
return mediaRule;
|
|
58
36
|
}
|
|
59
37
|
addStyleRule(selectorText, rule, transformValue) {
|
|
60
38
|
const mediaRule = this.addMediaRule(rule.breakpoint || defaultMediaRuleId);
|
|
61
|
-
|
|
39
|
+
this.#isDirty = true;
|
|
62
40
|
const styleRule = new StyleRule(selectorText, rule.style, transformValue);
|
|
63
|
-
styleRule.onChange =
|
|
41
|
+
styleRule.onChange = this.#onChangeRule;
|
|
64
42
|
if (mediaRule === void 0) {
|
|
65
43
|
throw new Error("No media rule found");
|
|
66
44
|
}
|
|
@@ -68,47 +46,47 @@ class CssEngine {
|
|
|
68
46
|
return styleRule;
|
|
69
47
|
}
|
|
70
48
|
addPlaintextRule(cssText) {
|
|
71
|
-
const rule =
|
|
49
|
+
const rule = this.#plainRules.get(cssText);
|
|
72
50
|
if (rule !== void 0) {
|
|
73
51
|
return rule;
|
|
74
52
|
}
|
|
75
|
-
|
|
76
|
-
return
|
|
53
|
+
this.#isDirty = true;
|
|
54
|
+
return this.#plainRules.set(cssText, new PlaintextRule(cssText));
|
|
77
55
|
}
|
|
78
56
|
addFontFaceRule(options) {
|
|
79
|
-
|
|
80
|
-
return
|
|
57
|
+
this.#isDirty = true;
|
|
58
|
+
return this.#fontFaceRules.push(new FontFaceRule(options));
|
|
81
59
|
}
|
|
82
60
|
clear() {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
61
|
+
this.#mediaRules.clear();
|
|
62
|
+
this.#plainRules.clear();
|
|
63
|
+
this.#fontFaceRules = [];
|
|
64
|
+
this.#isDirty = true;
|
|
87
65
|
}
|
|
88
66
|
render() {
|
|
89
|
-
|
|
90
|
-
|
|
67
|
+
this.#element.mount();
|
|
68
|
+
this.#sheet.replaceSync(this.cssText);
|
|
91
69
|
}
|
|
92
70
|
unmount() {
|
|
93
|
-
|
|
71
|
+
this.#element.unmount();
|
|
94
72
|
}
|
|
95
73
|
setAttribute(name, value) {
|
|
96
|
-
|
|
74
|
+
this.#element.setAttribute(name, value);
|
|
97
75
|
}
|
|
98
76
|
getAttribute(name) {
|
|
99
|
-
return
|
|
77
|
+
return this.#element.getAttribute(name);
|
|
100
78
|
}
|
|
101
79
|
get cssText() {
|
|
102
|
-
if (
|
|
103
|
-
return
|
|
80
|
+
if (this.#isDirty === false) {
|
|
81
|
+
return this.#cssText;
|
|
104
82
|
}
|
|
105
|
-
|
|
83
|
+
this.#isDirty = false;
|
|
106
84
|
const css = [];
|
|
107
|
-
css.push(...
|
|
108
|
-
for (const plaintextRule of
|
|
85
|
+
css.push(...this.#fontFaceRules.map((rule) => rule.cssText));
|
|
86
|
+
for (const plaintextRule of this.#plainRules.values()) {
|
|
109
87
|
css.push(plaintextRule.cssText);
|
|
110
88
|
}
|
|
111
|
-
const sortedMediaRules = Array.from(
|
|
89
|
+
const sortedMediaRules = Array.from(this.#mediaRules.values()).sort(
|
|
112
90
|
(ruleA, ruleB) => compareMedia(ruleA.options, ruleB.options)
|
|
113
91
|
);
|
|
114
92
|
for (const mediaRule of sortedMediaRules) {
|
|
@@ -117,18 +95,13 @@ class CssEngine {
|
|
|
117
95
|
css.push(cssText);
|
|
118
96
|
}
|
|
119
97
|
}
|
|
120
|
-
|
|
121
|
-
return
|
|
98
|
+
this.#cssText = css.join("\n");
|
|
99
|
+
return this.#cssText;
|
|
122
100
|
}
|
|
101
|
+
#onChangeRule = () => {
|
|
102
|
+
this.#isDirty = true;
|
|
103
|
+
};
|
|
123
104
|
}
|
|
124
|
-
_element = new WeakMap();
|
|
125
|
-
_mediaRules = new WeakMap();
|
|
126
|
-
_plainRules = new WeakMap();
|
|
127
|
-
_fontFaceRules = new WeakMap();
|
|
128
|
-
_sheet = new WeakMap();
|
|
129
|
-
_isDirty = new WeakMap();
|
|
130
|
-
_cssText = new WeakMap();
|
|
131
|
-
_onChangeRule = new WeakMap();
|
|
132
105
|
export {
|
|
133
106
|
CssEngine
|
|
134
107
|
};
|