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.

typeahead: Allow automatic insertion/removal of bulleting/numbering.

N-Shar-ma57c0b597f5a8
diff --git a/web/src/composebox_typeahead.js b/web/src/composebox_typeahead.js
index f8d06acfb79..53b2882eb2e 100644
--- a/web/src/composebox_typeahead.js
+++ b/web/src/composebox_typeahead.js
@@ -1,9 +1,11 @@
import $ from "jquery";
import _ from "lodash";
+import {insert} from "text-field-edit";
import * as typeahead from "../shared/src/typeahead";
import render_topic_typeahead_hint from "../templates/topic_typeahead_hint.hbs";
+import * as bulleted_numbered_list_util from "./bulleted_numbered_list_util";
import * as compose_pm_pill from "./compose_pm_pill";
import * as compose_state from "./compose_state";
import * as compose_ui from "./compose_ui";
@@ -151,40 +153,82 @@ export function should_enter_send(e) {
return this_enter_sends;
}
-export function handle_enter($textarea, e) {
- // Used only if Enter doesn't send.
-
- // Since this Enter doesn't send, we just want to do
- // the browser's default behavior for the "Enter" key.
- // Letting the browser handle it works great if the
- // key actually pressed was Enter or Shift-Enter.
-
- // But the default browser behavior for Ctrl/Alt/Meta
- // + Enter is to do nothing, so we need to emulate
- // the browser behavior for "Enter" in those cases.
- //
- // We do this using caret and range from jquery-caret.
- const has_non_shift_modifier_key = e.ctrlKey || e.metaKey || e.altKey;
- if (!has_non_shift_modifier_key) {
- // Use the native browser behavior.
- return;
- }
-
- // To properly emulate browser "Enter", if the
- // user had selected something in the textarea,
- // we need those characters to be cleared.
- const range = $textarea.range();
- if (range.length > 0) {
- $textarea.range(range.start, range.end).range("");
+function handle_bulleting_or_numbering($textarea, e) {
+ // handles automatic insertion or removal of bulleting or numbering
+ const before_text = split_at_cursor($textarea.val(), $textarea)[0];
+ const previous_line = bulleted_numbered_list_util.get_last_line(before_text);
+ 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 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
+ $textarea[0].setSelectionRange($textarea.caret() - 2, $textarea.caret());
+ insert($textarea[0], "");
+ compose_ui.autosize_textarea($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)) {
+ // if previous line was numbered, continue numbering with the new line
+ const previous_number_string = previous_line.slice(0, 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) === "") {
+ // below we select then replaces the last few characters in the textarea before
+ // the cursor - the numbering syntax - with an empty string
+ $textarea[0].setSelectionRange(
+ $textarea.caret() - previous_number_string.length - 2,
+ $textarea.caret(),
+ );
+ insert($textarea[0], "");
+ compose_ui.autosize_textarea($textarea);
+ e.preventDefault();
+ return;
+ }
+ const previous_number = Number.parseInt(previous_number_string, 10);
+ to_append = previous_number + 1 + ". ";
}
-
- // Now add the newline, remembering to resize the
- // textarea if needed.
- $textarea.caret("\n");
+ // if previous line was neither numbered nor bulleted, only add
+ // a new line to emulate default behaviour (to_append is blank)
+ // else we add the bulleting / numbering syntax to the new line
+ insert($textarea[0], "\n" + to_append);
compose_ui.autosize_textarea($textarea);
e.preventDefault();
}
+export function handle_enter($textarea, e) {
+ // Used only if Enter doesn't send. We need to emulate the
+ // browser's native "Enter" behavior because this code path
+ // includes `Ctrl+Enter` and other modifier key variants that
+ // should add a newline in the compose box in the enter-sends
+ // configuration.
+ //
+ // And while we're at it, we implement some fancy behavior for
+ // bulleted lists.
+
+ // To properly emulate browser "Enter", if the user had selected
+ // something in the textarea, we clear those characters
+
+ // If the selectionStart and selectionEnd are not the same, that
+ // means that some text was selected.
+ if ($textarea[0].selectionStart !== $textarea[0].selectionEnd) {
+ // Replace it with the newline, remembering to resize the
+ // textarea if needed.
+ insert($textarea[0], "\n");
+ compose_ui.autosize_textarea($textarea);
+ e.preventDefault();
+ } else {
+ // if nothing had been selected in the texarea we
+ // don't just want to emulate the browser's default
+ // behavior for the "Enter" key, but also handle automatic
+ // insertion or removal of bulleting / numbering.
+ handle_bulleting_or_numbering($textarea, e);
+ }
+}
+
// nextFocus is set on a keydown event to indicate where we should focus on keyup.
// We can't focus at the time of keydown because we need to wait for typeahead.
// And we can't compute where to focus at the time of keyup because only the keydown
diff --git a/web/tests/composebox_typeahead.test.js b/web/tests/composebox_typeahead.test.js
index 4236fa757fa..06143003923 100644
--- a/web/tests/composebox_typeahead.test.js
+++ b/web/tests/composebox_typeahead.test.js
@@ -52,6 +52,7 @@ const compose_pm_pill = zrequire("compose_pm_pill");
const compose_recipient = zrequire("compose_recipient");
const composebox_typeahead = zrequire("composebox_typeahead");
const settings_config = zrequire("settings_config");
+const text_field_edit = mock_esm("text-field-edit");
const pygments_data = zrequire("../generated/pygments_data.json");
const ct = composebox_typeahead;
@@ -1157,16 +1158,14 @@ test("initialize", ({override, override_rewire, mock_template}) => {
event.target.id = "some_non_existing_id";
$("form#send_message_form").trigger(event);
- // Set up jquery functions used in compose_textarea Enter
- // handler.
- let range_length = 0;
- $("#compose-textarea").range = () => ({
- length: range_length,
- range: noop,
- start: 0,
- end: 0 + range_length,
+ $("#compose-textarea")[0] = {
+ selectionStart: 0,
+ selectionEnd: 0,
+ };
+ override(text_field_edit, "insert", (_textarea, syntax) => {
+ assert.equal(syntax, "\n");
});
- $("#compose-textarea").caret = noop;
+ $("#compose-textarea").caret = () => $("#compose-textarea")[0].selectionStart;
event.key = "Enter";
event.target.id = "stream_message_recipient_topic";
@@ -1185,10 +1184,58 @@ test("initialize", ({override, override_rewire, mock_template}) => {
event.altKey = true;
$("form#send_message_form").trigger(event);
- // Cover case where there's a least one character there.
- range_length = 2;
+ // Cover cases where there's at least one character there.
+
+ // Test automatic bulleting.
+ $("#compose-textarea").val("- List item 1\n- List item 2");
+ $("#compose-textarea")[0].selectionStart = 27;
+ $("#compose-textarea")[0].selectionEnd = 27;
+ override(text_field_edit, "insert", (_textarea, syntax) => {
+ assert.equal(syntax, "\n- ");
+ });
+ $("form#send_message_form").trigger(event);
+
+ // Test removal of bullet.
+ $("#compose-textarea").val("- List item 1\n- List item 2\n- ");
+ $("#compose-textarea")[0].selectionStart = 30;
+ $("#compose-textarea")[0].selectionEnd = 30;
+ $("#compose-textarea")[0].setSelectionRange = (start, end) => {
+ assert.equal(start, 28);
+ assert.equal(end, 30);
+ };
+ override(text_field_edit, "insert", (_textarea, syntax) => {
+ assert.equal(syntax, "");
+ });
$("form#send_message_form").trigger(event);
+ // Test automatic numbering.
+ $("#compose-textarea").val("1. List item 1\n2. List item 2");
+ $("#compose-textarea")[0].selectionStart = 29;
+ $("#compose-textarea")[0].selectionEnd = 29;
+ override(text_field_edit, "insert", (_textarea, syntax) => {
+ assert.equal(syntax, "\n3. ");
+ });
+ $("form#send_message_form").trigger(event);
+
+ // Test removal of numbering.
+ $("#compose-textarea").val("1. List item 1\n2. List item 2\n3. ");
+ $("#compose-textarea")[0].selectionStart = 33;
+ $("#compose-textarea")[0].selectionEnd = 33;
+ $("#compose-textarea")[0].setSelectionRange = (start, end) => {
+ assert.equal(start, 30);
+ assert.equal(end, 33);
+ };
+ override(text_field_edit, "insert", (_textarea, syntax) => {
+ assert.equal(syntax, "");
+ });
+ $("form#send_message_form").trigger(event);
+
+ $("#compose-textarea").val("A");
+ $("#compose-textarea")[0].selectionStart = 4;
+ $("#compose-textarea")[0].selectionEnd = 4;
+ override(text_field_edit, "insert", (_textarea, syntax) => {
+ assert.equal(syntax, "\n");
+ });
event.altKey = false;
event.metaKey = true;
$("form#send_message_form").trigger(event);