@lightningjs/blits 0.5.7 → 0.5.9

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/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ # v0.5.8
4
+
5
+ _21 dec 2023_
6
+
7
+ - Changed transition-end callback to receive real prop value from node
8
+ - Implemented symbol for `wrapper` on Component (freeing up the name wrapper to be used in Component state)
9
+ - Added functionality to prevent a route ending up in the history stack (route option: `inHistory: false`)
10
+ - Improved router backtracking logic
11
+ - Added functionality to override route options during navigation
12
+ - Upgraded to latest version of the renderer (0.6.1) which contains a fix for animations not finishing correctly
13
+
14
+ # v0.5.8
15
+
16
+ _15 dec 2023_
17
+
18
+ - Internal refactor of the `Element`
19
+
3
20
  # v0.5.7
4
21
 
5
22
  _13 dec 2023_
@@ -31,6 +31,6 @@
31
31
  "vite": "^4.0.4"
32
32
  },
33
33
  "dependencies": {
34
- "@lightningjs/blits": "^0.5.7"
34
+ "@lightningjs/blits": "^0.5.9"
35
35
  }
36
36
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightningjs/blits",
3
- "version": "0.5.7",
3
+ "version": "0.5.9",
4
4
  "description": "Blits: The Lightning 3 App Development Framework",
5
5
  "bin": "bin/index.cjs",
6
6
  "exports": {
@@ -42,7 +42,7 @@
42
42
  "tape": "^5.5.0"
43
43
  },
44
44
  "dependencies": {
45
- "@lightningjs/renderer": "^0.6.0",
45
+ "@lightningjs/renderer": "^0.6.1",
46
46
  "@lightningjs/vite-plugin-import-chunk-url": "^0.3.0"
47
47
  }
48
48
  }
package/src/component.js CHANGED
@@ -121,7 +121,7 @@ const Component = (name = required('name'), config = required('config')) => {
121
121
  }
122
122
 
123
123
  this.parent = parentComponent
124
- this.wrapper = parentEl
124
+ this[symbols.wrapper] = parentEl
125
125
 
