@webstudio-is/css-engine 0.19.0 → 0.20.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.
@@ -22,6 +22,6 @@ __export(create_css_engine_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(create_css_engine_exports);
24
24
  var import_css_engine = require("./css-engine");
25
- const createCssEngine = () => {
26
- return new import_css_engine.CssEngine();
25
+ const createCssEngine = (options = {}) => {
26
+ return new import_css_engine.CssEngine(options);
27
27
  };
@@ -45,7 +45,7 @@ var import_style_sheet = require("./style-sheet");
45
45
  var _element, _mediaRules, _plainRules, _fontFaceRules, _sheet, _isDirty, _cssText, _onChangeRule;
46
46
  const defaultMediaRuleId = "__default-media-rule__";
47
47
  class CssEngine {
48
- constructor() {
48
+ constructor({ name }) {
49
49
  __privateAdd(this, _element, void 0);
50
50
  __privateAdd(this, _mediaRules, /* @__PURE__ */ new Map());
51
51
  __privateAdd(this, _plainRules, /* @__PURE__ */ new Map());
@@ -56,7 +56,7 @@ class CssEngine {
56
56
  __privateAdd(this, _onChangeRule, () => {
57
57
  __privateSet(this, _isDirty, true);
58
58
  });
59
- __privateSet(this, _element, new import_style_element.StyleElement());
59
+ __privateSet(this, _element, new import_style_element.StyleElement(name));
60
60
  __privateSet(this, _sheet, new import_style_sheet.StyleSheet(__privateGet(this, _element)));
61
61
  }
62
62
  addMediaRule(id, options) {
@@ -39,10 +39,12 @@ __export(style_element_exports, {
39
39
  StyleElement: () => StyleElement
40
40
  });
41
41
  module.exports = __toCommonJS(style_element_exports);
42
- var _element;
42
+ var _element, _name;
43
43
  class StyleElement {
44
- constructor() {
44
+ constructor(name = "") {
45
45
  __privateAdd(this, _element, void 0);
46
+ __privateAdd(this, _name, void 0);
47
+ __privateSet(this, _name, name);
46
48
  }
47
49
  get isMounted() {
48
50
  return __privateGet(this, _element)?.parentElement != null;
@@ -50,7 +52,7 @@ class StyleElement {
50
52
  mount() {
51
53
  if (this.isMounted === false) {
52
54
  __privateSet(this, _element, document.createElement("style"));
53
- __privateGet(this, _element).setAttribute("data-webstudio", "");
55
+ __privateGet(this, _element).setAttribute("data-webstudio", __privateGet(this, _name));
54
56
  document.head.appendChild(__privateGet(this, _element));
55
57
  }
56
58
  }
@@ -67,3 +69,4 @@ class StyleElement {
67
69
  }
68
70
  }
69
71
  _element = new WeakMap();
72
+ _name = new WeakMap();
@@ -66,6 +66,11 @@ const toValue = (value, options = defaultOptions) => {
66
66
  if (value.type === "rgb") {
67
67
  return `rgba(${value.r}, ${value.g}, ${value.b}, ${value.alpha})`;
68
68
  }
69
+ if (value.type === "image") {
70
+ return value.value.map(
71
+ (imageAsset) => `url(${imageAsset.value.path}) /* id=${imageAsset.value.id} */`
72
+ ).join(", ");
73
+ }
69
74
  assertUnreachable(value, `Unknown value type`);
70
75
  throw new Error("Unknown value type");
71
76
  };
@@ -1,6 +1,6 @@
1
1
  import { CssEngine } from "./css-engine";
2
- const createCssEngine = () => {
3
- return new CssEngine();
2
+ const createCssEngine = (options = {}) => {
3
+ return new CssEngine(options);
4
4
  };
5
5
  export {
6
6
  createCssEngine
@@ -27,7 +27,7 @@ import { StyleElement } from "./style-element";
27
27
  import { StyleSheet } from "./style-sheet";
28
28
  const defaultMediaRuleId = "__default-media-rule__";
29
29
  class CssEngine {
30
- constructor() {
30
+ constructor({ name }) {
31
31
  __privateAdd(this, _element, void 0);
32
32
  __privateAdd(this, _mediaRules, /* @__PURE__ */ new Map());
33
33
  __privateAdd(this, _plainRules, /* @__PURE__ */ new Map());
@@ -38,7 +38,7 @@ class CssEngine {
38
38
  __privateAdd(this, _onChangeRule, () => {
39
39
  __privateSet(this, _isDirty, true);
40
40
  });
41
- __privateSet(this, _element, new StyleElement());
41
+ __privateSet(this, _element, new StyleElement(name));
42
42
  __privateSet(this, _sheet, new StyleSheet(__privateGet(this, _element)));
43
43
  }
44
44
  addMediaRule(id, options) {
@@ -16,10 +16,12 @@ var __privateSet = (obj, member, value, setter) => {
16
16
  setter ? setter.call(obj, value) : member.set(obj, value);
17
17
  return value;
18
18
  };
19
- var _element;
19
+ var _element, _name;
20
20
  class StyleElement {
21
- constructor() {
21
+ constructor(name = "") {
22
22
  __privateAdd(this, _element, void 0);
23
+ __privateAdd(this, _name, void 0);
24
+ __privateSet(this, _name, name);
23
25
  }
24
26
  get isMounted() {
25
27
  return __privateGet(this, _element)?.parentElement != null;
@@ -27,7 +29,7 @@ class StyleElement {
27
29
  mount() {
28
30
  if (this.isMounted === false) {
29
31
  __privateSet(this, _element, document.createElement("style"));
30
- __privateGet(this, _element).setAttribute("data-webstudio", "");
32
+ __privateGet(this, _element).setAttribute("data-webstudio", __privateGet(this, _name));
31
33
  document.head.appendChild(__privateGet(this, _element));
32
34
  }
33
35
  }
@@ -44,6 +46,7 @@ class StyleElement {
44
46
  }
45
47
  }
46
48
  _element = new WeakMap();
49
+ _name = new WeakMap();
47
50
  export {
48
51
  StyleElement
49
52
  };
@@ -43,6 +43,11 @@ const toValue = (value, options = defaultOptions) => {
43
43
  if (value.type === "rgb") {
44
44
  return `rgba(${value.r}, ${value.g}, ${value.b}, ${value.alpha})`;
45
45
  }
46
+ if (value.type === "image") {
47
+ return value.value.map(
48
+ (imageAsset) => `url(${imageAsset.value.path}) /* id=${imageAsset.value.id} */`
49
+ ).join(", ");
50
+ }
46
51
  assertUnreachable(value, `Unknown value type`);
47
52
  throw new Error("Unknown value type");
48
53
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/css-engine",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "CSS Renderer for Webstudio",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -9,7 +9,7 @@
9
9
  "hyphenate-style-name": "^1.0.4",
10
10
  "react": "^17.0.2",
11
11
  "react-dom": "^17.0.2",
12
- "@webstudio-is/fonts": "^0.19.0"
12
+ "@webstudio-is/fonts": "^0.20.0"
13
13
  },
14
14
  "devDependencies": {
15
15
  "@jest/globals": "^29.3.1",
@@ -21,7 +21,7 @@
21
21
  "@types/react-dom": "^17.0.9",
22
22
  "jest": "^29.3.1",
23
23
  "typescript": "4.7.4",
24
- "@webstudio-is/css-data": "^0.19.0",
24
+ "@webstudio-is/css-data": "^0.20.0",
25
25
  "@webstudio-is/jest-config": "^1.0.2",
26
26
  "@webstudio-is/scripts": "^0.0.0",
27
27
  "@webstudio-is/storybook-config": "^0.0.0",
@@ -1,5 +1,5 @@
1
- import { CssEngine } from "./css-engine";
1
+ import { CssEngine, type CssEngineOptions } from "./css-engine";
2
2
 
3
- export const createCssEngine = () => {
4
- return new CssEngine();
3
+ export const createCssEngine = (options: CssEngineOptions = {}) => {
4
+ return new CssEngine(options);
5
5
  };
@@ -12,7 +12,7 @@ const mediaRuleOptions0 = { minWidth: 0 } as const;
12
12
  const mediaId = "0";
13
13
 
14
14
  export const Basic = () => {
15
- const engine = new CssEngine();
15
+ const engine = new CssEngine({ name: "test" });
16
16
  engine.addMediaRule(mediaId, mediaRuleOptions0);
17
17
  const rule = engine.addStyleRule(".test", {
18
18
  style: style0,
@@ -24,7 +24,7 @@ describe("CssEngine", () => {
24
24
  let engine: CssEngine;
25
25
 
26
26
  const reset = () => {
27
- engine = new CssEngine();
27
+ engine = new CssEngine({ name: "test" });
28
28
  };
29
29
 
30
30
  beforeEach(reset);
@@ -12,6 +12,8 @@ import { StyleSheet } from "./style-sheet";
12
12
 
13
13
  const defaultMediaRuleId = "__default-media-rule__";
14
14
 
15
+ export type CssEngineOptions = { name?: string };
16
+
15
17
  export class CssEngine {
16
18
  #element;
17
19
  #mediaRules: Map<string, MediaRule> = new Map();
@@ -20,8 +22,8 @@ export class CssEngine {
20
22
  #sheet: StyleSheet;
21
23
  #isDirty = false;
22
24
  #cssText = "";
23
- constructor() {
24
- this.#element = new StyleElement();
25
+ constructor({ name }: CssEngineOptions) {
26
+ this.#element = new StyleElement(name);
25
27
  this.#sheet = new StyleSheet(this.#element);
26
28
  }
27
29
  addMediaRule(id: string, options?: MediaRuleOptions) {
@@ -1,12 +1,16 @@
1
1
  export class StyleElement {
2
2
  #element?: HTMLStyleElement;
3
+ #name: string;
4
+ constructor(name = "") {
5
+ this.#name = name;
6
+ }
3
7
  get isMounted() {
4
8
  return this.#element?.parentElement != null;
5
9
  }
6
10
  mount() {
7
11
  if (this.isMounted === false) {
8
12
  this.#element = document.createElement("style");
9
- this.#element.setAttribute("data-webstudio", "");
13
+ this.#element.setAttribute("data-webstudio", this.#name);
10
14
  document.head.appendChild(this.#element);
11
15
  }
12
16
  }
@@ -61,6 +61,16 @@ export const toValue = (
61
61
  return `rgba(${value.r}, ${value.g}, ${value.b}, ${value.alpha})`;
62
62
  }
63
63
 
64
+ if (value.type === "image") {
65
+ // @todo image-set
66
+ return value.value
67
+ .map(
68
+ (imageAsset) =>
69
+ `url(${imageAsset.value.path}) /* id=${imageAsset.value.id} */`
70
+ )
71
+ .join(", ");
72
+ }
73
+
64
74
  // Will give ts error in case of missing type
65
75
  assertUnreachable(value, `Unknown value type`);
66
76