@nordcraft/runtime 1.0.33 → 1.0.35

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.
Files changed (40) hide show
  1. package/dist/components/createComponent.js +4 -0
  2. package/dist/components/createComponent.js.map +1 -1
  3. package/dist/components/createElement.js +9 -1
  4. package/dist/components/createElement.js.map +1 -1
  5. package/dist/components/createNode.js +3 -3
  6. package/dist/components/createNode.js.map +1 -1
  7. package/dist/custom-element.main.esm.js +17 -17
  8. package/dist/custom-element.main.esm.js.map +4 -4
  9. package/dist/debug/panicScreen.d.ts +6 -0
  10. package/dist/debug/panicScreen.js +25 -0
  11. package/dist/debug/panicScreen.js.map +1 -0
  12. package/dist/debug/sendEditorToast.d.ts +3 -0
  13. package/dist/debug/sendEditorToast.js +9 -0
  14. package/dist/debug/sendEditorToast.js.map +1 -0
  15. package/dist/editor/graphql.d.ts +12 -0
  16. package/dist/editor/graphql.js +150 -0
  17. package/dist/editor/graphql.js.map +1 -0
  18. package/dist/editor-preview.main.js +142 -71
  19. package/dist/editor-preview.main.js.map +1 -1
  20. package/dist/page.main.esm.js +3 -3
  21. package/dist/page.main.esm.js.map +4 -4
  22. package/dist/styles/CustomPropertyStyleSheet.d.ts +2 -1
  23. package/dist/styles/CustomPropertyStyleSheet.js +14 -5
  24. package/dist/styles/CustomPropertyStyleSheet.js.map +1 -1
  25. package/dist/styles/CustomPropertyStyleSheet.test.js +7 -7
  26. package/dist/styles/CustomPropertyStyleSheet.test.js.map +1 -1
  27. package/dist/utils/subscribeCustomProperty.d.ts +4 -1
  28. package/dist/utils/subscribeCustomProperty.js +11 -4
  29. package/dist/utils/subscribeCustomProperty.js.map +1 -1
  30. package/package.json +3 -3
  31. package/src/components/createComponent.ts +4 -0
  32. package/src/components/createElement.ts +10 -1
  33. package/src/components/createNode.ts +3 -3
  34. package/src/debug/panicScreen.ts +37 -0
  35. package/src/debug/sendEditorToast.ts +19 -0
  36. package/src/editor/graphql.ts +167 -0
  37. package/src/editor-preview.main.ts +319 -215
  38. package/src/styles/CustomPropertyStyleSheet.test.ts +7 -7
  39. package/src/styles/CustomPropertyStyleSheet.ts +22 -5
  40. package/src/utils/subscribeCustomProperty.ts +25 -12
@@ -3,20 +3,20 @@ import { CustomPropertyStyleSheet } from './CustomPropertyStyleSheet'
3
3
 
