Skip to content

Commit

Permalink
Update index.js after prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
sudodoki committed Apr 4, 2019
1 parent ad759b0 commit cbbd987
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,76 @@
'use strict';
"use strict";

var deselectCurrent = require('toggle-selection');
var deselectCurrent = require("toggle-selection");

var defaultMessage = 'Copy to clipboard: #{key}, Enter';
var defaultMessage = "Copy to clipboard: #{key}, Enter";

function format(message) {
var copyKey = (/mac os x/i.test(navigator.userAgent) ? '⌘' : 'Ctrl') + '+C';
var copyKey = (/mac os x/i.test(navigator.userAgent) ? "⌘" : "Ctrl") + "+C";
return message.replace(/#{\s*key\s*}/g, copyKey);
}

function copy(text, options) {
var debug, message, reselectPrevious, range, selection, mark, success = false;
if (!options) { options = {}; }
var debug,
message,
reselectPrevious,
range,
selection,
mark,
success = false;
if (!options) {
options = {};
}
debug = options.debug || false;
try {
reselectPrevious = deselectCurrent();

range = document.createRange();
selection = document.getSelection();

mark = document.createElement('span');
mark = document.createElement("span");
mark.textContent = text;
// reset user styles for span element
mark.style.all = 'unset';
mark.style.all = "unset";
// prevents scrolling to the end of the page
mark.style.position = 'fixed';
mark.style.position = "fixed";
mark.style.top = 0;
mark.style.clip = 'rect(0, 0, 0, 0)';
mark.style.clip = "rect(0, 0, 0, 0)";
// used to preserve spaces and line breaks
mark.style.whiteSpace = 'pre';
mark.style.whiteSpace = "pre";
// do not inherit user-select (it may be `none`)
mark.style.webkitUserSelect = 'text';
mark.style.MozUserSelect = 'text';
mark.style.msUserSelect = 'text';
mark.style.userSelect = 'text';
mark.addEventListener('copy', function(e) { e.stopPropagation(); });
mark.style.webkitUserSelect = "text";
mark.style.MozUserSelect = "text";
mark.style.msUserSelect = "text";
mark.style.userSelect = "text";
mark.addEventListener("copy", function(e) {
e.stopPropagation();
});

document.body.appendChild(mark);

range.selectNodeContents(mark);
selection.addRange(range);

var successful = document.execCommand('copy');
var successful = document.execCommand("copy");
if (!successful) {
throw new Error('copy command was unsuccessful');
throw new Error("copy command was unsuccessful");
}
success = true;
} catch (err) {
debug && console.error('unable to copy using execCommand: ', err);
debug && console.warn('trying IE specific stuff');
debug && console.error("unable to copy using execCommand: ", err);
debug && console.warn("trying IE specific stuff");
try {
window.clipboardData.setData('text', text);
window.clipboardData.setData("text", text);
success = true;
} catch (err) {
debug && console.error('unable to copy using clipboardData: ', err);
debug && console.error('falling back to prompt');
message = format('message' in options ? options.message : defaultMessage);
debug && console.error("unable to copy using clipboardData: ", err);
debug && console.error("falling back to prompt");
message = format("message" in options ? options.message : defaultMessage);
window.prompt(message, text);
}
} finally {
if (selection) {
if (typeof selection.removeRange == 'function') {
if (typeof selection.removeRange == "function") {
selection.removeRange(range);
} else {
selection.removeAllRanges();
Expand Down

0 comments on commit cbbd987

Please sign in to comment.