126
126
  Object.defineProperties(this, {
127
127
  type: {
package/src/element.js CHANGED
@@ -33,172 +33,212 @@ const parseToObject = (str) => {
33
33
  return JSON.parse(str.replace(/'/g, '"').replace(/([\w-_]+)\s*:/g, '"$1":'))
34
34
  }
35
35
 
36
- const transformations = {
37
- unpackTransition(obj) {
38
- if (typeof obj === 'object' && obj.constructor.name === 'Object') {
39
- if ('value' in obj) {
40
- return obj.value
41
- } else if ('transition' in obj) {
42
- return this.unpackTransition(obj.transition)
43
- } else {
44
- return obj
45
- }
46
- } else {
47
- return obj
36
+ const parsePercentage = function (v, base) {
37
+ if (typeof v === 'string' && v.endsWith('%')) {
38
+ return this.element.node._parent[base] * (parseFloat(v) / 100)
39
+ }
40
+ return v
41
+ }
42
+
43
+ const unpackTransition = (obj) => {
44
+ if (typeof obj === 'object' && obj.constructor === Object) {
45
+ if ('value' in obj) {
46
+ return obj.value
47
+ } else if ('transition' in obj) {
48
+ return unpackTransition(obj.transition)
48
49
  }
50
+ }
51
+ return obj
52
+ }
53
+
54
+ const Props = {
55
+ set parent(v) {
56
+ this._props.parent = v === 'root' ? renderer.root : v.node
57
+ this._set.add('parent')
49
58
  },
50
- remap(props) {
51
- 'w' in props && (props.width = props.w)
52
- delete props.w
53
- 'h' in props && (props.height = props.h)
54
- delete props.h
55
- 'z' in props && (props.zIndex = props.z)
56
- delete props.z
57
- 'wordWrap' in props && (props.width = props.wordWrap)
58
- delete props.wordWrap
59
- },
60
- parent(props) {
61
- if (props.parent === 'root') {
62
- props.parent = renderer.root
63
- } else {
64
- props.parent = props.parent.node
65
- }
59
+ set rotation(v) {
60
+ this._props.rotation = v * (Math.PI / 180)
61
+ this._set.add('rotation')
62
+ },
63
+ set w(v) {
64
+ this._props.width = parsePercentage.call(this, v, 'width')
65
+ this._set.add('width')
66
+ },
67
+ set width(v) {
68
+ this._props.width = parsePercentage.call(this, v, 'width')
69
+ this._set.add('width')
66
70
  },
67
- color(props) {
68
- if (
69
- typeof props.color === 'object' ||
70
- (isObjectString(props.color) && (props.color = parseToObject(props.color)))
71
- ) {
71
+ set h(v) {
72
+ this._props.height = parsePercentage.call(this, v, 'height')
73
+ this._set.add('height')
74
+ },
75
+ set height(v) {
76
+ this._props.height = parsePercentage.call(this, v, 'height')
77
+ this._set.add('height')
78
+ },
79
+ set x(v) {
80
+ this._props.x = parsePercentage.call(this, v, 'width')
81
+ this._set.add('x')
82
+ },
83
+ set y(v) {
84
+ this._props.y = parsePercentage.call(this, v, 'height')
85
+ this._set.add('y')
86
+ },
87
+ set z(v) {
88
+ this._props.zIndex = v
89
+ this._set.add('zIndex')
90
+ },
91
+ set zIndex(v) {
92
+ this._props.zIndex = v
93
+ this._set.add('zIndex')
94
+ },
95
+ set color(v) {
96
+ if (typeof v === 'object' || (isObjectString(v) && (v = parseToObject(v)))) {
72
97
  const map = {
73
98
  top: 'colorTop',
74
99
  bottom: 'colorBottom',
75
100
  left: 'colorLeft',
76
101
  right: 'colorRight',
77
102
  }
78
- Object.entries(props.color).forEach((color) => {
79
- props[map[color[0]]] = colors.normalize(color[1])
103
+ this._props.color = 0
104
+ Object.entries(v).forEach((color) => {
105
+ this._props[map[color[0]]] = colors.normalize(color[1])
106
+ this._set.add(map[color[0]])
80
107
  })
81
- delete props.color
82
108
  } else {
83
- props.color = colors.normalize(props.color)
109
+ this._props.color = colors.normalize(v)
84
110
  }
111
+ this._set.add('color')
85
112
  },
86
- percentage(props, prop) {
87
- const map = {
88
- w: 'width',
89
- width: 'width',
90
- x: 'width',
91
- h: 'height',
92
- height: 'height',
93
- y: 'height',
94
- }
95
-
96
- const base = map[prop]
97
- if (base) {
98
- props[prop] = this.node._parent[base] * (parseFloat(props[prop]) / 100)
113
+ set src(v) {
114
+ this._props.src = v
115
+ if (!this._set.has('color')) {
116
+ this._props.color = 0xffffffff
99
117
  }
118
+ this._set.add('src')
100
119
  },
101
- mount(props) {
102
- if (
103
- typeof props.mount === 'object' ||
104
- (isObjectString(props.mount) && (props.mount = parseToObject(props.mount)))
105
- ) {
106
- const map = {
107
- x: 'mountX',
108
- y: 'mountY',
120
+ set texture(v) {
121
+ this._props.texture = v
122
+ this._set.add('texture')
123
+ },
124
+ set mount(v) {
125
+ if (typeof v === 'object' || (isObjectString(v) && (v = parseToObject(v)))) {
126
+ if (v.x) {
127
+ this._props.mountX = v.x
128
+ this._set.add('mountX')
109
129
  }
110
- Object.entries(props.mount).forEach((mount) => {
111
- props[map[mount[0]]] = mount[1]
112
- })
113
- delete props.mount
130
+ if (v.y) {
131
+ this._props.mountY = v.y
132
+ this._set.add('mountY')
133
+ }
134
+ } else {
135
+ this._props.mountX = this._props.mountY = v
136
+ this._set.add('mountX')
137
+ this._set.add('mountY')
114
138
  }
115
139
  },
116
- pivot(props) {
117
- if (
118
- typeof props.pivot === 'object' ||
119
- (isObjectString(props.pivot) && (props.pivot = parseToObject(props.pivot)))
120
- ) {
121
- const map = {
122
- x: 'pivotX',
123
- y: 'pivotY',
140
+ set pivot(v) {
141
+ if (typeof v === 'object' || (isObjectString(v) && (v = parseToObject(v)))) {
142
+ if (v.x) {
143
+ this._set.add('pivotX')
144
+ this._props.pivotX = v.x
124
145
  }
125
- Object.entries(props.pivot).forEach((pivot) => {
126
- props[map[pivot[0]]] = pivot[1]
127
- })
128
- delete props.pivot
146
+ if (v.y) {
147
+ this._set.add('pivotY')
148
+ this._props.pivotY = v.y
149
+ }
150
+ } else {
151
+ this._props.pivotX = this._props.pivotY = v
152
+ this._set.add('pivotX')
153
+ this._set.add('pivotY')
129
154
  }
130
155
  },
131
- show(props) {
132
- props.alpha = props.show ? 1 : 0
133
- delete props.show
156
+ set scale(v) {
157
+ if (typeof v === 'object' || (isObjectString(v) && (v = parseToObject(v)))) {
158
+ if (v.x) this._props.scaleX = v.x
159
+ if (v.y) this._props.scaleY = v.y
160
+ } else {
161
+ this._props.scale = v
162
+ }
163
+ this._set.add('scale')
134
164
  },
135
- rotation(props) {
136
- props.rotation = props.rotation * (Math.PI / 180)
165
+ set show(v) {
166
+ this.alpha = v ? 1 : 0
137
167
  },
138
- text(props) {
139
- props.text = props.text !== undefined ? props.text.toString() : ''
168
+ set alpha(v) {
169
+ this._props.alpha = v
170
+ this._set.add('alpha')
140
171
  },
141
- textureColor(props) {
142
- if (!('color' in props)) {
143
- props.color = 'src' in props || 'texture' in props ? '0xffffffff' : '0x00000000'
144
- }
172
+ set text(v) {
173
+ this._props.text = v !== undefined ? v.toString() : ''
145
174
  },
146
- effects(props) {
147
- props.shader = renderer.createShader('DynamicShader', {
148
- effects: props.effects.map((eff) => {
175
+ set effects(v) {
176
+ this._props.shader = renderer.createShader('DynamicShader', {
177
+ effects: v.map((eff) => {
149
178
  if (eff.props && eff.props.color) {
150
179
  eff.props.color = colors.normalize(eff.props.color)
151
180
  }
152
181
  return eff
153
182
  }),
154
183
  })
155
-
156
- delete props.effects
184
+ this._set.add('effects')
157
185
  },
158
- src(props, setProperties) {
159
- if (setProperties.indexOf('color') === -1) {
160
- props.color = 0xffffffff
161
- }
186
+ set fontFamily(v) {
187
+ this._props.fontFamily = v
162
188
  },
163
- texture(props, setProperties) {
164
- this.src(props, setProperties)
189
+ set fontSize(v) {
190
+ this._props.fontSize = v
165
191
  },
166
- maxLines(props, setProperties, element) {
167
- if (props.maxLines) {
168
- props.height = props.maxLines * element.node.fontSize
169
- }
170
- delete props.maxLines
192
+ set wordWrap(v) {
193
+ this._props.width = v
194
+ },
195
+ set contain(v) {
196
+ this._props.contain = v
197
+ },
198
+ set maxLines(v) {
199
+ this.height = v * this.element.node.fontSize
200
+ },
201
+ set letterSpacing(v) {
202
+ this._props.letterSpacing = v
203
+ },
204
+ set textAlign(v) {
205
+ this._props.textAlign = v
171
206
  },
172
207
  }
173
208
 
174
209
  const Element = {
175
- defaults: {
176
- rotation: 0,
177
- },
210
+ defaults: {},
178
211
  populate(data) {
179
212
  const props = {
180
213
  ...this.defaults,
181
214
  ...this.config,
182
215
  ...data,
183
216
  }
217
+
184
218
  this.initData = data
185
219
 
186
220
  if (props[symbols.isSlot]) {
187
221
  this[symbols.isSlot] = true
188
222
  }
189
223
 
190
- transformations.remap(props)
191
- Object.keys(props).forEach((prop) => {
192
- if (transformations[prop]) {
193
- props[prop] = transformations.unpackTransition(props[prop])
194
- transformations[prop](props, this.setProperties)
224
+ this.props.element = this
225
+
226
+ for (const [prop, value] of Object.entries(props)) {
227
+ const descriptor = Object.getOwnPropertyDescriptor(Props, prop)
228
+ if (descriptor && descriptor.set) {
229
+ this.props[prop] = unpackTransition(value)
195
230
  }
196
- this.setProperties.push(prop)
197
- })
231
+ }
198
232
 
199
- transformations.textureColor(props, this.setProperties)
233
+ // correct for default white nodes
234
+ if (!this.props._set.has('color')) {
235
+ this.props._props.color =
236
+ this.props._set.has('src') || this.props._set.has('texture') ? 0xffffffff : 0
237
+ }
200
238
 
201
- this.node = props.__textnode ? renderer.createTextNode(props) : renderer.createNode(props)
239
+ this.node = props.__textnode
240
+ ? renderer.createTextNode(this.props._props)
241
+ : renderer.createNode(this.props._props)
202
242
 
203
243
  if (props['@loaded']) {
204
244
  this.node.on('loaded', (el, { type, dimensions }) => {
@@ -213,43 +253,24 @@ const Element = {
213
253
  }
214
254
  },
215
255
  set(prop, value) {
216
- if (isTransition(value) && this.setProperties.indexOf(prop) > -1) {
217
- this.animate(prop, value.transition)
218
- } else {
219
- const props = {}
220
- if (prop !== 'texture') {
221
- props[prop] = transformations.unpackTransition(value)
222
- } else {
223
- props[prop] = value
224
- }
256
+ const propsSet = new Set(this.props._set)
225
257
 
226
- transformations.remap(props)
227
- if (transformations[prop]) {
228
- transformations[prop](props, this.setProperties, this)
229
- }
258
+ this.props._props = {}
259
+ this.props[prop] = unpackTransition(value)
230
260
 
231
- Object.keys(props).forEach((prop) => {
232
- if (typeof props[prop] === 'string' && props[prop].endsWith('%')) {
233
- transformations.percentage.call(this, props, prop)
234
- }
235
- this.node[prop] = props[prop]
236
- })
261
+ for (const [p, v] of Object.entries(this.props._props)) {
262
+ if (isTransition(value) && propsSet.has(p)) {
263
+ return this.animate(p, v, value.transition)
264
+ } else {
265
+ this.node[p] = v
266
+ }
237
267
  }
238
- this.setProperties.indexOf(prop) === -1 && this.setProperties.push(prop)
239
268
  },
240
- delete() {
241
- Log.debug('Deleting Node', this.nodeId, this.node)
242
- this.node.parent = null
243
- },
244
- get nodeId() {
245
- return this.node && this.node.id
246
- },
247
- get ref() {
248
- return this.initData.ref || null
249
- },
250
- animate(prop, value) {
269
+ animate(prop, value, transition) {
270
+ if (this.node[prop] === value) return Promise.resolve()
271
+ // check if a transition is already schedule or running on the same prop
251
272
  if (this.scheduledTransitions[prop]) {
252
- clearTimeout(this.scheduledTransitions[prop].timeout)
273
+ // clearTimeout(this.scheduledTransitions[prop].timeout)
253
274
  if (this.scheduledTransitions[prop].f.state === 'running') {
254
275
  this.scheduledTransitions[prop].f.pause()
255
276
  // fastforward to final value
@@ -258,55 +279,73 @@ const Element = {
258
279
  }
259
280
 
260
281
  const props = {}
261
- props[prop] = transformations.unpackTransition(value)
262
-
263
- transformations.remap(props)
282
+ props[prop] = value
264
283
 
265
- if (transformations[prop]) {
266
- transformations[prop](props)
267
- }
284
+ // construct animate function
285
+ const f = this.node.animate(props, {
286
+ duration:
287
+ typeof transition === 'object'
288
+ ? 'duration' in transition
289
+ ? transition.duration
290
+ : 300
291
+ : 300,
292
+ easing:
293
+ typeof transition === 'object'
294
+ ? 'function' in transition
295
+ ? transition.function
296
+ : 'ease'
297
+ : 'ease',
298
+ })
268
299
 
269
- // works for now
270
- prop = Object.keys(props).pop()
300
+ // schedule transition
301
+ return new Promise((resolve) => {
302
+ this.scheduledTransitions[prop] = {
303
+ v: props[prop],
304
+ f,
305
+ timeout: setTimeout(() => {
306
+ // fire transition start callback if specified
307
+ transition.start &&
308
+ typeof transition.start === 'function' &&
309
+ transition.start.call(this.component, this, prop, props[prop])
271
310
 
272
- if (typeof props[prop] === 'string' && props[prop].endsWith('%')) {
273
- transformations.percentage.call(this, props, prop)
274
- }
275
-
276
- if (this.node[prop] !== props[prop]) {
277
- const f = this.node.animate(props, {
278
- duration: typeof value === 'object' ? ('duration' in value ? value.duration : 300) : 300,
279
- easing:
280
- typeof value === 'object' ? ('function' in value ? value.function : 'ease') : 'ease',
281
- })
282
- return new Promise((resolve) => {
283
- this.scheduledTransitions[prop] = {
284
- v: props[prop],
285
- f,
286
- timeout: setTimeout(() => {
287
- value.start &&
288
- typeof value.start === 'function' &&
289
- value.start.call(this.component, this, prop, props[prop])
311
+ // start the animation
312
+ try {
290
313
  f.start()
291
314
  .waitUntilStopped()
292
315
  .then(() => delete this.scheduledTransitions[prop])
293
316
  .then(() => {
294
- value.end &&
295
- typeof value.end === 'function' &&
296
- value.end.call(this.component, this, prop, props[prop])
317
+ // fire transition end callback if specified
318
+ transition.end &&
319
+ typeof transition.end === 'function' &&
320
+ transition.end.call(this.component, this, prop, this.node[prop])
297
321
  })
298
322
  .then(resolve)
299
- }, value.delay || 0),
300
- }
301
- })
302
- }
323
+ } catch (e) {
324
+ Log.error(e)
325
+ }
326
+ }, transition.delay || 0),
327
+ }
328
+ })
329
+ },
330
+ delete() {
331
+ Log.debug('Deleting Node', this.nodeId)
332
+ Object.values(this.scheduledTransitions).forEach((scheduledTransition) => {
333
+ clearTimeout(scheduledTransition.timeout)
334
+ })
335
+ this.node.parent = null
336
+ },
337
+ get nodeId() {
338
+ return this.node && this.node.id
339
+ },
340
+ get ref() {
341
+ return this.initData.ref || null
303
342
  },
304
343
  }
305
344
 
306
345
  export default (config, component) =>
307
346
  Object.assign(Object.create(Element), {
347
+ props: Object.assign(Object.create(Props), { _props: {}, _set: new Set() }),
308
348
  node: null,
309
- setProperties: [],
310
349
  scheduledTransitions: {},
311
350
  initData: {},
312
351
  config,
@@ -21,6 +21,17 @@ export default (component, routes) => {
21
21
  component.prototype[symbols.routes] = []
22
22
  Object.keys(routes).forEach((key) => {
23
23
  // todo: validate routes[key] for expected format etc.
24
- component.prototype[symbols.routes][key] = routes[key]
24
+ component.prototype[symbols.routes][key] = {
25
+ ...routes[key],
26
+ ...{
27
+ // merge default route options with route specific options
28
+ options: {
29
+ ...{
30
+ inHistory: true,
31
+ },
32
+ ...routes[key].options,
33
+ },
34
+ },
35
+ }
25
36
  })
26
37
  }
@@ -23,6 +23,7 @@ export default {
23
23
  type: Symbol('type'),
24
24
  watchers: Symbol('watchers'),
25
25
  watchKeys: Symbol('watchKeys'),
26
+ wrapper: Symbol('wrapper'),
26
27
 
27
28
  /* Utilizing the global Symbol registry for the following purposes */
28
29
 
@@ -27,6 +27,8 @@ export let navigating = false
27
27
 
28
28
  const cacheMap = new WeakMap()
29
29
  const history = []
30
+ let overrideOptions = {}
31
+ let navigatingBack = false
30
32
 
31
33
  export const getHash = () => {
32
34
  return (document.location.hash || '/').replace(/^#/, '')
@@ -38,7 +40,10 @@ export const matchHash = (path, routes = []) => {
38
40
  return r.path === path
39
41
  })
40
42
  .pop()
41
- if (route) currentRoute = route
43
+ if (route) {
44
+ route.options = { ...route.options, ...overrideOptions }
45
+ currentRoute = route
46
+ }
42
47
  return route
43
48
  }
44
49
 
@@ -50,7 +55,11 @@ export const navigate = async function () {
50
55
  const route = matchHash(hash, this.parent[symbols.routes])
51
56
 
52
57
  if (route) {
53
- if (history[history.length - 1] !== hash) history.push(hash)
58
+ // add the previous route (tecnhically still the current route at this point)
59
+ // into the history stack, unless navigating back or inHistory flag of route is false
60
+ if (navigatingBack === false && previousRoute && previousRoute.options.inHistory === true) {
61
+ history.push(previousRoute)
62
+ }
54
63
  // apply default transition if none specified
55
64
  if (!('transition' in route)) {
56
65
  route.transition = fadeInFadeOutTransition
@@ -82,7 +91,7 @@ export const navigate = async function () {
82
91
  view = view(this[symbols.props], holder, this)
83
92
  }
84
93
  } else {
85
- holder = view.wrapper
94
+ holder = view[symbols.wrapper]
86
95
  }
87
96
 
88
97
  this[symbols.children].push(view)
@@ -130,6 +139,9 @@ export const navigate = async function () {
130
139
  Log.error(`Route ${hash} not found`)
131
140
  }
132
141
  }
142
+
143
+ // reset navigating indicators
144
+ navigatingBack = false
133
145
  navigating = false
134
146
  }
135
147
 
@@ -139,11 +151,11 @@ const removeView = async (route, view, transition) => {
139
151
  if (Array.isArray(transition)) {
140
152
  for (let i = 0; i < transition.length; i++) {
141
153
  i === transition.length - 1
142
- ? await setOrAnimate(view.wrapper, transition[i])
143
- : setOrAnimate(view.wrapper, transition[i])
154
+ ? await setOrAnimate(view[symbols.wrapper], transition[i])
155
+ : setOrAnimate(view[symbols.wrapper], transition[i])
144
156
  }
145
157
  } else {
146
- await setOrAnimate(view.wrapper, transition)
158
+ await setOrAnimate(view[symbols.wrapper], transition)
147
159
  }
148
160
  }
149
161
 
@@ -165,18 +177,21 @@ const removeView = async (route, view, transition) => {
165
177
 
166
178
  const setOrAnimate = (node, transition, shouldAnimate = true) => {
167
179
  return shouldAnimate
168
- ? node.animate(transition.prop, transition)
180
+ ? node.animate(transition.prop, transition.value, transition)
169
181
  : node.set(transition.prop, transition.value)
170
182
  }
171
183
 
172
- export const to = (location) => {
184
+ export const to = (location, options = {}) => {
185
+ overrideOptions = options
173
186
  window.location.hash = `#${location}`
174
187
  }
175
188
 
176
189
  export const back = () => {
177
- if (history.length > 1) {
178
- to(history[history.length - 2])
179
- history.splice(-2, 2)
190
+ const route = history.pop()
191
+ if (route) {
192
+ // set indicator that we are navigating back (to prevent adding page to history stack)
193
+ navigatingBack = true
194
+ to(route.path)
180
195
  return true
181
196
  } else {
182
197
  return false