Handles browsers that do not support this API

This commit is contained in:
Zeno Rocha
2015-09-19 16:44:30 -07:00
parent 960d1a9dd9
commit 24f4bc77ed
2 changed files with 13 additions and 4 deletions

View File

@ -88,26 +88,35 @@ class Clipboard {
}
copy(actionAttr, selectedText, currentTrigger) {
let supported = document.queryCommandSupported(actionAttr);
try {
let successful = document.execCommand(actionAttr);
if (!successful) throw 'Invalid "data-action" attribute';
if (!successful) throw new Error('Invalid "data-action" attribute');
this.dispatchEvent(actionAttr, selectedText, currentTrigger);
window.getSelection().removeAllRanges();
}
catch (err) {
throw new Error(err);
supported = false;
}
if (!supported) this.notSupported(currentTrigger);
}
dispatchEvent(actionAttr, selectedText, currentTrigger) {
var event = new CustomEvent(actionAttr, {
let event = new CustomEvent(actionAttr, {
detail: selectedText
});
currentTrigger.dispatchEvent(event);
}
notSupported(currentTrigger) {
let event = new CustomEvent('no-support');
currentTrigger.dispatchEvent(event);
}
}
global.Clipboard = Clipboard;