@nordcraft/runtime 1.0.75 → 1.0.76

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/README.md +1 -1
  2. package/dist/api/createAPI.d.ts +3 -2
  3. package/dist/api/createAPI.js.map +1 -1
  4. package/dist/components/createComponent.js +5 -5
  5. package/dist/components/createComponent.js.map +1 -1
  6. package/dist/components/createElement.js +1 -1
  7. package/dist/components/createElement.js.map +1 -1
  8. package/dist/components/createNode.js +4 -4
  9. package/dist/components/createNode.js.map +1 -1
  10. package/dist/components/renderComponent.js +2 -2
  11. package/dist/components/renderComponent.js.map +1 -1
  12. package/dist/context/subscribeToContext.js +2 -5
  13. package/dist/context/subscribeToContext.js.map +1 -1
  14. package/dist/custom-element/ToddleComponent.js +4 -4
  15. package/dist/custom-element/ToddleComponent.js.map +1 -1
  16. package/dist/custom-element.main.esm.js +17 -17
  17. package/dist/custom-element.main.esm.js.map +3 -3
  18. package/dist/editor-preview.main.js +26 -29
  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 +3 -3
  22. package/dist/page.main.js +2 -2
  23. package/dist/page.main.js.map +1 -1
  24. package/dist/styles/CustomPropertyStyleSheet.d.ts +11 -10
  25. package/dist/styles/CustomPropertyStyleSheet.js.map +1 -1
  26. package/dist/utils/nodes.js +1 -1
  27. package/dist/utils/nodes.js.map +1 -1
  28. package/package.json +4 -4
  29. package/src/api/createAPI.ts +2 -1
  30. package/src/api/createAPIv2.ts +7 -7
  31. package/src/components/createComponent.ts +41 -35
  32. package/src/components/createElement.ts +1 -1
  33. package/src/components/createNode.ts +5 -5
  34. package/src/components/renderComponent.ts +2 -2
  35. package/src/context/subscribeToContext.ts +5 -5
  36. package/src/custom-element/ToddleComponent.ts +24 -21
  37. package/src/editor-preview.main.ts +40 -37
  38. package/src/page.main.ts +14 -12
  39. package/src/styles/CustomPropertyStyleSheet.ts +18 -14
  40. package/src/utils/nodes.ts +1 -1
