From 0a32166dbfafb79ad4efb186d77a75a23cc3eadb Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Fri, 4 Dec 2020 19:24:35 +0800 Subject: [PATCH] Fix: files counting error while using Shift key (#597) --- src/actions/explorer.js | 135 ++++++++++++++++++++++------------------ 1 file changed, 74 insertions(+), 61 deletions(-) diff --git a/src/actions/explorer.js b/src/actions/explorer.js index f1c0a86..521e60a 100644 --- a/src/actions/explorer.js +++ b/src/actions/explorer.js @@ -1,78 +1,91 @@ import { isMac } from "../utils"; export const removeSelectedTargets = fileIds => { - return { - type: "RMOVE_SELECTED_TARGETS", - fileIds, - }; + return { + type: "RMOVE_SELECTED_TARGETS", + fileIds + }; }; export const addSelectedTargets = targets => { - return { - type: "ADD_SELECTED_TARGETS", - targets, - }; + return { + type: "ADD_SELECTED_TARGETS", + targets + }; }; export const setSelectedTarget = targets => { - return { - type: "SET_SELECTED_TARGET", - targets, - }; + return { + type: "SET_SELECTED_TARGET", + targets + }; }; export const setLastSelect = (file, index) => { - return { - type: "SET_LAST_SELECT", - file, - index, - }; + return { + type: "SET_LAST_SELECT", + file, + index + }; }; -export const setShiftSelectedIds = (shiftSelectedIds) => { - return { - type: "SET_SHIFT_SELECTED_IDS", - shiftSelectedIds, - }; +export const setShiftSelectedIds = shiftSelectedIds => { + return { + type: "SET_SHIFT_SELECTED_IDS", + shiftSelectedIds + }; }; export const selectFile = (file, event, fileIndex) => { - const { ctrlKey, metaKey, shiftKey } = event - return (dispatch, getState) => { - // 多种组合操作忽略 - if ([ctrlKey, metaKey, shiftKey].filter(Boolean).length > 1) { - return - } - const isMacbook = isMac() - const { explorer } = getState() - const { selected, lastSelect, dirList, fileList, shiftSelectedIds } = explorer - if (shiftKey && !ctrlKey && !metaKey && selected.length !== 0 - // 点击类型一样 - && file.type === lastSelect.file.type) { - // shift 多选 - // 取消原有选择 - dispatch(removeSelectedTargets(shiftSelectedIds)) - // 添加新选择 - const begin = Math.min(lastSelect.index, fileIndex) - const end = Math.max(lastSelect.index, fileIndex) - const list = file.type === 'dir' ? dirList : fileList - const newShiftSelected = list.slice(begin, end+1) - dispatch(setShiftSelectedIds(newShiftSelected.map(file => file.id))) - return dispatch(addSelectedTargets(newShiftSelected)) - } - dispatch(setLastSelect(file, fileIndex)) - dispatch(setShiftSelectedIds([])) - if ((ctrlKey && !isMacbook) || (metaKey && isMacbook)) { - // Ctrl/Command 单击添加/删除 - const presentIndex = selected.findIndex(value => { - return value.id === file.id - }) - if (presentIndex !== -1) { - return dispatch(removeSelectedTargets([file.id])) - } - return dispatch(addSelectedTargets([file])) - } - // 单选 - return dispatch(setSelectedTarget([file])) - } -} + const { ctrlKey, metaKey, shiftKey } = event; + return (dispatch, getState) => { + // 多种组合操作忽略 + if ([ctrlKey, metaKey, shiftKey].filter(Boolean).length > 1) { + return; + } + const isMacbook = isMac(); + const { explorer } = getState(); + const { + selected, + lastSelect, + dirList, + fileList, + shiftSelectedIds + } = explorer; + if ( + shiftKey && + !ctrlKey && + !metaKey && + selected.length !== 0 && + // 点击类型一样 + file.type === lastSelect.file.type + ) { + // shift 多选 + // 取消原有选择 + dispatch(removeSelectedTargets(shiftSelectedIds)); + // 添加新选择 + const begin = Math.min(lastSelect.index, fileIndex); + const end = Math.max(lastSelect.index, fileIndex); + const list = file.type === "dir" ? dirList : fileList; + const newShiftSelected = list.slice(begin + 1, end + 1); + dispatch( + setShiftSelectedIds(newShiftSelected.map(file => file.id)) + ); + return dispatch(addSelectedTargets(newShiftSelected)); + } + dispatch(setLastSelect(file, fileIndex)); + dispatch(setShiftSelectedIds([])); + if ((ctrlKey && !isMacbook) || (metaKey && isMacbook)) { + // Ctrl/Command 单击添加/删除 + const presentIndex = selected.findIndex(value => { + return value.id === file.id; + }); + if (presentIndex !== -1) { + return dispatch(removeSelectedTargets([file.id])); + } + return dispatch(addSelectedTargets([file])); + } + // 单选 + return dispatch(setSelectedTarget([file])); + }; +};