Skip to content

Commit

Permalink
Quote part of a message (Fixes #19712)
Browse files Browse the repository at this point in the history
  • Loading branch information
asah committed Mar 11, 2022
1 parent 20368a9 commit b782236
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
19 changes: 19 additions & 0 deletions static/js/click_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as compose from "./compose";
import * as compose_actions from "./compose_actions";
import * as compose_error from "./compose_error";
import * as compose_state from "./compose_state";
import * as copy_and_paste from "./copy_and_paste";
import {media_breakpoints_num} from "./css_variables";
import * as dark_theme from "./dark_theme";
import * as emoji_picker from "./emoji_picker";
Expand Down Expand Up @@ -48,6 +49,11 @@ import * as unread_ops from "./unread_ops";
import * as user_profile from "./user_profile";
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;

export function initialize() {
// MESSAGE CLICKING

Expand Down Expand Up @@ -209,6 +215,19 @@ export function initialize() {
});
}

// https://stackoverflow.com/questions/50771169/jquery-on-textarea-selectionchange
$("#main_div").on("mouseup", ".message_content", () => {
last_message_content_selection = {
selection: window.getSelection(),
text: window.getSelection().toString(),
analysis: copy_and_paste.analyze_selection(window.getSelection()),
};
});
// https://stackoverflow.com/questions/12717782/jquery-input-element-unselect-text-event
$("#main_div").on("blur focus keydown mousedown", ".message_content", () => {
last_message_content_selection = null;
});

$("#main_div").on("click", ".star_container", function (e) {
e.stopPropagation();
popovers.hide_all();
Expand Down
54 changes: 39 additions & 15 deletions static/js/compose_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import $ from "jquery";
import * as fenced_code from "../shared/js/fenced_code";

import * as channel from "./channel";
import * as click_handlers from "./click_handlers";
import * as common from "./common";
import * as compose from "./compose";
import * as compose_fade from "./compose_fade";
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,42 @@ 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;
}

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

// replace content with quoted text
const selinfo = click_handlers.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

0 comments on commit b782236

Please sign in to comment.