Commit 17c53d7d authored by Tim Zallmann's avatar Tim Zallmann

Merge branch 'es-module-copy-to-clipboard' into 'master'

Refactor 'copy-to-clipboard.js'

See merge request gitlab-org/gitlab-ce!15531
parents f7254a40 bf711451
/* eslint-disable func-names, space-before-function-paren, one-var, no-var, one-var-declaration-per-line, prefer-template, quotes, no-unused-vars, prefer-arrow-callback, max-len */ import Clipboard from 'clipboard';
import Clipboard from 'vendor/clipboard'; function showTooltip(target, title) {
const $target = $(target);
const originalTitle = $target.data('original-title');
var genericError, genericSuccess, showTooltip; if (!$target.data('hideTooltip')) {
$target
.attr('title', title)
.tooltip('fixTitle')
.tooltip('show')
.attr('title', originalTitle)
.tooltip('fixTitle');
}
}
genericSuccess = function(e) { function genericSuccess(e) {
showTooltip(e.trigger, 'Copied'); showTooltip(e.trigger, 'Copied');
// Clear the selection and blur the trigger so it loses its border // Clear the selection and blur the trigger so it loses its border
e.clearSelection(); e.clearSelection();
return $(e.trigger).blur(); $(e.trigger).blur();
}; }
// Safari doesn't support `execCommand`, so instead we inform the user to /**
// copy manually. * Safari > 10 doesn't support `execCommand`, so instead we inform the user to copy manually.
// * See http://clipboardjs.com/#browser-support
// See http://clipboardjs.com/#browser-support */
genericError = function(e) { function genericError(e) {
var key; let key;
if (/Mac/i.test(navigator.userAgent)) { if (/Mac/i.test(navigator.userAgent)) {
key = '⌘'; // Command key = '⌘'; // Command
} else { } else {
key = 'Ctrl'; key = 'Ctrl';
} }
return showTooltip(e.trigger, "Press " + key + "-C to copy"); showTooltip(e.trigger, `Press ${key}-C to copy`);
}; }
showTooltip = function(target, title) {
var $target = $(target);
var originalTitle = $target.data('original-title');
if (!$target.data('hideTooltip')) {
$target
.attr('title', 'Copied')
.tooltip('fixTitle')
.tooltip('show')
.attr('title', originalTitle)
.tooltip('fixTitle');
}
};
$(function() { export default function initCopyToClipboard() {
const clipboard = new Clipboard('[data-clipboard-target], [data-clipboard-text]'); const clipboard = new Clipboard('[data-clipboard-target], [data-clipboard-text]');
clipboard.on('success', genericSuccess); clipboard.on('success', genericSuccess);
clipboard.on('error', genericError); 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` * This a workaround around ClipboardJS limitations to allow the context-specific copy/pasting
// attribute that ClipboardJS reads from. * of plain text or GFM. The Ruby `clipboard_button` helper sneaks a JSON hash with `text` and
// When ClipboardJS creates a new `textarea` (directly inside `body`, with a `readonly` attribute`), sets its value * `gfm` keys into the `data-clipboard-text` attribute that ClipboardJS reads from.
// to the value of this data attribute, focusses on it, and finally programmatically issues the 'Copy' command, * When ClipboardJS creates a new `textarea` (directly inside `body`, with a `readonly`
// this code intercepts the copy command/event at the last minute to deconstruct this JSON hash and set the * attribute`), sets its value to the value of this data attribute, focusses on it, and finally
// `text/plain` and `text/x-gfm` copy data types to the intended values. * programmatically issues the 'Copy' command, this code intercepts the copy command/event at
$(document).on('copy', 'body > textarea[readonly]', function(e) { * 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) => {
const clipboardData = e.originalEvent.clipboardData; const clipboardData = e.originalEvent.clipboardData;
if (!clipboardData) return; if (!clipboardData) return;
...@@ -71,4 +70,4 @@ $(function() { ...@@ -71,4 +70,4 @@ $(function() {
clipboardData.setData('text/plain', json.text); clipboardData.setData('text/plain', json.text);
clipboardData.setData('text/x-gfm', json.gfm); clipboardData.setData('text/x-gfm', json.gfm);
}); });
}); }
import './autosize'; import './autosize';
import './bind_in_out'; import './bind_in_out';
import initCopyAsGFM from './copy_as_gfm'; import initCopyAsGFM from './copy_as_gfm';
import initCopyToClipboard from './copy_to_clipboard';
import './details_behavior'; import './details_behavior';
import installGlEmojiElement from './gl_emoji'; import installGlEmojiElement from './gl_emoji';
import './quick_submit'; import './quick_submit';
...@@ -9,3 +10,4 @@ import './toggler_behavior'; ...@@ -9,3 +10,4 @@ import './toggler_behavior';
installGlEmojiElement(); installGlEmojiElement();
initCopyAsGFM(); initCopyAsGFM();
initCopyToClipboard();
...@@ -44,7 +44,6 @@ import './commits'; ...@@ -44,7 +44,6 @@ import './commits';
import './compare'; import './compare';
import './compare_autocomplete'; import './compare_autocomplete';
import './confirm_danger_modal'; import './confirm_danger_modal';
import './copy_to_clipboard';
import Flash, { removeFlashClickListener } from './flash'; import Flash, { removeFlashClickListener } from './flash';
import './gl_dropdown'; import './gl_dropdown';
import './gl_field_error'; import './gl_field_error';
......
This diff is collapsed.
...@@ -1288,13 +1288,13 @@ cli-width@^2.0.0: ...@@ -1288,13 +1288,13 @@ cli-width@^2.0.0:
version "2.1.0" version "2.1.0"
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.1.0.tgz#b234ca209b29ef66fc518d9b98d5847b00edf00a" resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.1.0.tgz#b234ca209b29ef66fc518d9b98d5847b00edf00a"
clipboard@^1.5.5: clipboard@^1.5.5, clipboard@^1.7.1:
version "1.6.1" version "1.7.1"
resolved "https://registry.yarnpkg.com/clipboard/-/clipboard-1.6.1.tgz#65c5b654812466b0faab82dc6ba0f1d2f8e4be53" resolved "https://registry.yarnpkg.com/clipboard/-/clipboard-1.7.1.tgz#360d6d6946e99a7a1fef395e42ba92b5e9b5a16b"
dependencies: dependencies:
good-listener "^1.2.0" good-listener "^1.2.2"
select "^1.1.2" select "^1.1.2"
tiny-emitter "^1.0.0" tiny-emitter "^2.0.0"
cliui@^2.1.0: cliui@^2.1.0:
version "2.1.0" version "2.1.0"
...@@ -2883,7 +2883,7 @@ globby@^6.1.0: ...@@ -2883,7 +2883,7 @@ globby@^6.1.0:
pify "^2.0.0" pify "^2.0.0"
pinkie-promise "^2.0.0" pinkie-promise "^2.0.0"
good-listener@^1.2.0: good-listener@^1.2.2:
version "1.2.2" version "1.2.2"
resolved "https://registry.yarnpkg.com/good-listener/-/good-listener-1.2.2.tgz#d53b30cdf9313dffb7dc9a0d477096aa6d145c50" resolved "https://registry.yarnpkg.com/good-listener/-/good-listener-1.2.2.tgz#d53b30cdf9313dffb7dc9a0d477096aa6d145c50"
dependencies: dependencies:
...@@ -6115,9 +6115,9 @@ timers-browserify@^2.0.2: ...@@ -6115,9 +6115,9 @@ timers-browserify@^2.0.2:
dependencies: dependencies:
setimmediate "^1.0.4" setimmediate "^1.0.4"
tiny-emitter@^1.0.0: tiny-emitter@^2.0.0:
version "1.1.0" version "2.0.2"
resolved "https://registry.yarnpkg.com/tiny-emitter/-/tiny-emitter-1.1.0.tgz#ab405a21ffed814a76c19739648093d70654fecb" resolved "https://registry.yarnpkg.com/tiny-emitter/-/tiny-emitter-2.0.2.tgz#82d27468aca5ade8e5fd1e6d22b57dd43ebdfb7c"
tmp@0.0.31, tmp@0.0.x: tmp@0.0.31, tmp@0.0.x:
version "0.0.31" version "0.0.31"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment