/
index.jsx
145 lines (130 loc) · 3.83 KB
/
index.jsx
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
/* global document, HTMLImageElement */
import classNames from 'classnames';
import map from 'lodash.map';
import trim from 'lodash.trim';
import CssSelectorGenerator from 'css-selector-generator';
import FaTags from 'react-icons/lib/fa/tags';
import PropTypes from 'prop-types';
import React from 'react';
import { connect } from 'react-redux';
import Highlighter from 'components/Highlighter';
import Sidebar from 'components/Sidebar';
import {
selectElement as _selectElement,
setExpanded as _setExpanded,
} from 'redux/proxyActions';
import {
currentAnnotationSelector,
currentFieldTypeSelector,
} from 'redux/selectors';
import styles from './style.scss';
const selectorGenerator = new CssSelectorGenerator({
// TODO(jrbotros): Exclude specific classes (e.g., `tether-*`) but allow others
selectors: ['id', 'tag', 'nthchild'],
});
class Main extends React.Component {
state = {
highlightTarget: null,
};
componentWillReceiveProps({ currentAnnotation }) {
if (currentAnnotation !== this.props.currentAnnotation) {
this.setState({ highlightTarget: null });
}
}
getWrappedText(node) {
if (node.nodeType === node.TEXT_NODE) {
return node.textContent;
}
if (node.tagName === 'BR') {
return this.props.currentFieldType === 'rich-text' ? '<br>' : '\n';
}
const text = map(node.childNodes, child => this.getWrappedText(child));
return trim(text.join(''));
}
getWrappedContent(node) {
switch (this.props.currentFieldType) {
case 'text':
case 'rich-text': {
return this.getWrappedText(node);
}
case 'image': {
return node instanceof HTMLImageElement && node.src ? node.src : null;
}
default: {
return null;
}
}
}
mouseOverMain = (event) => {
if (this.getWrappedContent(event.target)) {
this.setState({ highlightTarget: event.target });
}
}
clickMain = (event) => {
const content = this.getWrappedContent(event.target);
if (content) {
const selector = selectorGenerator.getSelector(event.target);
// Append the content if the `cmd` or `windows` key is pressed
this.props.selectElement(selector, content, event.metaKey);
event.preventDefault();
event.stopPropagation();
}
}
toggleOpen = () => {
this.setState({ highlightTarget: null });
this.props.setExpanded(!this.props.expanded);
}
render() {
return (
<div>
<div
className={
classNames(styles.kinaseSidebar, {
[styles.sidebarOpen]: this.props.expanded,
})
}
>
<Sidebar />
<div className={styles.sidebarToggle} onClick={this.toggleOpen}>
<FaTags />
</div>
</div>
<div
className={
classNames(styles.kinaseMain, {
[styles.sidebarOpen]: this.props.expanded,
})
}
onClick={this.clickMain}
onMouseOver={this.mouseOverMain}
dangerouslySetInnerHTML={{ __html: this.props.body }}
/>
<Highlighter target={this.state.highlightTarget} />
</div>
);
}
}
Main.propTypes = {
body: PropTypes.string.isRequired,
currentAnnotation: PropTypes.string,
currentFieldType: PropTypes.string,
expanded: PropTypes.bool.isRequired,
selectElement: PropTypes.func.isRequired,
setExpanded: PropTypes.func.isRequired,
};
Main.defaultProps = {
currentAnnotation: null,
currentFieldType: null,
};
export default connect(
state => ({
currentAnnotation: currentAnnotationSelector(state),
currentFieldType: currentFieldTypeSelector(state),
expanded: state.expanded,
}),
dispatch => ({
selectElement: (selector, content, append) => dispatch(
_selectElement(selector, content, append)),
setExpanded: expanded => dispatch(_setExpanded(expanded)),
}),
)(Main);