mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 21:13:36 +00:00
copy_messages: Remove clipboard.write usage.
The clipboard.write API requires new permissions and can throw NotAllowedError under certain circumstances in the browser (and always in the current version of Zulip Desktop). Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
committed by
Tim Abbott
parent
0ee4ab3601
commit
f8b027a4e2
@@ -141,31 +141,20 @@ function remove_div(_div: JQuery, ranges: Range[]): void {
|
||||
}, 0);
|
||||
}
|
||||
|
||||
async function copy_selection_to_clipboard(selection: Selection): Promise<void> {
|
||||
function copy_selection_to_clipboard(selection: Selection): void {
|
||||
const range = selection.getRangeAt(0);
|
||||
const div = document.createElement("div");
|
||||
div.append(range.cloneContents());
|
||||
const html_content = div.innerHTML.trim();
|
||||
const plain_text = selection.toString().trim();
|
||||
|
||||
// Reference: https://stackoverflow.com/a/77305170/21940401
|
||||
if (typeof ClipboardItem !== "undefined") {
|
||||
// Shiny new Clipboard API, not fully supported in Firefox.
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API#browser_compatibility
|
||||
const html = new Blob([html_content], {type: "text/html"});
|
||||
const text = new Blob([plain_text], {type: "text/plain"});
|
||||
const data = new ClipboardItem({"text/html": html, "text/plain": text});
|
||||
await navigator.clipboard.write([data]);
|
||||
} else {
|
||||
// Fallback using the deprecated `document.execCommand`.
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#browser_compatibility
|
||||
const cb = (e: ClipboardEvent): void => {
|
||||
e.clipboardData?.setData("text/html", html_content);
|
||||
e.clipboardData?.setData("text/plain", plain_text);
|
||||
e.preventDefault();
|
||||
};
|
||||
clipboard_handler.execute_copy(cb);
|
||||
}
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#browser_compatibility
|
||||
const cb = (e: ClipboardEvent): void => {
|
||||
e.clipboardData?.setData("text/html", html_content);
|
||||
e.clipboardData?.setData("text/plain", plain_text);
|
||||
e.preventDefault();
|
||||
};
|
||||
clipboard_handler.execute_copy(cb);
|
||||
}
|
||||
|
||||
// We want to grab the closest katex-display up the tree
|
||||
@@ -230,7 +219,7 @@ function improve_katex_selection_range(selection: Selection): void {
|
||||
}
|
||||
}
|
||||
|
||||
export async function copy_handler(): Promise<void> {
|
||||
export function copy_handler(): void {
|
||||
// This is the main handler for copying message content via
|
||||
// `Ctrl+C` in Zulip (note that this is totally independent of the
|
||||
// "select region" copy behavior on Linux; that is handled
|
||||
@@ -272,14 +261,14 @@ export async function copy_handler(): Promise<void> {
|
||||
// TODO: Add a reference for this statement, I just tested
|
||||
// it in console for various selection directions and found this
|
||||
// to be the case not sure why there is no online reference for it.
|
||||
await copy_selection_to_clipboard(selection);
|
||||
copy_selection_to_clipboard(selection);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!skip_same_td_check && start_id === end_id) {
|
||||
// Check whether the selection both starts and ends in the
|
||||
// same message. If so, Let the browser handle this.
|
||||
await copy_selection_to_clipboard(selection);
|
||||
copy_selection_to_clipboard(selection);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user