@lightningjs/blits 1.29.2 → 1.29.4

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.
@@ -25,6 +25,30 @@ import Focus from '../focus.js'
25
25
  import Announcer from '../announcer/announcer.js'
26
26
  import Settings from '../settings.js'
27
27
 
28
+ /**
29
+ * @typedef {import('../component.js').BlitsComponentFactory} BlitsComponentFactory - The component of the route
30
+ * @typedef {import('../component.js').BlitsComponent} BlitsComponent - The element of the route
31
+ * @typedef {import('../engines/L3/element.js').BlitsElement} BlitsElement - The element of the route
32
+ *
33
+ * @typedef {Object} Route
34
+ * @property {string} path - The path of the route
35
+ * @property {string} hash - The hash of the route
36
+ * @property {Object} params - The params of the route
37
+ * @property {Object} data - The data of the route
38
+ * @property {Object} options - The options of the route
39
+ * @property {Object} hooks - The hooks of the route
40
+ * @property {Object} transition - The transition of the route
41
+ * @property {Object} announce - The announce of the route
42
+ * @property {(options: Object, parentEl: BlitsElement, parentComponent: BlitsComponent, rootComponent?: BlitsComponent) => BlitsComponent} component - The component factory of the route
43
+ *
44
+ * @typedef {Object} Hash
45
+ * @property {string} path - The path of the hash
46
+ * @property {URLSearchParams} queryParams - The query params of the hash
47
+ * @property {string} hash - The hash
48
+ *
49
+ */
50
+
51
+ /** @type {Route} */
28
52
  export let currentRoute
29
53
  export const state = reactive(
30
54
  {
@@ -40,6 +64,14 @@ export const state = reactive(
40
64
 
41
65
  // Changed from WeakMap to Map to allow for caching of views by the url hash.
42
66
  // We are manually doing the cleanup of the cache when the route is not marked as keepAlive.
67
+
68
+ /**
69
+ * @typedef {Object} CacheMapEntry
70
+ * @property {BlitsComponent|BlitsComponentFactory} view - The view of the route
71
+ * @property {BlitsComponent} focus - The focus of the route
72
+ */
73
+
74
+ /** @type {Map<String, CacheMapEntry>} */
43
75
  const cacheMap = new Map()
44
76
  const history = []
45
77
 
@@ -48,6 +80,10 @@ let navigationData = {}
48
80
  let navigatingBack = false
49
81
  let previousFocus
50
82
 
83
+ /**
84
+ * Get the current hash
85
+ * @returns {Hash}
86
+ */
51
87
  export const getHash = () => {
52
88
  const hashParts = (document.location.hash || '/').replace(/^#/, '').split('?')
53
89
  return {
@@ -65,14 +101,34 @@ const normalizePath = (path) => {
65
101
  .toLowerCase()
66
102
  )
67
103
  }
104
+
105
+ /**
106
+ * Check if a value is an object
107
+ * @param {any} v
108
+ * @returns {boolean} True if v is an object
109
+ */
68
110
  const isObject = (v) => typeof v === 'object' && v !== null
69
111
 
112
+ /**
113
+ * Check if a value is a function
114
+ * @param {any} v
115
+ * @returns {boolean} True if v is a string
116
+ */
70
117
  const isString = (v) => typeof v === 'string'
71
118
 
119
+ /**
120
+ * Match a path to a route
121
+ *
122
+ * @param {string} path
123
+ * @param {Route[]} routes
124
+ * @returns {Route}
125
+ */
72
126
  export const matchHash = (path, routes = []) => {
73
127
  // remove trailing slashes
74
128
  const originalPath = path.replace(/^\/+|\/+$/g, '')
75
129
  path = normalizePath(path)
130
+
131
+ /** @type {boolean|Route} */
76
132
  let matchingRoute = false
77
133
  let i = 0
78
134
  while (!matchingRoute && i < routes.length) {
@@ -126,9 +182,22 @@ export const matchHash = (path, routes = []) => {
126
182
  }
127
183
  }
128
184
 
185
+ // @ts-ignore - Remove me when we have a better way to handle this
129
186
  return matchingRoute
130
187
  }
131
188
 
189
+ /**
190
+ * Navigate to a route
191
+ *
192
+ * This isn't the prettiest way to do this, but it works. The reason is that extends
193
+ * only works for Classes or Factory functions. As such we need to use this
194
+ * @typedef {BlitsComponent & {
195
+ * activeView: BlitsComponent
196
+ * }} RouterViewComponent
197
+ *
198
+ * @this {RouterViewComponent} this
199
+ * @returns {Promise<void>}
200
+ */
132
201
  export const navigate = async function () {
133
202
  Announcer.stop()
134
203
  Announcer.clear()
@@ -178,6 +247,7 @@ export const navigate = async function () {
178
247
  }
179
248
  // apply default transition if none specified
180
249
  if (!('transition' in route)) {
250
+ /** @ts-ignore */
181
251
  route.transition = fadeInFadeOutTransition
182
252
  }
183
253
  // a transition can be a function returning a dynamic transition object
@@ -186,8 +256,10 @@ export const navigate = async function () {
186
256
  route.transition = route.transition(previousRoute, route)
187
257
  }
188
258
 
259
+ /** @type {import('../engines/L3/element.js').BlitsElement} */
189
260
  let holder
190
261
  let routeData
262
+
191
263
  let { view, focus } = cacheMap.get(route.hash) || {}
192
264
 
193
265
  // Announce route change if a message has been specified for this route
@@ -236,7 +308,9 @@ export const navigate = async function () {
236
308
  }
237
309
  }
238
310
  if (typeof view === 'function') {
239
- view = view({ props }, holder, this)
311
+ // had to inline this because the tscompiler does not like LHS reassignments
312
+ // that also change the type of the variable in a variable union
313
+ view = /** @type {BlitsComponentFactory} */ (view)({ props }, holder, this)
240
314
  }
241
315
  } else {
242
316
  holder = view[symbols.holder]
@@ -267,7 +341,7 @@ export const navigate = async function () {
267
341
  previousFocus = Focus.get()
268
342
 
269
343
  // set focus to the view that we're routing to
270
- focus ? focus.$focus() : view.$focus()
344
+ focus ? focus.$focus() : /** @type {BlitsComponent} */ (view).$focus()
271
345
 
272
346
  // apply before settings to holder element
273
347
  if (route.transition.before) {
@@ -321,6 +395,13 @@ export const navigate = async function () {
321
395
  state.navigating = false
322
396
  }
323
397
 
398
+ /**
399
+ * Remove the currently active view
400
+ *
401
+ * @param {Route} route
402
+ * @param {BlitsComponent} view
403
+ * @param {Object} transition
404
+ */
324
405
  const removeView = async (route, view, transition) => {
325
406
  // apply out transition
326
407
  if (transition) {