forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionalPanel.js
More file actions
205 lines (178 loc) · 5.13 KB
/
Copy pathConditionalPanel.js
File metadata and controls
205 lines (178 loc) · 5.13 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
/* 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 ReactDOM from "react-dom";
import { connect } from "react-redux";
import "./ConditionalPanel.css";
import { toEditorLine } from "../../utils/editor";
import actions from "../../actions";
import {
getSelectedLocation,
getBreakpointForLine,
getConditionalPanelLine
} from "../../selectors";
type Props = {
breakpoint: ?Object,
selectedLocation: Object,
setBreakpointCondition: Function,
line: number,
editor: Object,
openConditionalPanel: () => void,
closeConditionalPanel: () => void
};
export class ConditionalPanel extends PureComponent<Props> {
cbPanel: null | Object;
input: ?HTMLInputElement;
panelNode: ?HTMLDivElement;
scrollParent: ?HTMLElement;
constructor() {
super();
this.cbPanel = null;
}
keepFocusOnInput() {
if (this.input) {
this.input.focus();
}
}
saveAndClose = () => {
if (this.input) {
this.setBreakpoint(this.input.value);
}
this.props.closeConditionalPanel();
};
onKey = (e: SyntheticKeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter") {
this.saveAndClose();
} else if (e.key === "Escape") {
this.props.closeConditionalPanel();
}
};
setBreakpoint(condition: string) {
const { selectedLocation, line } = this.props;
const sourceId = selectedLocation ? selectedLocation.sourceId : "";
const location = { sourceId, line };
return this.props.setBreakpointCondition(location, { condition });
}
clearConditionalPanel() {
if (this.cbPanel) {
this.cbPanel.clear();
this.cbPanel = null;
}
if (this.scrollParent) {
this.scrollParent.removeEventListener("scroll", this.repositionOnScroll);
}
}
repositionOnScroll = () => {
if (this.panelNode && this.scrollParent) {
const { scrollLeft } = this.scrollParent;
this.panelNode.style.transform = `translateX(${scrollLeft}px)`;
}
};
componentWillMount() {
if (this.props.line) {
return this.renderToWidget(this.props);
}
}
componentWillUpdate(nextProps: Props) {
if (nextProps.line) {
return this.renderToWidget(nextProps);
}
return this.clearConditionalPanel();
}
componentDidUpdate(prevProps: Props) {
this.keepFocusOnInput();
}
componentWillUnmount() {
// This is called if CodeMirror is re-initializing itself before the
// user closes the conditional panel. Clear the widget, and re-render it
// as soon as this component gets remounted
return this.clearConditionalPanel();
}
renderToWidget(props: Props) {
if (this.cbPanel) {
if (this.props.line && this.props.line == props.line) {
return props.closeConditionalPanel();
}
this.clearConditionalPanel();
}
const { selectedLocation, line, editor } = props;
const sourceId = selectedLocation ? selectedLocation.sourceId : "";
const editorLine = toEditorLine(sourceId, line);
this.cbPanel = editor.codeMirror.addLineWidget(
editorLine,
this.renderConditionalPanel(props),
{
coverGutter: true,
noHScroll: false
}
);
if (this.input) {
let parent: ?Node = this.input.parentNode;
while (parent) {
if (
parent instanceof HTMLElement &&
parent.classList.contains("CodeMirror-scroll")
) {
this.scrollParent = parent;
break;
}
parent = (parent.parentNode: ?Node);
}
if (this.scrollParent) {
this.scrollParent.addEventListener("scroll", this.repositionOnScroll);
this.repositionOnScroll();
}
}
}
renderConditionalPanel(props: Props) {
const { breakpoint } = props;
const condition = breakpoint ? breakpoint.condition : "";
const panel = document.createElement("div");
ReactDOM.render(
<div
className="conditional-breakpoint-panel"
onClick={() => this.keepFocusOnInput()}
onBlur={this.props.closeConditionalPanel}
ref={node => (this.panelNode = node)}
>
<div className="prompt">»</div>
<input
defaultValue={condition}
placeholder={L10N.getStr("editor.conditionalPanel.placeholder")}
onKeyDown={this.onKey}
ref={input => {
this.input = input;
this.keepFocusOnInput();
}}
/>
</div>,
panel
);
return panel;
}
render() {
return null;
}
}
const mapStateToProps = state => {
const line = getConditionalPanelLine(state);
const selectedLocation = getSelectedLocation(state);
return {
selectedLocation,
breakpoint: getBreakpointForLine(state, selectedLocation.sourceId, line),
line
};
};
const {
setBreakpointCondition,
openConditionalPanel,
closeConditionalPanel
} = actions;
const mapDispatchToProps = {
setBreakpointCondition,
openConditionalPanel,
closeConditionalPanel
};
export default connect(mapStateToProps, mapDispatchToProps)(ConditionalPanel);