@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 CHANGED
@@ -1,5 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ ## v1.2.0
4
+
5
+ _5 aug 2024_
6
+
7
+ - Added plugin system for Blits
8
+ - Added Language plugin, modeled after L2-SDK Language plugin
9
+ - Removed unused built-in Image component
10
+ - Marked `this.trigger()`-method as deprecated in definition file
11
+ - Removed double assignment of `this.node` during element creation
12
+
13
+ ## v1.1.0
14
+
15
+ _25 july 2024_
16
+
17
+ - Made `holdTimeout` configurable and added to launch settings
18
+ - Improved test coverage
19
+ - Removed imporChunkUrl dependency as it’s no longer needed
20
+ - Abstracted path from pre-compiler, to make it more reusable (in playground for example)
21
+ - Added `defaultFont` launch setting to boilerplate example
22
+
3
23
  ## v1.0.0
4
24
 
5
25
  _15 july 2024_
@@ -6,6 +6,7 @@ Blits.Launch(App, 'app', {
6
6
  h: 1080,
7
7
  multithreaded: false,
8
8
  debugLevel: 1,
9
+ defaultFont: 'lato',
9
10
  fonts: [
10
11
  {
11
12
  family: 'lato',
package/docs/_sidebar.md CHANGED
@@ -24,3 +24,4 @@
24
24
  - [For loop](/built-in/for-loop.md)
25
25
  - Plugins
26
26
  - [Text-to-Speech / Announcer](/plugins/text-to-speech-announcer.md)
27
+ - [Language](/plugins/language.md)
@@ -0,0 +1,174 @@
1
+ # Blits - Lightning 3 App Development Framework
2
+
3
+ ## Language plugin
4
+
5
+ Blits comes with a built-in Language plugin, that can be used to add internationalization (i18n) to your App.
6
+
7
+ The Blits Language plugin is a simple and lightweight implementation, closely modeled after the tried and tested Language plugin in the Lightning-SDK (for Lightning 2). It is designed to provide a flexible and easy way of translating texts to multiple languages, in a performant way.
8
+
9
+ ### Registering the plugin
10
+
11
+ While the Language plugin is provided in the core Blits package, it's an _optional_ plugin that needs to be registered before you're able to use it. As such the Language plugin is _tree-shakable_, i.e. for Apps that don't have a need for translation functionality, the Language plugin will not be part of the final App bundle.
12
+
13
+ The Language plugin should be imported from Blits and registered in the App's `index.js`, as demonstrated in the example below.
14
+
15
+ Make sure to place the `Blits.Plugin()` method _before_ calling the `Blits.Launch()` method
16
+
17
+ ```js
18
+ // index.js
19
+
20
+ import Blits from '@lightningjs/blits'
21
+ // import the language plugin
22
+ import { language } from '@lightningjs/blits/plugins'
23
+
24
+ import App from './App.js'
25
+
26
+ // Use the Blits Language plugin and
27
+ // optionally pass in a settings object
28
+ Blits.Plugin(language, {
29
+ file: '/assets/translations.json', // file with translations
30
+ language: 'EN-us', // default language
31
+ })
32
+
33
+
34
+ Blits.Launch(App, 'app', {
35
+ // launch settings
36
+ })
37
+ ```
38
+
39
+ The Language plugin accepts an optional configuration object with 2 keys:
40
+
41
+ - `file` - the JSON file with translations to be loaded at initialization
42
+ - `language` - the default language to use
43
+
44
+ After registration of the Language plugin, it will be availabe in each Blits Component as `this.$language`.
45
+
46
+ ### Translations file
47
+
48
+ The most common way of defining a set of translations, is to use a dedicated JSON file, modeled after
49
+ the following format:
50
+
51
+ ```json
52
+ {
53
+ "nl-NL": {
54
+ "hello": "Hallo, alles goed?",
55
+ "bye": "Doei!",
56
+ "replacement": "Mijn naam is {first} {last}, ik ben {age} jaar"
57
+ },
58
+ "en-US": {
59
+ "hello": "Hello, how are you doing?",
60
+ "bye": "Goodbye!",
61
+ "replacement": "My name is {first} {last}, I'm {age} years old"
62
+ },
63
+ "fr-FR": {
64
+ "hello": "Bonjour, ça va?",
65
+ "bye": "Au revoir!",
66
+ "replacement": "Je m'appelle {first} {last}, j'ai {age} ans"
67
+ },
68
+ "de-DE": {
69
+ "hello": "Gutentag, wie geht's?",
70
+ "bye": "Tschüss!",
71
+ "replacement": "Mein Name ist {first} {last}, ich bin {age} Jahre alt"
72
+ }
73
+ }
74
+ ```
75
+
76
+ Each top level key represents a language, which contains an object with translation key-value pairs. The language key
77
+ doesn't require to be in a specific format, but it's recommended to use ISO-codes.
78
+
79
+ When the JSON file is specified in the Plugin registration method, the language file is loaded automatically upon instantiation.
80
+
81
+ In case you want to load the file with translations manually, you can use the `this.$language.load()`-method anywhere in a Component and pass in the path to the JSON file as the first argument.
82
+
83
+ ### Defining translations manualy
84
+
85
+ As an alternative to a JSON file with translations, you can also define translations directly via an object, using the `translations()`-method.
86
+
87
+ The `this.$language.translations()`-methods accepts a translations object as it's first argument, following the same format as the JSON file. The method is typically used in an `init` or `ready` hook
88
+
89
+ ```js
90
+ Blits.Component('MyComponent', {
91
+ // ...
92
+ hooks: {
93
+ init() {
94
+ this.$language.translations({
95
+ english: {
96
+ hello: "Hello",
97
+ world: "World"
98
+ },
99
+ italian: {
100
+ hello: "Ciao",
101
+ world: "Mondo"
102
+ }
103
+ })
104
+ }
105
+ },
106
+ })
107
+ ```
108
+
109
+ ### Setting the language
110
+
111
+ In order to set (or change) the current language, the `this.$language.set()`-method can be used, passing in the new language code (matching one of the languages defined in the translations).
112
+
113
+ ```js
114
+ Blits.Component('MyComponent', {
115
+ // ...
116
+ input: {
117
+ enter() {
118
+ this.$language.set('pt-BR')
119
+ }
120
+ },
121
+ })
122
+ ```
123
+
124
+ ### Getting the current language
125
+
126
+ The current language can be retrieved via the `this.$language.get()` method. It will return the currently set language code.
127
+
128
+ ### Translating
129
+
130
+ The most important functionality in the Language plugin is translating. The Language plugin exposes a `translate()` method that takes as string to translate as it's first argument. It will return the translated value in the currently set language. If either the language or the string to translate can't be found, it will return the inputed string.
131
+
132
+ The translate method can be used both inside the javascript business logic, but also directly in the template.
133
+
134
+ ```js
135
+ Blits.Component('MyComponent', {
136
+ template: `
137
+ <Element>
138
+ <Text :content="$$language.translate('hello')" />
139
+ <Text y="100" :content="$$language.get()" />
140
+ </Element>
141
+ `,
142
+ hooks: {
143
+ ready() {
144
+ console.log(this.$language.translate('world'))
145
+ }
146
+ },
147
+ })
148
+ ```
149
+
150
+ Note that in the template definition 2 consecutive dollars signs are used (`$$language`). The first `$`-sign is used
151
+ to refer to the Component scope, as with any other state variable or computed property refereced in the template.
152
+
153
+ The second `$`-sign is needed, since the Language plugin itself is prefixed with a dollar sign on the Component scope as well (i.e `this.$language`).
154
+
155
+
156
+ #### Dynamic replacements
157
+
158
+ The `tranlate()`-method also supports dynamic replacements. This allows you to specify variables inside your translation string, and replace them with dynamic values passed into the `translate()`-method.
159
+
160
+ Replacements are marked in the translation value using _handlebars_, like so:
161
+
162
+ ```js
163
+ {
164
+ replacements = "My name is {first} {last}, I'm {age} years old"
165
+ }
166
+ ```
167
+
168
+ Now when you call the `translate()`-method and pass in an object with dynamic values, those will be replaced
169
+ in the final string accordingly:
170
+
171
+ ```js
172
+ this.$language.translate('replacements', {name: 'John', last: 'Doe', age: 18})
173
+ // result: My name is John Doe, I'm 18 years old
174
+ ```
package/docs/sidebar.json CHANGED
@@ -106,6 +106,10 @@
106
106
  {
107
107
  "text": "Text-to-Speech / Announcer",
108
108
  "link": "/plugins/text-to-speech-announcer"
109
+ },
110
+ {
111
+ "text": "Language",
112
+ "link": "/plugins/language"
109
113
  }
110
114
  ]
111
115
  }
package/index.d.ts CHANGED
@@ -18,6 +18,7 @@
18
18
  import Component from './src/component.js'
19
19
  import Application from './src/application.js'
20
20
  import Launch from './src/launch.js'
21
+ import Plugin from './src/plugin.js'
21
22
 
22
23
  /**
23
24
  * Blits - The Lightning 3 App Development Framework
@@ -26,6 +27,7 @@ declare module Blits {
26
27
  export { Component }
27
28
  export { Application }
28
29
  export { Launch }
30
+ export { Plugin }
29
31
  }
30
32
 
31
33
  export default Blits
package/index.js CHANGED
@@ -18,9 +18,11 @@
18
18
  import Component from './src/component.js'
19
19
  import Application from './src/application.js'
20
20
  import Launch from './src/launch.js'
21
+ import Plugin from './src/plugin.js'
21
22
 
22
23
  export default {
23
24
  Component,
24
25
  Application,
25
26
  Launch,
27
+ Plugin,
26
28
  }
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "@lightningjs/blits",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "Blits: The Lightning 3 App Development Framework",
5
5
  "bin": "bin/index.js",
6
6
  "exports": {
7
7
  ".": "./index.js",
8
8
  "./vite": "./vite/index.js",
9
- "./transitions": "./src/router/transitions/index.js"
9
+ "./transitions": "./src/router/transitions/index.js",
10
+ "./precompiler": "./src/lib/precompiler/precompiler.js",
11
+ "./plugins": "./src/plugins/index.js"
10
12
  },
11
13
  "scripts": {
12
14
  "test": "c8 npm run test:run",
@@ -46,7 +48,6 @@
46
48
  "dependencies": {
47
49
  "@lightningjs/msdf-generator": "^1.0.2",
48
50
  "@lightningjs/renderer": "^1.0.0",
49
- "@lightningjs/vite-plugin-import-chunk-url": "^0.3.0",
50
51
  "execa": "^8.0.1",
51
52
  "kolorist": "^1.8.0",
52
53
  "ora": "^7.0.1",
@@ -20,6 +20,7 @@ import Focus from './focus.js'
20
20
  import Settings from './settings.js'
21
21
 
22
22
  import symbols from './lib/symbols.js'
23
+ import { DEFAULT_HOLD_TIMEOUT_MS } from './constants.js'
23
24
 
24
25
  const Application = (config) => {
25
26
  const defaultKeyMap = {
@@ -61,7 +62,7 @@ const Application = (config) => {
61
62
  clearTimeout(holdTimeout)
62
63
  holdTimeout = setTimeout(() => {
63
64
  Focus.hold = true
64
- }, 50)
65
+ }, Settings.get('holdTimeout', DEFAULT_HOLD_TIMEOUT_MS))
65
66
  }
66
67
 
67
68
  keyUpHandler = () => {
@@ -22,8 +22,11 @@ import log from './log.js'
22
22
  import router from './router.js'
23
23
  import announcer from './announcer.js'
24
24
  import utils from './utils.js'
25
+ import symbols from '../../lib/symbols.js'
25
26
 
26
27
  export default Object.defineProperties(
27
- {},
28
+ {
29
+ [symbols['pluginsRegistered']]: false,
30
+ },
28
31
  { ...methods, ...scheduling, ...events, ...log, ...router, ...announcer, ...utils }
29
32
  )
@@ -21,7 +21,7 @@ export default {
21
21
  $log: {
22
22
  value: log('App'),
23
23
  writable: false,
24
- enumerable: false,
24
+ enumerable: true,
25
25
  configurable: false,
26
26
  },
27
27
  }
@@ -328,6 +328,12 @@ declare namespace Component {
328
328
  * Triggers a forced update on state variables.
329
329
  */
330
330
  $trigger: (key: string) => void
331
+ /**
332
+ * @deprecated
333
+ *
334
+ * Triggers a forced update on state variables.
335
+ * Deprecated: use `this.$trigger()` instead
336
+ */
331
337
  trigger: (key: string) => void
332
338
 
333
339
  /**
package/src/component.js CHANGED
@@ -34,6 +34,8 @@ import Settings from './settings.js'
34
34
  import setupComponent from './component/setup/index.js'
35
35
  import components from './components/index.js'
36
36
 
37
+ import { plugins } from './plugin.js'
38
+
37
39
  // object to store global components
38
40
  let globalComponents
39
41
 
@@ -202,7 +204,30 @@ const Component = (name = required('name'), config = required('config')) => {
202
204
  }
203
205
 
204
206
  const factory = (options = {}, parentEl, parentComponent, rootComponent) => {
205
- // setup the component once, using Base as the prototype
207
+ // Register user defined plugins once on the Base object
208
+ if (Base[symbols['pluginsRegistered']] === false) {
209
+ Object.keys(plugins).forEach((pluginName) => {
210
+ const prefixedPluginName = `$${pluginName}`
211
+ if (prefixedPluginName in Base) {
212
+ Log.warn(
213
+ `"${pluginName}" (this.${prefixedPluginName}) already exists as a property or plugin on the Base Component. You may be overwriting built-in functionality. Proceed with care!`
214
+ )
215
+ }
216
+
217
+ const plugin = plugins[pluginName]
218
+
219
+ Object.defineProperty(Base, prefixedPluginName, {
220
+ // instantiate the plugin, passing in provided options
221
+ value: plugin.plugin(plugin.options),
222
+ writable: false,
223
+ enumerable: true,
224
+ configurable: false,
225
+ })
226
+ })
227
+ Base[symbols['pluginsRegistered']] = true
228
+ }
229
+
230
+ // setup the component once per component type, using Base as the prototype
206
231
  if (!base) {
207
232
  Log.debug(`Setting up ${name} component`)
208
233
  base = setupComponent(Object.create(Base), config)