forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabs.js
More file actions
517 lines (450 loc) · 14.4 KB
/
Tabs.js
File metadata and controls
517 lines (450 loc) · 14.4 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
512
513
514
515
516
517
/* 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
import React, { PureComponent } from "react";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import * as I from "immutable";
import {
getSelectedSource,
getSourcesForTabs,
getActiveSearch,
getSearchTabs,
getSourceMetaData
} from "../../selectors";
import { isVisible } from "../../utils/ui";
import { getFilename, getFileURL, isPretty } from "../../utils/source";
import classnames from "classnames";
import actions from "../../actions";
import CloseButton from "../shared/Button/Close";
import { showMenu, buildMenu } from "devtools-contextmenu";
import { debounce } from "lodash";
import "./Tabs.css";
import PaneToggleButton from "../shared/Button/PaneToggle";
import Dropdown from "../shared/Dropdown";
import type { List } from "immutable";
import type { SourceRecord } from "../../reducers/sources";
import type { ActiveSearchType } from "../../reducers/ui";
import type { SourceMetaDataMap } from "../../reducers/ast";
type SourcesList = List<SourceRecord>;
/*
* Finds the hidden tabs by comparing the tabs' top offset.
* hidden tabs will have a great top offset.
*
* @param sourceTabs Immutable.list
* @param sourceTabEls HTMLCollection
*
* @returns Immutable.list
*/
function getHiddenTabs(sourceTabs: SourcesList, sourceTabEls) {
sourceTabEls = [].slice.call(sourceTabEls);
function getTopOffset() {
const topOffsets = sourceTabEls.map(t => t.getBoundingClientRect().top);
return Math.min(...topOffsets);
}
function hasTopOffset(el) {
// adding 10px helps account for cases where the tab might be offset by
// styling such as selected tabs which don't have a border.
const tabTopOffset = getTopOffset();
return el.getBoundingClientRect().top > tabTopOffset + 10;
}
return sourceTabs.filter((tab, index) => {
const element = sourceTabEls[index];
return element && hasTopOffset(element);
});
}
/**
* Clipboard function taken from
* https://dxr.mozilla.org/mozilla-central/source/devtools/shared/platform/content/clipboard.js
*/
function copyToTheClipboard(string) {
const doCopy = function(e: any) {
e.clipboardData.setData("text/plain", string);
e.preventDefault();
};
document.addEventListener("copy", doCopy);
document.execCommand("copy", false, null);
document.removeEventListener("copy", doCopy);
}
type Props = {
sourceTabs: SourcesList,
searchTabs: List<ActiveSearchType>,
selectedSource: SourceRecord,
selectSource: Object => void,
moveTab: (string, number) => void,
closeTab: string => void,
closeTabs: (List<string>) => void,
setActiveSearch: (?ActiveSearchType) => void,
closeActiveSearch: () => void,
activeSearch: string,
togglePrettyPrint: string => void,
togglePaneCollapse: () => void,
toggleActiveSearch: (?string) => void,
showSource: string => void,
horizontal: boolean,
startPanelCollapsed: boolean,
endPanelCollapsed: boolean,
searchOn: boolean,
sourceTabsMetaData: {
[key: string]: SourceMetaDataMap
}
};
type State = {
dropdownShown: boolean,
hiddenSourceTabs: SourcesList
};
class SourceTabs extends PureComponent<Props, State> {
onTabContextMenu: Function;
showContextMenu: Function;
updateHiddenSourceTabs: Function;
toggleSourcesDropdown: Function;
renderDropdownSource: Function;
renderTabs: Function;
renderTab: Function;
renderSourceTab: Function;
renderSearchTab: Function;
renderDropDown: Function;
renderStartPanelToggleButton: Function;
renderEndPanelToggleButton: Function;
onResize: Function;
constructor(props) {
super(props);
this.state = {
dropdownShown: false,
hiddenSourceTabs: I.List()
};
this.onTabContextMenu = this.onTabContextMenu.bind(this);
this.showContextMenu = this.showContextMenu.bind(this);
this.updateHiddenSourceTabs = this.updateHiddenSourceTabs.bind(this);
this.toggleSourcesDropdown = this.toggleSourcesDropdown.bind(this);
this.renderDropdownSource = this.renderDropdownSource.bind(this);
this.renderTabs = this.renderTabs.bind(this);
this.renderSourceTab = this.renderSourceTab.bind(this);
this.renderSearchTab = this.renderSearchTab.bind(this);
this.renderDropDown = this.renderDropdown.bind(this);
this.renderStartPanelToggleButton = this.renderStartPanelToggleButton.bind(
this
);
this.renderEndPanelToggleButton = this.renderEndPanelToggleButton.bind(
this
);
this.onResize = debounce(() => {
this.updateHiddenSourceTabs();
});
}
componentDidUpdate(prevProps) {
if (!(prevProps === this.props)) {
this.updateHiddenSourceTabs();
}
}
componentDidMount() {
this.updateHiddenSourceTabs();
window.addEventListener("resize", this.onResize);
}
componentWillUnmount() {
window.removeEventListener("resize", this.onResize);
}
onTabContextMenu(event, tab: string) {
event.preventDefault();
this.showContextMenu(event, tab);
}
showContextMenu(e, tab) {
const {
closeTab,
closeTabs,
sourceTabs,
showSource,
togglePrettyPrint
} = this.props;
const closeTabLabel = L10N.getStr("sourceTabs.closeTab");
const closeOtherTabsLabel = L10N.getStr("sourceTabs.closeOtherTabs");
const closeTabsToEndLabel = L10N.getStr("sourceTabs.closeTabsToEnd");
const closeAllTabsLabel = L10N.getStr("sourceTabs.closeAllTabs");
const revealInTreeLabel = L10N.getStr("sourceTabs.revealInTree");
const copyLinkLabel = L10N.getStr("copySourceUri2");
const prettyPrintLabel = L10N.getStr("sourceTabs.prettyPrint");
const closeTabKey = L10N.getStr("sourceTabs.closeTab.accesskey");
const closeOtherTabsKey = L10N.getStr(
"sourceTabs.closeOtherTabs.accesskey"
);
const closeTabsToEndKey = L10N.getStr(
"sourceTabs.closeTabsToEnd.accesskey"
);
const closeAllTabsKey = L10N.getStr("sourceTabs.closeAllTabs.accesskey");
const revealInTreeKey = L10N.getStr("sourceTabs.revealInTree.accesskey");
const copyLinkKey = L10N.getStr("copySourceUri2.accesskey");
const prettyPrintKey = L10N.getStr("sourceTabs.prettyPrint.accesskey");
const tabs = sourceTabs.map(t => t.get("id"));
const otherTabs = sourceTabs.filter(t => t.get("id") !== tab);
const sourceTab = sourceTabs.find(t => t.get("id") == tab);
const tabURLs = sourceTabs.map(thisTab => thisTab.get("url"));
const otherTabURLs = otherTabs.map(thisTab => thisTab.get("url"));
if (!sourceTab) {
return;
}
const isPrettySource = isPretty(sourceTab);
const closeTabMenuItem = {
id: "node-menu-close-tab",
label: closeTabLabel,
accesskey: closeTabKey,
disabled: false,
click: () => closeTab(sourceTab.get("url"))
};
const closeOtherTabsMenuItem = {
id: "node-menu-close-other-tabs",
label: closeOtherTabsLabel,
accesskey: closeOtherTabsKey,
disabled: false,
click: () => closeTabs(otherTabURLs)
};
const closeTabsToEndMenuItem = {
id: "node-menu-close-tabs-to-end",
label: closeTabsToEndLabel,
accesskey: closeTabsToEndKey,
disabled: false,
click: () => {
const tabIndex = tabs.findIndex(t => t == tab);
closeTabs(tabURLs.filter((t, i) => i > tabIndex));
}
};
const closeAllTabsMenuItem = {
id: "node-menu-close-all-tabs",
label: closeAllTabsLabel,
accesskey: closeAllTabsKey,
disabled: false,
click: () => closeTabs(tabURLs)
};
const showSourceMenuItem = {
id: "node-menu-show-source",
label: revealInTreeLabel,
accesskey: revealInTreeKey,
disabled: false,
click: () => showSource(tab)
};
const copySourceUri2 = {
id: "node-menu-copy-source-url",
label: copyLinkLabel,
accesskey: copyLinkKey,
disabled: false,
click: () => copyToTheClipboard(sourceTab.get("url"))
};
const prettyPrint = {
id: "node-menu-pretty-print",
label: prettyPrintLabel,
accesskey: prettyPrintKey,
disabled: false,
click: () => togglePrettyPrint(sourceTab.get("id"))
};
const items = [
{ item: closeTabMenuItem },
{ item: closeOtherTabsMenuItem, hidden: () => tabs.size === 1 },
{
item: closeTabsToEndMenuItem,
hidden: () => tabs.some((t, i) => t === tab && tabs.size - 1 === i)
},
{ item: closeAllTabsMenuItem },
{ item: { type: "separator" } },
{ item: copySourceUri2 }
];
if (!isPrettySource) {
items.push({ item: showSourceMenuItem });
items.push({ item: prettyPrint });
}
showMenu(e, buildMenu(items));
}
/*
* Updates the hiddenSourceTabs state, by
* finding the source tabs which are wrapped and are not on the top row.
*/
updateHiddenSourceTabs() {
if (!this.refs.sourceTabs) {
return;
}
const { selectedSource, sourceTabs, moveTab } = this.props;
const sourceTabEls = this.refs.sourceTabs.children;
const hiddenSourceTabs = getHiddenTabs(sourceTabs, sourceTabEls);
if (isVisible() && hiddenSourceTabs.indexOf(selectedSource) !== -1) {
return moveTab(selectedSource.get("url"), 0);
}
this.setState({ hiddenSourceTabs });
}
toggleSourcesDropdown(e) {
this.setState({
dropdownShown: !this.state.dropdownShown
});
}
getIconClass(source: SourceRecord) {
if (isPretty(source)) {
return "prettyPrint";
}
if (source.get("isBlackBoxed")) {
return "blackBox";
}
return "file";
}
renderDropdownSource(source: SourceRecord) {
const { selectSource } = this.props;
const filename = getFilename(source.toJS());
const onClick = () => selectSource(source.get("id"));
return (
<li key={source.get("id")} onClick={onClick}>
<img className={`dropdown-icon ${this.getIconClass(source)}`} />
{filename}
</li>
);
}
renderTabs() {
const { sourceTabs } = this.props;
if (!sourceTabs) {
return;
}
return (
<div className="source-tabs" ref="sourceTabs">
{sourceTabs.map(this.renderSourceTab)}
</div>
);
}
isProjectSearchEnabled() {
return this.props.activeSearch === "project";
}
isSourceSearchEnabled() {
return this.props.activeSearch === "source";
}
renderSearchTab(source: ActiveSearchType) {
const { closeTab, closeActiveSearch, setActiveSearch } = this.props;
function tabName(tab) {
return `${tab} search results`;
}
function onClickClose(ev) {
ev.stopPropagation();
closeActiveSearch();
closeTab(source);
}
const className = classnames("source-tab", {
active: this.isProjectSearchEnabled() || this.isSourceSearchEnabled(),
pretty: false
});
return (
<div
className={className}
key={source}
onClick={() => setActiveSearch(source)}
onContextMenu={e => this.onTabContextMenu(e, source)}
title={tabName(source)}
>
<div className="filename">{tabName(source)}</div>
<CloseButton
handleClick={onClickClose}
tooltip={L10N.getStr("sourceTabs.closeTabButtonTooltip")}
/>
</div>
);
}
renderSourceTab(source: SourceRecord) {
const { selectedSource, selectSource, closeTab } = this.props;
const filename = getFilename(source.toJS());
const active =
selectedSource &&
source.get("id") == selectedSource.get("id") &&
(!this.isProjectSearchEnabled() && !this.isSourceSearchEnabled());
const isPrettyCode = isPretty(source);
const sourceAnnotation = this.getSourceAnnotation(source);
function onClickClose(ev) {
ev.stopPropagation();
closeTab(source.get("url"));
}
const className = classnames("source-tab", {
active,
pretty: isPrettyCode
});
return (
<div
className={className}
key={source.get("id")}
onClick={() => selectSource(source.get("id"))}
onContextMenu={e => this.onTabContextMenu(e, source.get("id"))}
title={getFileURL(source.toJS())}
>
{sourceAnnotation}
<div className="filename">{filename}</div>
<CloseButton
handleClick={onClickClose}
tooltip={L10N.getStr("sourceTabs.closeTabButtonTooltip")}
/>
</div>
);
}
renderDropdown() {
const hiddenSourceTabs = this.state.hiddenSourceTabs;
if (!hiddenSourceTabs || hiddenSourceTabs.size == 0) {
return null;
}
const Panel = <ul>{hiddenSourceTabs.map(this.renderDropdownSource)}</ul>;
return <Dropdown panel={Panel} icon={"»"} />;
}
renderStartPanelToggleButton() {
return (
<PaneToggleButton
position="start"
collapsed={!this.props.startPanelCollapsed}
handleClick={this.props.togglePaneCollapse}
/>
);
}
renderEndPanelToggleButton() {
if (!this.props.horizontal) {
return;
}
return (
<PaneToggleButton
position="end"
collapsed={!this.props.endPanelCollapsed}
handleClick={this.props.togglePaneCollapse}
horizontal={this.props.horizontal}
/>
);
}
getSourceAnnotation(source) {
const sourceId = source.get("id");
const sourceMetaData = this.props.sourceTabsMetaData[sourceId];
if (sourceMetaData && sourceMetaData.isReactComponent) {
return <img className="react" />;
}
if (isPretty(source)) {
return <img className="prettyPrint" />;
}
if (source.get("isBlackBoxed")) {
return <img className="blackBox" />;
}
}
render() {
return (
<div className="source-header">
{this.renderStartPanelToggleButton()}
{this.renderTabs()}
{this.renderDropdown()}
{this.renderEndPanelToggleButton()}
</div>
);
}
}
export default connect(
state => {
const sourceTabs = getSourcesForTabs(state);
const sourceTabsMetaData = {};
sourceTabs.forEach(source => {
const sourceId = source ? source.get("id") : "";
sourceTabsMetaData[sourceId] = getSourceMetaData(state, sourceId);
});
return {
selectedSource: getSelectedSource(state),
searchTabs: getSearchTabs(state),
sourceTabs: sourceTabs,
activeSearch: getActiveSearch(state),
searchOn: getActiveSearch(state) === "source",
sourceTabsMetaData: sourceTabsMetaData
};
},
dispatch => bindActionCreators(actions, dispatch)
)(SourceTabs);