Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

clipboard_handler: Use async/await for copy_link_to_clipboard.

Anders Kaseorgb75ac64debd0
diff --git a/web/src/clipboard_handler.ts b/web/src/clipboard_handler.ts
index 3d61b1d0d2f..0892ffd074c 100644
--- a/web/src/clipboard_handler.ts
+++ b/web/src/clipboard_handler.ts
@@ -1,6 +1,5 @@
import assert from "minimalistic-assert";
-import * as blueslip from "./blueslip.ts";
import * as hash_util from "./hash_util.ts";
import * as popover_menus from "./popover_menus.ts";
import * as stream_data from "./stream_data.ts";
@@ -60,22 +59,13 @@ export async function copy_link_to_clipboard(link: string): Promise<void> {
}
/* istanbul ignore next */
-export function popover_copy_link_to_clipboard(
+export async function popover_copy_link_to_clipboard(
instance: typeof popover_menus.popover_instances.message_actions,
$element: JQuery,
- success_callback?: () => void,
-): void {
+): Promise<void> {
// Wrapper for copy_link_to_clipboard handling closing a popover
// and error handling.
const clipboard_text = String($element.attr("data-clipboard-text"));
- void copy_link_to_clipboard(clipboard_text)
- .then(() => {
- popover_menus.hide_current_popover_if_visible(instance);
- if (success_callback !== undefined) {
- success_callback();
- }
- })
- .catch((error: unknown) => {
- blueslip.error("Failed to copy to clipboard: ", {error: String(error)});
- });
+ await copy_link_to_clipboard(clipboard_text);
+ popover_menus.hide_current_popover_if_visible(instance);
}
diff --git a/web/src/hotkey.ts b/web/src/hotkey.ts
index ae7f7a31e66..93e39567b47 100644
--- a/web/src/hotkey.ts
+++ b/web/src/hotkey.ts
@@ -3,7 +3,6 @@ import assert from "minimalistic-assert";
import * as activity from "./activity.ts";
import * as activity_ui from "./activity_ui.ts";
-import * as blueslip from "./blueslip.ts";
import * as browser_history from "./browser_history.ts";
import * as clipboard_handler from "./clipboard_handler.ts";
import * as color_picker_popover from "./color_picker_popover.ts";
@@ -1487,21 +1486,16 @@ function process_hotkey(e: JQuery.KeyDownEvent, hotkey: Hotkey): boolean {
return true;
}
case "vim_right": {
- if (msg.url && !msg.locally_echoed) {
+ const url = msg.url;
+ if (url && !msg.locally_echoed) {
const $row = message_lists.current.selected_row();
const $message_time = $row.find("a.message-time");
- void clipboard_handler
- .copy_link_to_clipboard(msg.url)
- .then(() => {
- show_copied_confirmation(util.the($message_time), {
- custom_content: $t({defaultMessage: "Message link copied!"}),
- });
- })
- .catch((error: unknown) => {
- blueslip.error("Failed to copy link to clipboard: ", {
- error: String(error),
- });
+ void (async () => {
+ await clipboard_handler.copy_link_to_clipboard(url);
+ show_copied_confirmation(util.the($message_time), {
+ custom_content: $t({defaultMessage: "Message link copied!"}),
});
+ });
return true;
}
return false;
diff --git a/web/src/message_actions_popover.ts b/web/src/message_actions_popover.ts
index d67a0994e93..eeaba807194 100644
--- a/web/src/message_actions_popover.ts
+++ b/web/src/message_actions_popover.ts
@@ -240,17 +240,13 @@ export function initialize({
popover_menus.hide_current_popover_if_visible(instance);
});
- $popper.on("click", ".copy_link", (e) => {
- assert(e.currentTarget instanceof HTMLElement);
- clipboard_handler.popover_copy_link_to_clipboard(
- instance,
- $(e.currentTarget),
- () => {
- show_copied_confirmation(
- the($(instance.reference).closest(".message_controls")),
- );
- },
- );
+ $popper.on("click", ".copy_link", function (this: HTMLElement) {
+ void (async () => {
+ await clipboard_handler.popover_copy_link_to_clipboard(instance, $(this));
+ show_copied_confirmation(
+ the($(instance.reference).closest(".message_controls")),
+ );
+ });
});
},
onHidden(instance) {
diff --git a/web/src/stream_popover.ts b/web/src/stream_popover.ts
index cc4a9ae5152..a58bb4711f6 100644
--- a/web/src/stream_popover.ts
+++ b/web/src/stream_popover.ts
@@ -226,9 +226,8 @@ function build_stream_popover(opts: {elt: HTMLElement; stream_id: number}): void
e.stopPropagation();
});
- $popper.on("click", ".copy_stream_link", (e) => {
- assert(e.currentTarget instanceof HTMLElement);
- clipboard_handler.popover_copy_link_to_clipboard(instance, $(e.currentTarget));
+ $popper.on("click", ".copy_stream_link", function (this: HTMLElement) {
+ void clipboard_handler.popover_copy_link_to_clipboard(instance, $(this));
});
},
onHidden(instance) {
diff --git a/web/src/topic_popover.ts b/web/src/topic_popover.ts
index 2b5024ea8d5..da00d72e407 100644
--- a/web/src/topic_popover.ts
+++ b/web/src/topic_popover.ts
@@ -223,10 +223,13 @@ export function initialize(): void {
popover_menus.hide_current_popover_if_visible(instance);
});
- $popper.on("click", ".sidebar-popover-copy-link-to-topic", (e) => {
- assert(e.currentTarget instanceof HTMLElement);
- clipboard_handler.popover_copy_link_to_clipboard(instance, $(e.currentTarget));
- });
+ $popper.on(
+ "click",
+ ".sidebar-popover-copy-link-to-topic",
+ function (this: HTMLElement) {
+ void clipboard_handler.popover_copy_link_to_clipboard(instance, $(this));
+ },
+ );
},
onHidden(instance) {
const $elt = $(instance.reference).closest(".recent_view_focusable");