@nordcraft/runtime 1.0.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.
Files changed (168) hide show
  1. package/README.md +5 -0
  2. package/dist/api/createAPI.d.ts +20 -0
  3. package/dist/api/createAPI.js +319 -0
  4. package/dist/api/createAPI.js.map +1 -0
  5. package/dist/api/createAPIv2.d.ts +7 -0
  6. package/dist/api/createAPIv2.js +686 -0
  7. package/dist/api/createAPIv2.js.map +1 -0
  8. package/dist/components/createComponent.d.ts +13 -0
  9. package/dist/components/createComponent.js +216 -0
  10. package/dist/components/createComponent.js.map +1 -0
  11. package/dist/components/createElement.d.ts +3 -0
  12. package/dist/components/createElement.js +208 -0
  13. package/dist/components/createElement.js.map +1 -0
  14. package/dist/components/createNode.d.ts +22 -0
  15. package/dist/components/createNode.js +272 -0
  16. package/dist/components/createNode.js.map +1 -0
  17. package/dist/components/createSlot.d.ts +3 -0
  18. package/dist/components/createSlot.js +49 -0
  19. package/dist/components/createSlot.js.map +1 -0
  20. package/dist/components/createText.d.ts +23 -0
  21. package/dist/components/createText.js +68 -0
  22. package/dist/components/createText.js.map +1 -0
  23. package/dist/components/createText.test.d.ts +1 -0
  24. package/dist/components/createText.test.js +113 -0
  25. package/dist/components/createText.test.js.map +1 -0
  26. package/dist/components/renderComponent.d.ts +34 -0
  27. package/dist/components/renderComponent.js +66 -0
  28. package/dist/components/renderComponent.js.map +1 -0
  29. package/dist/context/isContextProvider.d.ts +2 -0
  30. package/dist/context/isContextProvider.js +5 -0
  31. package/dist/context/isContextProvider.js.map +1 -0
  32. package/dist/context/subscribeToContext.d.ts +4 -0
  33. package/dist/context/subscribeToContext.js +93 -0
  34. package/dist/context/subscribeToContext.js.map +1 -0
  35. package/dist/custom-components/components.d.ts +1 -0
  36. package/dist/custom-components/components.js +2 -0
  37. package/dist/custom-components/components.js.map +1 -0
  38. package/dist/custom-components/toddle-portal.d.ts +6 -0
  39. package/dist/custom-components/toddle-portal.js +20 -0
  40. package/dist/custom-components/toddle-portal.js.map +1 -0
  41. package/dist/custom-element/ToddleComponent.d.ts +37 -0
  42. package/dist/custom-element/ToddleComponent.js +244 -0
  43. package/dist/custom-element/ToddleComponent.js.map +1 -0
  44. package/dist/custom-element/defineComponents.d.ts +26 -0
  45. package/dist/custom-element/defineComponents.js +42 -0
  46. package/dist/custom-element/defineComponents.js.map +1 -0
  47. package/dist/custom-element.main.d.ts +3 -0
  48. package/dist/custom-element.main.esm.js +266 -0
  49. package/dist/custom-element.main.esm.js.map +7 -0
  50. package/dist/custom-element.main.js +14 -0
  51. package/dist/custom-element.main.js.map +1 -0
  52. package/dist/debug/logState.d.ts +4 -0
  53. package/dist/debug/logState.js +19 -0
  54. package/dist/debug/logState.js.map +1 -0
  55. package/dist/editor/drag-drop/dragEnded.d.ts +2 -0
  56. package/dist/editor/drag-drop/dragEnded.js +56 -0
  57. package/dist/editor/drag-drop/dragEnded.js.map +1 -0
  58. package/dist/editor/drag-drop/dragMove.d.ts +3 -0
  59. package/dist/editor/drag-drop/dragMove.js +74 -0
  60. package/dist/editor/drag-drop/dragMove.js.map +1 -0
  61. package/dist/editor/drag-drop/dragReorder.d.ts +3 -0
  62. package/dist/editor/drag-drop/dragReorder.js +92 -0
  63. package/dist/editor/drag-drop/dragReorder.js.map +1 -0
  64. package/dist/editor/drag-drop/dragStarted.d.ts +9 -0
  65. package/dist/editor/drag-drop/dragStarted.js +100 -0
  66. package/dist/editor/drag-drop/dragStarted.js.map +1 -0
  67. package/dist/editor/drag-drop/dropHighlight.d.ts +16 -0
  68. package/dist/editor/drag-drop/dropHighlight.js +50 -0
  69. package/dist/editor/drag-drop/dropHighlight.js.map +1 -0
  70. package/dist/editor/drag-drop/getInsertAreas.d.ts +20 -0
  71. package/dist/editor/drag-drop/getInsertAreas.js +220 -0
  72. package/dist/editor/drag-drop/getInsertAreas.js.map +1 -0
  73. package/dist/editor-preview.main.d.ts +19 -0
  74. package/dist/editor-preview.main.js +1303 -0
  75. package/dist/editor-preview.main.js.map +1 -0
  76. package/dist/events/handleAction.d.ts +3 -0
  77. package/dist/events/handleAction.js +307 -0
  78. package/dist/events/handleAction.js.map +1 -0
  79. package/dist/page.main.d.ts +7 -0
  80. package/dist/page.main.esm.js +8 -0
  81. package/dist/page.main.esm.js.map +7 -0
  82. package/dist/page.main.js +395 -0
  83. package/dist/page.main.js.map +1 -0
  84. package/dist/signal/signal.d.ts +19 -0
  85. package/dist/signal/signal.js +65 -0
  86. package/dist/signal/signal.js.map +1 -0
  87. package/dist/styles/style.d.ts +4 -0
  88. package/dist/styles/style.js +196 -0
  89. package/dist/styles/style.js.map +1 -0
  90. package/dist/utils/BatchQueue.d.ts +10 -0
  91. package/dist/utils/BatchQueue.js +25 -0
  92. package/dist/utils/BatchQueue.js.map +1 -0
  93. package/dist/utils/createFormulaCache.d.ts +3 -0
  94. package/dist/utils/createFormulaCache.js +81 -0
  95. package/dist/utils/createFormulaCache.js.map +1 -0
  96. package/dist/utils/findNearestLine.d.ts +13 -0
  97. package/dist/utils/findNearestLine.js +74 -0
  98. package/dist/utils/findNearestLine.js.map +1 -0
  99. package/dist/utils/findNearestLine.test.d.ts +1 -0
  100. package/dist/utils/findNearestLine.test.js +59 -0
  101. package/dist/utils/findNearestLine.test.js.map +1 -0
  102. package/dist/utils/getDragData.d.ts +1 -0
  103. package/dist/utils/getDragData.js +10 -0
  104. package/dist/utils/getDragData.js.map +1 -0
  105. package/dist/utils/getElementTagName.d.ts +3 -0
  106. package/dist/utils/getElementTagName.js +7 -0
  107. package/dist/utils/getElementTagName.js.map +1 -0
  108. package/dist/utils/nodes.d.ts +21 -0
  109. package/dist/utils/nodes.js +89 -0
  110. package/dist/utils/nodes.js.map +1 -0
  111. package/dist/utils/omitStyle.d.ts +2 -0
  112. package/dist/utils/omitStyle.js +13 -0
  113. package/dist/utils/omitStyle.js.map +1 -0
  114. package/dist/utils/rectHasPoint.d.ts +2 -0
  115. package/dist/utils/rectHasPoint.js +4 -0
  116. package/dist/utils/rectHasPoint.js.map +1 -0
  117. package/dist/utils/setAttribute.d.ts +4 -0
  118. package/dist/utils/setAttribute.js +57 -0
  119. package/dist/utils/setAttribute.js.map +1 -0
  120. package/dist/utils/tryStartViewTransition.d.ts +5 -0
  121. package/dist/utils/tryStartViewTransition.js +14 -0
  122. package/dist/utils/tryStartViewTransition.js.map +1 -0
  123. package/dist/utils/url.d.ts +2 -0
  124. package/dist/utils/url.js +36 -0
  125. package/dist/utils/url.js.map +1 -0
  126. package/package.json +25 -0
  127. package/src/api/createAPI.ts +375 -0
  128. package/src/api/createAPIv2.ts +931 -0
  129. package/src/components/createComponent.ts +280 -0
  130. package/src/components/createElement.ts +240 -0
  131. package/src/components/createNode.ts +381 -0
  132. package/src/components/createSlot.ts +61 -0
  133. package/src/components/createText.test.ts +117 -0
  134. package/src/components/createText.ts +104 -0
  135. package/src/components/renderComponent.ts +145 -0
  136. package/src/context/isContextProvider.ts +12 -0
  137. package/src/context/subscribeToContext.ts +135 -0
  138. package/src/custom-components/components.ts +1 -0
  139. package/src/custom-components/toddle-portal.ts +19 -0
  140. package/src/custom-element/ToddleComponent.ts +315 -0
  141. package/src/custom-element/defineComponents.ts +65 -0
  142. package/src/custom-element.main.ts +24 -0
  143. package/src/debug/logState.ts +30 -0
  144. package/src/editor/drag-drop/dragEnded.ts +75 -0
  145. package/src/editor/drag-drop/dragMove.ts +95 -0
  146. package/src/editor/drag-drop/dragReorder.ts +137 -0
  147. package/src/editor/drag-drop/dragStarted.ts +145 -0
  148. package/src/editor/drag-drop/dropHighlight.ts +82 -0
  149. package/src/editor/drag-drop/getInsertAreas.ts +235 -0
  150. package/src/editor/types.d.ts +36 -0
  151. package/src/editor-preview.main.ts +1782 -0
  152. package/src/events/handleAction.ts +387 -0
  153. package/src/page.main.ts +489 -0
  154. package/src/signal/signal.ts +74 -0
  155. package/src/styles/style.ts +254 -0
  156. package/src/types.d.ts +93 -0
  157. package/src/utils/BatchQueue.ts +24 -0
  158. package/src/utils/createFormulaCache.ts +96 -0
  159. package/src/utils/findNearestLine.test.ts +65 -0
  160. package/src/utils/findNearestLine.ts +92 -0
  161. package/src/utils/getDragData.ts +11 -0
  162. package/src/utils/getElementTagName.ts +14 -0
  163. package/src/utils/nodes.ts +125 -0
  164. package/src/utils/omitStyle.ts +19 -0
  165. package/src/utils/rectHasPoint.ts +5 -0
  166. package/src/utils/setAttribute.ts +56 -0
  167. package/src/utils/tryStartViewTransition.ts +32 -0
  168. package/src/utils/url.ts +45 -0
