-
-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathdevtools.ts
More file actions
492 lines (452 loc) · 13.1 KB
/
devtools.ts
File metadata and controls
492 lines (452 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import { isMessageAST } from '@intlify/core-base'
import {
isArray,
isBoolean,
isFunction,
isObject,
isString
} from '@intlify/shared'
import {
ComponentTreeNode,
Hooks,
setupDevtoolsPlugin
} from '@vue/devtools-api'
import type {
VueDevToolsIDs,
VueDevToolsTimelineEventPayloads,
VueDevToolsTimelineEvents
} from '@intlify/devtools-types'
import type {
ComponentStateBase,
CustomInspectorState,
DevtoolsPluginApi,
HookPayloads,
InspectedComponentData
} from '@vue/devtools-api'
import type {
App,
ComponentInternalInstance,
GenericComponentInstance
} from 'vue'
import type { Composer } from './composer'
import type { I18n, I18nInternal } from './i18n'
import type { VueI18nInternal } from './legacy'
type _I18n = I18n & I18nInternal
const VUE_I18N_COMPONENT_TYPES = 'vue-i18n: composer properties'
const VueDevToolsLabels: Record<VueDevToolsIDs, string> = {
'vue-devtools-plugin-vue-i18n': 'Vue I18n DevTools',
'vue-i18n-resource-inspector': 'Vue I18n DevTools',
'vue-i18n-timeline': 'Vue I18n'
}
const VueDevToolsPlaceholders: Record<'vue-i18n-resource-inspector', string> = {
'vue-i18n-resource-inspector': 'Search for scopes ...'
}
const VueDevToolsTimelineColors: Record<'vue-i18n-timeline', number> = {
'vue-i18n-timeline': 0xffcd19
}
let devtoolsApi: DevtoolsPluginApi<{}>
export async function enableDevTools(app: App, i18n: _I18n): Promise<boolean> {
return new Promise((resolve, reject) => {
try {
setupDevtoolsPlugin(
{
id: 'vue-devtools-plugin-vue-i18n',
label: VueDevToolsLabels['vue-devtools-plugin-vue-i18n'],
packageName: 'vue-i18n',
homepage: 'https://vue-i18n.intlify.dev',
logo: 'https://vue-i18n.intlify.dev/vue-i18n-devtools-logo.png',
componentStateTypes: [VUE_I18N_COMPONENT_TYPES],
app: app as any // eslint-disable-line @typescript-eslint/no-explicit-any
},
api => {
devtoolsApi = api
api.on.visitComponentTree(({ componentInstance, treeNode }) => {
updateComponentTreeTags(componentInstance, treeNode, i18n)
})
api.on.inspectComponent(({ componentInstance, instanceData }) => {
if (componentInstance.__VUE_I18N__ && instanceData) {
if (i18n.mode === 'legacy') {
// ignore global scope on legacy mode
if (
componentInstance.__VUE_I18N__ !==
(i18n.global as unknown as VueI18nInternal).__composer
) {
inspectComposer(
instanceData,
componentInstance.__VUE_I18N__ as Composer
)
}
} else {
inspectComposer(
instanceData,
componentInstance.__VUE_I18N__ as Composer
)
}
}
})
api.addInspector({
id: 'vue-i18n-resource-inspector',
label: VueDevToolsLabels['vue-i18n-resource-inspector'],
icon: 'language',
treeFilterPlaceholder:
VueDevToolsPlaceholders['vue-i18n-resource-inspector']
})
api.on.getInspectorTree(payload => {
if (
payload.app === app &&
payload.inspectorId === 'vue-i18n-resource-inspector'
) {
registerScope(payload, i18n)
}
})
const roots = new Map<
App,
ComponentInternalInstance | GenericComponentInstance
>()
api.on.getInspectorState(async payload => {
if (
payload.app === app &&
payload.inspectorId === 'vue-i18n-resource-inspector'
) {
api.unhighlightElement()
inspectScope(payload, i18n)
if (payload.nodeId === 'global') {
if (!roots.has(payload.app)) {
const [root] = await api.getComponentInstances(payload.app)
roots.set(payload.app, root)
}
api.highlightElement(roots.get(payload.app))
} else {
const instance = getComponentInstance(payload.nodeId, i18n)
instance && api.highlightElement(instance)
}
}
})
api.on.editInspectorState(payload => {
if (
payload.app === app &&
payload.inspectorId === 'vue-i18n-resource-inspector'
) {
editScope(payload, i18n)
}
})
api.addTimelineLayer({
id: 'vue-i18n-timeline',
label: VueDevToolsLabels['vue-i18n-timeline'],
color: VueDevToolsTimelineColors['vue-i18n-timeline']
})
resolve(true)
}
)
} catch (e) {
console.error(e)
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
reject(false)
}
})
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getI18nScopeLable(instance: any): string {
return (
instance.type.name ||
instance.type.displayName ||
instance.type.__file ||
'Anonymous'
)
}
function updateComponentTreeTags(
instance: any, // eslint-disable-line @typescript-eslint/no-explicit-any
treeNode: ComponentTreeNode,
i18n: _I18n
): void {
// prettier-ignore
const global = i18n.mode === 'composition'
? i18n.global
: (i18n.global as unknown as VueI18nInternal).__composer
if (instance && instance.__VUE_I18N__) {
// add custom tags local scope only
if (instance.__VUE_I18N__ !== global) {
const tag = {
label: `i18n (${getI18nScopeLable(instance)} Scope)`,
textColor: 0x000000,
backgroundColor: 0xffcd19
}
treeNode.tags.push(tag)
}
}
}
function inspectComposer(
instanceData: InspectedComponentData,
composer: Composer
): void {
const type = VUE_I18N_COMPONENT_TYPES
instanceData.state.push({
type,
key: 'locale',
editable: true,
value: composer.locale.value
})
instanceData.state.push({
type,
key: 'availableLocales',
editable: false,
value: composer.availableLocales
})
instanceData.state.push({
type,
key: 'fallbackLocale',
editable: true,
value: composer.fallbackLocale.value
})
instanceData.state.push({
type,
key: 'inheritLocale',
editable: true,
value: composer.inheritLocale
})
instanceData.state.push({
type,
key: 'messages',
editable: false,
value: getLocaleMessageValue(composer.messages.value)
})
if (!__LITE__) {
instanceData.state.push({
type,
key: 'datetimeFormats',
editable: false,
value: composer.datetimeFormats.value
})
instanceData.state.push({
type,
key: 'numberFormats',
editable: false,
value: composer.numberFormats.value
})
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getLocaleMessageValue(messages: any): Record<string, unknown> {
const value: Record<string, unknown> = {}
Object.keys(messages).forEach((key: string) => {
const v: unknown = messages[key]
if (isFunction(v) && 'source' in v) {
value[key] = getMessageFunctionDetails(v)
} else if (isMessageAST(v) && v.loc && v.loc.source) {
value[key] = v.loc.source
} else if (isObject(v)) {
value[key] = getLocaleMessageValue(v)
} else {
value[key] = v
}
})
return value
}
const ESC: Record<string, string> = {
'<': '<',
'>': '>',
'"': '"',
'&': '&'
}
function escape(s: string): string {
return s.replace(/[<>"&]/g, escapeChar)
}
function escapeChar(a: string): string {
return ESC[a] || a
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getMessageFunctionDetails(func: any): Record<string, unknown> {
const argString = func.source ? `("${escape(func.source)}")` : `(?)`
return {
_custom: {
type: 'function',
display: `<span>ƒ</span> ${argString}`
}
}
}
function registerScope(
payload: HookPayloads[Hooks.GET_INSPECTOR_TREE],
i18n: _I18n
): void {
payload.rootNodes.push({
id: 'global',
label: 'Global Scope'
})
// prettier-ignore
const global = i18n.mode === 'composition'
? i18n.global
: (i18n.global as unknown as VueI18nInternal).__composer
for (const [keyInstance, instance] of i18n.__instances) {
// prettier-ignore
const composer = i18n.mode === 'composition'
? instance
: (instance as unknown as VueI18nInternal).__composer
if (global === composer) {
continue
}
payload.rootNodes.push({
id: composer.id.toString(),
label: `${getI18nScopeLable(keyInstance)} Scope`
})
}
}
function getComponentInstance(
nodeId: string,
i18n: _I18n
): ComponentInternalInstance | GenericComponentInstance | null {
let instance: ComponentInternalInstance | GenericComponentInstance | null =
null
if (nodeId !== 'global') {
for (const [component, composer] of i18n.__instances.entries()) {
if (composer.id.toString() === nodeId) {
instance = component
break
}
}
}
return instance
}
function getComposer(nodeId: string, i18n: _I18n): Composer | null {
if (nodeId === 'global') {
return i18n.mode === 'composition'
? (i18n.global as unknown as Composer)
: (i18n.global as unknown as VueI18nInternal).__composer
} else {
const instance = Array.from(i18n.__instances.values()).find(
item => item.id.toString() === nodeId
)
if (instance) {
return i18n.mode === 'composition'
? (instance as unknown as Composer)
: (instance as unknown as VueI18nInternal).__composer
} else {
return null
}
}
}
function inspectScope(
payload: HookPayloads[Hooks.GET_INSPECTOR_STATE],
i18n: _I18n
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): any {
const composer = getComposer(payload.nodeId, i18n)
if (composer) {
// TODO:
// eslint-disable-next-line @typescript-eslint/no-explicit-any
payload.state = makeScopeInspectState(composer as any)
}
return null
}
function makeScopeInspectState(composer: Composer): CustomInspectorState {
const state: CustomInspectorState = {}
const localeType = 'Locale related info'
const localeStates: ComponentStateBase[] = [
{
type: localeType,
key: 'locale',
editable: true,
value: composer.locale.value
},
{
type: localeType,
key: 'fallbackLocale',
editable: true,
value: composer.fallbackLocale.value
},
{
type: localeType,
key: 'availableLocales',
editable: false,
value: composer.availableLocales
},
{
type: localeType,
key: 'inheritLocale',
editable: true,
value: composer.inheritLocale
}
]
state[localeType] = localeStates
const localeMessagesType = 'Locale messages info'
const localeMessagesStates: ComponentStateBase[] = [
{
type: localeMessagesType,
key: 'messages',
editable: false,
value: getLocaleMessageValue(composer.messages.value)
}
]
state[localeMessagesType] = localeMessagesStates
if (!__LITE__) {
const datetimeFormatsType = 'Datetime formats info'
const datetimeFormatsStates: ComponentStateBase[] = [
{
type: datetimeFormatsType,
key: 'datetimeFormats',
editable: false,
value: composer.datetimeFormats.value
}
]
state[datetimeFormatsType] = datetimeFormatsStates
const numberFormatsType = 'Datetime formats info'
const numberFormatsStates: ComponentStateBase[] = [
{
type: numberFormatsType,
key: 'numberFormats',
editable: false,
value: composer.numberFormats.value
}
]
state[numberFormatsType] = numberFormatsStates
}
return state
}
export function addTimelineEvent(
event: VueDevToolsTimelineEvents,
payload?: VueDevToolsTimelineEventPayloads[VueDevToolsTimelineEvents]
): void {
if (devtoolsApi) {
let groupId: string | undefined
if (payload && 'groupId' in payload) {
groupId = payload.groupId
delete payload.groupId
}
devtoolsApi.addTimelineEvent({
layerId: 'vue-i18n-timeline',
event: {
title: event,
groupId,
time: Date.now(),
meta: {},
data: payload || {},
logType:
event === 'compile-error'
? 'error'
: event === 'fallback' || event === 'missing'
? 'warning'
: 'default'
}
})
}
}
function editScope(
payload: HookPayloads[Hooks.EDIT_INSPECTOR_STATE],
i18n: _I18n
): void {
const composer = getComposer(payload.nodeId, i18n)
if (composer) {
const [field] = payload.path
if (field === 'locale' && isString(payload.state.value)) {
composer.locale.value = payload.state.value
} else if (
field === 'fallbackLocale' &&
(isString(payload.state.value) ||
isArray(payload.state.value) ||
isObject(payload.state.value))
) {
composer.fallbackLocale.value = payload.state.value
} else if (field === 'inheritLocale' && isBoolean(payload.state.value)) {
composer.inheritLocale = payload.state.value
}
}
}