From 625bbd477382a007f6b8d4fdbdffbae3075433f9 Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Fri, 25 Oct 2019 15:19:16 -0700 Subject: [PATCH] clean up selected lines reducer --- components/Carbon.js | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/components/Carbon.js b/components/Carbon.js index 50b021e..8073ba8 100644 --- a/components/Carbon.js +++ b/components/Carbon.js @@ -388,23 +388,30 @@ function selectedLinesReducer( ) { const newState = {} - if (type === 'GROUP' && prevLine) { - for (let i = Math.min(prevLine, lineNumber); i < Math.max(prevLine, lineNumber) + 1; i++) { - newState[i] = selected[prevLine] + switch (type) { + case 'GROUP': { + if (prevLine) { + for (let i = Math.min(prevLine, lineNumber); i < Math.max(prevLine, lineNumber) + 1; i++) { + newState[i] = selected[prevLine] + } + } + break } - } - if (type === 'MULTILINE') { - for (let i = 0; i < selectedLines.length; i++) { - newState[selectedLines[i] - 1] = true + case 'MULTILINE': { + for (let i = 0; i < selectedLines.length; i++) { + newState[selectedLines[i] - 1] = true + } + break } - } else { - for (let i = 0; i < numLines; i++) { - if (i != lineNumber) { - if (prevLine == null) { - newState[i] = false + default: { + for (let i = 0; i < numLines; i++) { + if (i != lineNumber) { + if (prevLine == null) { + newState[i] = false + } + } else { + newState[lineNumber] = selected[lineNumber] === true ? false : true } - } else { - newState[lineNumber] = selected[lineNumber] === true ? false : true } } } @@ -441,11 +448,8 @@ function useSelectedLines(props, editorRef) { return React.useCallback(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 type = e.shiftKey ? 'GROUP' : 'LINE' + dispatch({ type, lineNumber, numLines }) }, []) }