-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathCodeEditor.js
145 lines (118 loc) · 3.9 KB
/
CodeEditor.js
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
import PropTypes from 'prop-types';
import React from 'react';
import Frame from 'react-frame-component';
import styled from 'styled-components';
import get from 'lodash/get';
import MocksState from 'ui/states/MocksState';
import { getContentType } from 'ui/utils/string';
import CSS from '!!raw-loader!codemirror/lib/codemirror.css';
import JSMode from '!!raw-loader!codemirror/mode/javascript/javascript';
import JSClosebrackets from '!!raw-loader!codemirror/addon/edit/closebrackets';
import JSMatchbrackets from '!!raw-loader!codemirror/addon/edit/matchbrackets';
import XMLMode from '!!raw-loader!codemirror/mode/xml/xml';
import JS from '!!raw-loader!codemirror';
import JSBeautify from 'js-beautify';
const StyledFrame = styled(Frame)`
width: calc(100% - 5px);
height: 100%;
border: none;
`;
export class CodeEditor extends React.Component {
constructor(props) {
super(props);
const scripts = JS + JSMode + XMLMode + JSClosebrackets + JSMatchbrackets;
const mode = getContentType(props.contentType);
this.state = {
initialContent: `<!DOCTYPE html><html>
<head>
<style>
${CSS}
.CodeMirror {
height: 100%;
}
html, body, body > div, .frame-content {
height: 100%;
}
textarea {
opacity: 0;
}
</style>
</head>
<body style="margin: 0;">
<div></div>
<script>${scripts}</script>
<script>
function reportClick(event) {
parent.postMessage({ type: 'MIMIC_IFRAME' }, document.location.href);
}
document.addEventListener('click', reportClick);
document.addEventListener('contextmenu', reportClick);
document.addEventListener("DOMContentLoaded", function(event) {
setTimeout(function() {
var textArea = document.querySelector('textarea');
var cm = CodeMirror.fromTextArea(textArea, {
lineNumbers: false,
matchBrackets: true,
autoCloseBrackets: true,
mode: '${mode}'
});
cm.on('change', function (editor, change) {
var value = cm.getValue();
if (change.origin === 'paste') {
if (window.js_beautify) {
var cursorPosition = cm.getCursor();
var formattedValue = cm.getValue();
cm.setValue(formattedValue);
cm.setCursor(cursorPosition);
}
}
textArea.innerHTML = value;
textArea.value = value;
var event = new Event('input', { bubbles: true });
textArea.dispatchEvent(event);
});
});
}, 0)
</script>
</body>
</html>`
};
}
componentDidMount() {
window.addEventListener('message', this.onFrameClick);
}
componentWillUnmount() {
window.removeEventListener('message', this.onFrameClick);
}
onFrameClick = (message) => {
if (get(message, 'data.type') === 'MIMIC_IFRAME') {
MocksState.closeMenu();
}
};
shouldComponentUpdate() {
return false;
}
handleChange = (e) => {
this.props.onChange(e.target.value);
};
getValue() {
const mode = getContentType(this.props.contentType) || '';
if (mode.indexOf('json') !== -1) {
return JSBeautify.js(this.props.value || '', { 'indent_size': 2 });
}
return this.props.value;
}
render() {
return (
<StyledFrame className="editor-frame" initialContent={ this.state.initialContent }>
<textarea onChange={ this.handleChange } defaultValue={ this.getValue() } />
</StyledFrame>
);
}
}
CodeEditor.propTypes = {
contentType: PropTypes.string,
value: PropTypes.string,
onChange: PropTypes.func,
};
export default CodeEditor;