-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathCodeEditor.js
163 lines (137 loc) · 4.17 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import _ from 'lodash'
import PropTypes from 'prop-types'
import React from 'react'
import Ace from 'react-ace'
import 'brace/ext/language_tools'
import 'brace/mode/jsx'
import 'brace/theme/tomorrow_night_eighties'
import { componentInfoContext } from 'docs/src/utils'
const semanticUIReactCompleter = {
getCompletions(editor, session, pos, prefix, callback) {
const completions = []
const value = session.getValue()
const currentRow = value.split('\n')[pos.row]
const addComponentDisplayName = (displayName, score) => {
completions.push({
score,
caption: displayName,
value: displayName,
meta: 'Component',
})
}
const addPropsFromInfo = (info, score) => {
info.props.forEach((prop) => {
completions.push({
score,
caption: `${prop.name}`,
value: prop.name,
meta: `{${prop.type}} ${info.displayName}`,
})
})
}
const addPropsFromAPIPath = (apiPath, score) => {
const info = componentInfoContext.byAPIPath[apiPath]
if (!info) return
addPropsFromInfo(info, score)
}
const addPropsFromDisplayName = (displayName, score) => {
const info = componentInfoContext.byDisplayName[displayName]
if (!info) return
addPropsFromInfo(info, score)
}
//
// if we're on a semantic-ui-react import line, return top level components
//
if (/'semantic-ui-react'/.test(currentRow)) {
_.forEach(componentInfoContext.byDisplayName, (info, displayName) => {
if (info.isParent) addComponentDisplayName(displayName, 100)
})
return callback(null, completions)
}
//
// if we find a <Component on the same line, return props for it
//
const apiPath = currentRow.match(/<([A-Z][\w.]+)/)
if (apiPath) {
addPropsFromAPIPath(apiPath[1], 100)
return callback(null, completions)
}
//
// if we aren't sure where we are, return imported components, their props, and locals
//
const suirNamedImports = value.match(/import\s+\{([\s\S]+?)\}\s+from\s'semantic-ui-react'/)
const importedDisplayNames = _.words(suirNamedImports[1])
importedDisplayNames.forEach((displayName) => {
addPropsFromDisplayName(displayName, 200)
addComponentDisplayName(displayName, 100)
})
// local words
_.uniq(_.words(value)).forEach((word) => {
completions.push({
score: 0,
caption: word,
value: word,
meta: 'local',
})
})
callback(null, completions)
},
}
class CodeEditor extends React.Component {
editorRef = React.createRef()
name = `docs-editor-${_.uniqueId()}`
componentDidMount() {
this.setCursorVisibility(this.props.showCursor)
}
componentDidUpdate(prevProps) {
if (prevProps.showCursor !== this.props.showCursor) {
this.setCursorVisibility(this.props.showCursor)
}
// focus editor when editor it becomes active
if (prevProps.active !== this.props.active && this.props.active) {
this.editorRef.current.editor.focus()
}
}
handleChange = _.debounce((value, e) => {
this.props.onChange?.(value, e)
}, 300)
setCursorVisibility = (visible) => {
const cursor = this.editorRef.current.editor.renderer.$cursorLayer.element
cursor.style.display = visible ? '' : 'none'
}
render() {
return (
<Ace
editorProps={{ $blockScrolling: Infinity }}
enableBasicAutocompletion={[semanticUIReactCompleter]}
enableLiveAutocompletion
height='100px'
highlightActiveLine
highlightGutterLine
name={this.name}
maxLines={Infinity}
mode='jsx'
onChange={this.handleChange}
readOnly={false}
ref={this.editorRef}
setOptions={{ fixedWidthGutter: true, showFoldWidgets: false }}
showPrintMargin={false}
tabSize={2}
theme='tomorrow_night_eighties'
width='100%'
{...this.props}
/>
)
}
}
CodeEditor.propTypes = {
active: PropTypes.bool,
onChange: PropTypes.func,
showCursor: PropTypes.bool,
value: PropTypes.string.isRequired,
}
CodeEditor.defaultProps = {
active: true,
showCursor: true,
}
export default CodeEditor