@@ -0,0 +1,66 @@
1
+ import deepEqual from 'fast-deep-equal';
2
+ import { handleAction } from '../events/handleAction';
3
+ import { BatchQueue } from '../utils/BatchQueue';
4
+ import { createNode } from './createNode';
5
+ const BATCH_QUEUE = new BatchQueue();
6
+ export function renderComponent({ component, dataSignal, onEvent, isRootComponent, path, children, formulaCache, components, apis, abortSignal, root, providers, package: packageName, parentElement, instance, toddle, namespace, env, }) {
7
+ const ctx = {
8
+ triggerEvent: onEvent,
9
+ component,
10
+ components,
11
+ dataSignal,
12
+ isRootComponent,
13
+ apis,
14
+ formulaCache,
15
+ children,
16
+ abortSignal,
17
+ root,
18
+ providers,
19
+ package: packageName,
20
+ toddle,
21
+ env,
22
+ };
23
+ const rootElem = createNode({
24
+ id: 'root',
25
+ path,
26
+ dataSignal,
27
+ ctx,
28
+ parentElement,
29
+ namespace,
30
+ instance,
31
+ });
32
+ BATCH_QUEUE.add(() => {
33
+ let prev;
34
+ if (component.onAttributeChange?.actions &&
35
+ component.onAttributeChange.actions.length > 0) {
36
+ dataSignal
37
+ .map((data) => data.Attributes)
38
+ .subscribe((props) => {
39
+ if (prev) {
40
+ component.onAttributeChange?.actions.forEach((action) => {
41
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
42
+ handleAction(action, dataSignal.get(), ctx, new CustomEvent('attribute-change', {
43
+ detail: Object.entries(props).reduce((changes, [key, value]) => {
44
+ if (deepEqual(value, prev[key]) === false &&
45
+ component.attributes[key]?.name) {
46
+ changes[component.attributes[key]?.name] = {
47
+ current: prev[key],
48
+ new: value,
49
+ };
50
+ }
51
+ return changes;
52
+ }, {}),
53
+ }));
54
+ });
55
+ }
56
+ prev = props;
57
+ });
58
+ }
59
+ component.onLoad?.actions.forEach((action) => {
60
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
61
+ handleAction(action, dataSignal.get(), ctx);
62
+ });
63
+ });
64
+ return rootElem;
65
+ }
66
+ //# sourceMappingURL=renderComponent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"renderComponent.js","sourceRoot":"","sources":["../../src/components/renderComponent.ts"],"names":[],"mappings":"AAOA,OAAO,SAAS,MAAM,iBAAiB,CAAA;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAA;AASrD,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AA8BzC,MAAM,WAAW,GAAG,IAAI,UAAU,EAAE,CAAA;AAEpC,MAAM,UAAU,eAAe,CAAC,EAC9B,SAAS,EACT,UAAU,EACV,OAAO,EACP,eAAe,EACf,IAAI,EACJ,QAAQ,EACR,YAAY,EACZ,UAAU,EACV,IAAI,EACJ,WAAW,EACX,IAAI,EACJ,SAAS,EACT,OAAO,EAAE,WAAW,EACpB,aAAa,EACb,QAAQ,EACR,MAAM,EACN,SAAS,EACT,GAAG,GACkB;IACrB,MAAM,GAAG,GAAqB;QAC5B,YAAY,EAAE,OAAO;QACrB,SAAS;QACT,UAAU;QACV,UAAU;QACV,eAAe;QACf,IAAI;QACJ,YAAY;QACZ,QAAQ;QACR,WAAW;QACX,IAAI;QACJ,SAAS;QACT,OAAO,EAAE,WAAW;QACpB,MAAM;QACN,GAAG;KACJ,CAAA;IAED,MAAM,QAAQ,GAAG,UAAU,CAAC;QAC1B,EAAE,EAAE,MAAM;QACV,IAAI;QACJ,UAAU;QACV,GAAG;QACH,aAAa;QACb,SAAS;QACT,QAAQ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AO,CAAC,CAAC,MAAM,EAAE,EAAE;YAC3C,mEAAmE;YACnE,YAAY,CAAC,MAAM,EAAE,UAAU,CAAC,GAAG,EAAE,EAAE,GAAG,CAAC,CAAA;QAC7C,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IACF,OAAO,QAAQ,CAAA;AACjB,CAAC"}
@@ -0,0 +1,2 @@
1
+ import type { Component } from '@nordcraft/core/dist/component/component.types';
2
+ export declare const isContextProvider: (component: Component) => boolean | undefined;
@@ -0,0 +1,5 @@
1
+ export const isContextProvider = (component) => (component.formulas &&
2
+ Object.values(component.formulas).some(({ exposeInContext }) => exposeInContext)) ||
3
+ (component.workflows &&
4
+ Object.values(component.workflows).some(({ exposeInContext }) => exposeInContext));
5
+ //# sourceMappingURL=isContextProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isContextProvider.js","sourceRoot":"","sources":["../../src/context/isContextProvider.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,SAAoB,EAAE,EAAE,CACxD,CAAC,SAAS,CAAC,QAAQ;IACjB,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,IAAI,CACpC,CAAC,EAAE,eAAe,EAAE,EAAE,EAAE,CAAC,eAAe,CACzC,CAAC;IACJ,CAAC,SAAS,CAAC,SAAS;QAClB,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,IAAI,CACrC,CAAC,EAAE,eAAe,EAAE,EAAE,EAAE,CAAC,eAAe,CACzC,CAAC,CAAA"}
@@ -0,0 +1,4 @@
1
+ import type { Component, ComponentData } from '@nordcraft/core/dist/component/component.types';
2
+ import type { Signal } from '../signal/signal';
3
+ import type { ComponentContext } from '../types';
4
+ export declare function subscribeToContext(componentDataSignal: Signal<ComponentData>, component: Component, ctx: ComponentContext): void;
@@ -0,0 +1,93 @@
1
+ import { applyFormula } from '@nordcraft/core/dist/formula/formula';
2
+ import { mapObject } from '@nordcraft/core/dist/utils/collections';
3
+ import { isDefined } from '@nordcraft/core/dist/utils/util';
4
+ export function subscribeToContext(componentDataSignal, component, ctx) {
5
+ Object.entries(component.contexts ?? {}).forEach(([providerName, context]) => {
6
+ const provider = ctx.providers[[ctx.package, providerName].filter(isDefined).join('/')];
7
+ if (provider) {
8
+ context.formulas.forEach((formulaName) => {
9
+ const formulaDataSignal = provider.formulaDataSignals[formulaName];
10
+ if (!formulaDataSignal) {
11
+ // eslint-disable-next-line no-console
12
+ console.warn(`Provider ${providerName} does not expose a formula named "${formulaName}". Available formulas are: ["${Object.keys(provider.formulaDataSignals).join('", "')}"]`);
13
+ return;
14
+ }
15
+ formulaDataSignal.subscribe((value) => {
16
+ componentDataSignal.update((data) => ({
17
+ ...data,
18
+ Contexts: {
19
+ ...data.Contexts,
20
+ [providerName]: {
21
+ ...data.Contexts?.[providerName],
22
+ [formulaName]: value,
23
+ },
24
+ },
25
+ }));
26
+ });
27
+ });
28
+ }
29
+ // In preview and absence of a real provider, we fake providers with their testData values. This is useful for testing components in isolation.
30
+ // This is for preview mode only, and should preferably be stripped from the page and custom-elements runtime.
31
+ else if (!provider &&
32
+ ctx.env.runtime === 'preview' &&
33
+ ctx.toddle._preview) {
34
+ const testProvider = ctx.components?.find((comp) => comp.name ===
35
+ [ctx.package, providerName].filter(isDefined).join('/'));
36
+ if (!testProvider) {
37
+ // eslint-disable-next-line no-console
38
+ console.error(`Could not find provider "${providerName}". No such component exist.`);
39
+ return;
40
+ }
41
+ // Derive the package name from the provider name as we do not have a real component to work with
42
+ const [, testProviderPackage] = providerName.split('/').reverse();
43
+ const formulaContext = {
44
+ data: {
45
+ Attributes: mapObject(testProvider.attributes, ([name, attr]) => [
46
+ name,
47
+ attr.testValue,
48
+ ]),
49
+ },
50
+ component: testProvider,
51
+ root: ctx?.root,
52
+ formulaCache: {},
53
+ package: testProviderPackage ?? ctx?.package,
54
+ toddle: ctx.toddle,
55
+ env: ctx.env,
56
+ };
57
+ if (testProvider.route) {
58
+ formulaContext.data['URL parameters'] = {
59
+ ...Object.fromEntries(testProvider.route.path
60
+ .filter((p) => p.type === 'param')
61
+ .map((p) => [p.name, p.testValue])),
62
+ ...mapObject(testProvider.route.query, ([name, { testValue }]) => [
63
+ name,
64
+ testValue,
65
+ ]),
66
+ };
67
+ }
68
+ formulaContext.data.Variables = mapObject(testProvider.variables, ([name, variable]) => [
69
+ name,
70
+ applyFormula(variable.initialValue, formulaContext),
71
+ ]);
72
+ componentDataSignal.update((data) => ({
73
+ ...data,
74
+ Contexts: {
75
+ ...data.Contexts,
76
+ [providerName]: Object.fromEntries(context.formulas.map((formulaName) => {
77
+ const formula = testProvider.formulas?.[formulaName];
78
+ if (!formula) {
79
+ // eslint-disable-next-line no-console
80
+ console.warn(`Could not find formula "${formulaName}" in component "${providerName}"`);
81
+ return [formulaName, null];
82
+ }
83
+ return [
84
+ formulaName,
85
+ applyFormula(formula.formula, formulaContext),
86
+ ];
87
+ })),
88
+ },
89
+ }));
90
+ }
91
+ });
92
+ }
93
+ //# sourceMappingURL=subscribeToContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subscribeToContext.js","sourceRoot":"","sources":["../../src/context/subscribeToContext.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,YAAY,EAAE,MAAM,sCAAsC,CAAA;AACnE,OAAO,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAA;AAClE,OAAO,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAA;AAI3D,MAAM,UAAU,kBAAkB,CAChC,mBAA0C,EAC1C,SAAoB,EACpB,GAAqB;IAErB,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC,OAAO,CAC9C,CAAC,CAAC,YAAY,EAAE,OAAO,CAAC,EAAE,EAAE;QAC1B,MAAM,QAAQ,GACZ,GAAG,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,OAAO,EAAE,YAAY,CAAC,CAA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E,IAAI;wBACJ,SAAS;qBACV,CAAC;iBACH,CAAA;YACH,CAAC;YACD,cAAc,CAAC,IAAI,CAAC,SAAS,GAAG,SAAS,CACvC,YAAY,CAAC,SAAS,EACtB,CAAC,CAAC,IAAI,EAAE,QAAQ,CAAC,EAAE,EAAE,CAAC;gBACpB,IAAI;gBACJ,YAAY,CAAC,QAAQ,CAAC,YAAY,EAAE,cAAc,CAAC;aACpD,CACF,CAAA;YAED,mBAAmB,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;gBACpC,GAAG,IAAI;gBACP,QAAQ,EAAE;oBACR,GAAG,IAAI,CAAC,QAAQ;oBAChB,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC,WAAW,CAChC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE,EAAE;wBACnC,MAAM,OAAO,GAAG,YAAY,CAAC,QAAQ,EAAE,CAAC,WAAW,CAAC,CAAA;wBACpD,IAAI,CAAC,OAAO,EAAE,CAAC;4BACb,sCAAsC;4BACtC,OAAO,CAAC,IAAI,CACV,2BAA2B,WAAW,mBAAmB,YAAY,GAAG,CACzE,CAAA;4BACD,OAAO,CAAC,WAAW,EAAE,IAAI,CAAC,CAAA;wBAC5B,CAAC;wBAED,OAAO;4BACL,WAAW;4BACX,YAAY,CAAC,OAAO,CAAC,OAAO,EAAE,cAAc,CAAC;yBAC9C,CAAA;oBACH,CAAC,CAAC,CACH;iBACF;aACF,CAAC,CAAC,CAAA;QACL,CAAC;IACH,CAAC,CACF,CAAA;AACH,CAAC"}
@@ -0,0 +1 @@
1
+ import './toddle-portal';
@@ -0,0 +1,2 @@
1
+ import './toddle-portal';
2
+ //# sourceMappingURL=components.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"components.js","sourceRoot":"","sources":["../../src/custom-components/components.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,CAAA"}
@@ -0,0 +1,6 @@
1
+ declare class Portal extends HTMLElement {
2
+ root: HTMLElement;
3
+ constructor();
4
+ connectedCallback(): void;
5
+ disconnectedCallback(): void;
6
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ class Portal extends HTMLElement {
3
+ root;
4
+ constructor() {
5
+ // Always call super first in constructor
6
+ super();
7
+ this.root = document.createElement('div');
8
+ }
9
+ connectedCallback() {
10
+ for (const child of Array.from(this.children)) {
11
+ this.root.appendChild(child);
12
+ }
13
+ document.body.appendChild(this.root);
14
+ }
15
+ disconnectedCallback() {
16
+ this.root?.remove();
17
+ }
18
+ }
19
+ customElements.define('toddle-portal', Portal);
20
+ //# sourceMappingURL=toddle-portal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toddle-portal.js","sourceRoot":"","sources":["../../src/custom-components/toddle-portal.ts"],"names":[],"mappings":";AAAA,MAAM,MAAO,SAAQ,WAAW;IAC9B,IAAI,CAAa;IACjB;QACE,yCAAyC;QACzC,KAAK,EAAE,CAAA;QACP,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAA;IAC3C,CAAC;IAED,iBAAiB;QACf,KAAK,MAAM,KAAK,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC9C,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,CAAA;QAC9B,CAAC;QACD,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACtC,CAAC;IACD,oBAAoB;QAClB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,CAAA;IACrB,CAAC;CACF;AACD,cAAc,CAAC,MAAM,CAAC,eAAe,EAAE,MAAM,CAAC,CAAA"}
@@ -0,0 +1,37 @@
1
+ import type { Component, ComponentData } from '@nordcraft/core/dist/component/component.types';
2
+ import type { ToddleEnv } from '@nordcraft/core/dist/formula/formula';
3
+ import type { Theme } from '@nordcraft/core/dist/styling/theme';
4
+ import type { Toddle } from '@nordcraft/core/dist/types';
5
+ import type { Signal } from '../signal/signal';
6
+ import type { ComponentContext, LocationSignal } from '../types';
7
+ /**
8
+ * Base class for all toddle components
9
+ */
10
+ export declare class ToddleComponent extends HTMLElement {
11
+ #private;
12
+ /**
13
+ * Public reference to the toddle instance for debugging purposes. `el.toddle.errors` can be used to check for non-verbose errors.
14
+ */
15
+ toddle: Toddle<LocationSignal, never>;
16
+ constructor(component: Component, options: {
17
+ components: Component[];
18
+ themes: Theme[];
19
+ }, toddle: Toddle<LocationSignal, never>);
20
+ connectedCallback(): void;
21
+ disconnectedCallback(): void;
22
+ dispatch(eventName: string, data: any): void;
23
+ render(): void;
24
+ setAttribute(name: string, value: unknown): this;
25
+ getAttribute<T>(name: string): string | NonNullable<T> | null;
26
+ attributeChangedCallback(name: string, oldValue: never, newValue: string): void;
27
+ private getAttributeCaseInsensitive;
28
+ get __component(): Component;
29
+ get __ctx(): ComponentContext;
30
+ get __signal(): Signal<ComponentData>;
31
+ }
32
+ export declare const createSignal: ({ component, root, toddle, env, }: {
33
+ component: Component;
34
+ root: ShadowRoot;
35
+ toddle: Toddle<LocationSignal, never>;
36
+ env: ToddleEnv;
37
+ }) => Signal<ComponentData>;
@@ -0,0 +1,244 @@
1
+ import { isLegacyApi, sortApiObjects } from '@nordcraft/core/dist/api/api';
2
+ import { applyFormula } from '@nordcraft/core/dist/formula/formula';
3
+ import { createStylesheet } from '@nordcraft/core/dist/styling/style.css';
4
+ import { theme as defaultTheme } from '@nordcraft/core/dist/styling/theme.const';
5
+ import { mapObject } from '@nordcraft/core/dist/utils/collections';
6
+ import { createLegacyAPI } from '../api/createAPI';
7
+ import { createAPI } from '../api/createAPIv2';
8
+ import { renderComponent } from '../components/renderComponent';
9
+ import { isContextProvider } from '../context/isContextProvider';
10
+ import { signal } from '../signal/signal';
11
+ /**
12
+ * Base class for all toddle components
13
+ */
14
+ export class ToddleComponent extends HTMLElement {
15
+ /**
16
+ * Public reference to the toddle instance for debugging purposes. `el.toddle.errors` can be used to check for non-verbose errors.
17
+ */
18
+ toddle;
19
+ #component;
20
+ #ctx;
21
+ #shadowRoot;
22
+ #signal;
23
+ #files;
24
+ constructor(component, options, toddle) {
25
+ super();
26
+ this.toddle = toddle;
27
+ const internals = this.attachInternals();
28
+ if (internals.shadowRoot) {
29
+ // Not used yet, but can be used to hydrate rather than render the shadow dom
30
+ this.#shadowRoot = internals.shadowRoot;
31
+ }
32
+ else {
33
+ this.#shadowRoot = this.attachShadow({ mode: 'open' });
34
+ }
35
+ const env = {
36
+ branchName: toddle.branch || 'main',
37
+ isServer: false,
38
+ request: undefined,
39
+ runtime: 'custom-element',
40
+ logErrors: true,
41
+ };
42
+ this.#component = component;
43
+ this.#signal = createSignal({
44
+ component,
45
+ root: this.#shadowRoot,
46
+ toddle,
47
+ env,
48
+ });
49
+ this.#files = {
50
+ themes: options.themes,
51
+ };
52
+ // Call the abort signal if the component's datasignal is destroyed (component unmounted) to cancel any pending requests
53
+ const abortController = new AbortController();
54
+ this.#signal.subscribe(() => { }, {
55
+ destroy: () => abortController.abort(`Component ${component.name} unmounted`),
56
+ });
57
+ this.#ctx = {
58
+ triggerEvent: this.dispatch.bind(this),
59
+ root: this.#shadowRoot,
60
+ isRootComponent: true,
61
+ components: options.components,
62
+ component: this.#component,
63
+ dataSignal: this.#signal,
64
+ formulaCache: {},
65
+ apis: {},
66
+ abortSignal: abortController.signal,
67
+ children: {},
68
+ providers: {},
69
+ package: undefined,
70
+ toddle,
71
+ env,
72
+ };
73
+ }
74
+ connectedCallback() {
75
+ sortApiObjects(Object.entries(this.#component.apis)).forEach(([name, api]) => {
76
+ if (isLegacyApi(api)) {
77
+ this.#ctx.apis[name] = createLegacyAPI(api, this.#ctx);
78
+ }
79
+ else {
80
+ this.#ctx.apis[name] = createAPI(api, this.#ctx);
81
+ }
82
+ });
83
+ Object.values(this.#ctx.apis)
84
+ .filter((api) => api.triggerActions !== undefined)
85
+ .forEach((api) => {
86
+ api.triggerActions();
87
+ });
88
+ let providers = this.#ctx.providers;
89
+ if (isContextProvider(this.#component)) {
90
+ // Subscribe to exposed formulas and update the component's data signal
91
+ const formulaDataSignals = Object.fromEntries(Object.entries(this.#component.formulas ?? {})
92
+ .filter(([, formula]) => formula.exposeInContext)
93
+ .map(([name, formula]) => [
94
+ name,
95
+ this.#signal.map((data) => applyFormula(formula.formula, {
96
+ data,
97
+ component: this.#component,
98
+ formulaCache: this.#ctx.formulaCache,
99
+ root: this.#ctx.root,
100
+ package: this.#ctx.package,
101
+ toddle: this.#ctx.toddle,
102
+ env: this.#ctx.env,
103
+ })),
104
+ ]));
105
+ providers = {
106
+ ...providers,
107
+ [this.#component.name]: {
108
+ component: this.#component,
109
+ formulaDataSignals,
110
+ ctx: this.#ctx,
111
+ },
112
+ };
113
+ }
114
+ this.#ctx.providers = providers;
115
+ this.render();
116
+ }
117
+ disconnectedCallback() {
118
+ this.#signal.destroy();
119
+ }
120
+ dispatch(eventName, data) {
121
+ this.dispatchEvent(new CustomEvent(eventName, {
122
+ detail: data,
123
+ bubbles: true,
124
+ composed: true,
125
+ }));
126
+ }
127
+ render() {
128
+ const elements = renderComponent({
129
+ ...this.#ctx,
130
+ path: '0',
131
+ onEvent: this.dispatch.bind(this),
132
+ parentElement: this.#shadowRoot,
133
+ instance: { [this.#component.name]: 'root' },
134
+ });
135
+ this.#shadowRoot.innerHTML = '';
136
+ const styles = createStylesheet(this.#ctx.component, this.#ctx.components, this.#files.themes ? Object.values(this.#files.themes)[0] : defaultTheme, { includeResetStyle: true, createFontFaces: false });
137
+ const stylesElem = document.createElement('style');
138
+ stylesElem.appendChild(document.createTextNode(styles));
139
+ this.#shadowRoot.appendChild(stylesElem);
140
+ const rootElement = elements[0];
141
+ if (rootElement) {
142
+ this.#shadowRoot.appendChild(rootElement);
143
+ }
144
+ }
145
+ // Overload the setAttribute method to allow setting complex attributes
146
+ setAttribute(name, value) {
147
+ switch (typeof value) {
148
+ case 'number':
149
+ super.setAttribute(name, String(value));
150
+ break;
151
+ case 'string':
152
+ super.setAttribute(name, value);
153
+ // Return early, as signal is updated through attributeChangedCallback on string values
154
+ return this;
155
+ default:
156
+ super.setAttribute(name, `[Object ${typeof value}]`);
157
+ break;
158
+ }
159
+ // Update the signal with complex value
160
+ this.#signal.set({
161
+ ...this.#signal.get(),
162
+ Attributes: {
163
+ ...this.#signal.get().Attributes,
164
+ [name]: value,
165
+ },
166
+ });
167
+ return this;
168
+ }
169
+ // Overload the getAttribute method to point to source of truth (the signal)
170
+ getAttribute(name) {
171
+ return (this.#signal.get().Attributes[name] || super.getAttribute(name));
172
+ }
173
+ attributeChangedCallback(name, oldValue, newValue) {
174
+ const attributeName = this.getAttributeCaseInsensitive(name);
175
+ const currentRawValue = this.getAttribute(attributeName);
176
+ // Is this is a complex value that has already been set by `setAttribute`
177
+ if (newValue === '[Object object]') {
178
+ return;
179
+ }
180
+ //Has this just been set by `setAttribute` as a number
181
+ if (parseFloat(newValue) === currentRawValue) {
182
+ return;
183
+ }
184
+ this.#signal.set({
185
+ ...this.#signal.get(),
186
+ Attributes: {
187
+ ...this.#signal.get().Attributes,
188
+ [attributeName]: newValue,
189
+ },
190
+ });
191
+ }
192
+ getAttributeCaseInsensitive(name) {
193
+ const attributeName = Object.keys(this.#signal.get().Attributes).find((key) => key.toLowerCase() === name.toLowerCase());
194
+ // This should never happen (TM) as we only observe attributes that are defined on the component
195
+ if (!attributeName) {
196
+ throw new Error(`Unable to find attribute ${name} on component ${this.#component.name}`);
197
+ }
198
+ return attributeName;
199
+ }
200
+ // Debugging purposes
201
+ get __component() {
202
+ return this.#component;
203
+ }
204
+ get __ctx() {
205
+ return this.#ctx;
206
+ }
207
+ get __signal() {
208
+ return this.#signal;
209
+ }
210
+ }
211
+ export const createSignal = ({ component, root, toddle, env, }) => {
212
+ return signal({
213
+ // Pages are not supported as custom elements, so no need to add location signal
214
+ Location: undefined,
215
+ Variables: mapObject(component.variables, ([name, { initialValue }]) => {
216
+ if (!component) {
217
+ throw new Error(`Component not found`);
218
+ }
219
+ return [
220
+ name,
221
+ applyFormula(initialValue, {
222
+ data: {
223
+ Attributes: {},
224
+ },
225
+ component: component,
226
+ root,
227
+ package: undefined,
228
+ toddle,
229
+ env,
230
+ }),
231
+ ];
232
+ }),
233
+ Attributes: mapObject(component.attributes, ([name]) => [
234
+ name,
235
+ // TODO: Perhaps we can get it from the DOM already and set initial attributes already?
236
+ undefined,
237
+ ]),
238
+ Apis: mapObject(component.apis, ([name]) => [
239
+ name,
240
+ { data: null, isLoading: false, error: null },
241
+ ]),
242
+ });
243
+ };
244
+ //# sourceMappingURL=ToddleComponent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ToddleComponent.js","sourceRoot":"","sources":["../../src/custom-element/ToddleComponent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAA;AAM1E,OAAO,EAAE,YAAY,EAAE,MAAM,sCAAsC,CAAA;AACnE,OAAO,EAAE,gBAAgB,EAAE,MAAM,wCAAwC,CAAA;AAEzE,OAAO,EAAE,KAAK,IAAI,YAAY,EAAE,MAAM,0CAA0C,CAAA;AAEhF,OAAO,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAA;AAClE,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAC/D,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAA;AAEhE,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAGzC;;GAEG;AACH,MAAM,OAAO,eAAgB,SAAQ,WAAW;IAC9C;;OAEG;IACH,MAAM,CAA+B;IACrC,UAAU,CAAW;IACrB,IAAI,CAAkB;IACtB,WAAW,CAAY;IACvB,OAAO,CAAuB;IAC9B,MAAM,CAAqB;IAE3B,YAC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ACD,OAAO;gBACL,IAAI;gBACJ,YAAY,CAAC,YAAY,EAAE;oBACzB,IAAI,EAAE;wBACJ,UAAU,EAAE,EAAE;qBACf;oBACD,SAAS,EAAE,SAAS;oBACpB,IAAI;oBACJ,OAAO,EAAE,SAAS;oBAClB,MAAM;oBACN,GAAG;iBACJ,CAAC;aACH,CAAA;QACH,CAAC,CAAC;QACF,UAAU,EAAE,SAAS,CAAC,SAAS,CAAC,UAAU,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC;YACtD,IAAI;YACJ,uFAAuF;YACvF,SAAS;SACV,CAAC;QACF,IAAI,EAAE,SAAS,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC;YAC1C,IAAI;YACJ,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE;SAC9C,CAAC;KACH,CAAC,CAAA;AACJ,CAAC,CAAA"}
@@ -0,0 +1,26 @@
1
+ import type { Component } from '@nordcraft/core/dist/component/component.types';
2
+ import type { Theme } from '@nordcraft/core/dist/styling/theme';
3
+ import type { Toddle } from '@nordcraft/core/dist/types';
4
+ import type { LocationSignal } from '../types';
5
+ /**
6
+ * Define each component as a new web component
7
+ *
8
+ * Use them like: `<toddle-test my-attr="test" style="--my-color: rebeccapurple"></toddle-test>`
9
+ *
10
+ * You can access the component and `addEventListener` to it like:
11
+ *
12
+ * ```js
13
+ * const component = document.querySelector('toddle-test')
14
+ * component.addEventListener('my-event', (e) => {
15
+ * console.log(e.detail)
16
+ * })
17
+ * ```
18
+ *
19
+ * @param componentNames - The names of the components to define. These should be the names of the components in the app context
20
+ * @param options - A subset of the app context. This holds a list of all the components needed to define the components in `componentNames`
21
+ * @param toddle - Also available at `window.toddle`. However, multiple instances of toddle can exist on the same page, so we pass a reference here. We should ultimately remove the global scope reference as polite web components should be self-contained.
22
+ */
23
+ export declare const defineComponents: (componentNames: string[], options: {
24
+ components: Component[];
25
+ themes: Theme[];
26
+ }, toddle: Toddle<LocationSignal, never>) => void;
@@ -0,0 +1,42 @@
1
+ import { safeCustomElementName } from '@nordcraft/core/dist/utils/customElements';
2
+ import { ToddleComponent } from './ToddleComponent';
3
+ /**
4
+ * Define each component as a new web component
5
+ *
6
+ * Use them like: `<toddle-test my-attr="test" style="--my-color: rebeccapurple"></toddle-test>`
7
+ *
8
+ * You can access the component and `addEventListener` to it like:
9
+ *
10
+ * ```js
11
+ * const component = document.querySelector('toddle-test')
12
+ * component.addEventListener('my-event', (e) => {
13
+ * console.log(e.detail)
14
+ * })
15
+ * ```
16
+ *
17
+ * @param componentNames - The names of the components to define. These should be the names of the components in the app context
18
+ * @param options - A subset of the app context. This holds a list of all the components needed to define the components in `componentNames`
19
+ * @param toddle - Also available at `window.toddle`. However, multiple instances of toddle can exist on the same page, so we pass a reference here. We should ultimately remove the global scope reference as polite web components should be self-contained.
20
+ */
21
+ export const defineComponents = (componentNames, options, toddle) => {
22
+ componentNames
23
+ .map((name) => options.components.find((component) => component.name === name))
24
+ .forEach((component) => {
25
+ const tag = safeCustomElementName(component.name);
26
+ if (customElements.get(tag)) {
27
+ // eslint-disable-next-line no-console
28
+ console.warn(`Component ${tag} already defined`);
29
+ return;
30
+ }
31
+ customElements.define(tag, class extends ToddleComponent {
32
+ constructor() {
33
+ super(component, options, toddle);
34
+ }
35
+ // When read from DOM, all attributes are lower case, so we must observe them as such
36
+ static get observedAttributes() {
37
+ return Object.keys(component.attributes ?? {}).map((key) => key.toLowerCase());
38
+ }
39
+ });
40
+ });
41
+ };
42
+ //# sourceMappingURL=defineComponents.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defineComponents.js","sourceRoot":"","sources":["../../src/custom-element/defineComponents.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,qBAAqB,EAAE,MAAM,2CAA2C,CAAA;AAEjF,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAEnD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,cAAwB,EACxB,OAGC,EACD,MAAqC,EACrC,EAAE;IACF,cAAc;SACX,GAAG,CACF,CAAC,IAAI,EAAE,EAAE,CACP,OAAO,CAAC,UAAU,CAAC,IAAI,CACrB,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,IAAI,KAAK,IAAI,CAC1B,CACjB;SACA,OAAO,CAAC,CAAC,SAAS,EAAE,EAAE;QACrB,MAAM,GAAG,GAAG,qBAAqB,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA;QACjD,IAAI,cAAc,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YAC5B,sCAAsC;YACtC,OAAO,CAAC,IAAI,CAAC,aAAa,GAAG,kBAAkB,CAAC,CAAA;YAChD,OAAM;QACR,CAAC;QAED,cAAc,CAAC,MAAM,CACnB,GAAG,EACH,KAAM,SAAQ,eAAe;YAC3B;gBACE,KAAK,CAAC,SAAS,EAAE,OAAO,EAAE,MAAM,CAAC,CAAA;YACnC,CAAC;YAED,qFAAqF;YACrF,MAAM,KAAK,kBAAkB;gBAC3B,OAAO,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CACzD,GAAG,CAAC,WAAW,EAAE,CAClB,CAAA;YACH,CAAC;SACF,CACF,CAAA;IACH,CAAC,CAAC,CAAA;AACN,CAAC,CAAA"}
@@ -0,0 +1,3 @@
1
+ import { defineComponents } from './custom-element/defineComponents';
2
+ declare const loadCorePlugins: (toddle?: import("@nordcraft/core/dist/types").Toddle<import("./types").LocationSignal, import("./types").PreviewShowSignal>) => void;
3
+ export { defineComponents, loadCorePlugins };