@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.
@@ -0,0 +1,71 @@
1
+
2
+ /*
3
+ * Copyright 2024 Comcast Cable Communications Management, LLC
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ * SPDX-License-Identifier: Apache-2.0
17
+ */
18
+
19
+ declare namespace Plugin {
20
+
21
+ interface Plugin {
22
+ /**
23
+ * Name of the plugin. The plugin will be accessible on each Component's
24
+ * `this` scope under this name, prefixed with a `$` (i.e. myplugin => `this.$myplugin`)
25
+ */
26
+ name: string,
27
+ /**
28
+ * Singleton function that will be used to instantiate the plugin.
29
+ * Should do all necessary setup and ideally return an object with
30
+ * properties or methods that can be used in the App
31
+ */
32
+ plugin: () => any
33
+ }
34
+
35
+ }
36
+
37
+
38
+ /**
39
+ * Blits.Plugin()
40
+ *
41
+ * Method to register a plugin
42
+ */
43
+ declare function Plugin(
44
+ /**
45
+ * Plugin object or singleton function that will be used to instantiate the
46
+ * plugin. Should do all necessary setup and ideally return an object with
47
+ * properties or methods that can be used in the App
48
+ */
49
+ plugin: Plugin.Plugin | (() => any),
50
+ /**
51
+ * Name of the plugin or options object, to be passed into the plugin instantiation.
52
+ *
53
+ * When passing a string, the value will be used as **name** of the plugin. The plugin
54
+ * will be accessible on each Component's `this` scope under this name, prefixed with a `$`
55
+ * (i.e. myplugin => `this.$myplugin`)
56
+ *
57
+ * Passing a name is **required** when `plugin` argument is a singleton function
58
+ *
59
+ * When passing an object, the value will be considered to be an `options` object.
60
+ * The options object will be passed into the plugin instantiation method. Can
61
+ * be used to set default values
62
+ */
63
+ nameOrOptions?: string | object,
64
+ /**
65
+ * Object with options to be passed into the plugin instantiation method.
66
+ * Can be used to set default values
67
+ */
68
+ options?: object
69
+ ) : void
70
+
71
+ export default Plugin;
package/src/plugin.js ADDED
@@ -0,0 +1,23 @@
1
+ export const plugins = {}
2
+
3
+ const registerPlugin = (plugin, nameOrOptions = '', options = {}) => {
4
+ // map name and options depending on the arguments provided
5
+ let name
6
+ if (typeof nameOrOptions === 'object') {
7
+ options = nameOrOptions
8
+ name = ''
9
+ } else {
10
+ name = nameOrOptions
11
+ }
12
+
13
+ if (typeof plugin === 'function') {
14
+ if (name === '') {
15
+ throw Error('Error registering plugin: name is required for plugin')
16
+ }
17
+ plugins[name] = { plugin, options }
18
+ } else if (plugin.plugin) {
19
+ registerPlugin(plugin.plugin, plugin.name, options)
20
+ }
21
+ }
22
+
23
+ export default registerPlugin
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright 2023 Comcast Cable Communications Management, LLC
2
+ * Copyright 2024 Comcast Cable Communications Management, LLC
3
3
  * Licensed under the Apache License, Version 2.0 (the "License");
4
4
  * you may not use this file except in compliance with the License.
5
5
  * You may obtain a copy of the License at
@@ -15,19 +15,4 @@
15
15
  * SPDX-License-Identifier: Apache-2.0
16
16
  */
17
17
 
