Compare commits

...

12 Commits

Author SHA1 Message Date
dependabot[bot]
899378dee9 chore(deps): bump nanoid and mocha (#835)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-12-10 15:13:04 +00:00
dependabot[bot]
b1bcc3dd73 chore(deps): bump qs and body-parser (#833)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-12-10 15:08:42 +00:00
dependabot[bot]
5d497db230 chore(deps): bump decode-uri-component from 0.2.0 to 0.2.2 (#832)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-12-10 15:08:29 +00:00
dependabot[bot]
57ccdf4253 chore(deps): bump engine.io and socket.io (#831)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Helder Berto <helder.burato@gmail.com>
2022-12-10 15:08:17 +00:00
dependabot[bot]
07ba4a89da chore(deps): bump loader-utils from 1.4.0 to 1.4.2 (#830)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-12-10 15:07:47 +00:00
dependabot[bot]
f4daa96342 chore(deps): bump socket.io-parser from 4.0.4 to 4.0.5 (#829)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-12-10 15:07:35 +00:00
dependabot[bot]
b31ef09fd0 chore(deps): bump terser from 5.5.1 to 5.14.2 (#813)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-08-29 11:33:13 +01:00
Beto Muniz
2b2f9eef6f 2.0.11 2022-05-04 14:51:40 -03:00
Beto Muniz
21db7250ed Support more HTML input types. Close #800 (#808)
* Support more HTML input types.

* Improve test description. Remove .only

* Apply logic only when target is an input element
2022-05-04 14:47:44 -03:00
Patrick H. Lauke
08169bce8c Fix #805 don't blur() the trigger after a clipboard action (#807)
* Remove the `blur()` following a clipboard action

It's pointless to set `focus()` on the trigger first, if in the next step you're just going to `blur()` the active element anyway.

* Tweak test to not expect active element to be body

Since it's now not `blur()`ing anymore

* Fix test

see https://github.com/zenorocha/clipboard.js/pull/807#discussion_r862080076
2022-05-04 14:45:44 -03:00
dependabot[bot]
c7c7fda422 chore(deps): bump ansi-regex from 3.0.0 to 3.0.1 (#806)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-05-04 09:32:02 +01:00
Shabai Liu
9b0c87b184 Fix type for copy function (#795)
Co-authored-by: Shabai Liu <shabai_liu@apple.com>
2022-04-28 17:15:48 -03:00
12 changed files with 765 additions and 979 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "clipboard",
"version": "2.0.10",
"version": "2.0.11",
"description": "Modern copy to clipboard. No Flash. Just 3kb",
"license": "MIT",
"main": "dist/clipboard.js",

View File

@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>target-input-number</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<!-- 1. Define some markup -->
<input id="foo" type="number" value="0" />
<button
class="btn"
data-clipboard-action="copy"
data-clipboard-target="#foo"
>
Copy
</button>
<!-- 2. Include library -->
<script src="../dist/clipboard.min.js"></script>
<!-- 3. Instantiate clipboard -->
<script>
var clipboard = new ClipboardJS('.btn');
clipboard.on('success', function (e) {
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});
clipboard.on('error', function (e) {
console.log(e);
});
</script>
</body>
</html>

28
dist/clipboard.js vendored
View File

@@ -1,5 +1,5 @@
/*!
* clipboard.js v2.0.10
* clipboard.js v2.0.11
* https://clipboardjs.com/
*
* Licensed MIT © Zeno Rocha
@@ -94,6 +94,21 @@ function createFakeElement(value) {
/**
* Create fake copy action wrapper using a fake element.
* @param {String} target
* @param {Object} options
* @return {String}
*/
var fakeCopyAction = function fakeCopyAction(value, options) {
var fakeElement = createFakeElement(value);
options.container.appendChild(fakeElement);
var selectedText = select_default()(fakeElement);
command('copy');
fakeElement.remove();
return selectedText;
};
/**
* Copy action wrapper.
* @param {String|HTMLElement} target
@@ -101,6 +116,7 @@ function createFakeElement(value) {
* @return {String}
*/
var ClipboardActionCopy = function ClipboardActionCopy(target) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
container: document.body
@@ -108,11 +124,10 @@ var ClipboardActionCopy = function ClipboardActionCopy(target) {
var selectedText = '';
if (typeof target === 'string') {
var fakeElement = createFakeElement(target);
options.container.appendChild(fakeElement);
selectedText = select_default()(fakeElement);
command('copy');
fakeElement.remove();
selectedText = fakeCopyAction(target, options);
} else if (target instanceof HTMLInputElement && !['text', 'search', 'url', 'tel', 'password'].includes(target === null || target === void 0 ? void 0 : target.type)) {
// If input type doesn't support `setSelectionRange`. Simulate it. https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange
selectedText = fakeCopyAction(target.value, options);
} else {
selectedText = select_default()(target);
command('copy');
@@ -304,7 +319,6 @@ var Clipboard = /*#__PURE__*/function (_Emitter) {
trigger.focus();
}
document.activeElement.blur();
window.getSelection().removeAllRanges();
}
});

File diff suppressed because one or more lines are too long

1614
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -3,7 +3,7 @@
Package.describe({
name: 'zenorocha:clipboard',
summary: 'Modern copy to clipboard. No Flash. Just 3kb.',
version: '2.0.10',
version: '2.0.11',
git: 'https://github.com/zenorocha/clipboard.js',
});

View File

@@ -1,6 +1,6 @@
{
"name": "clipboard",
"version": "2.0.10",
"version": "2.0.11",
"description": "Modern copy to clipboard. No Flash. Just 2kb",
"homepage": "https://clipboardjs.com",
"repository": "zenorocha/clipboard.js",
@@ -36,7 +36,7 @@
"karma-sinon": "^1.0.4",
"karma-webpack": "^5.0.0-alpha.5",
"lint-staged": "^10.5.3",
"mocha": "^8.2.1",
"mocha": "^10.1.0",
"prettier": "2.2.1",
"sinon": "^9.2.3",
"tsd": "^0.7.2",

View File

@@ -2,6 +2,22 @@ import select from 'select';
import command from '../common/command';
import createFakeElement from '../common/create-fake-element';
/**
* Create fake copy action wrapper using a fake element.
* @param {String} target
* @param {Object} options
* @return {String}
*/
const fakeCopyAction = (value, options) => {
const fakeElement = createFakeElement(value);
options.container.appendChild(fakeElement);
const selectedText = select(fakeElement);
command('copy');
fakeElement.remove();
return selectedText;
};
/**
* Copy action wrapper.
* @param {String|HTMLElement} target
@@ -14,11 +30,13 @@ const ClipboardActionCopy = (
) => {
let selectedText = '';
if (typeof target === 'string') {
const fakeElement = createFakeElement(target);
options.container.appendChild(fakeElement);
selectedText = select(fakeElement);
command('copy');
fakeElement.remove();
selectedText = fakeCopyAction(target, options);
} else if (
target instanceof HTMLInputElement &&
!['text', 'search', 'url', 'tel', 'password'].includes(target?.type)
) {
// If input type doesn't support `setSelectionRange`. Simulate it. https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange
selectedText = fakeCopyAction(target.value, options);
} else {
selectedText = select(target);
command('copy');

2
src/clipboard.d.ts vendored
View File

@@ -39,7 +39,7 @@ declare class ClipboardJS {
/**
* Fires a copy action
*/
static copy(target: string | Element, options: CopyActionOptions): string;
static copy(target: string | Element, options?: CopyActionOptions): string;
/**
* Fires a cut action

View File

@@ -86,7 +86,6 @@ class Clipboard extends Emitter {
if (trigger) {
trigger.focus();
}
document.activeElement.blur();
window.getSelection().removeAllRanges();
},
});

View File

@@ -51,5 +51,19 @@ describe('ClipboardActionCopy', () => {
assert.equal(selectedText, text);
});
it('should select its value in a input number based on text', () => {
const value = 1;
document.querySelector('#input').setAttribute('type', 'number');
document.querySelector('#input').setAttribute('value', value);
const selectedText = ClipboardActionCopy(
document.querySelector('#input'),
{
container: document.body,
}
);
assert.equal(Number(selectedText), value);
});
});
});

View File

@@ -171,16 +171,16 @@ describe('Clipboard', () => {
});
describe('#clearSelection', () => {
it('should remove focus from target and text selection', (done) => {
it('should clear text selection without moving focus', (done) => {
let clipboard = new Clipboard('.btn');
clipboard.on('success', (e) => {
e.clearSelection();
let selectedElem = document.activeElement;
let selectedText = window.getSelection().toString();
e.clearSelection();
assert.equal(selectedElem, document.body);
assert.equal(selectedElem, e.trigger);
assert.equal(selectedText, '');
done();