2018-01-09 20:00:17 +03:00
|
|
|
const isSupported =
|
|
|
|
typeof document !== 'undefined' &&
|
|
|
|
document.queryCommandSupported &&
|
|
|
|
document.queryCommandSupported('copy');
|
2016-07-17 18:07:51 +03:00
|
|
|
|
2017-10-12 00:01:37 +03:00
|
|
|
export class ClipboardService {
|
|
|
|
static isSupported(): boolean {
|
2016-10-23 20:18:42 +03:00
|
|
|
return isSupported;
|
2016-07-17 18:07:51 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 00:01:37 +03:00
|
|
|
static selectElement(element: any): void {
|
2016-07-17 18:07:51 +03:00
|
|
|
let range;
|
|
|
|
let selection;
|
2018-01-22 21:30:53 +03:00
|
|
|
if ((document.body as any).createTextRange) {
|
|
|
|
range = (document.body as any).createTextRange();
|
2016-07-17 18:07:51 +03:00
|
|
|
range.moveToElementText(element);
|
|
|
|
range.select();
|
|
|
|
} else if (document.createRange && window.getSelection) {
|
|
|
|
selection = window.getSelection();
|
|
|
|
range = document.createRange();
|
|
|
|
range.selectNodeContents(element);
|
|
|
|
selection.removeAllRanges();
|
|
|
|
selection.addRange(range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 00:01:37 +03:00
|
|
|
static deselect(): void {
|
2018-01-22 21:30:53 +03:00
|
|
|
if ((document as any).selection) {
|
|
|
|
(document as any).selection.empty();
|
2017-10-12 00:01:37 +03:00
|
|
|
} else if (window.getSelection) {
|
2016-07-17 18:07:51 +03:00
|
|
|
window.getSelection().removeAllRanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 00:01:37 +03:00
|
|
|
static copySelected(): boolean {
|
2016-07-17 18:07:51 +03:00
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = document.execCommand('copy');
|
|
|
|
} catch (err) {
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-10-12 00:01:37 +03:00
|
|
|
static copyElement(element: any): boolean {
|
|
|
|
ClipboardService.selectElement(element);
|
2018-01-22 21:30:53 +03:00
|
|
|
const res = ClipboardService.copySelected();
|
|
|
|
if (res) {
|
|
|
|
ClipboardService.deselect();
|
|
|
|
}
|
2016-07-17 18:07:51 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-10-12 00:01:37 +03:00
|
|
|
static copyCustom(text: string): boolean {
|
2018-01-22 21:30:53 +03:00
|
|
|
const textArea = document.createElement('textarea');
|
2016-07-17 18:07:51 +03:00
|
|
|
textArea.style.position = 'fixed';
|
|
|
|
textArea.style.top = '0';
|
|
|
|
textArea.style.left = '0';
|
|
|
|
|
|
|
|
// Ensure it has a small width and height. Setting to 1px / 1em
|
|
|
|
// doesn't work as this gives a negative w/h on some browsers.
|
|
|
|
textArea.style.width = '2em';
|
|
|
|
textArea.style.height = '2em';
|
|
|
|
|
|
|
|
// We don't need padding, reducing the size if it does flash render.
|
|
|
|
textArea.style.padding = '0';
|
|
|
|
|
|
|
|
// Clean up any borders.
|
|
|
|
textArea.style.border = 'none';
|
|
|
|
textArea.style.outline = 'none';
|
|
|
|
textArea.style.boxShadow = 'none';
|
|
|
|
|
|
|
|
// Avoid flash of white box if rendered for any reason.
|
|
|
|
textArea.style.background = 'transparent';
|
|
|
|
|
|
|
|
textArea.value = text;
|
|
|
|
|
|
|
|
document.body.appendChild(textArea);
|
|
|
|
|
|
|
|
textArea.select();
|
|
|
|
|
2018-01-22 21:30:53 +03:00
|
|
|
const res = ClipboardService.copySelected();
|
2016-07-17 18:07:51 +03:00
|
|
|
|
|
|
|
document.body.removeChild(textArea);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|