Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
copy_messages: Improve multi message selection.
apoorvapendse3c6543875368
diff --git a/web/e2e-tests/copy-messages.test.ts b/web/e2e-tests/copy-messages.test.tsindex 92b57d673c4..549d8916777 100644--- a/web/e2e-tests/copy-messages.test.ts+++ b/web/e2e-tests/copy-messages.test.ts@@ -4,13 +4,24 @@ import type {Page} from "puppeteer";import * as common from "./lib/common.ts";+type PartialSelectionConfig = {+ select_start_message_partially: boolean;+ select_end_message_partially: boolean;+ start_text_node_offset?: number;+ end_text_node_offset?: number;+};async function copy_messages(page: Page,start_message: string,end_message: string,+ partial_selection_config?: PartialSelectionConfig,): Promise<string[]> {return await page.evaluate(- (start_message: string, end_message: string) => {+ (+ start_message: string,+ end_message: string,+ partial_selection_config?: PartialSelectionConfig,+ ) => {function get_message_node(message: string): Element {return [...document.querySelectorAll(".message-list .message_content")].find((node) => node.textContent?.trim() === message,@@ -19,8 +30,30 @@ async function copy_messages(// select messages from start_message to end_messageconst selectedRange = document.createRange();- selectedRange.setStartAfter(get_message_node(start_message));- selectedRange.setEndBefore(get_message_node(end_message));+ if (partial_selection_config?.select_start_message_partially) {+ const offset = partial_selection_config.start_text_node_offset!;+ const start_message_text_node =+ get_message_node(start_message).querySelector(".message_content p")?.firstChild;+ if (!(start_message_text_node instanceof Text)) {+ throw new TypeError("Expected a Text node");+ }+ selectedRange.setStart(start_message_text_node, offset);+ } else {+ selectedRange.setStartBefore(get_message_node(start_message));+ }+ if (partial_selection_config?.select_end_message_partially) {+ const offset = partial_selection_config.end_text_node_offset!;+ const end_message_text_node =+ get_message_node(end_message).querySelector(".message_content p")?.firstChild;+ if (!(end_message_text_node instanceof Text)) {+ throw new TypeError("Expected a Text node");+ }+ // For the last message, the offset will be from the end of the message,+ // just like how selecting text in the browser would work.+ selectedRange.setEnd(end_message_text_node, end_message_text_node.length - offset);+ } else {+ selectedRange.setEndAfter(get_message_node(end_message));+ }window.getSelection()!.removeAllRanges();window.getSelection()!.addRange(selectedRange);@@ -43,6 +76,7 @@ async function copy_messages(},start_message,end_message,+ partial_selection_config,);}@@ -167,6 +201,35 @@ async function test_timestamp_clipboard_has_datetime(page: Page): Promise<void>);}+async function test_multiple_message_selection_with_partially_selected_bookend_messages(+ page: Page,+): Promise<void> {+ const actual_copied_lines = await copy_messages(+ page,+ "copy paste test B",+ "copy paste test F",+ {+ select_start_message_partially: true,+ select_end_message_partially: true,+ start_text_node_offset: 5,+ end_text_node_offset: 7,+ },+ );+ const expected_copied_lines = [+ "Verona > copy-paste-topic #1 Today",+ // w/o partial selection: "Desdemona: copy paste test B",+ "Desdemona: paste test B",+ "Verona > copy-paste-topic #2 Today",+ "Desdemona: copy paste test C",+ "Desdemona: copy paste test D",+ "Desdemona: copy paste test E",+ "Verona > copy-paste-topic #3 Today",+ // w/o partial selection: "Desdemona: copy paste test F",+ "Desdemona: copy paste",+ ];+ assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);+}+async function copy_paste_test(page: Page): Promise<void> {await common.log_in(page);await common.send_multiple_messages(page, [@@ -213,6 +276,7 @@ async function copy_paste_test(page: Page): Promise<void> {await test_copying_all_from_prev_first_from_next(page);await test_copying_messages_from_several_topics(page);await test_timestamp_clipboard_has_datetime(page);+ await test_multiple_message_selection_with_partially_selected_bookend_messages(page);}await common.run_test(copy_paste_test);diff --git a/web/src/copy_messages.ts b/web/src/copy_messages.tsindex f4ca939e7ce..fefd52479ee 100644--- a/web/src/copy_messages.ts+++ b/web/src/copy_messages.ts@@ -4,8 +4,12 @@import $ from "jquery";import assert from "minimalistic-assert";+import * as blueslip from "./blueslip.ts";import * as message_lists from "./message_lists.ts";import * as rows from "./rows.ts";+import {the} from "./util.ts";++type RangeContainer = "start" | "end";function find_boundary_tr($initial_tr: JQuery,@@ -52,6 +56,83 @@ function construct_recipient_header($message_row: JQuery): JQuery {.replace(/\s$/, "");return $("<p>").append($("<strong>").text(message_header_content));}++// Returns the selected `.message_content`s in the current range.+function get_selected_message_content_elements(): NodeListOf<HTMLElement> | undefined {+ return document+ .getSelection()+ ?.getRangeAt(0)+ .cloneContents()+ .querySelectorAll(".message_content");+}++// Returns the the inner HTML of the `.message_content` element+// for the first or last message of a single range selection.+// The caller is expected to only pass the first or last message+// from a selection range, as the intermediate selected messages+// anyways contain the entire `.message_content` HTML.+function get_html_for_bookend_message_content(+ selected_message_content_element: Node | undefined,+): string {+ assert(window.getSelection()?.rangeCount === 1);+ assert(+ selected_message_content_element !== undefined &&+ selected_message_content_element instanceof HTMLElement,+ );++ // Special case for /me messages.+ // We wrap the /me message content in a `div` to ensure newlines are+ // inserted before and after the message content, which is important+ // when copy pasting multiple messages.+ if (selected_message_content_element.classList.contains("status-message")) {+ return `<div>` + selected_message_content_element.outerHTML + `</div>`;+ }+ return selected_message_content_element.innerHTML;+}++function is_container_within_message_row(type: RangeContainer): boolean {+ const range_count = window.getSelection()?.rangeCount;+ assert(range_count && range_count > 1);++ const target_range_idx = type === "start" ? 0 : range_count - 1;+ const range = window.getSelection()!.getRangeAt(target_range_idx);+ const container = type === "start" ? range.startContainer : range.endContainer;+ assert(container !== undefined);+ return get_nearest_html_element(container)?.closest(".message_row") !== null;+}++function maybe_expand_selection_for_first_and_last_messages(+ copy_rows: JQuery[],+ range_count: number,+): void {+ // This is only meant for a multi-message selection involving+ // multiple ranges.+ assert(range_count > 1 && copy_rows.length > 1);++ if (navigator.userAgent.includes("Chrome")) {+ blueslip.error("Multiple ranges detected in Chrome for a multi-message selection.");+ }++ // We only want to expand the selection ranges if start/end range lies within a message.+ // Not having these checks could alter the range in weird ways, like a message header+ // selection getting removed to select the first message that follows that header.+ if (is_container_within_message_row("start")) {+ const $start = copy_rows[0];+ assert($start?.[0] !== undefined);+ const start_node = the($start.find(".message_content"));+ window.getSelection()?.getRangeAt(0).setStartBefore(start_node);+ }++ if (is_container_within_message_row("end")) {+ const $end = copy_rows.at(-1);+ assert($end?.[0] !== undefined);+ const end_node = the($end.find(".message_content"));+ window+ .getSelection()+ ?.getRangeAt(range_count - 1)+ .setEndAfter(end_node);+ }+}/*The techniques we use in this code date back to2013 and may be obsolete today (and may not have@@ -71,7 +152,63 @@ function construct_copy_div($div: JQuery, start_id: number, end_id: number): voiif (message_lists.current === undefined) {return;}+ let $first_message_element;+ let $last_message_element;const copy_rows = rows.visible_range(start_id, end_id);+ const range_count = window.getSelection()?.rangeCount;+ if (range_count && range_count > 1) {+ // Expand selection to select content from all the messages+ // belonging to the multi-message selection.+ // We do this only for Firefox where multi-message selections are+ // broken down into multiple-ranges.++ // NOTE: FF 147 introduces Chrome-like behavior by not splitting up+ // the selection into multiple ranges for non-selectable elements.+ // We should get rid of this when that becomes the baseline.+ // Details: https://github.com/zulip/zulip/pull/35100#issuecomment-4683858639+ maybe_expand_selection_for_first_and_last_messages(copy_rows, range_count);+ } else {+ // Instead of copying the entire content of the first and last message,+ // we only use the content that is part of the selection.+ // This is only done on Chrome for now, because of the behavior of having+ // a single range for a multi-message selection.+ const selected_message_content_elements = get_selected_message_content_elements();+ assert(selected_message_content_elements !== undefined);+ // Case where the last message doesn't have any highlighted `.message_content`.+ // Here, end_id is set to id of the message whose username at the top+ // was highlighted, but has no highlighted `.message_content`.+ // (See analyze_selection for details.)+ // So the actually useful/contentful last message of this selection is+ // at copy_rows[copy_rows.length - 2]+ if (selected_message_content_elements.length === copy_rows.length - 1) {+ copy_rows.splice(-1, 1);+ if (copy_rows.length === 0) {+ // In case this just involved selecting the username of a message.+ return;+ }+ }+ assert(copy_rows[0] && copy_rows.at(-1));+ const first_selected_message_content_element = the(copy_rows[0]).querySelector(+ ".message_content",+ );+ const last_selected_message_content_element = the(copy_rows.at(-1)!).querySelector(+ ".message_content",+ );+ assert(first_selected_message_content_element && last_selected_message_content_element);+ $first_message_element = $(+ get_html_for_bookend_message_content(selected_message_content_elements[0]),+ );++ // We don't want to append the same content as the first message in the selection+ // if we are trying to get the `.message_content` HTML for the last+ // message when there is only one `.message_content` in the selected range.+ if (selected_message_content_elements.length > 1) {+ const len = selected_message_content_elements.length;+ $last_message_element = $(+ get_html_for_bookend_message_content(selected_message_content_elements[len - 1]),+ );+ }+ }const $start_row = copy_rows[0];assert($start_row !== undefined);@@ -80,7 +217,9 @@ function construct_copy_div($div: JQuery, start_id: number, end_id: number): voilet should_include_start_recipient_header = false;let last_recipient_row_id = start_recipient_row_id;- for (const $row of copy_rows) {+ for (let i = 0; i < copy_rows.length; i += 1) {+ const $row = copy_rows[i];+ assert($row !== undefined && $row[0] instanceof HTMLElement);const recipient_row_id = rows.id_for_recipient_row(rows.get_message_recipient_row($row));// if we found a message from another recipient,// it means that we have messages from several recipients,@@ -93,7 +232,16 @@ function construct_copy_div($div: JQuery, start_id: number, end_id: number): voi}const message = message_lists.current.get(rows.id($row));assert(message !== undefined);- const $content = $(message.content);+ let $content;++ if (i === 0 && $first_message_element) {+ $content = $first_message_element;+ } else if (i === copy_rows.length - 1 && $last_message_element) {+ $content = $last_message_element;+ } else {+ $content = $(message.content);+ }+$content.first().prepend($("<span>").text(message.sender_full_name + ": ")