-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathapi.ts
398 lines (343 loc) · 10.8 KB
/
api.ts
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
import type {
Bridge,
} from '@vue-devtools/shared-utils'
import {
HookEvents,
PluginPermission,
StateEditor,
getPluginDefaultSettings,
getPluginSettings,
hasPluginPermission,
setPluginSettings,
} from '@vue-devtools/shared-utils'
import type {
App,
ComponentDevtoolsOptions,
ComponentInstance,
ComponentTreeNode,
CustomInspectorOptions,
DevtoolsPluginApi,
EditStatePayload,
HookPayloads,
TimelineEventOptions,
TimelineLayerOptions,
WithId,
} from '@vue/devtools-api'
import {
Hooks,
now,
} from '@vue/devtools-api'
import { DevtoolsHookable } from './hooks'
import type { BackendContext } from './backend-context'
import type { Plugin } from './plugin'
import type { DevtoolsBackend } from './backend'
import type { AppRecord } from './app-record'
const pluginOn: DevtoolsHookable[] = []
export class DevtoolsApi {
bridge: Bridge
ctx: BackendContext
backend: DevtoolsBackend
on: DevtoolsHookable
stateEditor: StateEditor = new StateEditor()
constructor(backend: DevtoolsBackend, ctx: BackendContext) {
this.backend = backend
this.ctx = ctx
this.bridge = ctx.bridge
this.on = new DevtoolsHookable(ctx)
}
async callHook<T extends Hooks>(eventType: T, payload: HookPayloads[T], ctx: BackendContext = this.ctx) {
payload = await this.on.callHandlers(eventType, payload, ctx)
for (const on of pluginOn) {
payload = await on.callHandlers(eventType, payload, ctx)
}
return payload
}
async transformCall(callName: string, ...args) {
const payload = await this.callHook(Hooks.TRANSFORM_CALL, {
callName,
inArgs: args,
outArgs: args.slice(),
})
return payload.outArgs
}
async getAppRecordName(app: App, defaultName: string): Promise<string> {
const payload = await this.callHook(Hooks.GET_APP_RECORD_NAME, {
app,
name: null,
})
if (payload.name) {
return payload.name
}
else {
return `App ${defaultName}`
}
}
async getAppRootInstance(app: App) {
const payload = await this.callHook(Hooks.GET_APP_ROOT_INSTANCE, {
app,
root: null,
})
return payload.root
}
async registerApplication(app: App) {
await this.callHook(Hooks.REGISTER_APPLICATION, {
app,
})
}
async walkComponentTree(instance: ComponentInstance, maxDepth = -1, filter: string = null, recursively = false) {
const payload = await this.callHook(Hooks.WALK_COMPONENT_TREE, {
componentInstance: instance,
componentTreeData: null,
maxDepth,
filter,
recursively,
})
return payload.componentTreeData
}
async visitComponentTree(instance: ComponentInstance, treeNode: ComponentTreeNode, filter: string = null, app: App) {
const payload = await this.callHook(Hooks.VISIT_COMPONENT_TREE, {
app,
componentInstance: instance,
treeNode,
filter,
})
return payload.treeNode
}
async walkComponentParents(instance: ComponentInstance) {
const payload = await this.callHook(Hooks.WALK_COMPONENT_PARENTS, {
componentInstance: instance,
parentInstances: [],
})
return payload.parentInstances
}
async inspectComponent(instance: ComponentInstance, app: App) {
const payload = await this.callHook(Hooks.INSPECT_COMPONENT, {
app,
componentInstance: instance,
instanceData: null,
})
return payload.instanceData
}
async getComponentBounds(instance: ComponentInstance) {
const payload = await this.callHook(Hooks.GET_COMPONENT_BOUNDS, {
componentInstance: instance,
bounds: null,
})
return payload.bounds
}
async getComponentName(instance: ComponentInstance) {
const payload = await this.callHook(Hooks.GET_COMPONENT_NAME, {
componentInstance: instance,
name: null,
})
return payload.name
}
async getComponentInstances(app: App) {
const payload = await this.callHook(Hooks.GET_COMPONENT_INSTANCES, {
app,
componentInstances: [],
})
return payload.componentInstances
}
async getElementComponent(element: HTMLElement | any) {
const payload = await this.callHook(Hooks.GET_ELEMENT_COMPONENT, {
element,
componentInstance: null,
})
return payload.componentInstance
}
async getComponentRootElements(instance: ComponentInstance) {
const payload = await this.callHook(Hooks.GET_COMPONENT_ROOT_ELEMENTS, {
componentInstance: instance,
rootElements: [],
})
return payload.rootElements
}
async editComponentState(instance: ComponentInstance, dotPath: string, type: string, state: EditStatePayload, app: App) {
const arrayPath = dotPath.split('.')
const payload = await this.callHook(Hooks.EDIT_COMPONENT_STATE, {
app,
componentInstance: instance,
path: arrayPath,
type,
state,
set: (object, path = arrayPath, value = state.value, cb?) => this.stateEditor.set(object, path, value, cb || this.stateEditor.createDefaultSetCallback(state)),
})
return payload.componentInstance
}
async getComponentDevtoolsOptions(instance: ComponentInstance): Promise<ComponentDevtoolsOptions> {
const payload = await this.callHook(Hooks.GET_COMPONENT_DEVTOOLS_OPTIONS, {
componentInstance: instance,
options: null,
})
return payload.options || {}
}
async getComponentRenderCode(instance: ComponentInstance): Promise<{
code: string
}> {
const payload = await this.callHook(Hooks.GET_COMPONENT_RENDER_CODE, {
componentInstance: instance,
code: null,
})
return {
code: payload.code,
}
}
async inspectTimelineEvent(eventData: TimelineEventOptions & WithId, app: App) {
const payload = await this.callHook(Hooks.INSPECT_TIMELINE_EVENT, {
event: eventData.event,
layerId: eventData.layerId,
app,
data: eventData.event.data,
all: eventData.all,
})
return payload.data
}
async clearTimeline() {
await this.callHook(Hooks.TIMELINE_CLEARED, {})
}
async getInspectorTree(inspectorId: string, app: App, filter: string) {
const payload = await this.callHook(Hooks.GET_INSPECTOR_TREE, {
inspectorId,
app,
filter,
rootNodes: [],
})
return payload.rootNodes
}
async getInspectorState(inspectorId: string, app: App, nodeId: string) {
const payload = await this.callHook(Hooks.GET_INSPECTOR_STATE, {
inspectorId,
app,
nodeId,
state: null,
})
return payload.state
}
async editInspectorState(inspectorId: string, app: App, nodeId: string, dotPath: string, type: string, state: EditStatePayload) {
const arrayPath = dotPath.split('.')
await this.callHook(Hooks.EDIT_INSPECTOR_STATE, {
inspectorId,
app,
nodeId,
path: arrayPath,
type,
state,
set: (object, path = arrayPath, value = state.value, cb?) => this.stateEditor.set(object, path, value, cb || this.stateEditor.createDefaultSetCallback(state)),
})
}
now() {
return now()
}
}
export class DevtoolsPluginApiInstance<TSettings = any> implements DevtoolsPluginApi<TSettings> {
bridge: Bridge
ctx: BackendContext
plugin: Plugin
appRecord: AppRecord
backendApi: DevtoolsApi
on: DevtoolsHookable
private defaultSettings: TSettings
constructor(plugin: Plugin, appRecord: AppRecord, ctx: BackendContext) {
this.bridge = ctx.bridge
this.ctx = ctx
this.plugin = plugin
this.appRecord = appRecord
this.backendApi = appRecord.backend.api
this.defaultSettings = getPluginDefaultSettings(plugin.descriptor.settings)
this.on = new DevtoolsHookable(ctx, plugin)
pluginOn.push(this.on)
}
// Plugin API
async notifyComponentUpdate(instance: ComponentInstance = null) {
if (!this.enabled || !this.hasPermission(PluginPermission.COMPONENTS)) {
return
}
if (instance) {
this.ctx.hook.emit(HookEvents.COMPONENT_UPDATED, ...await this.backendApi.transformCall(HookEvents.COMPONENT_UPDATED, instance))
}
else {
this.ctx.hook.emit(HookEvents.COMPONENT_UPDATED)
}
}
addTimelineLayer(options: TimelineLayerOptions) {
if (!this.enabled || !this.hasPermission(PluginPermission.TIMELINE)) {
return false
}
this.ctx.hook.emit(HookEvents.TIMELINE_LAYER_ADDED, options, this.plugin)
return true
}
addTimelineEvent(options: TimelineEventOptions) {
if (!this.enabled || !this.hasPermission(PluginPermission.TIMELINE)) {
return false
}
this.ctx.hook.emit(HookEvents.TIMELINE_EVENT_ADDED, options, this.plugin)
return true
}
addInspector(options: CustomInspectorOptions) {
if (!this.enabled || !this.hasPermission(PluginPermission.CUSTOM_INSPECTOR)) {
return false
}
this.ctx.hook.emit(HookEvents.CUSTOM_INSPECTOR_ADD, options, this.plugin)
return true
}
sendInspectorTree(inspectorId: string) {
if (!this.enabled || !this.hasPermission(PluginPermission.CUSTOM_INSPECTOR)) {
return false
}
this.ctx.hook.emit(HookEvents.CUSTOM_INSPECTOR_SEND_TREE, inspectorId, this.plugin)
return true
}
sendInspectorState(inspectorId: string) {
if (!this.enabled || !this.hasPermission(PluginPermission.CUSTOM_INSPECTOR)) {
return false
}
this.ctx.hook.emit(HookEvents.CUSTOM_INSPECTOR_SEND_STATE, inspectorId, this.plugin)
return true
}
selectInspectorNode(inspectorId: string, nodeId: string) {
if (!this.enabled || !this.hasPermission(PluginPermission.CUSTOM_INSPECTOR)) {
return false
}
this.ctx.hook.emit(HookEvents.CUSTOM_INSPECTOR_SELECT_NODE, inspectorId, nodeId, this.plugin)
return true
}
getComponentBounds(instance: ComponentInstance) {
return this.backendApi.getComponentBounds(instance)
}
getComponentName(instance: ComponentInstance) {
return this.backendApi.getComponentName(instance)
}
getComponentInstances(app: App) {
return this.backendApi.getComponentInstances(app)
}
highlightElement(instance: ComponentInstance) {
if (!this.enabled || !this.hasPermission(PluginPermission.COMPONENTS)) {
return false
}
this.ctx.hook.emit(HookEvents.COMPONENT_HIGHLIGHT, instance.__VUE_DEVTOOLS_UID__, this.plugin)
return true
}
unhighlightElement() {
if (!this.enabled || !this.hasPermission(PluginPermission.COMPONENTS)) {
return false
}
this.ctx.hook.emit(HookEvents.COMPONENT_UNHIGHLIGHT, this.plugin)
return true
}
getSettings(pluginId?: string) {
return getPluginSettings(pluginId ?? this.plugin.descriptor.id, this.defaultSettings)
}
setSettings(value: TSettings, pluginId?: string) {
setPluginSettings(pluginId ?? this.plugin.descriptor.id, value)
}
now() {
return now()
}
private get enabled() {
return hasPluginPermission(this.plugin.descriptor.id, PluginPermission.ENABLED)
}
private hasPermission(permission: PluginPermission) {
return hasPluginPermission(this.plugin.descriptor.id, permission)
}
}