@nordcraft/runtime 1.0.0 → 1.0.1

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.
@@ -1,4 +1,4 @@
1
- import deepEqual from 'fast-deep-equal';
1
+ import fastDeepEqual from 'fast-deep-equal';
2
2
  export class Signal {
3
3
  value;
4
4
  subscribers;
@@ -17,7 +17,7 @@ export class Signal {
17
17
  this.value = value;
18
18
  return;
19
19
  }
20
- if (deepEqual(value, this.value) === false) {
20
+ if (fastDeepEqual(value, this.value) === false) {
21
21
  this.value = value;
22
22
  this.subscribers.forEach(({ notify }) => notify(this.value));
23
23
  }
@@ -60,6 +60,6 @@ export function signal(value) {
60
60
  if (typeof window !== 'undefined') {
61
61
  ;
62
62
  window.signal = signal;
63
- window.deepEqual = deepEqual;
63
+ window.deepEqual = fastDeepEqual;
64
64
  }
65
65
  //# sourceMappingURL=signal.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"signal.js","sourceRoot":"","sources":["../../src/signal/signal.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,iBAAiB,CAAA;AAEvC,MAAM,OAAO,MAAM;IACjB,KAAK,CAAG;IACR,WAAW,CAGT;IACF,aAAa,CAAmB;IAEhC,YAAY,KAAQ;QAClB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAClB,IAAI,CAAC,WAAW,GAAG,IAAI,GAAG,EAAE,CAAA;QAC5B,IAAI,CAAC,aAAa,GAAG,EAAE,CAAA;IACzB,CAAC;IACD,GAAG;QACD,OAAO,IAAI,CAAC,KAAK,CAAA;IACnB,CAAC;IACD,GAAG,CAAC,KAAQ;QACV,0FAA0F;QAC1F,IAAI,IAAI,CAAC,WAAW,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;YAChC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;YAClB,OAAM;QACR,CAAC;QAED,IAAI,SAAS,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,KAA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,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;QACrC,OAAO,CAAC,aAAa,CAAC,IAAI,CACxB,IAAI,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE;YAC/C,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE;SACjC,CAAC,CACH,CAAA;QACD,OAAO,OAAO,CAAA;IAChB,CAAC;CACF;AAED,MAAM,UAAU,MAAM,CAAI,KAAQ;IAChC,OAAO,IAAI,MAAM,CAAC,KAAK,CAAC,CAAA;AAC1B,CAAC;AAED,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;IAClC,CAAC;IAAC,MAAc,CAAC,MAAM,GAAG,MAAM,CAC/B;IAAC,MAAc,CAAC,SAAS,GAAG,aAAa,CAAA;AAC5C,CAAC"}
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.0",
8
- "@nordcraft/std-lib": "1.0.0",
7
+ "@nordcraft/core": "1.0.1",
8
+ "@nordcraft/std-lib": "1.0.1",
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.0"
24
+ "version": "1.0.1"
25
25
  }
@@ -5,7 +5,7 @@ import type {
5
5
  } from '@nordcraft/core/dist/component/component.types'
6
6
  import type { ToddleEnv } from '@nordcraft/core/dist/formula/formula'
7
7
  import type { Toddle } from '@nordcraft/core/dist/types'
8
- import deepEqual from 'fast-deep-equal'
8
+ import fastDeepEqual from 'fast-deep-equal'
9
9
  import { handleAction } from '../events/handleAction'
10
10
  import type { Signal } from '../signal/signal'
11
11
  import type {
@@ -117,7 +117,7 @@ export function renderComponent({
117
117
  [key, value],
118
118
  ) => {
119
119
  if (
120
- deepEqual(value, prev![key]) === false &&
120
+ fastDeepEqual(value, prev![key]) === false &&
121
121
  component.attributes[key]?.name
122
122
  ) {
123
123
  changes[component.attributes[key]?.name] = {
@@ -2,11 +2,13 @@
2
2
  import type {
3
3
  ActionModel,
4
4
  ComponentData,
5
+ SetMultiUrlParameterAction,
5
6
  SetURLParameterAction,
6
7
  } from '@nordcraft/core/dist/component/component.types'
7
8
  import { applyFormula } from '@nordcraft/core/dist/formula/formula'
8
9
  import { mapValues, omitKeys } from '@nordcraft/core/dist/utils/collections'
9
10
  import { isDefined, toBoolean } from '@nordcraft/core/dist/utils/util'
11
+ import fastDeepEqual from 'fast-deep-equal'
10
12
  import type { ComponentContext, Location } from '../types'
11
13
  import { getLocationUrl } from '../utils/url'
12
14
 
@@ -145,6 +147,90 @@ export function handleAction(
145
147
  })
146
148
  break
147
149
  }
150
+ case 'SetURLParameters': {
151
+ const parameters = Object.entries(action.parameters ?? {})
152
+ if (parameters.length === 0) {
153
+ return
154
+ }
155
+ ctx.toddle.locationSignal.update((current) => {
156
+ if (!current.route) {
157
+ // A route must exist for us to update/validate against it
158
+ return current
159
+ }
160
+ // We default to push for state changes and replace for query changes
161
+ let historyMode: SetMultiUrlParameterAction['historyMode'] = 'replace'
162
+ const queryUpdates: Record<string, string> = {}
163
+ const pathUpdates: Record<string, string> = {}
164
+ const urlParameterCtx = {
165
+ data,
166
+ component: ctx.component,
167
+ formulaCache: ctx.formulaCache,
168
+ root: ctx.root,
169
+ package: ctx.package,
170
+ toddle: ctx.toddle,
171
+ env: ctx.env,
172
+ }
173
+ // Only match on p.type === 'param'
174
+ const isValidPathParameter = (param: string) =>
175
+ current.route?.path.some(
176
+ (p) => p.name === param && p.type === 'param',
177
+ )
178
+ const isValidQueryParameter = (param: string) =>
179
+ Object.values(current.route?.query ?? {}).some(
180
+ (q) => q.name === param,
181
+ )
182
+
183
+ for (const [parameter, formula] of parameters) {
184
+ const value = applyFormula(formula, urlParameterCtx) ?? null
185
+ if (isValidPathParameter(parameter)) {
186
+ historyMode = 'push'
187
+ pathUpdates[parameter] = value as string
188
+ } else if (isValidQueryParameter(parameter)) {
189
+ queryUpdates[parameter] = value as string
190
+ }
191
+ }
192
+ if (
193
+ Object.keys(pathUpdates).length === 0 &&
194
+ Object.keys(queryUpdates).length === 0
195
+ ) {
196
+ // No path/query parameter matched
197
+ // We'll exit early to avoid deep equal below
198
+ return current
199
+ }
200
+
201
+ const newLocation = {
202
+ ...current,
203
+ params: {
204
+ ...omitKeys(current.params, Object.keys(pathUpdates)),
205
+ ...pathUpdates,
206
+ },
207
+ query: {
208
+ ...omitKeys(current.query, Object.keys(queryUpdates)),
209
+ ...queryUpdates,
210
+ },
211
+ }
212
+ if (fastDeepEqual(newLocation, current)) {
213
+ // No path/query parameter matched
214
+ return current
215
+ }
216
+
217
+ const currentUrl = getLocationUrl(current)
218
+ const historyUrl = getLocationUrl(newLocation)
219
+ if (historyUrl !== currentUrl) {
220
+ // Default to the historyMode from the action, and fallback
221
+ // to the default (push for path change, replace for query change)
222
+ historyMode = action.historyMode ?? historyMode
223
+ // Update the window's history state
224
+ if (historyMode === 'push') {
225
+ window.history.pushState({}, '', historyUrl)
226
+ } else {
227
+ window.history.replaceState({}, '', historyUrl)
228
+ }
229
+ }
230
+ return newLocation
231
+ })
232
+ break
233
+ }
148
234
  case 'Fetch': {
149
235
  const api = ctx.apis[action.api]
150
236
  if (!api) {
@@ -1,4 +1,4 @@
1
- import deepEqual from 'fast-deep-equal'
1
+ import fastDeepEqual from 'fast-deep-equal'
2
2
 
3
3
  export class Signal<T> {
4
4
  value: T
@@ -23,7 +23,7 @@ export class Signal<T> {
23
23
  return
24
24
  }
25
25
 
26
- if (deepEqual(value, this.value) === false) {
26
+ if (fastDeepEqual(value, this.value) === false) {
27
27
  this.value = value
28
28
  this.subscribers.forEach(({ notify }) => notify(this.value))
29
29
  }
@@ -70,5 +70,5 @@ export function signal<T>(value: T) {
70
70
 
71
71
  if (typeof window !== 'undefined') {
72
72
  ;(window as any).signal = signal
73
- ;(window as any).deepEqual = deepEqual
73
+ ;(window as any).deepEqual = fastDeepEqual
74
74
  }