2017-11-28 16:16:49 -05:00
|
|
|
import Clipboard from 'clipboard';
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-11-28 16:26:45 -05:00
|
|
|
function showTooltip(target, title) {
|
|
|
|
const $target = $(target);
|
|
|
|
const originalTitle = $target.data('original-title');
|
|
|
|
|
|
|
|
if (!$target.data('hideTooltip')) {
|
|
|
|
$target
|
|
|
|
.attr('title', title)
|
|
|
|
.tooltip('fixTitle')
|
|
|
|
.tooltip('show')
|
|
|
|
.attr('title', originalTitle)
|
|
|
|
.tooltip('fixTitle');
|
|
|
|
}
|
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-11-28 16:26:45 -05:00
|
|
|
function genericSuccess(e) {
|
2017-03-11 02:30:44 -05:00
|
|
|
showTooltip(e.trigger, 'Copied');
|
|
|
|
// Clear the selection and blur the trigger so it loses its border
|
|
|
|
e.clearSelection();
|
2017-11-28 16:26:45 -05:00
|
|
|
$(e.trigger).blur();
|
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-11-28 16:37:10 -05:00
|
|
|
/**
|
|
|
|
* Safari > 10 doesn't support `execCommand`, so instead we inform the user to copy manually.
|
|
|
|
* See http://clipboardjs.com/#browser-support
|
|
|
|
*/
|
2017-11-28 16:26:45 -05:00
|
|
|
function genericError(e) {
|
|
|
|
let key;
|
2017-03-11 02:30:44 -05:00
|
|
|
if (/Mac/i.test(navigator.userAgent)) {
|
|
|
|
key = '⌘'; // Command
|
|
|
|
} else {
|
|
|
|
key = 'Ctrl';
|
|
|
|
}
|
2017-11-28 16:26:45 -05:00
|
|
|
showTooltip(e.trigger, `Press ${key}-C to copy`);
|
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-11-28 16:34:51 -05:00
|
|
|
export default function initCopyToClipboard() {
|
2017-04-06 17:10:14 -04:00
|
|
|
const clipboard = new Clipboard('[data-clipboard-target], [data-clipboard-text]');
|
2017-03-11 02:30:44 -05:00
|
|
|
clipboard.on('success', genericSuccess);
|
2017-04-06 17:10:14 -04:00
|
|
|
clipboard.on('error', genericError);
|
|
|
|
|
2017-11-28 16:26:45 -05:00
|
|
|
/**
|
|
|
|
* This a workaround around ClipboardJS limitations to allow the context-specific copy/pasting
|
|
|
|
* of plain text or GFM. The Ruby `clipboard_button` helper sneaks a JSON hash with `text` and
|
|
|
|
* `gfm` keys into the `data-clipboard-text` attribute that ClipboardJS reads from.
|
|
|
|
* When ClipboardJS creates a new `textarea` (directly inside `body`, with a `readonly`
|
|
|
|
* attribute`), sets its value to the value of this data attribute, focusses on it, and finally
|
|
|
|
* programmatically issues the 'Copy' command, this code intercepts the copy command/event at
|
|
|
|
* the last minute to deconstruct this JSON hash and set the `text/plain` and `text/x-gfm` copy
|
|
|
|
* data types to the intended values.
|
|
|
|
*/
|
|
|
|
$(document).on('copy', 'body > textarea[readonly]', (e) => {
|
2017-04-06 17:10:14 -04:00
|
|
|
const clipboardData = e.originalEvent.clipboardData;
|
|
|
|
if (!clipboardData) return;
|
|
|
|
|
|
|
|
const text = e.target.value;
|
|
|
|
|
|
|
|
let json;
|
|
|
|
try {
|
|
|
|
json = JSON.parse(text);
|
|
|
|
} catch (ex) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json.text || !json.gfm) return;
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
clipboardData.setData('text/plain', json.text);
|
|
|
|
clipboardData.setData('text/x-gfm', json.gfm);
|
|
|
|
});
|
2017-11-28 16:34:51 -05:00
|
|
|
}
|