daybrush / selecto

Selecto.js is a component that allows you to select elements in the drag area using the mouse or touch.
https://daybrush.com/selecto
MIT License
2k stars 81 forks source link

Question: Are you able to choose select-click instead of just click for an element? #146

Closed britneydossett closed 1 year ago

britneydossett commented 1 year ago

Environments

Description

Hi! I was wondering if it's possible to use shift-click to select elements on click initially, instead of just clicking the element? Shift-click works after I have selected other elements, however we don't want every item to be selectable every time you click it, because sometimes it interferes with the editability of that element.

For example, when I click on the cell surrounding Heading 3, I am not able to click to edit Heading 3. I would like to be able to shift-click it for selection and click it normally to edit.

Screenshot 2023-07-11 at 13 53 30

Thank you!

britneydossett commented 1 year ago

Oh, I figured it out. For those wondering, I created a variable and whenever someone clicks shift, then I set selectByClick to true in the keydown event.

<script>
let clickable = false
</script>

<Selecto
    selectableTargets={['.cell']}
    dragContainer={document.body}
    hitRate={40}
    selectByClick={clickable}
    selectFromInside={false}
    toggleContinueSelect={'shift'}
    on:selectStart={({ detail: e }) => {
      e.added.forEach(el => {
        el.classList.add('selected')
      })
      e.removed.forEach(el => {
        el.classList.remove('selected')
      })
    }}
    on:selectEnd={({ detail: e }) => {
      e.afterAdded.forEach(el => {
        el.classList.add('selected')
      })
      e.afterRemoved.forEach(el => {
        el.classList.remove('selected')
      })
      if (e.selected.length === 0) {
        clickable = false
      }
    }}
    on:keydown={e => {
      e.preventDefault()
      clickable = true
    }}
  />