Skip to content

Instantly share code, notes, and snippets.

@simonwep
Last active December 30, 2020 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonwep/b0c25725a4715c1c5aab501d6a782a71 to your computer and use it in GitHub Desktop.
Save simonwep/b0c25725a4715c1c5aab501d6a782a71 to your computer and use it in GitHub Desktop.
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