Просмотр исходного кода

@uppy/provider-views: Fix range selection not resetting and computing correctly (#4415)

* Fix range selection not resetting and computing correctly

* Remove comment about deselecting previous selections

* Add range selection to Unsplash — just pass the recordShiftKeyPress prop

---------

Co-authored-by: Artur Paikin <artur@arturpaikin.com>
Terence C 1 год назад
Родитель
Сommit
327655ca7e

+ 1 - 0
packages/@uppy/provider-views/src/Item/components/GridLi.jsx

@@ -33,6 +33,7 @@ function GridListItem (props) {
         className={checkBoxClassName}
         className={checkBoxClassName}
         onChange={toggleCheckbox}
         onChange={toggleCheckbox}
         onKeyDown={recordShiftKeyPress}
         onKeyDown={recordShiftKeyPress}
+        onMouseDown={recordShiftKeyPress}
         name="listitem"
         name="listitem"
         id={id}
         id={id}
         checked={isChecked}
         checked={isChecked}

+ 1 - 0
packages/@uppy/provider-views/src/Item/components/ListLi.jsx

@@ -36,6 +36,7 @@ function ListItem (props) {
           className={`uppy-u-reset uppy-ProviderBrowserItem-checkbox ${isChecked ? 'uppy-ProviderBrowserItem-checkbox--is-checked' : ''}`}
           className={`uppy-u-reset uppy-ProviderBrowserItem-checkbox ${isChecked ? 'uppy-ProviderBrowserItem-checkbox--is-checked' : ''}`}
           onChange={toggleCheckbox}
           onChange={toggleCheckbox}
           onKeyDown={recordShiftKeyPress}
           onKeyDown={recordShiftKeyPress}
+          onMouseDown={recordShiftKeyPress}
           // for the <label/>
           // for the <label/>
           name="listitem"
           name="listitem"
           id={id}
           id={id}

+ 1 - 0
packages/@uppy/provider-views/src/ProviderView/ProviderView.jsx

@@ -124,6 +124,7 @@ export default class ProviderView extends View {
       this.username = res.username || this.username
       this.username = res.username || this.username
       this.#updateFilesAndFolders(res, files, folders)
       this.#updateFilesAndFolders(res, files, folders)
       this.plugin.setPluginState({ directories: updatedDirectories, filterInput: '' })
       this.plugin.setPluginState({ directories: updatedDirectories, filterInput: '' })
+      this.lastCheckbox = undefined
     } catch (err) {
     } catch (err) {
       this.handleError(err)
       this.handleError(err)
     } finally {
     } finally {

+ 2 - 1
packages/@uppy/provider-views/src/SearchProviderView/SearchProviderView.jsx

@@ -138,12 +138,13 @@ export default class SearchProviderView extends View {
 
 
     const targetViewOptions = { ...this.opts, ...viewOptions }
     const targetViewOptions = { ...this.opts, ...viewOptions }
     const { files, folders, filterInput, loading, currentSelection } = this.plugin.getPluginState()
     const { files, folders, filterInput, loading, currentSelection } = this.plugin.getPluginState()
-    const { isChecked, toggleCheckbox, filterItems } = this
+    const { isChecked, toggleCheckbox, filterItems, recordShiftKeyPress } = this
     const hasInput = filterInput !== ''
     const hasInput = filterInput !== ''
 
 
     const browserProps = {
     const browserProps = {
       isChecked,
       isChecked,
       toggleCheckbox,
       toggleCheckbox,
+      recordShiftKeyPress,
       currentSelection,
       currentSelection,
       files: hasInput ? filterItems(files) : files,
       files: hasInput ? filterItems(files) : files,
       folders: hasInput ? filterItems(folders) : folders,
       folders: hasInput ? filterItems(folders) : folders,

+ 8 - 7
packages/@uppy/provider-views/src/View.js

@@ -123,31 +123,32 @@ export default class View {
     const { folders, files } = this.plugin.getPluginState()
     const { folders, files } = this.plugin.getPluginState()
     const items = this.filterItems(folders.concat(files))
     const items = this.filterItems(folders.concat(files))
     // Shift-clicking selects a single consecutive list of items
     // Shift-clicking selects a single consecutive list of items
-    // starting at the previous click and deselects everything else.
+    // starting at the previous click.
     if (this.lastCheckbox && this.isShiftKeyPressed) {
     if (this.lastCheckbox && this.isShiftKeyPressed) {
+      const { currentSelection } = this.plugin.getPluginState()
       const prevIndex = items.indexOf(this.lastCheckbox)
       const prevIndex = items.indexOf(this.lastCheckbox)
       const currentIndex = items.indexOf(file)
       const currentIndex = items.indexOf(file)
-      const currentSelection = (prevIndex < currentIndex)
+      const newSelection = (prevIndex < currentIndex)
         ? items.slice(prevIndex, currentIndex + 1)
         ? items.slice(prevIndex, currentIndex + 1)
         : items.slice(currentIndex, prevIndex + 1)
         : items.slice(currentIndex, prevIndex + 1)
-      const reducedCurrentSelection = []
+      const reducedNewSelection = []
 
 
       // Check restrictions on each file in currentSelection,
       // Check restrictions on each file in currentSelection,
       // reduce it to only contain files that pass restrictions
       // reduce it to only contain files that pass restrictions
-      for (const item of currentSelection) {
+      for (const item of newSelection) {
         const { uppy } = this.plugin
         const { uppy } = this.plugin
         const restrictionError = uppy.validateRestrictions(
         const restrictionError = uppy.validateRestrictions(
           remoteFileObjToLocal(item),
           remoteFileObjToLocal(item),
-          [...uppy.getFiles(), ...reducedCurrentSelection],
+          [...uppy.getFiles(), ...reducedNewSelection],
         )
         )
 
 
         if (!restrictionError) {
         if (!restrictionError) {
-          reducedCurrentSelection.push(item)
+          reducedNewSelection.push(item)
         } else {
         } else {
           uppy.info({ message: restrictionError.message }, 'error', uppy.opts.infoTimeout)
           uppy.info({ message: restrictionError.message }, 'error', uppy.opts.infoTimeout)
         }
         }
       }
       }
-      this.plugin.setPluginState({ currentSelection: reducedCurrentSelection })
+      this.plugin.setPluginState({ currentSelection: [...new Set([...currentSelection, ...reducedNewSelection])] })
       return
       return
     }
     }