@@ -453,7 +453,7 @@ export const createRoot = (
453
453
 
454
454
  const node = getDOMNodeFromNodeId(selectedNodeId)
455
455
  const element =
456
- component?.nodes[node?.getAttribute('data-node-id') ?? '']
456
+ component?.nodes?.[node?.getAttribute('data-node-id') ?? '']
457
457
  if (
458
458
  node &&
459
459
  element &&
@@ -544,7 +544,7 @@ export const createRoot = (
544
544
  return false
545
545
  }
546
546
  const nodeId = getNodeId(component, id.split('.').slice(1))
547
- const node = nodeId ? component?.nodes[nodeId] : undefined
547
+ const node = nodeId ? component?.nodes?.[nodeId] : undefined
548
548
  if (!node) {
549
549
  return false
550
550
  }
@@ -562,7 +562,7 @@ export const createRoot = (
562
562
  if (message.data.metaKey) {
563
563
  // Figure out if the clicked element is a text element
564
564
  // or if one of its descendants is a text element
565
- const root = component.nodes.root
565
+ const root = component.nodes?.root
566
566
  if (root && id) {
567
567
  const nodeLookup = getNodeAndAncestors(component, root, id)
568
568
  if (nodeLookup?.node.type === 'text') {
@@ -574,7 +574,7 @@ export const createRoot = (
574
574
  const firstTextChild =
575
575
  nodeLookup?.node.type === 'element'
576
576
  ? nodeLookup.node.children.find(
577
- (c) => component?.nodes[c]?.type === 'text',
577
+ (c) => component?.nodes?.[c]?.type === 'text',
578
578
  )
579
579
  : undefined
580
580
  if (firstTextChild) {
@@ -603,7 +603,7 @@ export const createRoot = (
603
603
  mode === 'design'
604
604
  ) {
605
605
  // Figure out if the clicked element is a component
606
- const root = component.nodes.root
606
+ const root = component.nodes?.root
607
607
  if (root) {
608
608
  const nodeLookup = getNodeAndAncestors(component, root, id)
609
609
  if (
@@ -659,7 +659,7 @@ export const createRoot = (
659
659
  }
660
660
  case 'introspect_qraphql_api': {
661
661
  const { apiKey } = message.data
662
- const api = component?.apis[apiKey]
662
+ const api = component?.apis?.[apiKey]
663
663
  if (api && !isLegacyApi(api) && component) {
664
664
  const formulaContext: FormulaContext = {
665
665
  component,
@@ -737,7 +737,7 @@ export const createRoot = (
737
737
  parent: parentDataId,
738
738
  index: !isNaN(nextSiblingId)
739
739
  ? nextSiblingId
740
- : component?.nodes[parentNodeId]?.children?.length,
740
+ : component?.nodes?.[parentNodeId]?.children?.length,
741
741
  })
742
742
  dragState = null
743
743
  })
@@ -965,20 +965,23 @@ export const createRoot = (
965
965
  // If style variant targets a pseudo-element, apply styles to it instead
966
966
  let pseudoElement = ''
967
967
  if (component && styleVariantSelection) {
968
- const nodeLookup = getNodeAndAncestors(
969
- component,
970
- component.nodes.root,
971
- styleVariantSelection.nodeId,
972
- )
968
+ const rootNode = component.nodes?.root
969
+ if (rootNode) {
970
+ const nodeLookup = getNodeAndAncestors(
971
+ component,
972
+ rootNode,
973
+ styleVariantSelection.nodeId,
974
+ )
973
975
 
974
- if (
975
- (nodeLookup?.node.type === 'element' ||
976
- nodeLookup?.node.type === 'component') &&
977
- nodeLookup.node.variants?.[
978
- styleVariantSelection.styleVariantIndex
979
- ].pseudoElement
980
- ) {
981
- pseudoElement = `::${nodeLookup.node.variants[styleVariantSelection.styleVariantIndex].pseudoElement}`
976
+ if (
977
+ (nodeLookup?.node.type === 'element' ||
978
+ nodeLookup?.node.type === 'component') &&
979
+ nodeLookup.node.variants?.[
980
+ styleVariantSelection.styleVariantIndex
981
+ ].pseudoElement
982
+ ) {
983
+ pseudoElement = `::${nodeLookup.node.variants[styleVariantSelection.styleVariantIndex].pseudoElement}`
984
+ }
982
985
  }
983
986
  }
984
987
 
@@ -1078,7 +1081,7 @@ export const createRoot = (
1078
1081
  }
1079
1082
  if (mode === 'design') {
1080
1083
  if (selectedNodeId !== null) {
1081
- const root = _component?.nodes.root
1084
+ const root = _component?.nodes?.root
1082
1085
  if (root) {
1083
1086
  const nodeLookup = getNodeAndAncestors(
1084
1087
  _component,
@@ -1106,7 +1109,7 @@ export const createRoot = (
1106
1109
  nodeId: selectedNodeId,
1107
1110
  styleVariantIndex: variantIndex,
1108
1111
  }
1109
- const root = component?.nodes.root
1112
+ const root = component?.nodes?.root
1110
1113
  if (root && component) {
1111
1114
  const nodeLookup = getNodeAndAncestors(component, root, selectedNodeId)
1112
1115
  if (nodeLookup) {
@@ -1187,10 +1190,10 @@ export const createRoot = (
1187
1190
  if (
1188
1191
  fastDeepEqual(ctx?.component.attributes, _component.attributes) === false
1189
1192
  ) {
1190
- Attributes = mapObject(_component.attributes, ([name, { testValue }]) => [
1191
- name,
1192
- testValue,
1193
- ])
1193
+ Attributes = mapObject(
1194
+ _component.attributes ?? {},
1195
+ ([name, { testValue }]) => [name, testValue],
1196
+ )
1194
1197
  }
1195
1198
  if (
1196
1199
  _component.route &&
@@ -1234,10 +1237,10 @@ export const createRoot = (
1234
1237
  )
1235
1238
  }
1236
1239
 
1237
- Attributes = mapObject(_component.attributes, ([name, { testValue }]) => [
1238
- name,
1239
- testValue,
1240
- ])
1240
+ Attributes = mapObject(
1241
+ _component.attributes ?? {},
1242
+ ([name, { testValue }]) => [name, testValue],
1243
+ )
1241
1244
  }
1242
1245
  if (
1243
1246
  fastDeepEqual(
@@ -1282,7 +1285,7 @@ export const createRoot = (
1282
1285
  const formulaContext: FormulaContext = {
1283
1286
  data: {
1284
1287
  Attributes: mapObject(
1285
- providerComponent.attributes,
1288
+ providerComponent.attributes ?? {},
1286
1289
  ([name, attr]) => [name, attr.testValue],
1287
1290
  ),
1288
1291
  // Recursively resolve contexts providers before their children to build up the fake context tree in preview mode
@@ -1316,7 +1319,7 @@ export const createRoot = (
1316
1319
  }
1317
1320
  }
1318
1321
  formulaContext.data.Variables = mapObject(
1319
- providerComponent.variables,
1322
+ providerComponent.variables ?? {},
1320
1323
  ([name, variable]) => [
1321
1324
  name,
1322
1325
  applyFormula(variable.initialValue, formulaContext),
@@ -1350,7 +1353,7 @@ export const createRoot = (
1350
1353
  fastDeepEqual(_component.variables, ctx?.component.variables) === false
1351
1354
  ) {
1352
1355
  Variables = mapObject(
1353
- _component.variables,
1356
+ _component.variables ?? {},
1354
1357
  ([name, { initialValue }]) => [
1355
1358
  name,
1356
1359
  applyFormula(initialValue, {
@@ -1388,7 +1391,7 @@ export const createRoot = (
1388
1391
  for (const api in newCtx.component.apis) {
1389
1392
  // check if the api has changed (ignoring onCompleted and onFailed).
1390
1393
  const apiInstance = newCtx.component.apis[api]
1391
- const previousApiInstance = ctx?.component.apis[api]
1394
+ const previousApiInstance = ctx?.component.apis?.[api]
1392
1395
  if (isLegacyApi(apiInstance)) {
1393
1396
  if (
1394
1397
  fastDeepEqual(
@@ -1405,7 +1408,7 @@ export const createRoot = (
1405
1408
  Apis: omitKeys(data.Apis ?? {}, [
1406
1409
  ...Object.keys(data.Apis ?? {}).filter(
1407
1410
  // remove any data from an api that is not part of the component
1408
- (key) => !newCtx.component.apis[key],
1411
+ (key) => !newCtx.component.apis?.[key],
1409
1412
  ),
1410
1413
  api,
1411
1414
  ]),
@@ -1605,7 +1608,7 @@ export const createRoot = (
1605
1608
  const updateConditionalElements = () => {
1606
1609
  const displayedNodes: string[] = []
1607
1610
  if (selectedNodeId && component) {
1608
- const root = component.nodes.root
1611
+ const root = component.nodes?.root
1609
1612
  if (root) {
1610
1613
  const nodeLookup = getNodeAndAncestors(component, root, selectedNodeId)
1611
1614
  if (isNodeOrAncestorConditional(nodeLookup)) {
@@ -1721,7 +1724,7 @@ function getNodeId(component: Component, path: string[]) {
1721
1724
  if (nextChild === undefined || currentId === undefined) {
1722
1725
  return currentId ?? null
1723
1726
  }
1724
- const currentNode = component.nodes[currentId]
1727
+ const currentNode = component.nodes?.[currentId]
1725
1728
  if (!currentNode?.children) {
1726
1729
  return null
1727
1730
  }
package/src/page.main.ts CHANGED
@@ -162,7 +162,7 @@ export const createRoot = (domNode: HTMLElement) => {
162
162
  ...window.toddle.pageState,
163
163
  // Re-initialize variables since some of them might rely on client-side
164
164
  // state (e.g. localStorage, sensors etc.)
165
- Variables: mapObject(component.variables, ([name, variable]) => [
165
+ Variables: mapObject(component.variables ?? {}, ([name, variable]) => [
166
166
  name,
167
167
  applyFormula(variable.initialValue, {
168
168
  data: window.toddle.pageState,
@@ -213,17 +213,19 @@ export const createRoot = (domNode: HTMLElement) => {
213
213
  }
214
214
 
215
215
  // Note: this function must run procedurally to ensure apis (which are in correct order) can reference each other
216
- sortApiObjects(Object.entries(component.apis)).forEach(([name, api]) => {
217
- if (isLegacyApi(api)) {
218
- ctx.apis[name] = createLegacyAPI(api, ctx)
219
- } else {
220
- ctx.apis[name] = createAPI({
221
- apiRequest: api,
222
- ctx,
223
- componentData: dataSignal.get(),
224
- })
225
- }
226
- })
216
+ sortApiObjects(Object.entries(component.apis ?? {})).forEach(
217
+ ([name, api]) => {
218
+ if (isLegacyApi(api)) {
219
+ ctx.apis[name] = createLegacyAPI(api, ctx)
220
+ } else {
221
+ ctx.apis[name] = createAPI({
222
+ apiRequest: api,
223
+ ctx,
224
+ componentData: dataSignal.get(),
225
+ })
226
+ }
227
+ },
228
+ )
227
229
  // Trigger actions for all APIs after all of them are created.
228
230
  Object.values(ctx.apis)
229
231
  .filter(isContextApiV2)
@@ -1,4 +1,5 @@
1
1
  import type { MediaQuery } from '@nordcraft/core/dist/component/component.types'
2
+ import type { Nullable } from '@nordcraft/core/dist/types'
2
3
 
3
4
  /**
4
5
  * CustomPropertyStyleSheet is a utility class that manages CSS custom properties
@@ -15,7 +16,10 @@ export class CustomPropertyStyleSheet {
15
16
  // Selector to rule index mapping
16
17
  private ruleMap: Map<string, CSSStyleRule | CSSNestedDeclarations> | undefined
17
18
 
18
- constructor(root: Document | ShadowRoot, styleSheet?: CSSStyleSheet | null) {
19
+ constructor(
20
+ root: Document | ShadowRoot,
21
+ styleSheet?: Nullable<CSSStyleSheet>,
22
+ ) {
19
23
  if (styleSheet) {
20
24
  this.styleSheet = styleSheet
21
25
  } else {
@@ -30,10 +34,10 @@ export class CustomPropertyStyleSheet {
30
34
  public registerProperty(
31
35
  selector: string,
32
36
  name: string,
33
- options?: {
34
- mediaQuery?: MediaQuery
35
- startingStyle?: boolean
36
- },
37
+ options?: Nullable<{
38
+ mediaQuery?: Nullable<MediaQuery>
39
+ startingStyle?: Nullable<boolean>
40
+ }>,
37
41
  ): (newValue: string) => void {
38
42
  this.ruleMap ??= this.hydrateFromBase()
39
43
  const fullSelector = CustomPropertyStyleSheet.getFullSelector(
@@ -69,11 +73,11 @@ export class CustomPropertyStyleSheet {
69
73
  public unregisterProperty(
70
74
  selector: string,
71
75
  name: string,
72
- options?: {
73
- mediaQuery?: MediaQuery
74
- startingStyle?: boolean
75
- deepClean?: boolean
76
- },
76
+ options?: Nullable<{
77
+ mediaQuery?: Nullable<MediaQuery>
78
+ startingStyle?: Nullable<boolean>
79
+ deepClean?: Nullable<boolean>
80
+ }>,
77
81
  ): void {
78
82
  if (!this.ruleMap) {
79
83
  return
@@ -161,10 +165,10 @@ export class CustomPropertyStyleSheet {
161
165
 
162
166
  private static getFullSelector(
163
167
  selector: string,
164
- options?: {
165
- mediaQuery?: MediaQuery
166
- startingStyle?: boolean
167
- },
168
+ options?: Nullable<{
169
+ mediaQuery?: Nullable<MediaQuery>
170
+ startingStyle?: Nullable<boolean>
171
+ }>,
168
172
  ) {
169
173
  let result =
170
174
  selector + (options?.startingStyle ? ' { @starting-style { }}' : ' { }')
@@ -38,7 +38,7 @@ export const getNodeAndAncestors = (
38
38
  nodeId: path.slice(0, i + 1).join('.'),
39
39
  })
40
40
  }
41
- return component.nodes[node.children[childIndex]]
41
+ return component.nodes?.[node.children[childIndex]]
42
42
  default:
43
43
  return undefined
44
44
  }