@knowark/componarkjs 1.13.4 → 1.14.1
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/README.md +57 -45
- package/lib/base/component/component.js +142 -20
- package/lib/base/component/component.test.js +753 -374
- package/lib/base/component/index.js +3 -0
- package/lib/base/styles/index.js +4 -1
- package/lib/base/utils/define.js +30 -7
- package/lib/base/utils/define.test.js +129 -42
- package/lib/base/utils/format.js +12 -6
- package/lib/base/utils/format.test.js +16 -16
- package/lib/base/utils/helpers.js +42 -9
- package/lib/base/utils/helpers.test.js +134 -115
- package/lib/base/utils/index.js +1 -0
- package/lib/base/utils/slots.js +3 -2
- package/lib/base/utils/slots.test.js +38 -38
- package/lib/base/utils/uuid.js +1 -1
- package/lib/base/utils/uuid.test.js +13 -13
- package/lib/components/audio/components/audio.js +36 -3
- package/lib/components/audio/components/audio.test.js +120 -90
- package/lib/components/audio/index.js +1 -0
- package/lib/components/audio/styles/index.js +5 -1
- package/lib/components/camera/components/camera.js +15 -0
- package/lib/components/camera/components/camera.test.js +96 -91
- package/lib/components/camera/index.js +1 -0
- package/lib/components/camera/styles/index.js +5 -1
- package/lib/components/capture/components/capture.js +48 -4
- package/lib/components/capture/components/capture.test.js +165 -97
- package/lib/components/capture/index.js +1 -0
- package/lib/components/droparea/components/droparea-preview.js +114 -19
- package/lib/components/droparea/components/droparea-preview.test.js +344 -80
- package/lib/components/droparea/components/droparea.js +82 -6
- package/lib/components/droparea/components/droparea.test.js +309 -299
- package/lib/components/droparea/index.js +1 -0
- package/lib/components/droparea/styles/index.js +5 -1
- package/lib/components/emit/components/emit.js +34 -4
- package/lib/components/emit/components/emit.test.js +192 -134
- package/lib/components/emit/index.js +1 -0
- package/lib/components/index.js +2 -1
- package/lib/components/list/components/item.js +6 -0
- package/lib/components/list/components/item.test.js +69 -68
- package/lib/components/list/components/list.js +51 -7
- package/lib/components/list/components/list.test.js +358 -227
- package/lib/components/list/index.js +1 -0
- package/lib/components/paginator/components/paginator.js +34 -8
- package/lib/components/paginator/components/paginator.test.js +146 -143
- package/lib/components/paginator/index.js +1 -0
- package/lib/components/paginator/styles/index.js +5 -1
- package/lib/components/spinner/components/spinner.js +10 -0
- package/lib/components/spinner/components/spinner.test.js +36 -41
- package/lib/components/spinner/index.js +1 -0
- package/lib/components/spinner/styles/index.js +5 -1
- package/lib/components/splitview/components/splitview.detail.js +10 -1
- package/lib/components/splitview/components/splitview.detail.test.js +75 -73
- package/lib/components/splitview/components/splitview.js +54 -11
- package/lib/components/splitview/components/splitview.master.js +37 -2
- package/lib/components/splitview/components/splitview.master.test.js +52 -52
- package/lib/components/splitview/components/splitview.test.js +135 -31
- package/lib/components/splitview/index.js +1 -0
- package/lib/components/translate/components/translate.js +65 -14
- package/lib/components/translate/components/translate.test.js +658 -131
- package/lib/components/translate/index.js +1 -0
- package/lib/index.js +3 -0
- package/package.json +4 -27
- package/scripts/node-test-setup.js +94 -0
- package/tsconfig.json +1 -1
- package/types/base/component/component.d.ts +43 -8
- package/types/base/component/component.d.ts.map +1 -1
- package/types/base/component/index.d.ts +4 -6
- package/types/base/component/index.d.ts.map +1 -1
- package/types/base/styles/index.d.ts +3 -2
- package/types/base/styles/index.d.ts.map +1 -1
- package/types/base/utils/define.d.ts +3 -2
- package/types/base/utils/define.d.ts.map +1 -1
- package/types/base/utils/format.d.ts +12 -6
- package/types/base/utils/format.d.ts.map +1 -1
- package/types/base/utils/helpers.d.ts +27 -7
- package/types/base/utils/helpers.d.ts.map +1 -1
- package/types/base/utils/slots.d.ts +8 -10
- package/types/base/utils/slots.d.ts.map +1 -1
- package/types/base/utils/uuid.d.ts +1 -1
- package/types/base/utils/uuid.d.ts.map +1 -1
- package/types/components/audio/components/audio.d.ts +23 -9
- package/types/components/audio/components/audio.d.ts.map +1 -1
- package/types/components/audio/styles/index.d.ts +3 -2
- package/types/components/audio/styles/index.d.ts.map +1 -1
- package/types/components/camera/components/camera.d.ts +11 -3
- package/types/components/camera/components/camera.d.ts.map +1 -1
- package/types/components/camera/styles/index.d.ts +3 -2
- package/types/components/camera/styles/index.d.ts.map +1 -1
- package/types/components/capture/components/capture.d.ts +23 -3
- package/types/components/capture/components/capture.d.ts.map +1 -1
- package/types/components/droparea/components/droparea-preview.d.ts +64 -11
- package/types/components/droparea/components/droparea-preview.d.ts.map +1 -1
- package/types/components/droparea/components/droparea.d.ts +58 -13
- package/types/components/droparea/components/droparea.d.ts.map +1 -1
- package/types/components/droparea/styles/index.d.ts +3 -2
- package/types/components/droparea/styles/index.d.ts.map +1 -1
- package/types/components/emit/components/emit.d.ts +15 -3
- package/types/components/emit/components/emit.d.ts.map +1 -1
- package/types/components/list/components/item.d.ts +8 -1
- package/types/components/list/components/item.d.ts.map +1 -1
- package/types/components/list/components/list.d.ts +23 -5
- package/types/components/list/components/list.d.ts.map +1 -1
- package/types/components/paginator/components/paginator.d.ts +32 -8
- package/types/components/paginator/components/paginator.d.ts.map +1 -1
- package/types/components/paginator/styles/index.d.ts +3 -2
- package/types/components/paginator/styles/index.d.ts.map +1 -1
- package/types/components/spinner/components/spinner.d.ts +14 -3
- package/types/components/spinner/components/spinner.d.ts.map +1 -1
- package/types/components/spinner/styles/index.d.ts +3 -2
- package/types/components/spinner/styles/index.d.ts.map +1 -1
- package/types/components/splitview/components/splitview.d.ts +22 -4
- package/types/components/splitview/components/splitview.d.ts.map +1 -1
- package/types/components/splitview/components/splitview.detail.d.ts +12 -2
- package/types/components/splitview/components/splitview.detail.d.ts.map +1 -1
- package/types/components/splitview/components/splitview.master.d.ts +12 -1
- package/types/components/splitview/components/splitview.master.d.ts.map +1 -1
- package/types/components/translate/components/translate.d.ts +44 -10
- package/types/components/translate/components/translate.d.ts.map +1 -1
|
@@ -1,29 +1,46 @@
|
|
|
1
1
|
import { Component } from "#base/index.js"
|
|
2
2
|
|
|
3
3
|
const tag = 'ark-translate'
|
|
4
|
+
/**
|
|
5
|
+
* Translation component.
|
|
6
|
+
* @typedef {{key:string, namespace:string}} TranslatedKey
|
|
7
|
+
* @typedef {Record<string, Record<string, Record<string, string>>>} DictionaryStore
|
|
8
|
+
* @typedef {{language?:string, root?:string}} TransliterateOptions
|
|
9
|
+
*/
|
|
4
10
|
export class Translate extends Component {
|
|
11
|
+
/** @type {DictionaryStore} */
|
|
12
|
+
dictionary
|
|
13
|
+
/** @param {object} context
|
|
14
|
+
* @returns {this} */
|
|
5
15
|
init (context = {}) {
|
|
6
16
|
this.languages = this.languages
|
|
7
17
|
this.global = context.global || window
|
|
8
18
|
this.endpoint = context.endpoint || this.endpoint || '/locales'
|
|
9
19
|
this.namespace = context.namespace || this.namespace || 'default'
|
|
10
20
|
this.root = context.root || this.root || 'body'
|
|
21
|
+
/** @type {DictionaryStore} */
|
|
11
22
|
this.dictionary = context.dictionary || {}
|
|
12
23
|
|
|
13
24
|
const dictionary = (
|
|
14
25
|
/** @type {HTMLTemplateElement} */ (this.querySelector('template')))
|
|
15
26
|
if (dictionary) {
|
|
16
27
|
const content = dictionary.content.textContent
|
|
17
|
-
|
|
28
|
+
try {
|
|
29
|
+
this.dictionary = /** @type {DictionaryStore} */ (JSON.parse(content))
|
|
30
|
+
} catch (error) {
|
|
31
|
+
this.emit('error', error)
|
|
32
|
+
}
|
|
18
33
|
}
|
|
19
34
|
|
|
20
35
|
return super.init(context)
|
|
21
36
|
}
|
|
22
37
|
|
|
38
|
+
/** @returns {string[]} */
|
|
23
39
|
reflectedProperties () {
|
|
24
40
|
return ['languages', 'endpoint', 'namespace', 'root']
|
|
25
41
|
}
|
|
26
42
|
|
|
43
|
+
/** @returns {this} */
|
|
27
44
|
render () {
|
|
28
45
|
const languages = this.languages.split(',').filter(
|
|
29
46
|
item => Boolean(item) && item.trim())
|
|
@@ -31,33 +48,52 @@ export class Translate extends Component {
|
|
|
31
48
|
|
|
32
49
|
this.content = `
|
|
33
50
|
<select listen on-change="onLanguageChanged">
|
|
34
|
-
${languages.map(code =>
|
|
35
|
-
|
|
36
|
-
|
|
51
|
+
${languages.map(code => {
|
|
52
|
+
const normalizedCode = code.trim()
|
|
53
|
+
const language = LANGUAGE_LIST[normalizedCode]
|
|
54
|
+
const name = language ? language.name : normalizedCode
|
|
55
|
+
return `<option value="${normalizedCode}">${name}</option>`
|
|
56
|
+
}).join('')}
|
|
37
57
|
</select>
|
|
38
58
|
`
|
|
39
59
|
return super.render()
|
|
40
60
|
}
|
|
41
61
|
|
|
62
|
+
/** @param {Event} event
|
|
63
|
+
* @returns {Promise<void>} */
|
|
42
64
|
async onLanguageChanged (event) {
|
|
43
65
|
event.stopPropagation()
|
|
44
|
-
const
|
|
66
|
+
const target = /** @type {HTMLSelectElement|null} */ (event.target)
|
|
67
|
+
const language = target?.value
|
|
45
68
|
await this.transliterate({ language })
|
|
46
69
|
}
|
|
47
70
|
|
|
71
|
+
/** @param {TransliterateOptions} options
|
|
72
|
+
* @returns {Promise<void>} */
|
|
48
73
|
async transliterate (options = {}) {
|
|
49
74
|
const language = options.language || 'es'
|
|
50
75
|
const root = this.global.document.querySelector(
|
|
51
76
|
options.root || this.root)
|
|
77
|
+
if (!root) return
|
|
78
|
+
|
|
79
|
+
const dictionaries = {}
|
|
52
80
|
|
|
53
81
|
for (const node of root.querySelectorAll('[data-i18n]')) {
|
|
54
82
|
const { key, namespace } = this.parseKey(node.dataset.i18n)
|
|
55
|
-
|
|
83
|
+
dictionaries[namespace] || (
|
|
84
|
+
dictionaries[namespace] = await this.resolveDictionary(
|
|
85
|
+
language, namespace
|
|
86
|
+
)
|
|
87
|
+
)
|
|
88
|
+
const dictionary = /** @type {Record<string, string>} */ (
|
|
89
|
+
dictionaries[namespace])
|
|
56
90
|
|
|
57
91
|
node.textContent = dictionary[key] || node.textContent
|
|
58
92
|
}
|
|
59
93
|
}
|
|
60
94
|
|
|
95
|
+
/** @param {string} value
|
|
96
|
+
* @returns {TranslatedKey} */
|
|
61
97
|
parseKey (value) {
|
|
62
98
|
let key = value
|
|
63
99
|
let namespace = this.namespace
|
|
@@ -71,21 +107,36 @@ export class Translate extends Component {
|
|
|
71
107
|
return { key, namespace }
|
|
72
108
|
}
|
|
73
109
|
|
|
110
|
+
/** @param {string} language
|
|
111
|
+
* @param {string} namespace
|
|
112
|
+
* @returns {Promise<Record<string,string>>} */
|
|
74
113
|
async resolveDictionary (language, namespace) {
|
|
75
|
-
|
|
76
|
-
|
|
114
|
+
/** @type {DictionaryStore} */
|
|
115
|
+
const translations = this.dictionary
|
|
116
|
+
/** @type {Record<string, string>|null} */
|
|
117
|
+
let dictionary = /** @type {Record<string, string>|null} */ (
|
|
118
|
+
/** @type {unknown} */ (translations[language]?.[namespace]) || null)
|
|
77
119
|
|
|
78
120
|
if (dictionary !== null) return dictionary
|
|
79
121
|
|
|
80
|
-
if (!this.global.fetch) return {}
|
|
122
|
+
if (!this.global.fetch) return /** @type {Record<string, string>} */ ({})
|
|
81
123
|
|
|
82
124
|
const url = `${this.endpoint}/${language}/${namespace}.json`
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
125
|
+
try {
|
|
126
|
+
const response = await this.global.fetch(url)
|
|
127
|
+
if (response.ok === false) return /** @type {Record<string, string>} */ ({})
|
|
128
|
+
dictionary = /** @type {Record<string, string>} */ (
|
|
129
|
+
await response.json())
|
|
130
|
+
} catch (error) {
|
|
131
|
+
this.emit('error', error)
|
|
132
|
+
return /** @type {Record<string, string>} */ ({})
|
|
133
|
+
}
|
|
86
134
|
|
|
87
|
-
|
|
88
|
-
|
|
135
|
+
const cache = /** @type {Record<string, Record<string, Record<string, string>>>} */ (
|
|
136
|
+
/** @type {unknown} */ (translations))
|
|
137
|
+
cache[language] = cache[language] || {}
|
|
138
|
+
cache[language][namespace] = /** @type {Record<string, string>} */ (
|
|
139
|
+
/** @type {unknown} */ (dictionary || {}))
|
|
89
140
|
|
|
90
141
|
return dictionary
|
|
91
142
|
}
|