@lightningjs/blits 1.0.0 → 1.2.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.
- package/CHANGELOG.md +20 -0
- package/boilerplate/js/default/src/index.js +1 -0
- package/docs/_sidebar.md +1 -0
- package/docs/plugins/language.md +174 -0
- package/docs/sidebar.json +4 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/package.json +4 -3
- package/src/application.js +2 -1
- package/src/component/base/index.js +4 -1
- package/src/component/base/log.js +1 -1
- package/src/component.d.ts +6 -0
- package/src/component.js +26 -1
- package/src/component.test.js +340 -4
- package/src/components/index.js +0 -2
- package/src/components/index.orig.js +0 -2
- package/src/constants.js +14 -0
- package/src/engines/L3/element.js +1 -1
- package/src/engines/L3/launch.js +3 -3
- package/src/focus.js +3 -2
- package/src/helpers/fetchJson.js +34 -0
- package/src/launch.d.ts +12 -1
- package/src/launch.js +1 -1
- package/src/lib/codegenerator/generator.js +13 -4
- package/src/lib/codegenerator/generator.test.js +52 -0
- package/src/lib/componentId.js +9 -0
- package/src/lib/componentId.test.js +2 -1
- package/src/lib/precompiler/precompiler.js +1 -3
- package/src/lib/symbols.js +1 -0
- package/src/plugin.d.ts +71 -0
- package/src/plugin.js +23 -0
- package/src/{components/Image.orig.js → plugins/index.js} +2 -17
- package/src/plugins/language.js +122 -0
- package/vite/index.js +1 -4
- package/vite/preCompiler.js +3 -1
- package/src/components/Image.js +0 -52
package/src/component.test.js
CHANGED
|
@@ -17,6 +17,12 @@
|
|
|
17
17
|
|
|
18
18
|
import test from 'tape'
|
|
19
19
|
import Component from './component.js'
|
|
20
|
+
import { initLog } from './lib/log.js'
|
|
21
|
+
import symbols from './lib/symbols.js'
|
|
22
|
+
import util from 'node:util'
|
|
23
|
+
import { renderer, stage } from './launch.js'
|
|
24
|
+
|
|
25
|
+
initLog()
|
|
20
26
|
|
|
21
27
|
test('Type', (assert) => {
|
|
22
28
|
const expected = 'function'
|
|
@@ -37,15 +43,345 @@ test('Component - Factory function', (assert) => {
|
|
|
37
43
|
test('Component - Factory requires a name to be passed', (assert) => {
|
|
38
44
|
assert.throws(() => {
|
|
39
45
|
Component()
|
|
40
|
-
}, '
|
|
46
|
+
}, 'Throw an error when no name argument has been passed')
|
|
41
47
|
|
|
42
48
|
assert.end()
|
|
43
49
|
})
|
|
44
50
|
|
|
45
|
-
test('Component - Factory requires a
|
|
51
|
+
test('Component - Factory requires a config to be passed', (assert) => {
|
|
46
52
|
assert.throws(() => {
|
|
47
|
-
Component()
|
|
48
|
-
}, 'Throw an error when no
|
|
53
|
+
Component('Foo')
|
|
54
|
+
}, 'Throw an error when no config argument has been passed')
|
|
55
|
+
|
|
56
|
+
assert.end()
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('Component - Instance should create internal id', (assert) => {
|
|
60
|
+
const button = Component('Button', {})()
|
|
61
|
+
const heading = Component('Heading', {})()
|
|
62
|
+
|
|
63
|
+
assert.equal(button[symbols.id], 1, 'Button instance object should have correct internal id')
|
|
64
|
+
assert.equal(heading[symbols.id], 2, 'Heading instance object should have correct internal id')
|
|
65
|
+
assert.end()
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('Component - Instance should create component Id', (assert) => {
|
|
69
|
+
const foo = Component('Foo', {})
|
|
70
|
+
const bar = Component('Bar', {})
|
|
71
|
+
const fooInstance0 = foo()
|
|
72
|
+
const fooInstance1 = foo()
|
|
73
|
+
const barInstance0 = bar()
|
|
74
|
+
|
|
75
|
+
assert.equal(
|
|
76
|
+
fooInstance0.componentId,
|
|
77
|
+
'BlitsComponent::Foo_1',
|
|
78
|
+
'First Foo instance should have correct id'
|
|
79
|
+
)
|
|
80
|
+
assert.equal(
|
|
81
|
+
fooInstance1.componentId,
|
|
82
|
+
'BlitsComponent::Foo_2',
|
|
83
|
+
'Second Foo instance should have correct id'
|
|
84
|
+
)
|
|
85
|
+
assert.equal(
|
|
86
|
+
barInstance0.componentId,
|
|
87
|
+
'BlitsComponent::Bar_1',
|
|
88
|
+
'First Bar instance should have correct id'
|
|
89
|
+
)
|
|
90
|
+
assert.end()
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
test('Component - Instance should initiate lifecycle object', (assert) => {
|
|
94
|
+
const foo = Component('Foo', {})()
|
|
95
|
+
|
|
96
|
+
assert.ok(foo.lifecycle, 'Lifecycle object should be initialized')
|
|
97
|
+
assert.equal(
|
|
98
|
+
foo.lifecycle.component,
|
|
99
|
+
foo,
|
|
100
|
+
'Lifecycle object should have a reference to foo instance'
|
|
101
|
+
)
|
|
102
|
+
assert.equal(
|
|
103
|
+
foo.lifecycle.previous,
|
|
104
|
+
null,
|
|
105
|
+
'Lifecycle object should have previous state not initialized'
|
|
106
|
+
)
|
|
107
|
+
assert.equal(foo.lifecycle.current, 'init', 'Lifecycle object should have initial current state')
|
|
108
|
+
assert.end()
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
test('Component - Instance should set a parent reference', (assert) => {
|
|
112
|
+
const parentElement = {}
|
|
113
|
+
const parentComponent = {}
|
|
114
|
+
|
|
115
|
+
const foo = Component('Foo', {})({}, parentElement, parentComponent)
|
|
116
|
+
|
|
117
|
+
assert.equal(
|
|
118
|
+
foo.parent,
|
|
119
|
+
parentComponent,
|
|
120
|
+
'Foo instance object should have parent component object reference'
|
|
121
|
+
)
|
|
122
|
+
assert.equal(
|
|
123
|
+
foo[symbols.holder],
|
|
124
|
+
parentElement,
|
|
125
|
+
'Foo instance object should have parent element object reference'
|
|
126
|
+
)
|
|
127
|
+
assert.end()
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
test('Component - Instance should set a root reference', (assert) => {
|
|
131
|
+
const root = {}
|
|
132
|
+
|
|
133
|
+
const foo = Component('Foo', {})({}, {}, {}, root)
|
|
134
|
+
|
|
135
|
+
assert.equal(
|
|
136
|
+
foo.rootParent,
|
|
137
|
+
root,
|
|
138
|
+
'Foo instance object should have root component object reference'
|
|
139
|
+
)
|
|
140
|
+
assert.end()
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
test('Component - Instance should initialize reactive props', (assert) => {
|
|
144
|
+
const foo = Component(
|
|
145
|
+
'Foo',
|
|
146
|
+
{}
|
|
147
|
+
)({
|
|
148
|
+
props: { foo: 'bar' },
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
const props = foo[symbols.props]
|
|
152
|
+
|
|
153
|
+
assert.ok(props, 'Foo instance should have props defined')
|
|
154
|
+
assert.ok(util.types.isProxy(props), 'Foo instance props should be proxy')
|
|
155
|
+
assert.equal(props.foo, 'bar')
|
|
156
|
+
assert.end()
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
test('Component - Instance should initialize timeouts array', (assert) => {
|
|
160
|
+
const foo = Component('Foo', {})()
|
|
161
|
+
|
|
162
|
+
const timeouts = foo[symbols.timeouts]
|
|
163
|
+
|
|
164
|
+
assert.ok(Array.isArray(timeouts), 'Foo instance timeouts should be an array')
|
|
165
|
+
assert.end()
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
test('Component - Instance should initialize intervals array', (assert) => {
|
|
169
|
+
const foo = Component('Foo', {})()
|
|
170
|
+
|
|
171
|
+
const intervals = foo[symbols.intervals]
|
|
49
172
|
|
|
173
|
+
assert.ok(Array.isArray(intervals), 'Foo instance intervals should be an array')
|
|
50
174
|
assert.end()
|
|
51
175
|
})
|
|
176
|
+
|
|
177
|
+
test('Component - Instance should initialize originalState', (assert) => {
|
|
178
|
+
const config = {
|
|
179
|
+
state() {
|
|
180
|
+
return {
|
|
181
|
+
foo: 'bar',
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
}
|
|
185
|
+
const foo = Component('Foo', config)()
|
|
186
|
+
|
|
187
|
+
const state = foo[symbols.originalState]
|
|
188
|
+
|
|
189
|
+
assert.equal(state.foo, 'bar', 'Foo instance should store originalState properties')
|
|
190
|
+
assert.equal(state.hasFocus, false, 'Foo instance should store originalState hasFocus property')
|
|
191
|
+
assert.end()
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
test('Component - Instance should initialize reactive state', (assert) => {
|
|
195
|
+
const config = {
|
|
196
|
+
state() {
|
|
197
|
+
return {
|
|
198
|
+
foo: 'bar',
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
}
|
|
202
|
+
const foo = Component('Foo', config)()
|
|
203
|
+
|
|
204
|
+
const state = foo[symbols.state]
|
|
205
|
+
|
|
206
|
+
assert.ok(state, 'Foo instance should have state defined')
|
|
207
|
+
assert.ok(util.types.isProxy(state), 'Foo instance state should be proxy')
|
|
208
|
+
assert.equal(state.foo, 'bar')
|
|
209
|
+
assert.end()
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
test('Component - Instance should initialize children', (assert) => {
|
|
213
|
+
const parent = {}
|
|
214
|
+
const expected = []
|
|
215
|
+
const config = {
|
|
216
|
+
code: {
|
|
217
|
+
render: () => {},
|
|
218
|
+
effects: [],
|
|
219
|
+
},
|
|
220
|
+
state() {
|
|
221
|
+
return {
|
|
222
|
+
foo: 'bar',
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
}
|
|
226
|
+
const capture = assert.capture(config.code, 'render', () => expected)
|
|
227
|
+
const foo = Component('Foo', config)({}, parent)
|
|
228
|
+
|
|
229
|
+
const children = foo[symbols.children]
|
|
230
|
+
|
|
231
|
+
assert.ok(Array.isArray(children), 'Foo instance children should be an array')
|
|
232
|
+
assert.equal(children, expected, 'Children should be an expected array')
|
|
233
|
+
const args = capture()[0].args
|
|
234
|
+
assert.equal(args[0], parent, 'Render should be called with parent parameter')
|
|
235
|
+
assert.equal(args[1], foo, 'Render should be called with Foo component instance')
|
|
236
|
+
assert.equal(args[2], config, 'Render should be called with config parameter')
|
|
237
|
+
assert.ok(args[3].Layout, 'Render should be called with global components object')
|
|
238
|
+
assert.end()
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
test('Component - Instance should initialize wrapper', (assert) => {
|
|
242
|
+
const expected = {}
|
|
243
|
+
const config = {
|
|
244
|
+
code: {
|
|
245
|
+
render: () => [expected],
|
|
246
|
+
effects: [],
|
|
247
|
+
},
|
|
248
|
+
}
|
|
249
|
+
const foo = Component('Foo', config)()
|
|
250
|
+
|
|
251
|
+
const wrapper = foo[symbols.wrapper]
|
|
252
|
+
|
|
253
|
+
assert.equal(wrapper, expected, 'Wrapper should be a first element of the children')
|
|
254
|
+
assert.end()
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
test('Component - Instance should initialize slots', (assert) => {
|
|
258
|
+
const expected = {
|
|
259
|
+
[symbols.isSlot]: true,
|
|
260
|
+
}
|
|
261
|
+
const config = {
|
|
262
|
+
code: {
|
|
263
|
+
render: () => [{}, {}, expected, {}],
|
|
264
|
+
effects: [],
|
|
265
|
+
},
|
|
266
|
+
}
|
|
267
|
+
const foo = Component('Foo', config)()
|
|
268
|
+
|
|
269
|
+
const slots = foo[symbols.slots]
|
|
270
|
+
|
|
271
|
+
assert.ok(Array.isArray(slots), 'Slots should be an array')
|
|
272
|
+
assert.equal(slots[0], expected, 'Slot should be a expected element of the children')
|
|
273
|
+
assert.end()
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
test('Component - Instance should initialize hook events', (assert) => {
|
|
277
|
+
const wrapper = {
|
|
278
|
+
node: {
|
|
279
|
+
on() {},
|
|
280
|
+
},
|
|
281
|
+
}
|
|
282
|
+
const rendererCapture = assert.capture(renderer, 'on')
|
|
283
|
+
const nodeCapture = assert.capture(wrapper.node, 'on')
|
|
284
|
+
const config = {
|
|
285
|
+
code: {
|
|
286
|
+
render: () => [wrapper],
|
|
287
|
+
effects: [],
|
|
288
|
+
},
|
|
289
|
+
hooks: {
|
|
290
|
+
frameTick() {},
|
|
291
|
+
idle() {},
|
|
292
|
+
attach() {},
|
|
293
|
+
detach() {},
|
|
294
|
+
enter() {},
|
|
295
|
+
exit() {},
|
|
296
|
+
},
|
|
297
|
+
}
|
|
298
|
+
Component('Foo', config)()
|
|
299
|
+
|
|
300
|
+
const rendererCalls = rendererCapture()
|
|
301
|
+
assert.equal(
|
|
302
|
+
rendererCalls[0].args[0],
|
|
303
|
+
'frameTick',
|
|
304
|
+
'`frameTick` event should be registered in Renderer'
|
|
305
|
+
)
|
|
306
|
+
assert.equal(rendererCalls[1].args[0], 'idle', '`idle` event should be registered in Renderer')
|
|
307
|
+
const nodeCalls = nodeCapture()
|
|
308
|
+
assert.equal(
|
|
309
|
+
nodeCalls[0].args[0],
|
|
310
|
+
'inBounds',
|
|
311
|
+
'`inBounds` event should be registered in the wrapper'
|
|
312
|
+
)
|
|
313
|
+
assert.equal(
|
|
314
|
+
nodeCalls[1].args[0],
|
|
315
|
+
'outOfBounds',
|
|
316
|
+
'`outOfBounds` event should be registered in the wrapper'
|
|
317
|
+
)
|
|
318
|
+
assert.equal(
|
|
319
|
+
nodeCalls[2].args[0],
|
|
320
|
+
'inViewport',
|
|
321
|
+
'`inViewport` event should be registered in the wrapper'
|
|
322
|
+
)
|
|
323
|
+
assert.equal(
|
|
324
|
+
nodeCalls[3].args[0],
|
|
325
|
+
'outOfViewport',
|
|
326
|
+
'`outOfViewport` event should be registered in the wrapper'
|
|
327
|
+
)
|
|
328
|
+
assert.end()
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
test('Component - Instance should execute all side effects', (assert) => {
|
|
332
|
+
const root = {}
|
|
333
|
+
const capture = assert.captureFn(() => {})
|
|
334
|
+
const children = []
|
|
335
|
+
const config = {
|
|
336
|
+
code: {
|
|
337
|
+
render: () => children,
|
|
338
|
+
effects: [capture],
|
|
339
|
+
},
|
|
340
|
+
}
|
|
341
|
+
const foo = Component('Foo', config)({}, {}, {}, root)
|
|
342
|
+
|
|
343
|
+
const calls = capture.calls[0]
|
|
344
|
+
assert.equals(calls.receiver, stage, 'Effect`s receiver should be the stage object')
|
|
345
|
+
assert.equals(calls.args[0], foo, 'Effect should be invoked with foo component instance')
|
|
346
|
+
assert.equals(calls.args[1], children, 'Effect should be invoked with component`s children')
|
|
347
|
+
assert.equals(calls.args[2], config, 'Effect should be invoked with config object')
|
|
348
|
+
assert.ok(calls.args[3].Layout, 'Effect should be invoked with global components object')
|
|
349
|
+
assert.equals(calls.args[4], root, 'Effect should be invoked with root component')
|
|
350
|
+
assert.ok(typeof calls.args[5] === 'function', 'Effect should be invoked with effect function')
|
|
351
|
+
assert.end()
|
|
352
|
+
})
|
|
353
|
+
|
|
354
|
+
test('Component - Instance should have all symbols configured', (assert) => {
|
|
355
|
+
const config = {
|
|
356
|
+
state() {
|
|
357
|
+
return {
|
|
358
|
+
foo: 'bar',
|
|
359
|
+
}
|
|
360
|
+
},
|
|
361
|
+
watch: {
|
|
362
|
+
foo(value, oldValue) {},
|
|
363
|
+
},
|
|
364
|
+
}
|
|
365
|
+
const foo = Component('Foo', config)()
|
|
366
|
+
|
|
367
|
+
Object.getOwnPropertySymbols(foo).forEach((symbol) => {
|
|
368
|
+
const descriptor = Object.getOwnPropertyDescriptor(foo, symbol)
|
|
369
|
+
assert.ok(descriptor.enumerable === false, `${symbol.description} should not be enumerable`)
|
|
370
|
+
assert.ok(descriptor.configurable === false, `${symbol.description} should not be configurable`)
|
|
371
|
+
})
|
|
372
|
+
assert.end()
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
test('Component - Instance should have ready state after the next process tick', (assert) => {
|
|
376
|
+
assert.plan(1)
|
|
377
|
+
|
|
378
|
+
const foo = Component('Foo', {})()
|
|
379
|
+
|
|
380
|
+
setTimeout(() => {
|
|
381
|
+
assert.equal(
|
|
382
|
+
foo.lifecycle.state,
|
|
383
|
+
'ready',
|
|
384
|
+
'Foo component lifecycle should be eventually in a ready state'
|
|
385
|
+
)
|
|
386
|
+
})
|
|
387
|
+
})
|
package/src/components/index.js
CHANGED
|
@@ -15,7 +15,6 @@
|
|
|
15
15
|
* SPDX-License-Identifier: Apache-2.0
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
import Image from './Image.js'
|
|
19
18
|
import Circle from './Circle.js'
|
|
20
19
|
import RouterView from './RouterView.js'
|
|
21
20
|
import Sprite from './Sprite.js'
|
|
@@ -23,7 +22,6 @@ import FPScounter from './FPScounter.js'
|
|
|
23
22
|
import Layout from './Layout.js'
|
|
24
23
|
|
|
25
24
|
export default () => ({
|
|
26
|
-
Image: Image(),
|
|
27
25
|
Circle: Circle(),
|
|
28
26
|
RouterView: RouterView(),
|
|
29
27
|
Sprite: Sprite(),
|
|
@@ -15,7 +15,6 @@
|
|
|
15
15
|
* SPDX-License-Identifier: Apache-2.0
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
import Image from './Image.js'
|
|
19
18
|
import Circle from './Circle.js'
|
|
20
19
|
import RouterView from './RouterView.js'
|
|
21
20
|
import Sprite from './Sprite.js'
|
|
@@ -23,7 +22,6 @@ import FPScounter from './FPScounter.js'
|
|
|
23
22
|
import Layout from './Layout.js'
|
|
24
23
|
|
|
25
24
|
export default () => ({
|
|
26
|
-
Image: Image(),
|
|
27
25
|
Circle: Circle(),
|
|
28
26
|
RouterView: RouterView(),
|
|
29
27
|
Sprite: Sprite(),
|
package/src/constants.js
ADDED
|
@@ -272,7 +272,7 @@ const Element = {
|
|
|
272
272
|
|
|
273
273
|
this.node = props.__textnode
|
|
274
274
|
? renderer.createTextNode({ ...textDefaults, ...this.props.props })
|
|
275
|
-
:
|
|
275
|
+
: renderer.createNode(this.props.props)
|
|
276
276
|
|
|
277
277
|
if (props['@loaded']) {
|
|
278
278
|
this.node.on('loaded', (el, { type, dimensions }) => {
|
package/src/engines/L3/launch.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
|
|
18
18
|
import { RendererMain } from '@lightningjs/renderer'
|
|
19
19
|
import { Log } from '../../lib/log.js'
|
|
20
|
-
import {
|
|
20
|
+
import { SCREEN_RESOLUTIONS } from '../../constants.js'
|
|
21
21
|
import colors from '../../lib/colors/colors.js'
|
|
22
22
|
import fontLoader from './fontLoader.js'
|
|
23
23
|
import shaderLoader from './shaderLoader.js'
|
|
@@ -42,8 +42,8 @@ You can remove the option from your \`src/index.js\`-file. And you can safely re
|
|
|
42
42
|
fpsUpdateInterval: settings.fpsInterval || 1000,
|
|
43
43
|
deviceLogicalPixelRatio:
|
|
44
44
|
settings.pixelRatio ||
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
SCREEN_RESOLUTIONS[settings.screenResolution] ||
|
|
46
|
+
SCREEN_RESOLUTIONS[window.innerHeight] ||
|
|
47
47
|
1,
|
|
48
48
|
numImageWorkers:
|
|
49
49
|
'webWorkersLimit' in settings
|
package/src/focus.js
CHANGED
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
|
|
18
18
|
import symbols from './lib/symbols.js'
|
|
19
19
|
import { navigating } from './router/router.js'
|
|
20
|
+
import Settings from './settings.js'
|
|
21
|
+
import { DEFAULT_HOLD_TIMEOUT_MS } from './constants.js'
|
|
20
22
|
|
|
21
23
|
let focusedComponent = null
|
|
22
24
|
let focusChain = []
|
|
@@ -48,8 +50,7 @@ export default {
|
|
|
48
50
|
focusChain = []
|
|
49
51
|
}
|
|
50
52
|
},
|
|
51
|
-
|
|
52
|
-
this.hold ? 50 : 0
|
|
53
|
+
this.hold ? Settings.get('holdTimeout', DEFAULT_HOLD_TIMEOUT_MS) : 0
|
|
53
54
|
)
|
|
54
55
|
}
|
|
55
56
|
},
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* If not stated otherwise in this file or this component's LICENSE file the
|
|
3
|
+
* following copyright and licenses apply:
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2020 Metrological
|
|
6
|
+
*
|
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the License);
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
*
|
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export default (file) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
const xhr = new XMLHttpRequest()
|
|
23
|
+
xhr.onreadystatechange = function () {
|
|
24
|
+
if (xhr.readyState == XMLHttpRequest.DONE) {
|
|
25
|
+
// file protocol returns 0
|
|
26
|
+
// http(s) protocol returns 200
|
|
27
|
+
if (xhr.status === 0 || xhr.status === 200) resolve(JSON.parse(xhr.responseText))
|
|
28
|
+
else reject(xhr.statusText)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
xhr.open('GET', file)
|
|
32
|
+
xhr.send(null)
|
|
33
|
+
})
|
|
34
|
+
}
|
package/src/launch.d.ts
CHANGED
|
@@ -243,7 +243,18 @@ export interface Settings {
|
|
|
243
243
|
*
|
|
244
244
|
* Defaults to `webgl`
|
|
245
245
|
*/
|
|
246
|
-
renderMode?: RenderModes
|
|
246
|
+
renderMode?: RenderModes,
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* The time, in milliseconds, after which Blits considers a key press a _hold_ key press
|
|
250
|
+
*
|
|
251
|
+
* During a hold key press the focus delegation behaviour is different: when scrolling
|
|
252
|
+
* through a long list, focus is not handed over to each individual list item, creating a
|
|
253
|
+
* smoother experience
|
|
254
|
+
*
|
|
255
|
+
* Defaults to `50` (ms)
|
|
256
|
+
*/
|
|
257
|
+
holdTimeout?: number
|
|
247
258
|
}
|
|
248
259
|
|
|
249
260
|
/**
|
package/src/launch.js
CHANGED
|
@@ -458,7 +458,7 @@ const generateCode = function (templateObject, parent = false, options = {}) {
|
|
|
458
458
|
const interpolate = (val, component = 'component.') => {
|
|
459
459
|
if (val === undefined) return val
|
|
460
460
|
const replaceString = /('.*?')+/gi
|
|
461
|
-
const replaceDollar =
|
|
461
|
+
const replaceDollar = /\$(\$(?=\$)|\$?)/g
|
|
462
462
|
const matches = val.matchAll(replaceString)
|
|
463
463
|
const restore = []
|
|
464
464
|
|
|
@@ -471,10 +471,19 @@ const interpolate = (val, component = 'component.') => {
|
|
|
471
471
|
i++
|
|
472
472
|
}
|
|
473
473
|
|
|
474
|
-
// replace
|
|
475
|
-
val = val.replace(replaceDollar,
|
|
474
|
+
// replace dollar signs using the regex and function
|
|
475
|
+
val = val.replace(replaceDollar, (match, group1) => {
|
|
476
|
+
// single dollar sign
|
|
477
|
+
if (group1 === '') {
|
|
478
|
+
return component
|
|
479
|
+
}
|
|
480
|
+
// consecutive dollar sign, replace only the first $
|
|
481
|
+
else if (group1 === '$') {
|
|
482
|
+
return component + '$'
|
|
483
|
+
}
|
|
484
|
+
})
|
|
476
485
|
|
|
477
|
-
// restore string
|
|
486
|
+
// restore string replacement
|
|
478
487
|
restore.forEach((el, idx) => {
|
|
479
488
|
val = val.replace(`[@@REPLACEMENT${idx}@@]`, el)
|
|
480
489
|
})
|
|
@@ -2290,3 +2290,55 @@ test('Generate code for a template with a simple for-loop on an Element with an
|
|
|
2290
2290
|
|
|
2291
2291
|
assert.end()
|
|
2292
2292
|
})
|
|
2293
|
+
|
|
2294
|
+
test('Generate code for a template with double $$ (i.e. referencing a Blits plugin)', (assert) => {
|
|
2295
|
+
const templateObject = {
|
|
2296
|
+
children: [
|
|
2297
|
+
{
|
|
2298
|
+
[Symbol.for('componentType')]: 'Text',
|
|
2299
|
+
':content': "$$language.translate('hello')",
|
|
2300
|
+
},
|
|
2301
|
+
],
|
|
2302
|
+
}
|
|
2303
|
+
|
|
2304
|
+
const expectedRender = `
|
|
2305
|
+
function anonymous(parent, component, context, components, effect, getRaw) {
|
|
2306
|
+
const elms = []
|
|
2307
|
+
let componentType
|
|
2308
|
+
const rootComponent = component
|
|
2309
|
+
const elementConfig0 = {}
|
|
2310
|
+
|
|
2311
|
+
elms[0] = this.element({ parent: parent || 'root' }, component)
|
|
2312
|
+
elementConfig0['content'] = component.$language.translate('hello')
|
|
2313
|
+
elementConfig0['__textnode'] = true
|
|
2314
|
+
elms[0].populate(elementConfig0)
|
|
2315
|
+
|
|
2316
|
+
return elms
|
|
2317
|
+
}
|
|
2318
|
+
`
|
|
2319
|
+
|
|
2320
|
+
const expectedEffect1 = `
|
|
2321
|
+
function anonymous(component, elms, context, components, rootComponent, effect) {
|
|
2322
|
+
elms[0].set('content', component.$language.translate('hello'))
|
|
2323
|
+
}
|
|
2324
|
+
`
|
|
2325
|
+
|
|
2326
|
+
const actual = generator.call(scope, templateObject)
|
|
2327
|
+
|
|
2328
|
+
assert.equal(
|
|
2329
|
+
normalize(actual.render.toString()),
|
|
2330
|
+
normalize(expectedRender),
|
|
2331
|
+
'Generator should return a render function with the correct code'
|
|
2332
|
+
)
|
|
2333
|
+
assert.ok(
|
|
2334
|
+
Array.isArray(actual.effects) && actual.effects.length === 1,
|
|
2335
|
+
'Generator should return an effects array with 1 item'
|
|
2336
|
+
)
|
|
2337
|
+
assert.equal(
|
|
2338
|
+
normalize(actual.effects[0].toString()),
|
|
2339
|
+
normalize(expectedEffect1),
|
|
2340
|
+
'Generator should return first render function with the correct code'
|
|
2341
|
+
)
|
|
2342
|
+
|
|
2343
|
+
assert.end()
|
|
2344
|
+
})
|
package/src/lib/componentId.js
CHANGED
|
@@ -25,3 +25,12 @@ export const createHumanReadableId = (name) => {
|
|
|
25
25
|
export const createInternalId = () => {
|
|
26
26
|
return ++counter
|
|
27
27
|
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Resets the counter value back to 0.
|
|
31
|
+
* Used in automated tests to ensure consistent results, not intended
|
|
32
|
+
* to be used in the actual framework code!
|
|
33
|
+
*/
|
|
34
|
+
export const resetCounter = () => {
|
|
35
|
+
counter = 0
|
|
36
|
+
}
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import test from 'tape'
|
|
19
|
-
import { createHumanReadableId, createInternalId } from './componentId.js'
|
|
19
|
+
import { createHumanReadableId, createInternalId, resetCounter } from './componentId.js'
|
|
20
20
|
|
|
21
21
|
test('Type createHumandReadableId', (assert) => {
|
|
22
22
|
const expected = 'function'
|
|
@@ -59,6 +59,7 @@ test('Type createInternalId', (assert) => {
|
|
|
59
59
|
})
|
|
60
60
|
|
|
61
61
|
test('Returns an internal id, disregarding the component name', (assert) => {
|
|
62
|
+
resetCounter()
|
|
62
63
|
const expected = 1
|
|
63
64
|
const actual = createInternalId('Spinner')
|
|
64
65
|
|
|
@@ -17,7 +17,6 @@
|
|
|
17
17
|
|
|
18
18
|
import parser from '../templateparser/parser.js'
|
|
19
19
|
import generator from '../codegenerator/generator.js'
|
|
20
|
-
import path from 'path'
|
|
21
20
|
|
|
22
21
|
export default (source, filePath) => {
|
|
23
22
|
if (
|
|
@@ -46,8 +45,7 @@ export default (source, filePath) => {
|
|
|
46
45
|
resourceName = source.match(/Blits\.Component\(['"](.*)['"]\s*,/)[1]
|
|
47
46
|
}
|
|
48
47
|
|
|
49
|
-
const
|
|
50
|
-
const parsed = parser(templateContent, resourceName, null, componentPath)
|
|
48
|
+
const parsed = parser(templateContent, resourceName, null, filePath)
|
|
51
49
|
|
|
52
50
|
// Generate the code
|
|
53
51
|
const code = generator.call({ components: {} }, parsed)
|
package/src/lib/symbols.js
CHANGED