refactor: Extract copy to clipboard function
This commit is contained in:
parent
a79073480b
commit
7239269980
2 changed files with 51 additions and 26 deletions
|
@ -1,4 +1,5 @@
|
||||||
import browser from '../scripts/browser';
|
import browser from '../scripts/browser';
|
||||||
|
import { copy } from '../scripts/clipboard';
|
||||||
import globalize from '../scripts/globalize';
|
import globalize from '../scripts/globalize';
|
||||||
import actionsheet from './actionSheet/actionSheet';
|
import actionsheet from './actionSheet/actionSheet';
|
||||||
import { appHost } from './apphost';
|
import { appHost } from './apphost';
|
||||||
|
@ -366,32 +367,11 @@ import toast from './toast/toast';
|
||||||
break;
|
break;
|
||||||
case 'copy-stream': {
|
case 'copy-stream': {
|
||||||
const downloadHref = apiClient.getItemDownloadUrl(itemId);
|
const downloadHref = apiClient.getItemDownloadUrl(itemId);
|
||||||
const textAreaCopy = function () {
|
copy(downloadHref).then(() => {
|
||||||
const textArea = document.createElement('textarea');
|
toast(globalize.translate('CopyStreamURLSuccess'));
|
||||||
textArea.value = downloadHref;
|
}).catch(() => {
|
||||||
document.body.appendChild(textArea);
|
prompt(globalize.translate('CopyStreamURL'), downloadHref);
|
||||||
textArea.focus();
|
});
|
||||||
textArea.select();
|
|
||||||
|
|
||||||
if (document.execCommand('copy')) {
|
|
||||||
toast(globalize.translate('CopyStreamURLSuccess'));
|
|
||||||
} else {
|
|
||||||
prompt(globalize.translate('CopyStreamURL'), downloadHref);
|
|
||||||
}
|
|
||||||
document.body.removeChild(textArea);
|
|
||||||
};
|
|
||||||
|
|
||||||
/* eslint-disable-next-line compat/compat */
|
|
||||||
if (navigator.clipboard === undefined) {
|
|
||||||
textAreaCopy();
|
|
||||||
} else {
|
|
||||||
/* eslint-disable-next-line compat/compat */
|
|
||||||
navigator.clipboard.writeText(downloadHref).then(function () {
|
|
||||||
toast(globalize.translate('CopyStreamURLSuccess'));
|
|
||||||
}).catch(function () {
|
|
||||||
textAreaCopy();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
getResolveFunction(resolve, id)();
|
getResolveFunction(resolve, id)();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
45
src/scripts/clipboard.js
Normal file
45
src/scripts/clipboard.js
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
/**
|
||||||
|
* Copies text to the clipboard using the textarea.
|
||||||
|
* @param {string} text - Text to copy.
|
||||||
|
* @returns {Promise<void>} Promise.
|
||||||
|
*/
|
||||||
|
function textAreaCopy(text) {
|
||||||
|
const textArea = document.createElement('textarea');
|
||||||
|
textArea.value = text;
|
||||||
|
document.body.appendChild(textArea);
|
||||||
|
textArea.focus();
|
||||||
|
textArea.select();
|
||||||
|
|
||||||
|
let ret;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (document.execCommand('copy')) {
|
||||||
|
ret = Promise.resolve();
|
||||||
|
} else {
|
||||||
|
ret = Promise.reject();
|
||||||
|
}
|
||||||
|
} catch (_) {
|
||||||
|
ret = Promise.reject();
|
||||||
|
}
|
||||||
|
|
||||||
|
document.body.removeChild(textArea);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copies text to the clipboard.
|
||||||
|
* @param {string} text - Text to copy.
|
||||||
|
* @returns {Promise<void>} Promise.
|
||||||
|
*/
|
||||||
|
export function copy(text) {
|
||||||
|
/* eslint-disable-next-line compat/compat */
|
||||||
|
if (navigator.clipboard === undefined) {
|
||||||
|
return textAreaCopy(text);
|
||||||
|
} else {
|
||||||
|
/* eslint-disable-next-line compat/compat */
|
||||||
|
return navigator.clipboard.writeText(text).catch(() => {
|
||||||
|
return textAreaCopy(text);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue