diff --git a/static/js/stream_popover.js b/static/js/stream_popover.js index fdfbcf7441..e9f686de07 100644 --- a/static/js/stream_popover.js +++ b/static/js/stream_popover.js @@ -1,3 +1,4 @@ +import ClipboardJS from "clipboard"; import $ from "jquery"; import _ from "lodash"; @@ -188,6 +189,25 @@ function update_spectrum(popover, update_func) { popover_root.css("top", top + "px"); } +// Builds the `Copy link to topic` topic action. +function build_topic_link_clipboard(url) { + if (!url) { + return; + } + + const copy_event = new ClipboardJS(".sidebar-popover-copy-link-to-topic", { + text() { + return url; + }, + }); + + // Hide the topic popover once the url is successfully + // copied to clipboard. + copy_event.on("success", () => { + hide_topic_popover(); + }); +} + function build_stream_popover(opts) { const elt = opts.elt; const stream_id = opts.stream_id; @@ -391,12 +411,15 @@ export function register_click_handlers() { const stream_li = $(elt).closest(".narrow-filter").expectOne(); const stream_id = elem_to_stream_id(stream_li); const topic_name = $(elt).closest("li").expectOne().attr("data-topic-name"); + const url = $(elt).closest("li").find(".topic-name").expectOne().prop("href"); build_topic_popover({ elt, stream_id, topic_name, }); + + build_topic_link_clipboard(url); }); $("#global_filters").on("click", ".all-messages-sidebar-menu-icon", build_all_messages_popover); diff --git a/static/templates/topic_sidebar_actions.hbs b/static/templates/topic_sidebar_actions.hbs index c438d3cf6b..32c3117822 100644 --- a/static/templates/topic_sidebar_actions.hbs +++ b/static/templates/topic_sidebar_actions.hbs @@ -40,6 +40,13 @@ +
  • + + + {{t "Copy link to topic"}} + +
  • + {{#if can_move_topic}}