2021-12-23 01:10:27 -05:00
|
|
|
import ClipboardJS from 'clipboard';
|
2021-02-14 13:09:20 -05:00
|
|
|
import $ from 'jquery';
|
2021-12-23 01:10:27 -05:00
|
|
|
|
|
|
|
import { parseBoolean } from '~/lib/utils/common_utils';
|
|
|
|
import { __ } from '~/locale';
|
|
|
|
import { fixTitle, add, show, hide, once } from '~/tooltips';
|
|
|
|
|
|
|
|
const CLIPBOARD_SUCCESS_EVENT = 'clipboard-success';
|
|
|
|
const CLIPBOARD_ERROR_EVENT = 'clipboard-error';
|
|
|
|
const I18N_ERROR_MESSAGE = __('Copy failed. Please manually copy the value.');
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-11-28 16:26:45 -05:00
|
|
|
function showTooltip(target, title) {
|
2021-02-23 10:10:47 -05:00
|
|
|
const { title: originalTitle } = target.dataset;
|
|
|
|
|
|
|
|
once('hidden', (tooltip) => {
|
|
|
|
if (tooltip.target === target) {
|
2020-10-14 11:08:42 -04:00
|
|
|
target.setAttribute('title', originalTitle);
|
2021-12-23 01:10:27 -05:00
|
|
|
target.setAttribute('aria-label', originalTitle);
|
2020-10-14 11:08:42 -04:00
|
|
|
fixTitle(target);
|
2021-02-23 10:10:47 -05:00
|
|
|
}
|
|
|
|
});
|
2020-10-14 11:08:42 -04:00
|
|
|
|
|
|
|
target.setAttribute('title', title);
|
2021-12-23 01:10:27 -05:00
|
|
|
target.setAttribute('aria-label', title);
|
2020-10-14 11:08:42 -04:00
|
|
|
fixTitle(target);
|
|
|
|
show(target);
|
2021-12-23 01:10:27 -05:00
|
|
|
setTimeout(() => {
|
|
|
|
hide(target);
|
|
|
|
}, 1000);
|
2017-11-28 16:26:45 -05:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-11-28 16:26:45 -05:00
|
|
|
function genericSuccess(e) {
|
2021-12-23 01:10:27 -05:00
|
|
|
// Clear the selection
|
2017-03-11 02:30:44 -05:00
|
|
|
e.clearSelection();
|
2021-12-23 01:10:27 -05:00
|
|
|
e.trigger.focus();
|
|
|
|
e.trigger.dispatchEvent(new Event(CLIPBOARD_SUCCESS_EVENT));
|
|
|
|
|
|
|
|
const { clipboardHandleTooltip = true } = e.trigger.dataset;
|
|
|
|
if (parseBoolean(clipboardHandleTooltip)) {
|
|
|
|
// Update tooltip
|
|
|
|
showTooltip(e.trigger, __('Copied'));
|
|
|
|
}
|
2017-11-28 16:26:45 -05:00
|
|
|
}
|
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) {
|
2021-12-23 01:10:27 -05:00
|
|
|
e.trigger.dispatchEvent(new Event(CLIPBOARD_ERROR_EVENT));
|
|
|
|
|
|
|
|
const { clipboardHandleTooltip = true } = e.trigger.dataset;
|
|
|
|
if (parseBoolean(clipboardHandleTooltip)) {
|
|
|
|
showTooltip(e.trigger, I18N_ERROR_MESSAGE);
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2017-11-28 16:26:45 -05:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-11-28 16:34:51 -05:00
|
|
|
export default function initCopyToClipboard() {
|
2021-12-23 01:10:27 -05:00
|
|
|
const clipboard = new ClipboardJS('[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.
|
|
|
|
*/
|
2020-12-23 16:10:24 -05:00
|
|
|
$(document).on('copy', 'body > textarea[readonly]', (e) => {
|
2018-06-16 17:50:13 -04:00
|
|
|
const { clipboardData } = e.originalEvent;
|
2017-04-06 17:10:14 -04:00
|
|
|
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);
|
|
|
|
});
|
2021-12-23 01:10:27 -05:00
|
|
|
|
|
|
|
return clipboard;
|
2017-11-28 16:34:51 -05:00
|
|
|
}
|
2020-10-27 11:08:39 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Programmatically triggers a click event on a
|
|
|
|
* "copy to clipboard" button, causing its
|
|
|
|
* contents to be copied. Handles some of the messiniess
|
|
|
|
* around managing the button's tooltip.
|
|
|
|
* @param {HTMLElement} btnElement
|
|
|
|
*/
|
|
|
|
export function clickCopyToClipboardButton(btnElement) {
|
|
|
|
// Ensure the button has already been tooltip'd.
|
2021-02-23 10:10:47 -05:00
|
|
|
add([btnElement], { show: true });
|
2020-10-27 11:08:39 -04:00
|
|
|
|
|
|
|
btnElement.click();
|
|
|
|
}
|
2021-12-23 01:10:27 -05:00
|
|
|
|
|
|
|
export { CLIPBOARD_SUCCESS_EVENT, CLIPBOARD_ERROR_EVENT, I18N_ERROR_MESSAGE };
|