forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColumnBreakpoint.js
More file actions
130 lines (104 loc) · 3.64 KB
/
Copy pathColumnBreakpoint.js
File metadata and controls
130 lines (104 loc) · 3.64 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
/* 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 { PureComponent } from "react";
import classnames from "classnames";
import { showMenu } from "devtools-contextmenu";
import { getDocument } from "../../utils/editor";
import { breakpointItems, createBreakpointItems } from "./menus/breakpoints";
// eslint-disable-next-line max-len
import type { ColumnBreakpoint as ColumnBreakpointType } from "../../selectors/visibleColumnBreakpoints";
import type { BreakpointItemActions } from "./menus/breakpoints";
import type { Source, Context } from "../../types";
type Bookmark = {
clear: Function
};
type Props = {
cx: Context,
editor: Object,
source: Source,
columnBreakpoint: ColumnBreakpointType,
breakpointActions: BreakpointItemActions
};
const breakpointButton = document.createElement("button");
breakpointButton.innerHTML =
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 11 13" width="11" height="13"><path d="M5.07.5H1.5c-.54 0-1 .46-1 1v10c0 .54.46 1 1 1h3.57c.58 0 1.15-.26 1.53-.7l3.7-5.3-3.7-5.3C6.22.76 5.65.5 5.07.5z"/></svg>';
function makeBookmark({ breakpoint }, { onClick, onContextMenu }) {
const bp = breakpointButton.cloneNode(true);
const isActive = breakpoint && !breakpoint.disabled;
const isDisabled = breakpoint && breakpoint.disabled;
const condition = breakpoint && breakpoint.options.condition;
const logValue = breakpoint && breakpoint.options.logValue;
bp.className = classnames("column-breakpoint", {
"has-condition": condition,
"has-log": logValue,
active: isActive,
disabled: isDisabled
});
bp.setAttribute("title", logValue || condition || "");
bp.onclick = onClick;
// NOTE: flow does not know about oncontextmenu
(bp: any).oncontextmenu = onContextMenu;
return bp;
}
export default class ColumnBreakpoint extends PureComponent<Props> {
addColumnBreakpoint: Function;
bookmark: ?Bookmark;
addColumnBreakpoint = (nextProps: ?Props) => {
const { columnBreakpoint, source } = nextProps || this.props;
const sourceId = source.id;
const doc = getDocument(sourceId);
if (!doc) {
return;
}
const { line, column } = columnBreakpoint.location;
const widget = makeBookmark(columnBreakpoint, {
onClick: this.onClick,
onContextMenu: this.onContextMenu
});
this.bookmark = doc.setBookmark({ line: line - 1, ch: column }, { widget });
};
clearColumnBreakpoint = () => {
if (this.bookmark) {
this.bookmark.clear();
this.bookmark = null;
}
};
onClick = (event: MouseEvent) => {
event.stopPropagation();
event.preventDefault();
const { cx, columnBreakpoint, breakpointActions } = this.props;
if (columnBreakpoint.breakpoint) {
breakpointActions.removeBreakpoint(cx, columnBreakpoint.breakpoint);
} else {
breakpointActions.addBreakpoint(cx, columnBreakpoint.location);
}
};
onContextMenu = (event: MouseEvent) => {
event.stopPropagation();
event.preventDefault();
const {
cx,
columnBreakpoint: { breakpoint, location },
breakpointActions
} = this.props;
const items = breakpoint
? breakpointItems(cx, breakpoint, breakpointActions)
: createBreakpointItems(cx, location, breakpointActions);
showMenu(event, items);
};
componentDidMount() {
this.addColumnBreakpoint();
}
componentWillUnmount() {
this.clearColumnBreakpoint();
}
componentDidUpdate() {
this.clearColumnBreakpoint();
this.addColumnBreakpoint();
}
render() {
return null;
}
}