@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
71 lines • 2.63 kB
JavaScript
export class Clipboard {
constructor() {
this._local = "";
this._allowWriteToGlobalClipboard = false;
this._allowReadFromGlobalClipboard = false;
}
init() {
if (window.navigator.clipboard == null) {
this._allowWriteToGlobalClipboard = this._allowReadFromGlobalClipboard = false;
return;
}
this._allowWriteToGlobalClipboard = this._allowReadFromGlobalClipboard = true;
}
async read() {
if (!this._allowReadFromGlobalClipboard)
return this._local;
const previousFocusedElement = document.activeElement;
var textArea = this._createTextArea();
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
return await window.navigator.clipboard.readText();
}
catch (e) {
console.warn(e);
this._allowReadFromGlobalClipboard = false;
return this._local;
}
finally {
document.body.removeChild(textArea);
previousFocusedElement === null || previousFocusedElement === void 0 ? void 0 : previousFocusedElement.focus();
}
}
async write(text) {
this._local = text;
if (this._allowWriteToGlobalClipboard) {
const previousFocusedElement = document.activeElement;
var textArea = this._createTextArea();
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
await window.navigator.clipboard.writeText(text);
}
catch (e) {
console.warn(e);
this._allowWriteToGlobalClipboard = false;
}
finally {
document.body.removeChild(textArea);
previousFocusedElement === null || previousFocusedElement === void 0 ? void 0 : previousFocusedElement.focus();
}
}
}
_createTextArea() {
const textArea = document.createElement("textarea");
textArea.style.position = "fixed";
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.width = "2em";
textArea.style.height = "2em";
textArea.style.padding = "0";
textArea.style.background = "transparent";
textArea.style.border = "none";
textArea.style.outline = "none";
textArea.style.boxShadow = "none";
return textArea;
}
}
//# sourceMappingURL=Clipboard.js.map