Skip to content

Commit

Permalink
fix: change async clipboard api with execCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
xianshenglu committed Nov 11, 2023
1 parent e3f16f1 commit e000306
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions core/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ export default class Clipboard {

_handleCopy(e) {
this._remoteClipboard = e.clipboardData.getData('text/plain');
if (navigator.clipboard.writeText) {
navigator.clipboard.writeText(this._remoteClipboard).catch(() => {/* Do nothing */});
}
this._copy(this._remoteClipboard)
}
/**
* Has a better browser support compared with navigator.clipboard.writeText.
* Also, no permission required.
*/
_copy(text) {
const textarea = document.createElement('textarea');
textarea.innerHTML = text;
document.body.appendChild(textarea);
textarea.select();
const result = document.execCommand('copy');
document.body.removeChild(textarea);
}
/**
* @param {ClipboardEvent} e
Expand Down

0 comments on commit e000306

Please sign in to comment.