-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathCodeEditor.js
More file actions
40 lines (33 loc) · 1.15 KB
/
CodeEditor.js
File metadata and controls
40 lines (33 loc) · 1.15 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
const React = require('react')
const ReactDOM = require('react-dom')
const AceEditor = require('react-ace').default
const hash = require('object-hash')
require('ace-builds/src-noconflict/mode-javascript')
require('ace-builds/src-noconflict/theme-github')
function onChange (newValue) {
console.log('change', newValue)
}
const CodeEditor = (props) => {
return (
<div language={props.language} code={props.code}>
<AceEditor
mode={props.language}
theme='github'
onChange={onChange}
name={`code-editor-${hash({ language: props.language, code: props.code })}`}
editorProps={{ $blockScrolling: true }}
defaultValue={props.code}
/>
</div>
)
}
if (typeof window !== 'undefined') {
const componentContainers = document.querySelectorAll('.react-component-CodeEditor')
for (const componentContainer of componentContainers) {
const div = componentContainer.children[0]
const language = div.getAttribute('language')
const code = div.getAttribute('code')
ReactDOM.hydrate(React.createElement(CodeEditor, { language: language, code: code }), componentContainer)
}
}
module.exports = CodeEditor