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: Rewrite quote-and-reply cursor positioning logic.

Tim Abbott8d1013acdb59
diff --git a/static/js/compose_actions.js b/static/js/compose_actions.js
index e44c35fa695..ea2c62f9dcf 100644
--- a/static/js/compose_actions.js
+++ b/static/js/compose_actions.js
@@ -463,6 +463,7 @@ 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();
+ const quoting_placeholder = "[Quoting…]";
if (compose_state.has_message_content()) {
// The user already started typing a message,
@@ -482,7 +483,7 @@ export function quote_and_reply(opts) {
respond_to_message(opts);
}
- compose_ui.insert_syntax_and_focus("[Quoting…]\n", textarea);
+ compose_ui.insert_syntax_and_focus(quoting_placeholder + "\n", textarea);
function replace_content(message) {
// Final message looks like:
@@ -501,10 +502,26 @@ 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}`;
- compose_ui.replace_syntax("[Quoting…]", content, textarea);
+
+ const placeholder_offset = $(textarea).val().indexOf(quoting_placeholder);
+ compose_ui.replace_syntax(quoting_placeholder, content, textarea);
compose_ui.autosize_textarea($("#compose-textarea"));
- // Update textarea caret to point to the new line after quoted message.
- textarea.caret(prev_caret + content.length + 1);
+
+ // When replacing content in a textarea, we need to move the
+ // cursor to preserve its logical position if and only if the
+ // content we just added was before the current cursor
+ // position. If we do, we need to move it by the increase in
+ // the length of the content before the placeholder.
+ if (prev_caret >= placeholder_offset + quoting_placeholder.length) {
+ textarea.caret(prev_caret + content.length - quoting_placeholder.length);
+ } else if (prev_caret > placeholder_offset) {
+ /* In the rare case that our cursor was inside the
+ * placeholder, we treat that as though the cursor was
+ * just after the placeholder. */
+ textarea.caret(placeholder_offset + content.length + 1);
+ } else {
+ textarea.caret(prev_caret);
+ }
}
if (message && message.raw_content) {