Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit f64b4a1

Browse files
escatonjasonLaster
authored andcommitted
[Outline] Add "Copy Function" context menu item to functions (#5725)
* [Outline] Add "Copy Function" context menu item to functions * review fixes * perf optimization * Revert "perf optimization" This reverts commit 7b241bd. * update jest snapshots
1 parent 298fbce commit f64b4a1

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/components/PrimaryPanes/Outline.js

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@
66

77
import React, { Component } from "react";
88
import { bindActionCreators } from "redux";
9+
import { showMenu } from "devtools-contextmenu";
910
import { connect } from "react-redux";
11+
12+
import { copyToTheClipboard } from "../../utils/clipboard";
13+
import { findFunctionText } from "../../utils/function";
14+
1015
import actions from "../../actions";
11-
import { getSelectedSource, getSymbols } from "../../selectors";
16+
import {
17+
getSelectedSource,
18+
getSymbols,
19+
getSelectedLocation
20+
} from "../../selectors";
1221

1322
import "./Outline.css";
1423
import PreviewFunction from "../shared/PreviewFunction";
@@ -25,7 +34,10 @@ type Props = {
2534
selectLocation: ({ sourceId: string, line: number }) => void,
2635
selectedSource: ?SourceRecord,
2736
onAlphabetizeClick: Function,
28-
alphabetizeOutline: boolean
37+
alphabetizeOutline: boolean,
38+
getFunctionText: Function,
39+
flashLineRange: Function,
40+
selectedLocation: any
2941
};
3042

3143
export class Outline extends Component<Props> {
@@ -39,6 +51,45 @@ export class Outline extends Component<Props> {
3951
selectLocation({ sourceId: selectedSourceId, line: startLine });
4052
}
4153

54+
onContextMenu(event: SyntheticEvent<HTMLElement>, func: SymbolDeclaration) {
55+
event.stopPropagation();
56+
event.preventDefault();
57+
58+
const {
59+
selectedSource,
60+
getFunctionText,
61+
flashLineRange,
62+
selectedLocation
63+
} = this.props;
64+
65+
const copyFunctionKey = L10N.getStr("copyFunction.accesskey");
66+
const copyFunctionLabel = L10N.getStr("copyFunction.label");
67+
68+
if (!selectedSource) {
69+
return;
70+
}
71+
72+
const sourceLine = func.location.start.line;
73+
const functionText = getFunctionText(sourceLine);
74+
75+
const copyFunctionItem = {
76+
id: "node-menu-copy-function",
77+
label: copyFunctionLabel,
78+
accesskey: copyFunctionKey,
79+
disabled: !functionText,
80+
click: () => {
81+
flashLineRange({
82+
start: func.location.start.line,
83+
end: func.location.end.line,
84+
sourceId: selectedLocation.sourceId
85+
});
86+
return copyToTheClipboard(functionText);
87+
}
88+
};
89+
const menuOptions = [copyFunctionItem];
90+
showMenu(event, menuOptions);
91+
}
92+
4293
renderPlaceholder() {
4394
const placeholderMessage = this.props.selectedSource
4495
? L10N.getStr("outline.noFunctions")
@@ -61,6 +112,7 @@ export class Outline extends Component<Props> {
61112
key={`${name}:${location.start.line}:${location.start.column}`}
62113
className="outline-list__element"
63114
onClick={() => this.selectItem(location)}
115+
onContextMenu={e => this.onContextMenu(e, func)}
64116
>
65117
<span className="outline-list__element-icon">λ</span>
66118
<PreviewFunction func={{ name, parameterNames }} />
@@ -162,7 +214,14 @@ export default connect(
162214
const selectedSource = getSelectedSource(state);
163215
return {
164216
symbols: getSymbols(state, selectedSource && selectedSource.toJS()),
165-
selectedSource
217+
selectedSource,
218+
selectedLocation: getSelectedLocation(state),
219+
getFunctionText: line =>
220+
findFunctionText(
221+
line,
222+
selectedSource.toJS(),
223+
getSymbols(state, selectedSource.toJS())
224+
)
166225
};
167226
},
168227
dispatch => bindActionCreators(actions, dispatch)

src/components/tests/__snapshots__/Outline.spec.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ exports[`Outline should render a list of functions when properties change 1`] =
1212
className="outline-list__element"
1313
key="my_example_function1:21:undefined"
1414
onClick={[Function]}
15+
onContextMenu={[Function]}
1516
>
1617
<span
1718
className="outline-list__element-icon"
@@ -31,6 +32,7 @@ exports[`Outline should render a list of functions when properties change 1`] =
3132
className="outline-list__element"
3233
key="my_example_function2:22:undefined"
3334
onClick={[Function]}
35+
onContextMenu={[Function]}
3436
>
3537
<span
3638
className="outline-list__element-icon"
@@ -73,6 +75,7 @@ exports[`Outline should render ignore anonimous functions 1`] = `
7375
className="outline-list__element"
7476
key="my_example_function1:21:undefined"
7577
onClick={[Function]}
78+
onContextMenu={[Function]}
7679
>
7780
<span
7881
className="outline-list__element-icon"

0 commit comments

Comments
 (0)