refactor onGutterClick to remove editorRef

main
Mike Fix 5 years ago
parent 348ee2050d
commit 7510dcb5ea

@ -109,11 +109,10 @@ class Carbon extends React.PureComponent {
/> />
) : null} ) : null}
<CodeMirror <CodeMirror
ref={this.props.editorRef}
className={`CodeMirror__container window-theme__${config.windowTheme}`} className={`CodeMirror__container window-theme__${config.windowTheme}`}
onBeforeChange={this.onBeforeChange}
value={this.props.children} value={this.props.children}
options={options} options={options}
onBeforeChange={this.onBeforeChange}
onGutterClick={this.props.onGutterClick} onGutterClick={this.props.onGutterClick}
/> />
{config.watermark && <Watermark light={light} />} {config.watermark && <Watermark light={light} />}
@ -305,27 +304,16 @@ function selectedLinesReducer({ prevLine, selected }, { type, lineNumber, numLin
} }
} }
function CarbonContainer(props, ref) { function useGutterClickHandler(props) {
useModeLoader() const editorRef = React.useRef(null)
const [state, dispatch] = React.useReducer(selectedLinesReducer, { const [state, dispatch] = React.useReducer(selectedLinesReducer, {
prevLine: null, prevLine: null,
selected: {} selected: {}
}) })
function onGutterClick(editor, lineNumber, gutter, e) {
const numLines = editor.display.view.length
if (e.shiftKey) {
dispatch({ type: 'GROUP', lineNumber, numLines })
} else {
dispatch({ type: 'LINE', lineNumber, numLines })
}
}
const editorRef = React.useRef(null)
React.useEffect(() => { React.useEffect(() => {
if (editorRef.current) { if (editorRef.current) {
editorRef.current.editor.display.view.forEach((line, i) => { editorRef.current.display.view.forEach((line, i) => {
if (line.text && line.gutter) { if (line.text && line.gutter) {
line.text.style.opacity = state.selected[i] === false ? 0.5 : 1 line.text.style.opacity = state.selected[i] === false ? 0.5 : 1
line.gutter.style.opacity = state.selected[i] === false ? 0.5 : 1 line.gutter.style.opacity = state.selected[i] === false ? 0.5 : 1
@ -334,7 +322,23 @@ function CarbonContainer(props, ref) {
} }
}, [state.selected, props.children, props.config]) }, [state.selected, props.children, props.config])
return <Carbon {...props} innerRef={ref} editorRef={editorRef} onGutterClick={onGutterClick} /> return function onGutterClick(editor, lineNumber, gutter, e) {
editorRef.current = editor
const numLines = editor.display.view.length
if (e.shiftKey) {
dispatch({ type: 'GROUP', lineNumber, numLines })
} else {
dispatch({ type: 'LINE', lineNumber, numLines })
}
}
}
function CarbonContainer(props, ref) {
useModeLoader()
const onGutterClick = useGutterClickHandler(props)
return <Carbon {...props} innerRef={ref} onGutterClick={onGutterClick} />
} }
export default React.forwardRef(CarbonContainer) export default React.forwardRef(CarbonContainer)

Loading…
Cancel
Save