Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compose: Quote part of a message (Fixes #19712) #21834

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions frontend_tests/node_tests/compose_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const compose_ui = zrequire("compose_ui");
const compose = zrequire("compose");
const compose_state = zrequire("compose_state");
const compose_actions = zrequire("compose_actions");
const message_list_view = zrequire("message_list_view");
const message_lists = zrequire("message_lists");
const stream_data = zrequire("stream_data");

Expand Down Expand Up @@ -415,6 +416,33 @@ test("quote_and_reply", ({override, override_rewire}) => {
"translated: @_**Steve Stephenson|90** [said](https://chat.zulip.org/#narrow/stream/92-learning/topic/Tornado):\n````quote\n```\nmultiline code block\nshoudln't mess with quotes\n```\n````";
quote_and_reply(opts);
assert.ok(replaced);

selected_message = {
type: "stream",
stream: "devel",
topic: "test",
sender_full_name: "Steve Stephenson",
sender_id: 90,
raw_content: "```\nmultiline code block\nshoudln't mess with quotes\n```",
};

override_rewire(compose_ui, "replace_syntax", (syntax, replacement) => {
assert.equal(replacement, expected_replacement);
replaced = true;
});
message_list_view.last_message_content_selection = {
text: "```ode block\nshoudln't mes```",
analysis: {
skip_same_td_check: false,
start_id: 100,
end_id: 100,
},
};
replaced = false;
expected_replacement =
"translated: @_**Steve Stephenson|90** [said](https://chat.zulip.org/#narrow/stream/92-learning/topic/Tornado):\n````quote\n```ode block\nshoudln't mes```\n````";
quote_and_reply(opts);
assert.ok(replaced);
});

test("get_focus_area", () => {
Expand Down
63 changes: 48 additions & 15 deletions static/js/compose_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as compose_validate from "./compose_validate";
import * as drafts from "./drafts";
import * as hash_util from "./hash_util";
import {$t} from "./i18n";
import * as message_list_view from "./message_list_view";
import * as message_lists from "./message_lists";
import * as message_viewport from "./message_viewport";
import * as narrow_state from "./narrow_state";
Expand Down Expand Up @@ -472,8 +473,8 @@ export function on_topic_narrow() {

export function quote_and_reply(opts) {
const $textarea = $("#compose-textarea");
const message_id = message_lists.current.selected_id();
const message = message_lists.current.selected_message();
let message_id = message_lists.current.selected_id();
let message = message_lists.current.selected_message();
const quoting_placeholder = $t({defaultMessage: "[Quoting…]"});

if (compose_state.has_message_content()) {
Expand All @@ -496,7 +497,7 @@ export function quote_and_reply(opts) {

compose_ui.insert_syntax_and_focus(quoting_placeholder + "\n", $textarea);

function replace_content(message) {
function replace_content(message, raw_content) {
// Final message looks like:
// @_**Iago|5** [said](link to message):
// ```quote
Expand All @@ -511,8 +512,8 @@ export function quote_and_reply(opts) {
},
);
content += "\n";
const fence = fenced_code.get_unused_fence(message.raw_content);
content += `${fence}quote\n${message.raw_content}\n${fence}`;
const fence = fenced_code.get_unused_fence(raw_content);
content += `${fence}quote\n${raw_content}\n${fence}`;

const placeholder_offset = $($textarea).val().indexOf(quoting_placeholder);
compose_ui.replace_syntax(quoting_placeholder, content, $textarea);
Expand All @@ -535,19 +536,51 @@ export function quote_and_reply(opts) {
}
}

if (message && message.raw_content) {
replace_content(message);
function fetch_and_replace(message_id, message, raw_content) {
if (message && raw_content) {
replace_content(message, raw_content);
return;
}

if (message_id === undefined) {
compose_ui.replace_syntax(quoting_placeholder, "", $textarea);
return;
}

channel.get({
url: "/json/messages/" + message_id,
idempotent: true,
success(data) {
message.raw_content = data ? data.raw_content : "";
replace_content(message, message.raw_content);
},
});
}

// replace content with quoted text
//
// note: we can't call window.getSelection() here because it's "too late"
// in the case that the user called quote/reply from the dropdown menu
// (which un-selects the content). Hence, we save it earlier on-click
const selinfo = message_list_view.last_message_content_selection;
if (
selinfo !== null &&
selinfo.text !== "" &&
// Quote the selected text iff a single message's text is selected.
// Otherwise, just fall through and quote the selected message.
selinfo.analysis !== null &&
!selinfo.analysis.skip_same_td_check &&
selinfo.analysis.start_id === selinfo.analysis.end_id
) {
if (selinfo.analysis.start_id !== message_id) {
message_id = selinfo.analysis.start_id;
message = message_lists.current.get(message_id);
}
fetch_and_replace(message_id, message, selinfo.text.trim());
return;
}

channel.get({
url: "/json/messages/" + message_id,
idempotent: true,
success(data) {
message.raw_content = data.raw_content;
replace_content(message);
},
});
fetch_and_replace(message_id, message, message.raw_content);
}

export function on_narrow(opts) {
Expand Down
17 changes: 17 additions & 0 deletions static/js/message_list_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as color_class from "./color_class";
import * as compose from "./compose";
import * as compose_fade from "./compose_fade";
import * as condense from "./condense";
import * as copy_and_paste from "./copy_and_paste";
import * as hash_util from "./hash_util";
import {$t} from "./i18n";
import * as message_edit from "./message_edit";
Expand All @@ -37,6 +38,11 @@ import * as submessage from "./submessage";
import * as timerender from "./timerender";
import * as util from "./util";

// track the most recent selection because the selection gets lost
// anytime a user clicks (e.g. menu) but we might want to know this
// content (e.g. quote_and_reply).
export let last_message_content_selection = null;

function same_day(earlier_msg, later_msg) {
if (earlier_msg === undefined || later_msg === undefined) {
return false;
Expand Down Expand Up @@ -876,6 +882,17 @@ export class MessageListView {
condense.condense_and_collapse($dom_messages);
}

// needed because window selection is easily lost, e.g. popups, menus etc.
// https://stackoverflow.com/questions/50771169/jquery-on-textarea-selectionchange
$dom_messages.off("mouseup", "*").on("mouseup", "*", () => {
last_message_content_selection = {
selection: window.getSelection(),
text: window.getSelection().toString(),
analysis: copy_and_paste.analyze_selection(window.getSelection()),
swallow_next_blur: false,
};
});

restore_scroll_position();

const last_message_group = this._message_groups.at(-1);
Expand Down