use useReducer for selected lines

main
Mike Fix 5 years ago
parent 55e01231d9
commit 348ee2050d

@ -265,11 +265,7 @@ class Carbon extends React.PureComponent {
} }
const modesLoaded = new Set() const modesLoaded = new Set()
function CarbonContainer(props, ref) { function useModeLoader() {
const [selectedLines, setSelected] = React.useState({})
const editorRef = React.useRef(null)
const prevLine = React.useRef(null)
React.useEffect(() => { React.useEffect(() => {
LANGUAGES.filter(language => language.mode !== 'auto' && language.mode !== 'text').forEach( LANGUAGES.filter(language => language.mode !== 'auto' && language.mode !== 'text').forEach(
language => { language => {
@ -282,49 +278,61 @@ function CarbonContainer(props, ref) {
} }
) )
}, []) }, [])
}
function onGutterClick(editor, lineNumber, gutter, e) { function selectedLinesReducer({ prevLine, selected }, { type, lineNumber, numLines }) {
setSelected(currState => {
const newState = {} const newState = {}
if (e.shiftKey && prevLine.current != null) { if (type === 'GROUP' && prevLine) {
for ( for (let i = Math.min(prevLine, lineNumber); i < Math.max(prevLine, lineNumber) + 1; i++) {
let i = Math.min(prevLine.current, lineNumber); newState[i] = selected[prevLine]
i < Math.max(prevLine.current, lineNumber) + 1;
i++
) {
newState[i] = currState[prevLine.current]
} }
} else {
return { ...currState, ...newState } for (let i = 0; i < numLines; i++) {
}
for (let i = 0; i < editor.display.view.length; i++) {
if (i != lineNumber) { if (i != lineNumber) {
if (prevLine.current == null) { if (prevLine == null) {
newState[i] = false newState[i] = false
} }
} else { } else {
newState[lineNumber] = currState[lineNumber] === true ? false : true newState[lineNumber] = selected[lineNumber] === true ? false : true
}
} }
} }
return { ...currState, ...newState } return {
selected: { ...selected, ...newState },
prevLine: lineNumber
}
}
function CarbonContainer(props, ref) {
useModeLoader()
const [state, dispatch] = React.useReducer(selectedLinesReducer, {
prevLine: null,
selected: {}
}) })
prevLine.current = lineNumber 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.editor.display.view.forEach((line, i) => {
if (line.text && line.gutter) { if (line.text && line.gutter) {
line.text.style.opacity = selectedLines[i] === false ? 0.5 : 1 line.text.style.opacity = state.selected[i] === false ? 0.5 : 1
line.gutter.style.opacity = selectedLines[i] === false ? 0.5 : 1 line.gutter.style.opacity = state.selected[i] === false ? 0.5 : 1
} }
}) })
} }
}, [selectedLines, props.children, props.config]) }, [state.selected, props.children, props.config])
return <Carbon {...props} innerRef={ref} editorRef={editorRef} onGutterClick={onGutterClick} /> return <Carbon {...props} innerRef={ref} editorRef={editorRef} onGutterClick={onGutterClick} />
} }

Loading…
Cancel
Save