Skip to content

Instantly share code, notes, and snippets.

@mdamien
Created May 9, 2016 14:54
Show Gist options
  • Save mdamien/239713473b5b24c9544f195172c0412c to your computer and use it in GitHub Desktop.
Save mdamien/239713473b5b24c9544f195172c0412c to your computer and use it in GitHub Desktop.
import Selectize from 'selectize';
// https://gist.github.com/mdamien/a0f2bd91750a2d45c8ab
function copyToClipboard(text) {
const el = document.createElement('textarea');
el.style.position = 'absolute';
el.style.left = '-9999px';
el.setAttribute('readonly', '');
el.value = text;
document.body.appendChild(el);
el.select();
const success = document.execCommand('copy');
document.body.removeChild(el);
return success;
}
Selectize.define('copy_selection', function() {
this.onKeyDown = (() => {
const original = this.onKeyDown;
return function(e) {
const res = original.apply(this, arguments);
const KEY_C = 67;
if (e.keyCode === KEY_C && this.isCmdDown) {
let selection = this.$activeItems
.map(x => $(x).data('value'))
.filter(x => x);
// remove duplicates
selection = Array.from(new Set(selection));
copyToClipboard(selection.join(this.settings.delimiter || ' '));
this.focus();
}
return res;
};
})();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment