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.

compose: Make Quote and Forward menu items follow the text selection.

Aman Agrawal430213a38e51
diff --git a/web/src/compose_reply.ts b/web/src/compose_reply.ts
index 3a78111026c..36cc74dc6bf 100644
--- a/web/src/compose_reply.ts
+++ b/web/src/compose_reply.ts
@@ -246,17 +246,29 @@ function get_highlighted_selection(): HighlightedSelection {
return {type: "multi_message", message_ids};
}
-// Returns the selected text within `message_id`, if the entire current
-// selection is inside that message, and undefined otherwise. Selecting
-// only a message's sender name or timestamp yields no quotable content,
-// which we report the same way as having no selection at all.
-export function get_selection_within_message(message_id: number): string | undefined {
+// What the message actions menu opened on `message_id` should quote or
+// forward, given the text selection at the time the menu was opened.
+export type QuoteMenuSelection =
+ | {kind: "full_message"}
+ | {kind: "message_selection"; quote_content: string}
+ | {kind: "selected_messages"; message_ids: number[]};
+
+export function get_quote_menu_selection(message_id: number): QuoteMenuSelection {
const selection = get_highlighted_selection();
- if (selection.type !== "single_message" || selection.message_id !== message_id) {
- return undefined;
+ if (selection.type === "multi_message" && selection.message_ids.includes(message_id)) {
+ return {kind: "selected_messages", message_ids: selection.message_ids};
+ }
+ if (selection.type === "single_message" && selection.message_id === message_id) {
+ const quote_content = get_message_selection();
+ // Selecting only a sender name or timestamp lands outside
+ // `.message_content`, leaving nothing quotable to offer.
+ if (quote_content.trim() !== "") {
+ return {kind: "message_selection", quote_content};
+ }
}
- const content = get_message_selection();
- return content.trim() === "" ? undefined : content;
+ // With no selection, or one that does not cover this message, the menu
+ // acts on the message it was opened from.
+ return {kind: "full_message"};
}
function get_quote_target_for_single_message(opts: {
@@ -560,6 +572,13 @@ function replace_quoting_placeholder_with(info: {
}
export function quote_messages(opts: QuoteMessageOpts): void {
+ // The message actions menu passes the ids it recorded when the menu was
+ // opened, since the selection they came from no longer exists.
+ const selected_message_ids = opts.highlighted_message_ids;
+ if (selected_message_ids !== undefined && selected_message_ids.length > 1) {
+ quote_multiple_messages(opts);
+ return;
+ }
if (opts.message_id) {
quote_single_message(opts);
return;
diff --git a/web/src/message_actions_popover.ts b/web/src/message_actions_popover.ts
index 8f60ec1cd24..c989242fbe0 100644
--- a/web/src/message_actions_popover.ts
+++ b/web/src/message_actions_popover.ts
@@ -27,15 +27,53 @@ import {the} from "./util.ts";
let message_actions_popover_keyboard_toggle = false;
-// The text selected within the message whose ⋮ button was last pressed,
+// What the message whose ⋮ button was last pressed should quote or forward,
// recorded on mousedown because the selection does not survive the click.
-let quote_content_at_button_mousedown:
- {message_id: number; quote_content: string | undefined} | undefined;
+let quote_menu_selection_at_button_mousedown:
+ {message_id: number; quote_menu_selection: compose_reply.QuoteMenuSelection} | undefined;
+
+// What each open menu was rendered to quote or forward. Keyed by instance so
+// that hiding one menu cannot disturb another that has since been opened.
+const quote_menu_selection_by_instance = new WeakMap<
+ tippy.Instance,
+ compose_reply.QuoteMenuSelection
+>();
function get_action_menu_menu_items(): JQuery {
return $("[data-tippy-root] #message-actions-menu-dropdown li:not(.divider) a");
}
+function quote_from_menu(
+ message_id: number,
+ quote_menu_selection: compose_reply.QuoteMenuSelection,
+ forward_message: boolean,
+): void {
+ switch (quote_menu_selection.kind) {
+ case "full_message":
+ compose_reply.quote_messages({
+ trigger: "popover respond",
+ message_id,
+ forward_message,
+ });
+ return;
+ case "message_selection":
+ compose_reply.quote_messages({
+ trigger: "popover respond",
+ message_id,
+ quote_content: quote_menu_selection.quote_content,
+ forward_message,
+ });
+ return;
+ case "selected_messages":
+ compose_reply.quote_messages({
+ trigger: "popover respond",
+ highlighted_message_ids: quote_menu_selection.message_ids,
+ forward_message,
+ });
+ return;
+ }
+}
+
function focus_first_action_popover_item(): void {
// For now I recommend only calling this when the user opens the menu with a hotkey.
// Our popup menus act kind of funny when you mix keyboard and mouse.
@@ -70,7 +108,7 @@ export function toggle_message_actions_menu(message: Message): boolean {
message_actions_popover_keyboard_toggle = true;
// This synthetic click has no mousedown of its own, so make sure it
// cannot consume a stale selection left behind by an earlier ⋮ press.
- quote_content_at_button_mousedown = undefined;
+ quote_menu_selection_at_button_mousedown = undefined;
$popover_reference.trigger("click");
return true;
}
@@ -94,9 +132,9 @@ export function initialize({
return;
}
const message_id = rows.id($row);
- quote_content_at_button_mousedown = {
+ quote_menu_selection_at_button_mousedown = {
message_id,
- quote_content: compose_reply.get_selection_within_message(message_id),
+ quote_menu_selection: compose_reply.get_quote_menu_selection(message_id),
};
},
);
@@ -120,22 +158,27 @@ export function initialize({
popover_menus.on_show_prep(instance);
const $row = $(instance.reference).closest(".message_row");
const message_id = rows.id($row);
- const args = popover_menus_data.get_actions_popover_content_context(message_id);
+ // Mouse opens rely on what we read on mousedown; keyboard opens
+ // have no mousedown, but do still have the live selection.
+ const at_mousedown = quote_menu_selection_at_button_mousedown;
+ quote_menu_selection_at_button_mousedown = undefined;
+ const quote_menu_selection =
+ at_mousedown?.message_id === message_id
+ ? at_mousedown.quote_menu_selection
+ : compose_reply.get_quote_menu_selection(message_id);
+ quote_menu_selection_by_instance.set(instance, quote_menu_selection);
+ const args = popover_menus_data.get_actions_popover_content_context(
+ message_id,
+ quote_menu_selection,
+ );
instance.setContent(parse_html(render_message_actions_popover(args)));
$row.addClass("has_actions_popover");
},
onMount(instance) {
const $row = $(instance.reference).closest(".message_row");
const message_id = rows.id($row);
- // If the user has selected text within this message, quote only
- // that. Mouse opens rely on what we read on mousedown; keyboard
- // opens have no mousedown, but do still have the live selection.
- const at_mousedown = quote_content_at_button_mousedown;
- quote_content_at_button_mousedown = undefined;
- const quote_content =
- at_mousedown?.message_id === message_id
- ? at_mousedown.quote_content
- : compose_reply.get_selection_within_message(message_id);
+ const quote_menu_selection = quote_menu_selection_by_instance.get(instance);
+ assert(quote_menu_selection !== undefined);
if (message_actions_popover_keyboard_toggle) {
focus_first_action_popover_item();
message_actions_popover_keyboard_toggle = false;
@@ -146,23 +189,14 @@ export function initialize({
// instance.hide gets called.
const $popper = $(instance.popper);
$popper.one("click", ".respond_button", (e) => {
- compose_reply.quote_messages({
- trigger: "popover respond",
- message_id,
- quote_content,
- });
+ quote_from_menu(message_id, quote_menu_selection, false);
e.preventDefault();
e.stopPropagation();
popover_menus.hide_current_popover_if_visible(instance);
});
$popper.one("click", ".forward_button", (e) => {
- compose_reply.quote_messages({
- trigger: "popover respond",
- message_id,
- quote_content,
- forward_message: true,
- });
+ quote_from_menu(message_id, quote_menu_selection, true);
e.preventDefault();
e.stopPropagation();
popover_menus.hide_current_popover_if_visible(instance);
diff --git a/web/src/popover_menus_data.ts b/web/src/popover_menus_data.ts
index e507b41a54d..1069acc40c1 100644
--- a/web/src/popover_menus_data.ts
+++ b/web/src/popover_menus_data.ts
@@ -4,6 +4,7 @@
import assert from "minimalistic-assert";
import * as buddy_data from "./buddy_data.ts";
+import type {QuoteMenuSelection} from "./compose_reply.ts";
import * as gear_menu_util from "./gear_menu_util.ts";
import * as hash_util from "./hash_util.ts";
import {$t} from "./i18n.ts";
@@ -147,7 +148,41 @@ type BillingInfo = {
show_plans: boolean;
};
-export function get_actions_popover_content_context(message_id: number): ActionPopoverContext {
+// The Quote and Forward labels spell out what the menu item will act on,
+// so that clicking it is not a surprise when there is a text selection.
+function get_quote_menu_labels(kind: QuoteMenuSelection["kind"]): {
+ quote_message_menu_item: string;
+ forward_message_menu_item: string;
+} {
+ switch (kind) {
+ case "full_message":
+ return {
+ quote_message_menu_item: $t({defaultMessage: "Quote message"}),
+ forward_message_menu_item: $t({defaultMessage: "Forward message"}),
+ };
+ case "message_selection":
+ return {
+ quote_message_menu_item: $t({defaultMessage: "Quote selection"}),
+ forward_message_menu_item: $t({defaultMessage: "Forward selection"}),
+ };
+ case "selected_messages":
+ return {
+ quote_message_menu_item: $t({defaultMessage: "Quote selected messages"}),
+ forward_message_menu_item: $t({defaultMessage: "Forward selected messages"}),
+ };
+ default: {
+ // Fail loudly rather than mislabel the menu item if a new kind
+ // of selection is added without wording to go with it.
+ const unexpected_kind: never = kind;
+ throw new Error(`Unexpected quote menu selection kind: ${String(unexpected_kind)}`);
+ }
+ }
+}
+
+export function get_actions_popover_content_context(
+ message_id: number,
+ quote_menu_selection: QuoteMenuSelection,
+): ActionPopoverContext {
assert(message_lists.current !== undefined);
const $message_row = message_lists.current.get_row(message_id);
const message = message_lists.current.get(message_id);
@@ -214,8 +249,9 @@ export function get_actions_popover_content_context(message_id: number): ActionP
}
const should_display_quote_message = not_spectator;
- const quote_message_menu_item = $t({defaultMessage: "Quote message"});
- const forward_message_menu_item = $t({defaultMessage: "Forward message"});
+ const {quote_message_menu_item, forward_message_menu_item} = get_quote_menu_labels(
+ quote_menu_selection.kind,
+ );
const conversation_time_url = hash_util.by_conversation_and_time_url(message);
diff --git a/web/tests/compose_reply.test.cjs b/web/tests/compose_reply.test.cjs
index ef47ec08728..c4cc2f69f4f 100644
--- a/web/tests/compose_reply.test.cjs
+++ b/web/tests/compose_reply.test.cjs
@@ -391,24 +391,34 @@ run_test("build_and_process_quote_assets_for_messages", ({override}) => {
);
});
-run_test("get_selection_within_message", ({override_rewire}) => {
+run_test("get_quote_menu_selection", ({override_rewire}) => {
override_rewire(compose_reply, "get_highlighted_message_ids", () => undefined);
- assert.equal(compose_reply.get_selection_within_message(10), undefined, "no selection");
+ assert.deepEqual(compose_reply.get_quote_menu_selection(10), {kind: "full_message"});
override_rewire(compose_reply, "get_highlighted_message_ids", () => [10]);
override_rewire(compose_reply, "get_message_selection", () => "partial text");
- assert.equal(compose_reply.get_selection_within_message(10), "partial text");
+ assert.deepEqual(compose_reply.get_quote_menu_selection(10), {
+ kind: "message_selection",
+ quote_content: "partial text",
+ });
- // The selection is inside a different message than the one asked about.
- assert.equal(compose_reply.get_selection_within_message(99), undefined);
+ // The selection is inside a different message than the menu was opened on.
+ assert.deepEqual(compose_reply.get_quote_menu_selection(99), {kind: "full_message"});
// Selecting only a sender name or timestamp lands outside
- // `.message_content`, so there is nothing quotable to return.
+ // `.message_content`, so there is nothing quotable to offer.
override_rewire(compose_reply, "get_message_selection", () => " ".repeat(3));
- assert.equal(compose_reply.get_selection_within_message(10), undefined);
+ assert.deepEqual(compose_reply.get_quote_menu_selection(10), {kind: "full_message"});
- // A selection spanning several messages is not a within-message one;
+ // A selection spanning several messages, opened from inside the range;
// we return early without consulting the selected text at all.
- override_rewire(compose_reply, "get_highlighted_message_ids", () => [10, 11]);
- assert.equal(compose_reply.get_selection_within_message(10), undefined);
+ override_rewire(compose_reply, "get_highlighted_message_ids", () => [10, 11, 12]);
+ assert.deepEqual(compose_reply.get_quote_menu_selection(11), {
+ kind: "selected_messages",
+ message_ids: [10, 11, 12],
+ });
+
+ // ...and opened from a message outside the range, where the menu acts on
+ // that one message only.
+ assert.deepEqual(compose_reply.get_quote_menu_selection(99), {kind: "full_message"});
});
diff --git a/web/tests/popover_menus_data.test.cjs b/web/tests/popover_menus_data.test.cjs
index d1463907e2e..37ad5d7119d 100644
--- a/web/tests/popover_menus_data.test.cjs
+++ b/web/tests/popover_menus_data.test.cjs
@@ -240,7 +240,9 @@ test("my_message_all_actions", ({override}) => {
];
add_message_with_view(list, messages);
- const response = popover_menus_data.get_actions_popover_content_context(1);
+ const response = popover_menus_data.get_actions_popover_content_context(1, {
+ kind: "full_message",
+ });
assert.equal(response.message_id, 1);
assert.equal(response.stream_id, 1);
assert.equal(response.editability_menu_item, "translated: Edit message");
@@ -256,6 +258,24 @@ test("my_message_all_actions", ({override}) => {
assert.equal(response.should_display_quote_message, true);
assert.equal(response.quote_message_menu_item, "translated: Quote message");
assert.equal(response.forward_message_menu_item, "translated: Forward message");
+
+ // The Quote/Forward labels spell out what a text selection will act on.
+ const selection_response = popover_menus_data.get_actions_popover_content_context(1, {
+ kind: "message_selection",
+ quote_content: "selected text",
+ });
+ assert.equal(selection_response.quote_message_menu_item, "translated: Quote selection");
+ assert.equal(selection_response.forward_message_menu_item, "translated: Forward selection");
+
+ const messages_response = popover_menus_data.get_actions_popover_content_context(1, {
+ kind: "selected_messages",
+ message_ids: [1, 2],
+ });
+ assert.equal(messages_response.quote_message_menu_item, "translated: Quote selected messages");
+ assert.equal(
+ messages_response.forward_message_menu_item,
+ "translated: Forward selected messages",
+ );
});
test("not_my_message_view_actions", ({override}) => {
@@ -290,7 +310,9 @@ test("not_my_message_view_actions", ({override}) => {
add_message_with_view(list, messages);
- const response = popover_menus_data.get_actions_popover_content_context(1);
+ const response = popover_menus_data.get_actions_popover_content_context(1, {
+ kind: "full_message",
+ });
assert.equal(response.view_source_menu_item, "translated: View original message");
assert.equal(response.editability_menu_item, undefined);
@@ -334,7 +356,9 @@ test("not_my_message_view_source_and_move", ({override}) => {
add_message_with_view(list, messages);
- const response = popover_menus_data.get_actions_popover_content_context(1);
+ const response = popover_menus_data.get_actions_popover_content_context(1, {
+ kind: "full_message",
+ });
assert.equal(response.view_source_menu_item, "translated: View original message");
assert.equal(response.editability_menu_item, undefined);
assert.equal(response.move_message_menu_item, "translated: Move messages");