Skip to content

Commit

Permalink
rendered_markdown: Fix code getting copied when visiting playground.
Browse files Browse the repository at this point in the history
  • Loading branch information
nimishmedatwal committed Apr 27, 2024
1 parent 5c0cccc commit 3b734fa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
9 changes: 5 additions & 4 deletions web/src/rendered_markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,15 @@ export const update_elements = ($content: JQuery): void => {
$view_in_playground_button.attr("data-tippy-content", title);
$view_in_playground_button.attr("aria-label", title);
}

const clipboard = new ClipboardJS($buttonContainer[0], {
const $copy_button = $buttonContainer.find(".copy_codeblock");
const clipboard = new ClipboardJS($copy_button[0], {
text(copy_element) {
return $(copy_element).siblings("code").text();
const $code = $(copy_element).parent().siblings("code");
return $code.text();
},
});

clipboard.on("success", () => {
const $copy_button = $buttonContainer.find(".copy_codeblock");
show_copied_confirmation($copy_button[0]);
});
$codehilite.addClass("zulip-code-block");
Expand Down
14 changes: 8 additions & 6 deletions web/tests/rendered_markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,12 +529,14 @@ function assert_clipboard_setup() {
assert.equal(clipboard_args[0], "copy-code-stub");
const text = clipboard_args[1].text({
to_$: () => ({
siblings(arg) {
assert.equal(arg, "code");
return {
text: () => "text",
};
},
parent: () => ({
siblings(arg) {
assert.equal(arg, "code");
return {
text: () => "text",
};
},
}),
}),
});
assert.equal(text, "text");
Expand Down

0 comments on commit 3b734fa

Please sign in to comment.