forked from hull-ships/hull-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharea.jsx
More file actions
64 lines (56 loc) · 1.66 KB
/
Copy patharea.jsx
File metadata and controls
64 lines (56 loc) · 1.66 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
import _ from "lodash";
import React, { Component, PropTypes } from "react";
import Codemirror from "./react-codemirror";
import stringify from "json-stable-stringify";
const nop = function nop() { };
export default class Area extends Component {
static defaultProps = {
highlight: [],
onChange: nop,
wrap: false,
javascript: true,
style: {}
}
static propTypes = {
highlight: React.PropTypes.array
}
componentDidUpdate() {
this.props.highlight.length && this.cm && this.cm.addOverlay({token:this.buildHighlighter()})
}
shouldComponentUpdate(nextProps, nextState) {
const { value } = this.props;
if (value === nextProps.value) return false;
return true;
}
buildHighlighter(){
const tokens = _.map(this.props.highlight, (t) => `("${t}":)` );
const rgs = `(${tokens.join("|")})`;
const rgx = new RegExp(rgs, "gi");
return function highlighter(stream) {
// https://codemirror.net/doc/manual.html#token
// https://codemirror.net/addon/search/search.js
stream.skipToEnd();
const match = rgx.exec(stream.string);
if (match && match.index) return "searching";
return undefined;
};
}
render() {
let { wrap, style, onChange, value } = this.props;
if (typeof value !== "string") value = stringify(value, { space: 2 });
return (<Codemirror
style={style}
ref = { c => this.cm = c && c.getCodeMirror() }
value={value}
onChange={onChange}
options={{
mode: {
name: this.props.javascript ? "javascript" : "application/ld+json",
json: true
},
lineWrapping: wrap,
readOnly: true
}}
/>);
}
}