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.

quote: Introduce `get_highlighted_message_ids`.

Apoorva Pendsedf0e7d8e622e
diff --git a/web/src/compose_reply.ts b/web/src/compose_reply.ts
index 4468ce123e3..86140e3e68f 100644
--- a/web/src/compose_reply.ts
+++ b/web/src/compose_reply.ts
@@ -22,6 +22,7 @@ import * as narrow_state from "./narrow_state.ts";
import * as people from "./people.ts";
import * as recent_view_ui from "./recent_view_ui.ts";
import * as recent_view_util from "./recent_view_util.ts";
+import * as rows from "./rows.ts";
import * as stream_data from "./stream_data.ts";
import * as sub_store from "./sub_store.ts";
import * as topic_link_util from "./topic_link_util.ts";
@@ -34,6 +35,7 @@ type QuoteMessageOpts = {
reply_type?: "personal";
trigger?: string;
forward_message?: boolean;
+ highlighted_message_ids?: number[];
};
const quoting_placeholder = $t({defaultMessage: "[Quoting…]"});
@@ -191,27 +193,37 @@ export function reply_with_mention(opts: {
compose_ui.insert_syntax_and_focus(mention);
}
-export let get_highlighted_message_id = (selection = window.getSelection()): number | undefined => {
- // Returns the message_id if the selection is entirely within a message,
- // otherwise returns undefined.
+export let get_highlighted_message_ids = (
+ selection = window.getSelection(),
+): number[] | undefined => {
+ // Returns the message_ids for a selection.
assert(selection !== null);
if (!selection.toString()) {
return undefined;
}
const {start_id, end_id} = copy_messages.analyze_selection(selection);
- if (start_id === end_id) {
- return start_id;
+ // Unlikely to ever occur.
+ if (start_id === undefined && end_id === undefined) {
+ return undefined;
+ }
+ // This is a weird case and we should fallback to quoting
+ // the selected message.
+ if (start_id === undefined || end_id === undefined) {
+ return undefined;
}
- return undefined;
+ return rows.get_ids_in_range(start_id, end_id);
};
-export function rewire_get_highlighted_message_id(value: typeof get_highlighted_message_id): void {
- get_highlighted_message_id = value;
+export function rewire_get_highlighted_message_ids(
+ value: typeof get_highlighted_message_ids,
+): void {
+ get_highlighted_message_ids = value;
}
function get_quote_target_for_single_message(opts: {
message_id?: number;
quote_content?: string | undefined;
+ highlighted_message_ids?: number[];
}): {
message_id: number;
message: Message;
@@ -228,11 +240,13 @@ function get_quote_target_for_single_message(opts: {
}
} else {
// If triggered via hotkey
- const highlighted_msg_id = get_highlighted_message_id();
- if (highlighted_msg_id) {
+ if (opts.highlighted_message_ids) {
+ assert(opts.highlighted_message_ids.length === 1);
+ const highlighted_message_id = opts.highlighted_message_ids[0];
+ assert(highlighted_message_id !== undefined);
// If the current content selection is entirely within a message,
// we quote that selection.
- message_id = highlighted_msg_id;
+ message_id = highlighted_message_id;
quote_content = get_message_selection();
} else {
// Else we pick the currently focused message.
@@ -396,7 +410,17 @@ function replace_content(message: Message, raw_content: string, forward_message?
}
export function quote_message(opts: QuoteMessageOpts): void {
- quote_single_message(opts);
+ if (opts.message_id) {
+ quote_single_message(opts);
+ return;
+ }
+ const highlighted_message_ids = get_highlighted_message_ids();
+ if (highlighted_message_ids === undefined) {
+ quote_single_message(opts);
+ } else if (highlighted_message_ids.length === 1) {
+ opts.highlighted_message_ids = highlighted_message_ids;
+ quote_single_message(opts);
+ }
}
function quote_single_message(opts: QuoteMessageOpts): void {
diff --git a/web/src/message_actions_popover.ts b/web/src/message_actions_popover.ts
index e8d52a95732..2daf55f5a28 100644
--- a/web/src/message_actions_popover.ts
+++ b/web/src/message_actions_popover.ts
@@ -102,7 +102,12 @@ export function initialize({
const $row = $(instance.reference).closest(".message_row");
const message_id = rows.id($row);
let quote_content: string | undefined;
- if (compose_reply.get_highlighted_message_id() === message_id) {
+ const highlighted_message_ids = compose_reply.get_highlighted_message_ids();
+ if (
+ highlighted_message_ids &&
+ highlighted_message_ids?.length === 1 &&
+ highlighted_message_ids[0] === message_id
+ ) {
// If the user has selected text within this message, quote only that.
// We track the selection right now, before the popover option for Quote
// and reply is clicked, since by then the selection is lost, due to the
diff --git a/web/tests/compose_actions.test.cjs b/web/tests/compose_actions.test.cjs
index f3838b002f8..d84f1bbf7e6 100644
--- a/web/tests/compose_actions.test.cjs
+++ b/web/tests/compose_actions.test.cjs
@@ -464,7 +464,7 @@ test("reply_with_mention", ({override, override_rewire, mock_template}) => {
test("quote_message", ({disallow, override, override_rewire}) => {
override_rewire(compose_recipient, "on_compose_select_recipient_update", noop);
override_rewire(compose_recipient, "update_recipient_row_attention_level", noop);
- override_rewire(compose_reply, "get_highlighted_message_id", () => undefined);
+ override_rewire(compose_reply, "get_highlighted_message_ids", () => undefined);
const $elem = $("#send_message_form");
const $textarea = $("textarea#compose-textarea");
const $indicator = $("#compose-limit-indicator");
@@ -933,7 +933,7 @@ ${fence}`;
opts = {
trigger: "hotkey",
};
- override_rewire(compose_reply, "get_highlighted_message_id", () => 50);
+ override_rewire(compose_reply, "get_highlighted_message_ids", () => [50]);
override_rewire(compose_reply, "get_message_selection", () => "Hello world");
const stub = make_stub();
@@ -965,9 +965,10 @@ ${fence}`;
// to quote, then message_id passed to `respond_to_message` will be same as as the
// id of the message having the pointer.
const message_with_pointer = highlighted_message;
- override_rewire(compose_reply, "get_highlighted_message_id", () => undefined);
+ override_rewire(compose_reply, "get_highlighted_message_ids", () => undefined);
override(message_lists.current, "selected_id", () => 100);
override(message_lists.current, "get", (id) => (id === 100 ? message_with_pointer : undefined));
+ opts = {trigger: "hotkey"};
expected_replacement = quote_message_template({
channel_object,
diff --git a/web/tests/compose_ui.test.cjs b/web/tests/compose_ui.test.cjs
index dcc127f843e..b4c005b1e9e 100644
--- a/web/tests/compose_ui.test.cjs
+++ b/web/tests/compose_ui.test.cjs
@@ -588,7 +588,7 @@ run_test("quote_message", ({override, override_rewire}) => {
// quoting a message, the quoted message should be placed
// at the beginning of compose-box.
override(message_lists.current, "selected_id", () => 100);
- override_rewire(compose_reply, "get_highlighted_message_id", () => undefined);
+ override_rewire(compose_reply, "get_highlighted_message_ids", () => undefined);
compose_reply.quote_message({});
quote_text = "Testing with compose-box closed initially.";