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
|
/* 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/>. */
import PropTypes from "devtools/client/shared/vendor/react-prop-types";
import React, { PureComponent } from "devtools/client/shared/vendor/react";
import { connect } from "devtools/client/shared/vendor/react-redux";
import Popup from "./Popup";
import { getIsCurrentThreadPaused } from "../../../selectors/index";
import actions from "../../../actions/index";
const EXCEPTION_MARKER = "mark-text-exception";
class Preview extends PureComponent {
target = null;
constructor(props) {
super(props);
this.state = { selecting: false };
}
static get propTypes() {
return {
editor: PropTypes.object.isRequired,
editorRef: PropTypes.object.isRequired,
isPaused: PropTypes.bool.isRequired,
getExceptionPreview: PropTypes.func.isRequired,
getPreview: PropTypes.func,
};
}
componentDidMount() {
this.updateListeners();
}
componentWillUnmount() {
const { codeMirror } = this.props.editor;
const codeMirrorWrapper = codeMirror.getWrapperElement();
codeMirror.off("tokenenter", this.onTokenEnter);
codeMirror.off("scroll", this.onScroll);
codeMirrorWrapper.removeEventListener("mouseup", this.onMouseUp);
codeMirrorWrapper.removeEventListener("mousedown", this.onMouseDown);
}
updateListeners(prevProps) {
const { codeMirror } = this.props.editor;
const codeMirrorWrapper = codeMirror.getWrapperElement();
codeMirror.on("tokenenter", this.onTokenEnter);
codeMirror.on("scroll", this.onScroll);
codeMirrorWrapper.addEventListener("mouseup", this.onMouseUp);
codeMirrorWrapper.addEventListener("mousedown", this.onMouseDown);
}
// Note that these events are emitted by utils/editor/tokens.js
onTokenEnter = async ({ target, tokenPos }) => {
// Use a temporary object to uniquely identify the asynchronous processing of this user event
// and bail out if we started hovering another token.
const tokenId = {};
this.currentTokenId = tokenId;
const { editor, getPreview, getExceptionPreview } = this.props;
const isTargetException = target.classList.contains(EXCEPTION_MARKER);
let preview;
if (isTargetException) {
preview = await getExceptionPreview(target, tokenPos, editor.codeMirror);
}
if (!preview && this.props.isPaused && !this.state.selecting) {
preview = await getPreview(target, tokenPos, editor.codeMirror);
}
// Prevent modifying state and showing this preview if we started hovering another token
if (!preview || this.currentTokenId !== tokenId) {
return;
}
this.setState({ preview });
};
onMouseUp = () => {
if (this.props.isPaused) {
this.setState({ selecting: false });
}
};
onMouseDown = () => {
if (this.props.isPaused) {
this.setState({ selecting: true });
}
};
onScroll = () => {
if (this.props.isPaused) {
this.clearPreview();
}
};
clearPreview = () => {
this.setState({ preview: null });
};
render() {
const { preview } = this.state;
if (!preview || this.state.selecting) {
return null;
}
return React.createElement(Popup, {
preview: preview,
editor: this.props.editor,
editorRef: this.props.editorRef,
clearPreview: this.clearPreview,
});
}
}
const mapStateToProps = state => {
return {
isPaused: getIsCurrentThreadPaused(state),
};
};
export default connect(mapStateToProps, {
addExpression: actions.addExpression,
getPreview: actions.getPreview,
getExceptionPreview: actions.getExceptionPreview,
})(Preview);
|