4
4
  describe('CustomPropertyStyleSheet', () => {
5
5
  test('it creates a new stylesheet', () => {
6
- const instance = new CustomPropertyStyleSheet()
6
+ const instance = new CustomPropertyStyleSheet(document)
7
7
  expect(instance).toBeInstanceOf(CustomPropertyStyleSheet)
8
8
  expect(instance.getStyleSheet().cssRules).toHaveLength(0)
9
9
  })
10
10
 
11
11
  test('it adds a property definition', () => {
12
- const instance = new CustomPropertyStyleSheet()
12
+ const instance = new CustomPropertyStyleSheet(document)
13
13
  instance.registerProperty('.my-class', '--my-property')
14
14
  expect(instance.getStyleSheet().cssRules.length).toBe(1)
15
15
  expect(instance.getStyleSheet().cssRules[0].cssText).toBe('.my-class { }')
16
16
  })
17
17
 
18
18
  test('it puts different selectors in different rules', () => {
19
- const instance = new CustomPropertyStyleSheet()
19
+ const instance = new CustomPropertyStyleSheet(document)
20
20
  instance.registerProperty('.my-class', '--my-property')('256px')
21
21
  instance.registerProperty('.my-other-class', '--my-property')('256px')
22
22
  expect(instance.getStyleSheet().cssRules.length).toBe(2)
@@ -29,7 +29,7 @@ describe('CustomPropertyStyleSheet', () => {
29
29
  })
30
30
 
31
31
  test('it can update properties', () => {
32
- const instance = new CustomPropertyStyleSheet()
32
+ const instance = new CustomPropertyStyleSheet(document)
33
33
  const setter = instance.registerProperty('.my-class', '--my-property')
34
34
  setter('256px')
35
35
  expect(instance.getStyleSheet().cssRules.length).toBe(1)
@@ -44,7 +44,7 @@ describe('CustomPropertyStyleSheet', () => {
44
44
  })
45
45
 
46
46
  test('it works with media queries', () => {
47
- const instance = new CustomPropertyStyleSheet()
47
+ const instance = new CustomPropertyStyleSheet(document)
48
48
  instance.registerProperty('.my-class', '--my-property', {
49
49
  mediaQuery: { 'max-width': '600px' },
50
50
  })('256px')
@@ -55,7 +55,7 @@ describe('CustomPropertyStyleSheet', () => {
55
55
  })
56
56
 
57
57
  test('it unregisters a property', () => {
58
- const instance = new CustomPropertyStyleSheet()
58
+ const instance = new CustomPropertyStyleSheet(document)
59
59
  const setter = instance.registerProperty('.my-class', '--my-property')
60
60
  setter('256px')
61
61
  const setter2 = instance.registerProperty(
@@ -76,7 +76,7 @@ describe('CustomPropertyStyleSheet', () => {
76
76
  })
77
77
 
78
78
  test('it unregisters a property with media queries', () => {
79
- const instance = new CustomPropertyStyleSheet()
79
+ const instance = new CustomPropertyStyleSheet(document)
80
80
  const setter = instance.registerProperty(
81
81
  '.my-class-with-media',
82
82
  '--my-property-with-media',
@@ -15,12 +15,12 @@ export class CustomPropertyStyleSheet {
15
15
  // Selector to rule index mapping
16
16
  private ruleMap: Map<string, CSSStyleRule | CSSNestedDeclarations> | undefined
17
17
 
18
- constructor(styleSheet?: CSSStyleSheet | null) {
18
+ constructor(root: Document | ShadowRoot, styleSheet?: CSSStyleSheet | null) {
19
19
  if (styleSheet) {
20
20
  this.styleSheet = styleSheet
21
21
  } else {
22
22
  this.styleSheet = new CSSStyleSheet()
23
- document.adoptedStyleSheets.push(this.getStyleSheet())
23
+ root.adoptedStyleSheets.push(this.getStyleSheet())
24
24
  }
25
25
  }
26
26
 
@@ -44,7 +44,10 @@ export class CustomPropertyStyleSheet {
44
44
  // Check if the selector already exists
45
45
  let rule = this.ruleMap.get(fullSelector)
46
46
  if (!rule) {
47
- const ruleIndex = this.styleSheet.insertRule(fullSelector)
47
+ const ruleIndex = this.styleSheet.insertRule(
48
+ fullSelector,
49
+ this.styleSheet.cssRules.length,
50
+ )
48
51
  let newRule = this.styleSheet.cssRules[ruleIndex]
49
52
 
50
53
  // We are only interested in the dynamic style, so get the actual style rule, not media or other nested rules. Loop until we are at the bottom most rule.
@@ -69,6 +72,7 @@ export class CustomPropertyStyleSheet {
69
72
  options?: {
70
73
  mediaQuery?: MediaQuery
71
74
  startingStyle?: boolean
75
+ deepClean?: boolean
72
76
  },
73
77
  ): void {
74
78
  if (!this.ruleMap) {
@@ -80,8 +84,21 @@ export class CustomPropertyStyleSheet {
80
84
  options,
81
85
  )
82
86
 
83
- // We only clean up the property, as we assume that the rule will be reused.
84
- this.ruleMap.get(fullSelector)?.style.removeProperty(name)
87
+ const rule = this.ruleMap.get(fullSelector)
88
+ if (!rule) {
89
+ return
90
+ }
91
+
92
+ rule.style.removeProperty(name)
93
+
94
+ // Cleaning up empty selectors is probably not necessary in production and may have performance implications.
95
+ // However, it is required for the editor-preview as it is a dynamic environment and things may get reordered and canvas reused.
96
+ if (options?.deepClean && rule.style.length === 0) {
97
+ this.styleSheet.deleteRule(
98
+ Array.from(this.ruleMap.keys()).indexOf(fullSelector),
99
+ )
100
+ this.ruleMap.delete(fullSelector)
101
+ }
85
102
  }
86
103
 
87
104
  public getStyleSheet(): CSSStyleSheet {
@@ -2,40 +2,53 @@ import type { StyleVariant } from '@nordcraft/core/dist/component/component.type
2
2
  import type { Signal } from '../signal/signal'
3
3
 
4
4
  import { CUSTOM_PROPERTIES_STYLESHEET_ID } from '@nordcraft/core/dist/styling/theme.const'
5
+ import type { Runtime } from '@nordcraft/core/dist/types'
5
6
  import { CustomPropertyStyleSheet } from '../styles/CustomPropertyStyleSheet'
6
7
 
7
- const CUSTOM_PROPERTIES_STYLESHEET = new CustomPropertyStyleSheet(
8
- (
9
- document.getElementById(CUSTOM_PROPERTIES_STYLESHEET_ID) as
10
- | HTMLStyleElement
11
- | undefined
12
- )?.sheet,
13
- )
8
+ let customPropertiesStylesheet: CustomPropertyStyleSheet | undefined
14
9
 
15
10
  export function subscribeCustomProperty({
16
11
  selector,
17
12
  customPropertyName,
18
13
  signal,
19
14
  variant,
15
+ root,
16
+ runtime,
20
17
  }: {
21
18
  selector: string
22
19
  customPropertyName: string
23
20
  signal: Signal<string>
24
21
  variant?: StyleVariant
22
+ root: Document | ShadowRoot
23
+ runtime: Runtime
25
24
  }) {
25
+ customPropertiesStylesheet ??= new CustomPropertyStyleSheet(
26
+ root,
27
+ (
28
+ root.getElementById(CUSTOM_PROPERTIES_STYLESHEET_ID) as
29
+ | HTMLStyleElement
30
+ | undefined
31
+ )?.sheet,
32
+ )
33
+
26
34
  signal.subscribe(
27
- CUSTOM_PROPERTIES_STYLESHEET.registerProperty(
35
+ customPropertiesStylesheet.registerProperty(
28
36
  selector,
29
37
  customPropertyName,
30
38
  variant,
31
39
  ),
32
40
  {
33
- destroy: () =>
34
- CUSTOM_PROPERTIES_STYLESHEET.unregisterProperty(
41
+ destroy: () => {
42
+ customPropertiesStylesheet?.unregisterProperty(
35
43
  selector,
36
44
  customPropertyName,
37
- variant,
38
- ),
45
+ {
46
+ deepClean: runtime === 'preview',
47
+ mediaQuery: variant?.mediaQuery,
48
+ startingStyle: variant?.startingStyle,
49
+ },
50
+ )
51
+ },
39
52
  },
40
53
  )
41
54
  }