Last active
December 30, 2020 17:48
-
-
Save simonwep/b0c25725a4715c1c5aab501d6a782a71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const selection = new SelectionArea({ | |
// All elements in this container can be selected | |
selectables: ['.box-wrap > div'], | |
// The container is also the boundary in this case | |
boundaries: ['.box-wrap'] | |
}).on('start', ({store, event}) => { | |
// Remove class if the user isn't pressing the control key or ⌘ key | |
if (!event.ctrlKey && !event.metaKey) { | |
// Unselect all elements | |
for (const el of store.stored) { | |
el.classList.remove('selected'); | |
} | |
// Clear previous selection | |
selection.clearSelection(); | |
} | |
}).on('move', ({store: {changed: {added, removed}}}) => { | |
// Add a custom class to the elements that where selected. | |
for (const el of added) { | |
el.classList.add('selected'); | |
} | |
// Remove the class from elements that where removed | |
// since the last selection | |
for (const el of removed) { | |
el.classList.remove('selected'); | |
} | |
}).on('stop', () => { | |
selection.keepSelection(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment