Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
compose: Preserve indentation when continuing lists.
Hari200320053c1e6ef9951e
diff --git a/web/src/bulleted_numbered_list_util.ts b/web/src/bulleted_numbered_list_util.tsindex bbb6048aeb9..b5bb35989bc 100644--- a/web/src/bulleted_numbered_list_util.ts+++ b/web/src/bulleted_numbered_list_util.ts@@ -1,5 +1,7 @@export const get_last_line = (text: string): string => text.slice(text.lastIndexOf("\n") + 1);+export const get_indent = (line: string): string => /^(\s*)/.exec(line)![1] ?? "";+export const is_bulleted = (line: string): boolean =>line.startsWith("- ") || line.startsWith("* ") || line.startsWith("+ ");diff --git a/web/src/composebox_typeahead.ts b/web/src/composebox_typeahead.tsindex d7e713628db..b7e62041313 100644--- a/web/src/composebox_typeahead.ts+++ b/web/src/composebox_typeahead.ts@@ -295,29 +295,37 @@ function handle_bulleting_or_numbering(assert(val !== undefined);const before_text = split_at_cursor(val, $textarea)[0];const previous_line = bulleted_numbered_list_util.get_last_line(before_text);+ const indent = bulleted_numbered_list_util.get_indent(previous_line);+ const trimmed_previous_line = previous_line.slice(indent.length);let to_append = "";// if previous line was bulleted, automatically add a bullet to the new line- if (bulleted_numbered_list_util.is_bulleted(previous_line)) {+ if (bulleted_numbered_list_util.is_bulleted(trimmed_previous_line)) {// if previous line had only bullet, remove it and stay on the same line- if (bulleted_numbered_list_util.strip_bullet(previous_line) === "") {- // below we select and replace the last 2 characters in the textarea before- // the cursor - the bullet syntax - with an empty string- util.the($textarea).setSelectionRange($textarea.caret() - 2, $textarea.caret());+ if (bulleted_numbered_list_util.strip_bullet(trimmed_previous_line) === "") {+ // below we select and replace the last few characters in the textarea before+ // the cursor - the indentation and bullet syntax - with an empty string+ util.the($textarea).setSelectionRange(+ $textarea.caret() - indent.length - 2,+ $textarea.caret(),+ );compose_ui.insert_and_scroll_into_view("", $textarea);e.preventDefault();return;}- // use same bullet syntax as the previous line- to_append = previous_line.slice(0, 2);- } else if (bulleted_numbered_list_util.is_numbered(previous_line)) {+ // use same indentation and bullet syntax as the previous line+ to_append = indent + trimmed_previous_line.slice(0, 2);+ } else if (bulleted_numbered_list_util.is_numbered(trimmed_previous_line)) {// if previous line was numbered, continue numbering with the new line- const previous_number_string = previous_line.slice(0, previous_line.indexOf("."));+ const previous_number_string = trimmed_previous_line.slice(+ 0,+ trimmed_previous_line.indexOf("."),+ );// if previous line had only numbering, remove it and stay on the same line- if (bulleted_numbered_list_util.strip_numbering(previous_line) === "") {+ if (bulleted_numbered_list_util.strip_numbering(trimmed_previous_line) === "") {// below we select then replaces the last few characters in the textarea before- // the cursor - the numbering syntax - with an empty string+ // the cursor - the indentation and numbering syntax - with an empty stringutil.the($textarea).setSelectionRange(- $textarea.caret() - previous_number_string.length - 2,+ $textarea.caret() - indent.length - previous_number_string.length - 2,$textarea.caret(),);compose_ui.insert_and_scroll_into_view("", $textarea);@@ -325,7 +333,7 @@ function handle_bulleting_or_numbering(return;}const previous_number = Number.parseInt(previous_number_string, 10);- to_append = previous_number + 1 + ". ";+ to_append = indent + (previous_number + 1) + ". ";}// if previous line was neither numbered nor bulleted, only add// a new line to emulate default behaviour (to_append is blank)diff --git a/web/tests/composebox_typeahead.test.cjs b/web/tests/composebox_typeahead.test.cjsindex e2f30cd6948..9e506c41eb6 100644--- a/web/tests/composebox_typeahead.test.cjs+++ b/web/tests/composebox_typeahead.test.cjs@@ -1910,6 +1910,50 @@ test("initialize", ({override, override_rewire, mock_template}) => {});$("form#send_message_form").trigger(event);+ // Test automatic bulleting with indentation (sub-list).+ $("textarea#compose-textarea").val("- Message 1\n - Message 2");+ $("textarea#compose-textarea")[0].selectionStart = 25;+ $("textarea#compose-textarea")[0].selectionEnd = 25;+ override(compose_ui, "insert_and_scroll_into_view", (content, _textarea) => {+ assert.equal(content, "\n - ");+ });+ $("form#send_message_form").trigger(event);++ // Test removal of indented bullet.+ $("textarea#compose-textarea").val("- Message 1\n - Message 2\n - ");+ $("textarea#compose-textarea")[0].selectionStart = 30;+ $("textarea#compose-textarea")[0].selectionEnd = 30;+ $("textarea#compose-textarea")[0].setSelectionRange = (start, end) => {+ assert.equal(start, 26);+ assert.equal(end, 30);+ };+ override(compose_ui, "insert_and_scroll_into_view", (content, _textarea) => {+ assert.equal(content, "");+ });+ $("form#send_message_form").trigger(event);++ // Test automatic numbering with indentation (sub-list).+ $("textarea#compose-textarea").val("- Message 1\n 1. Message 2");+ $("textarea#compose-textarea")[0].selectionStart = 26;+ $("textarea#compose-textarea")[0].selectionEnd = 26;+ override(compose_ui, "insert_and_scroll_into_view", (content, _textarea) => {+ assert.equal(content, "\n 2. ");+ });+ $("form#send_message_form").trigger(event);++ // Test removal of indented numbering.+ $("textarea#compose-textarea").val("- Message 1\n 1. Message 2\n 1. ");+ $("textarea#compose-textarea")[0].selectionStart = 32;+ $("textarea#compose-textarea")[0].selectionEnd = 32;+ $("textarea#compose-textarea")[0].setSelectionRange = (start, end) => {+ assert.equal(start, 27);+ assert.equal(end, 32);+ };+ override(compose_ui, "insert_and_scroll_into_view", (content, _textarea) => {+ assert.equal(content, "");+ });+ $("form#send_message_form").trigger(event);+$("textarea#compose-textarea").val("A");$("textarea#compose-textarea")[0].selectionStart = 4;$("textarea#compose-textarea")[0].selectionEnd = 4;