18
- import Component from '../component.js'
19
-
20
- export default () =>
21
- Component('Image', {
22
- template: `
23
- <Element :imageSource="$imageSource" />`,
24
- props: ['src'],
25
- computed: {
26
- imageSource() {
27
- if (/^(?:https?:)?\/\//i.test(this.src)) {
28
- return this.src
29
- }
30
- return `${window.location.protocol}//${window.location.host}/${this.src}`
31
- },
32
- },
33
- })
18
+ export { default as language } from './language.js'
@@ -0,0 +1,122 @@
1
+ /*
2
+ * Copyright 2024 Comcast Cable Communications Management, LLC
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ *
15
+ * SPDX-License-Identifier: Apache-2.0
16
+ */
17
+
18
+ import fetchJson from '../helpers/fetchJson'
19
+ import { Log } from '../lib/log'
20
+ import { reactive } from '../lib/reactivity/reactive'
21
+
22
+ export default {
23
+ name: 'language',
24
+ plugin(options = {}) {
25
+ let translations = {}
26
+ let dictionary = {}
27
+
28
+ const state = reactive({
29
+ language: '',
30
+ loaded: 0,
31
+ })
32
+
33
+ const setLanguage = (language) => {
34
+ // define the dictionary
35
+ dictionary = translations[language] || {}
36
+ // set the current language in the reactive state
37
+ state.language = language
38
+
39
+ // warnings
40
+ if (Object.keys(translations).length === 0) {
41
+ Log.warn(
42
+ 'No translations loaded. Please load a file with translations or specify a translations object manually'
43
+ )
44
+ }
45
+ if (language in translations === false) {
46
+ Log.warn(`Language ${language} not available in the loaded translations`)
47
+ }
48
+ }
49
+
50
+ const loadLanguageFile = (filePath) => {
51
+ return fetchJson(filePath)
52
+ .then((result) => {
53
+ setTranslations(result)
54
+ })
55
+ .catch((e) => {
56
+ Log.error(e)
57
+ })
58
+ }
59
+
60
+ const setTranslations = (translationsObject) => {
61
+ translations = translationsObject
62
+ dictionary = translations[state.language] || {}
63
+ state.loaded++
64
+ }
65
+
66
+ if ('file' in options) {
67
+ loadLanguageFile(options.file).then(() => {
68
+ if ('language' in options) {
69
+ setLanguage(options.language)
70
+ }
71
+ })
72
+ } else if ('language' in options) {
73
+ setLanguage(options.language)
74
+ }
75
+
76
+ // Public API language plugin
77
+ return {
78
+ translate(key) {
79
+ // reference state.language, to trigger reactive effects for
80
+ // translate functions when language is modified
81
+ state.language
82
+
83
+ // reference state.loaded, to trigger reactive effects for
84
+ // translate functions when language is modified
85
+ state.loaded
86
+
87
+ let replacements = [...arguments].slice(1)
88
+
89
+ // no replacements so just translated string
90
+ if (replacements.length === 0) {
91
+ return (dictionary && dictionary[key]) || key
92
+ } else {
93
+ if (replacements.length === 1 && typeof replacements[0] === 'object') {
94
+ replacements = replacements.pop()
95
+ }
96
+
97
+ return Object.keys(
98
+ // maps array input to an object {0: 'item1', 1: 'item2'}
99
+ Array.isArray(replacements) ? Object.assign({}, replacements) : replacements
100
+ ).reduce((text, replacementKey) => {
101
+ return text.replace(
102
+ new RegExp('{\\s?' + replacementKey + '\\s?}', 'g'),
103
+ replacements[replacementKey]
104
+ )
105
+ }, (dictionary && dictionary[key]) || key)
106
+ }
107
+ },
108
+ get language() {
109
+ return state.language
110
+ },
111
+ set(language) {
112
+ setLanguage(language)
113
+ },
114
+ translations(translationsObject) {
115
+ setTranslations(translationsObject)
116
+ },
117
+ load(file) {
118
+ return loadLanguageFile(file)
119
+ },
120
+ }
121
+ },
122
+ }
package/vite/index.js CHANGED
@@ -15,13 +15,10 @@
15
15
  * SPDX-License-Identifier: Apache-2.0
16
16
  */
17
17
 
18
- import { importChunkUrl } from '@lightningjs/vite-plugin-import-chunk-url'
19
-
20
18
  import preCompiler from './preCompiler.js'
21
19
  import msdfGenerator from './msdfGenerator.js'
22
20
 
23
- export { importChunkUrl as importChunkUrl } from '@lightningjs/vite-plugin-import-chunk-url'
24
21
  export { default as preCompiler } from './preCompiler.js'
25
22
  export { default as msdfGenerator } from './msdfGenerator.js'
26
23
 
27
- export default [importChunkUrl(), preCompiler(), msdfGenerator()]
24
+ export default [preCompiler(), msdfGenerator()]
@@ -16,6 +16,7 @@
16
16
  */
17
17
 
18
18
  import compiler from '../src/lib/precompiler/precompiler.js'
19
+ import path from 'path'
19
20
 
20
21
  export default function () {
21
22
  let config
@@ -26,7 +27,8 @@ export default function () {
26
27
  },
27
28
  transform(source, filePath) {
28
29
  if (config.blits && config.blits.precompile === false) return source
29
- return compiler(source, filePath)
30
+ const relativePath = path.relative(process.cwd(), filePath)
31
+ return compiler(source, relativePath)
30
32
  },
31
33
  }
32
34
  }
@@ -1,52 +0,0 @@
1
- /*
2
- * Copyright 2023 Comcast Cable Communications Management, LLC
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- *
15
- * SPDX-License-Identifier: Apache-2.0
16
- */
17
-
18
- import Component from '../component.js'
19
-
20
- export default () =>
21
- Component('Image', {
22
- /* eslint-disable no-unused-vars */
23
- code: {
24
- render: function anonymous(parent, component, context, components, effect, getRaw) {
25
- const elms = []
26
- let componentType
27
- const rootComponent = component
28
- const elementConfig0 = {}
29
-
30
- elms[0] = this.element({ parent: parent || 'root' }, component)
31
-
32
- elementConfig0['imageSource'] = component.imageSource
33
- elms[0].populate(elementConfig0)
34
- return elms
35
- },
36
- effects: [
37
- function anonymous(component, elms, context, components, rootComponent, effect) {
38
- elms[0].set('imageSource', component.imageSource)
39
- },
40
- ],
41
- context: {},
42
- },
43
- props: ['src'],
44
- computed: {
45
- imageSource() {
46
- if (/^(?:https?:)?\/\//i.test(this.src)) {
47
- return this.src
48
- }
49
- return `${window.location.protocol}//${window.location.host}/${this.src}`
50
- },
51
- },
52
- })