debian-mirror-gitlab/app/assets/javascripts/behaviors/copy_to_clipboard.js

92 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import Clipboard from 'clipboard';
2021-03-11 19:13:27 +05:30
import $ from 'jquery';
2019-07-31 22:56:46 +05:30
import { sprintf, __ } from '~/locale';
2021-04-17 20:07:23 +05:30
import { fixTitle, add, show, once } from '~/tooltips';
2018-03-17 18:26:18 +05:30
function showTooltip(target, title) {
2021-04-17 20:07:23 +05:30
const { title: originalTitle } = target.dataset;
once('hidden', (tooltip) => {
if (tooltip.target === target) {
2021-01-03 14:25:43 +05:30
target.setAttribute('title', originalTitle);
fixTitle(target);
2021-04-17 20:07:23 +05:30
}
});
2021-01-03 14:25:43 +05:30
target.setAttribute('title', title);
fixTitle(target);
show(target);
2021-04-17 20:07:23 +05:30
setTimeout(() => target.blur(), 1000);
2018-03-17 18:26:18 +05:30
}
function genericSuccess(e) {
// Clear the selection and blur the trigger so it loses its border
e.clearSelection();
2020-05-24 23:13:21 +05:30
showTooltip(e.trigger, __('Copied'));
2018-03-17 18:26:18 +05:30
}
/**
* Safari > 10 doesn't support `execCommand`, so instead we inform the user to copy manually.
* See http://clipboardjs.com/#browser-support
*/
function genericError(e) {
let key;
if (/Mac/i.test(navigator.userAgent)) {
key = '⌘'; // Command
} else {
key = 'Ctrl';
}
2019-07-31 22:56:46 +05:30
showTooltip(e.trigger, sprintf(__(`Press %{key}-C to copy`), { key }));
2018-03-17 18:26:18 +05:30
}
export default function initCopyToClipboard() {
const clipboard = new Clipboard('[data-clipboard-target], [data-clipboard-text]');
clipboard.on('success', genericSuccess);
clipboard.on('error', genericError);
/**
* 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.
*/
2021-03-08 18:12:59 +05:30
$(document).on('copy', 'body > textarea[readonly]', (e) => {
2018-11-08 19:23:39 +05:30
const { clipboardData } = e.originalEvent;
2018-03-17 18:26:18 +05:30
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-01-29 00:20:46 +05:30
/**
* 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-04-17 20:07:23 +05:30
add([btnElement], { show: true });
2021-01-29 00:20:46 +05:30
btnElement.click();
}