forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.js
More file actions
511 lines (447 loc) · 12.8 KB
/
Copy pathcomponent.js
File metadata and controls
511 lines (447 loc) · 12.8 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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
const { Component, createFactory } = require("react");
const dom = require("react-dom-factories");
const { connect } = require("react-redux");
const { bindActionCreators } = require("redux");
import Components from "devtools-components";
const Tree = createFactory(Components.Tree);
require("./index.css");
const classnames = require("classnames");
const { MODE } = require("../reps/constants");
const Utils = require("./utils");
const {
getChildren,
getClosestGripNode,
getParent,
getValue,
nodeHasAccessors,
nodeHasProperties,
nodeIsBlock,
nodeIsDefaultProperties,
nodeIsFunction,
nodeIsGetter,
nodeIsMapEntry,
nodeIsMissingArguments,
nodeIsOptimizedOut,
nodeIsPrimitive,
nodeIsPrototype,
nodeIsSetter,
nodeIsUninitializedBinding,
nodeIsUnmappedBinding,
nodeIsUnscopedBinding,
nodeIsWindow,
nodeIsLongString,
nodeHasFullText
} = Utils.node;
import type { CachedNodes, Node, NodeContents, Props } from "./types";
type DefaultProps = {
autoExpandAll: boolean,
autoExpandDepth: number
};
// This implements a component that renders an interactive inspector
// for looking at JavaScript objects. It expects descriptions of
// objects from the protocol, and will dynamically fetch children
// properties as objects are expanded.
//
// If you want to inspect a single object, pass the name and the
// protocol descriptor of it:
//
// ObjectInspector({
// name: "foo",
// desc: { writable: true, ..., { value: { actor: "1", ... }}},
// ...
// })
//
// If you want multiple top-level objects (like scopes), you can pass
// an array of manually constructed nodes as `roots`:
//
// ObjectInspector({
// roots: [{ name: ... }, ...],
// ...
// });
// There are 3 types of nodes: a simple node with a children array, an
// object that has properties that should be children when they are
// fetched, and a primitive value that should be displayed with no
// children.
class ObjectInspector extends Component<Props> {
static defaultProps: DefaultProps;
constructor(props: Props) {
super();
this.cachedNodes = new Map();
const self: any = this;
self.getItemChildren = this.getItemChildren.bind(this);
self.renderTreeItem = this.renderTreeItem.bind(this);
self.setExpanded = this.setExpanded.bind(this);
self.focusItem = this.focusItem.bind(this);
self.getRoots = this.getRoots.bind(this);
}
shouldComponentUpdate(nextProps: Props) {
const { expandedPaths, focusedItem, loadedProperties, roots } = this.props;
if (roots !== nextProps.roots) {
// Since the roots changed, we assume the properties did as well,
// so we need to cleanup the component internal state.
// We can clear the cachedNodes to avoid bugs and memory leaks.
this.cachedNodes.clear();
// The rootsChanged action will be handled in a middleware to release the
// actors of the old roots, as well as cleanup the state properties
// (expandedPaths, loadedProperties, …).
this.props.rootsChanged(nextProps);
// We don't render right away since the state is going to be changed by
// the rootsChanged action. The `state.forceUpdate` flag will be set
// to `true` so we can execute a new render cycle with the cleaned state.
return false;
}
if (nextProps.forceUpdate === true) {
return true;
}
// We should update if:
// - there are new loaded properties
// - OR the expanded paths number changed, and all of them have properties
// loaded
// - OR the expanded paths number did not changed, but old and new sets
// differ
// - OR the focused node changed.
return (
loadedProperties.size !== nextProps.loadedProperties.size ||
(expandedPaths.size !== nextProps.expandedPaths.size &&
[...nextProps.expandedPaths].every(path =>
nextProps.loadedProperties.has(path)
)) ||
(expandedPaths.size === nextProps.expandedPaths.size &&
[...nextProps.expandedPaths].some(key => !expandedPaths.has(key))) ||
focusedItem !== nextProps.focusedItem
);
}
componentDidUpdate(prevProps) {
if (this.props.forceUpdate) {
// If the component was updated, we can then reset the forceUpdate flag.
this.props.forceUpdated();
}
}
componentWillUnmount() {
const { releaseActor } = this.props;
if (typeof releaseActor !== "function") {
return;
}
const { actors } = this.props;
for (const actor of actors) {
releaseActor(actor);
}
}
props: Props;
cachedNodes: CachedNodes;
getItemChildren(item: Node): Array<Node> | NodeContents | null {
const { loadedProperties } = this.props;
const { cachedNodes } = this;
return getChildren({
loadedProperties,
cachedNodes,
item
});
}
getRoots(): Array<Node> {
return this.props.roots;
}
getNodeKey(item: Node): string {
return item.path && typeof item.path.toString === "function"
? item.path.toString()
: JSON.stringify(item);
}
setExpanded(item: Node, expand: boolean) {
if (nodeIsPrimitive(item)) {
return;
}
const {
createObjectClient,
createLongStringClient,
loadedProperties,
nodeExpand,
nodeCollapse,
roots
} = this.props;
if (expand === true) {
const gripItem = getClosestGripNode(item);
const value = getValue(gripItem);
const isRoot =
value &&
roots.some(root => {
const rootValue = getValue(root);
return rootValue && rootValue.actor === value.actor;
});
const actor = isRoot || !value ? null : value.actor;
nodeExpand(
item,
actor,
loadedProperties,
createObjectClient,
createLongStringClient
);
} else {
nodeCollapse(item);
}
}
focusItem(item: Node) {
const { focusable = true, focusedItem, nodeFocus, onFocus } = this.props;
if (focusable && focusedItem !== item) {
nodeFocus(item);
if (focusedItem !== item && onFocus) {
onFocus(item);
}
}
}
// eslint-disable-next-line complexity
getTreeItemLabelAndValue(
item: Node,
depth: number,
expanded: boolean
): {
value?: string | Element,
label?: string
} {
const label = item.name;
const isPrimitive = nodeIsPrimitive(item);
if (nodeIsOptimizedOut(item)) {
return {
label,
value: dom.span({ className: "unavailable" }, "(optimized away)")
};
}
if (nodeIsUninitializedBinding(item)) {
return {
label,
value: dom.span({ className: "unavailable" }, "(uninitialized)")
};
}
if (nodeIsUnmappedBinding(item)) {
return {
label,
value: dom.span({ className: "unavailable" }, "(unmapped)")
};
}
if (nodeIsUnscopedBinding(item)) {
return {
label,
value: dom.span({ className: "unavailable" }, "(unscoped)")
};
}
const itemValue = getValue(item);
const unavailable =
isPrimitive &&
itemValue &&
itemValue.hasOwnProperty &&
itemValue.hasOwnProperty("unavailable");
if (nodeIsMissingArguments(item) || unavailable) {
return {
label,
value: dom.span({ className: "unavailable" }, "(unavailable)")
};
}
if (
nodeIsFunction(item) &&
!nodeIsGetter(item) &&
!nodeIsSetter(item) &&
(this.props.mode === MODE.TINY || !this.props.mode)
) {
return {
label: Utils.renderRep(item, {
...this.props,
functionName: label
})
};
}
if (
nodeHasProperties(item) ||
nodeHasAccessors(item) ||
nodeIsMapEntry(item) ||
nodeIsLongString(item) ||
isPrimitive
) {
const repProps = { ...this.props };
if (depth > 0) {
repProps.mode = this.props.mode === MODE.LONG ? MODE.SHORT : MODE.TINY;
}
if (expanded) {
repProps.mode = MODE.TINY;
}
if (nodeIsLongString(item)) {
repProps.member = {
open: nodeHasFullText(item) && expanded
};
}
return {
label,
value: Utils.renderRep(item, repProps)
};
}
return {
label
};
}
renderTreeItemLabel(
label,
item: Node,
depth: number,
focused: boolean,
expanded: boolean
) {
if (label === null || typeof label === "undefined") {
return null;
}
const { onLabelClick } = this.props;
return dom.span(
{
className: "object-label",
onClick: onLabelClick
? event => {
event.stopPropagation();
// If the user selected text, bail out.
if (Utils.selection.documentHasSelection()) {
return;
}
onLabelClick(item, {
depth,
focused,
expanded,
setExpanded: this.setExpanded
});
}
: undefined
},
label
);
}
getTreeTopElementProps(
item: Node,
depth: number,
focused: boolean,
expanded: boolean
): Object {
const { onCmdCtrlClick, onDoubleClick, dimTopLevelWindow } = this.props;
const parentElementProps: Object = {
className: classnames("node object-node", {
focused,
lessen:
!expanded &&
(nodeIsDefaultProperties(item) ||
nodeIsPrototype(item) ||
(dimTopLevelWindow === true && nodeIsWindow(item) && depth === 0)),
block: nodeIsBlock(item)
}),
onClick: e => {
if (e.metaKey && onCmdCtrlClick) {
onCmdCtrlClick(item, {
depth,
event: e,
focused,
expanded
});
e.stopPropagation();
return;
}
// If this click happened because the user selected some text, bail out.
// Note that if the user selected some text before and then clicks here,
// the previously selected text will be first unselected, unless the
// user clicked on the arrow itself. Indeed because the arrow is an
// image, clicking on it does not remove any existing text selection.
// So we need to also check if the arrow was clicked.
if (
Utils.selection.documentHasSelection() &&
!(e.target && e.target.matches && e.target.matches(".arrow"))
) {
e.stopPropagation();
}
}
};
if (onDoubleClick) {
parentElementProps.onDoubleClick = e => {
e.stopPropagation();
onDoubleClick(item, {
depth,
focused,
expanded
});
};
}
return parentElementProps;
}
renderTreeItem(
item: Node,
depth: number,
focused: boolean,
arrow: Object,
expanded: boolean
) {
const { label, value } = this.getTreeItemLabelAndValue(
item,
depth,
expanded
);
const labelElement = this.renderTreeItemLabel(
label,
item,
depth,
focused,
expanded
);
const delimiter =
value && labelElement
? dom.span({ className: "object-delimiter" }, ": ")
: null;
return dom.div(
this.getTreeTopElementProps(item, depth, focused, expanded),
arrow,
labelElement,
delimiter,
value
);
}
render() {
const {
autoExpandAll = true,
autoExpandDepth = 1,
focusable = true,
disableWrap = false,
expandedPaths,
focusedItem,
inline
} = this.props;
return Tree({
className: classnames({
inline,
nowrap: disableWrap,
"object-inspector": true
}),
autoExpandAll,
autoExpandDepth,
isExpanded: item => expandedPaths && expandedPaths.has(item.path),
isExpandable: item => nodeIsPrimitive(item) === false,
focused: focusedItem,
getRoots: this.getRoots,
getParent,
getChildren: this.getItemChildren,
getKey: this.getNodeKey,
onExpand: item => this.setExpanded(item, true),
onCollapse: item => this.setExpanded(item, false),
onFocus: focusable ? this.focusItem : null,
renderItem: this.renderTreeItem
});
}
}
function mapStateToProps(state, props) {
return {
actors: state.actors,
expandedPaths: state.expandedPaths,
// If the root changes, we want to pass a possibly new focusedItem property
focusedItem:
state.roots !== props.roots ? props.focusedItem : state.focusedItem,
loadedProperties: state.loadedProperties,
forceUpdate: state.forceUpdate
};
}
function mapDispatchToProps(dispatch) {
return bindActionCreators(require("./actions"), dispatch);
}
module.exports = connect(mapStateToProps, mapDispatchToProps)(ObjectInspector);