hardlounge/client/js/clipboard.ts

35 lines
871 B
TypeScript
Raw Permalink Normal View History

export default function (chat: HTMLDivElement) {
2019-10-31 14:49:09 +00:00
// Disable in Firefox as it already copies flex text correctly
// @ts-expect-error Property 'InstallTrigger' does not exist on type 'Window & typeof globalThis'.ts(2339)
2019-10-31 14:49:09 +00:00
if (typeof window.InstallTrigger !== "undefined") {
return;
}
2017-08-26 16:36:18 +00:00
const selection = window.getSelection();
if (!selection) {
return;
}
2017-08-26 16:36:18 +00:00
// If selection does not span multiple elements, do nothing
if (selection.anchorNode === selection.focusNode) {
return;
}
const range = selection.getRangeAt(0);
const documentFragment = range.cloneContents();
const div = document.createElement("div");
div.id = "js-copy-hack";
div.appendChild(documentFragment);
chat.appendChild(div);
selection.selectAllChildren(div);
window.setTimeout(() => {
chat.removeChild(div);
selection.removeAllRanges();
selection.addRange(range);
}, 0);
2019-11-16 17:24:03 +00:00
}