@nordcraft/runtime 1.0.39 → 1.0.41

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/package.json CHANGED
@@ -4,8 +4,8 @@
4
4
  "type": "module",
5
5
  "homepage": "https://github.com/nordcraftengine/nordcraft",
6
6
  "dependencies": {
7
- "@nordcraft/core": "1.0.39",
8
- "@nordcraft/std-lib": "1.0.39",
7
+ "@nordcraft/core": "1.0.41",
8
+ "@nordcraft/std-lib": "1.0.41",
9
9
  "fast-deep-equal": "3.1.3",
10
10
  "path-to-regexp": "6.3.0"
11
11
  },
@@ -21,5 +21,5 @@
21
21
  "files": ["dist", "src"],
22
22
  "main": "dist/page.main.js",
23
23
  "types": "dist/page.main.d.ts",
24
- "version": "1.0.39"
24
+ "version": "1.0.41"
25
25
  }
@@ -16,7 +16,10 @@ import type {
16
16
  ToddleEnv,
17
17
  } from '@nordcraft/core/dist/formula/formula'
18
18
  import { applyFormula } from '@nordcraft/core/dist/formula/formula'
19
- import type { PluginFormula } from '@nordcraft/core/dist/formula/formulaTypes'
19
+ import {
20
+ isToddleFormula,
21
+ type PluginFormula,
22
+ } from '@nordcraft/core/dist/formula/formulaTypes'
20
23
  import { valueFormula } from '@nordcraft/core/dist/formula/formulaUtils'
21
24
  import { getClassName } from '@nordcraft/core/dist/styling/className'
22
25
  import type { OldTheme, Theme } from '@nordcraft/core/dist/styling/theme'
@@ -31,6 +34,7 @@ import type {
31
34
  Toddle,
32
35
  } from '@nordcraft/core/dist/types'
33
36
  import { mapObject, omitKeys } from '@nordcraft/core/dist/utils/collections'
37
+ import type { PluginAction } from '@nordcraft/ssr/dist/ssr.types'
34
38
  import * as libActions from '@nordcraft/std-lib/dist/actions'
35
39
  import * as libFormulas from '@nordcraft/std-lib/dist/formulas'
36
40
  import fastDeepEqual from 'fast-deep-equal'
@@ -85,6 +89,17 @@ type ToddlePreviewEvent =
85
89
  }
86
90
  >
87
91
  }
92
+ | {
93
+ type: 'global_formulas'
94
+ formulas: Record<
95
+ string,
96
+ PluginFormula<FormulaHandlerV2> | PluginFormula<string>
97
+ >
98
+ }
99
+ | {
100
+ type: 'global_actions'
101
+ actions: Record<string, PluginActionV2 | PluginAction>
102
+ }
88
103
  | { type: 'theme'; theme: Theme | OldTheme }
89
104
  | { type: 'mode'; mode: 'design' | 'test' }
90
105
  | { type: 'attrs'; attrs: Record<string, unknown> }
@@ -160,13 +175,7 @@ enum TextNodeComputedStyles {
160
175
 
161
176
  let env: ToddleEnv
162
177
 
163
- export const initGlobalObject = ({
164
- formulas,
165
- actions,
166
- }: {
167
- formulas: Record<string, Record<string, PluginFormula<FormulaHandlerV2>>>
168
- actions: Record<string, Record<string, PluginActionV2>>
169
- }) => {
178
+ export const initGlobalObject = () => {
170
179
  env = {
171
180
  isServer: false,
172
181
  branchName: window.__toddle.branch,
@@ -175,14 +184,14 @@ export const initGlobalObject = ({
175
184
  logErrors: true,
176
185
  }
177
186
  window.toddle = (() => {
178
- const legacyActions: Record<string, ActionHandler> = {}
179
- const legacyFormulas: Record<string, FormulaHandler> = {}
187
+ const legacyActions: Record<string, ActionHandler | undefined> = {}
188
+ const legacyFormulas: Record<string, FormulaHandler | undefined> = {}
180
189
  const argumentInputDataList: Record<string, ArgumentInputDataFunction> = {}
181
190
  const toddle: Toddle<LocationSignal, PreviewShowSignal> = {
182
191
  isEqual: fastDeepEqual,
183
192
  errors: [],
184
- formulas,
185
- actions,
193
+ formulas: {},
194
+ actions: {},
186
195
  registerAction: (name, handler) => {
187
196
  if (legacyActions[name]) {
188
197
  console.error('There already exists an action with the name ', name)
@@ -190,6 +199,13 @@ export const initGlobalObject = ({
190
199
  }
191
200
  legacyActions[name] = handler
192
201
  },
202
+ clearLegacyActions: () => {
203
+ Object.keys(legacyActions)
204
+ .filter((key) => !key.startsWith('@toddle/'))
205
+ .forEach((key) => {
206
+ delete legacyActions[key]
207
+ })
208
+ },
193
209
  getAction: (name) => legacyActions[name],
194
210
  registerFormula: (name, handler, getArgumentInputData) => {
195
211
  if (legacyFormulas[name]) {
@@ -201,6 +217,13 @@ export const initGlobalObject = ({
201
217
  argumentInputDataList[name] = getArgumentInputData
202
218
  }
203
219
  },
220
+ clearLegacyFormulas: () => {
221
+ Object.keys(legacyFormulas)
222
+ .filter((key) => !key.startsWith('@toddle/'))
223
+ .forEach((key) => {
224
+ delete legacyFormulas[key]
225
+ })
226
+ },
204
227
  getFormula: (name) => legacyFormulas[name],
205
228
  getCustomAction: (name, packageName) => {
206
229
  return (
@@ -415,6 +438,45 @@ export const createRoot = (
415
438
 
416
439
  break
417
440
  }
441
+ case 'global_formulas': {
442
+ const formulas: Record<string, PluginFormula<FormulaHandlerV2>> = {}
443
+ window.toddle.clearLegacyFormulas?.()
444
+ Object.entries(message.data.formulas ?? {}).forEach(
445
+ ([name, formula]) => {
446
+ if (
447
+ !isToddleFormula<FormulaHandlerV2 | string>(formula) &&
448
+ typeof formula.name === 'string' &&
449
+ formula.version === undefined
450
+ ) {
451
+ // Legacy formulas are self-registering. We need to execute them to register them
452
+ Function(formula.handler as unknown as string)()
453
+ return
454
+ }
455
+ formulas[name] = formula as PluginFormula<FormulaHandlerV2>
456
+ },
457
+ )
458
+ window.toddle.formulas[window.__toddle.project] = formulas
459
+ break
460
+ }
461
+ case 'global_actions': {
462
+ const actions: Record<string, PluginActionV2> = {}
463
+ window.toddle.clearLegacyActions?.()
464
+ Object.entries(message.data.actions ?? {}).forEach(
465
+ ([name, action]) => {
466
+ if (
467
+ typeof action.name === 'string' &&
468
+ action.version === undefined
469
+ ) {
470
+ // Legacy actions are self-registering. We need to execute them to register them
471
+ Function(action.handler)()
472
+ return
473
+ }
474
+ actions[name] = action as PluginActionV2
475
+ },
476
+ )
477
+ window.toddle.actions[window.__toddle.project] = actions
478
+ break
479
+ }
418
480
  case 'packages': {
419
481
  if (message.data.packages) {
420
482
  packageComponents = Object.values(message.data.packages ?? {})