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: Add button to format bulleted lists.

N-Shar-ma3c0a6d8ee341
diff --git a/web/src/bulleted_numbered_list_util.js b/web/src/bulleted_numbered_list_util.js
new file mode 100644
index 00000000000..ca8809774a6
--- /dev/null
+++ b/web/src/bulleted_numbered_list_util.js
@@ -0,0 +1,12 @@
+export const get_last_line = (text) => text.slice(text.lastIndexOf("\n") + 1);
+
+export const is_bulleted = (line) =>
+ line.startsWith("- ") || line.startsWith("* ") || line.startsWith("+ ");
+
+// testing against regex for string with numbered syntax, that is,
+// any string starting with digit/s followed by a period and space
+export const is_numbered = (line) => /^\d+\. /.test(line);
+
+export const strip_bullet = (line) => line.slice(2);
+
+export const strip_numbering = (line) => line.slice(line.indexOf(" ") + 1);
diff --git a/web/src/compose_ui.js b/web/src/compose_ui.js
index 151aab6685e..2093e2fa06b 100644
--- a/web/src/compose_ui.js
+++ b/web/src/compose_ui.js
@@ -5,6 +5,7 @@ import autosize from "autosize";
import $ from "jquery";
import {insert, replace, set, wrapSelection} from "text-field-edit";
+import * as bulleted_numbered_list_util from "./bulleted_numbered_list_util";
import * as common from "./common";
import {$t} from "./i18n";
import * as loading from "./loading";
@@ -352,14 +353,23 @@ export function format_text($textarea, type, inserted_content) {
const field = $textarea.get(0);
let range = $textarea.range();
let text = $textarea.val();
- const selected_text = range.text;
- // Remove new line and space around selected text.
- const left_trim_length = range.text.length - range.text.trimStart().length;
- const right_trim_length = range.text.length - range.text.trimEnd().length;
+ // Remove new line and space around selected text, except list formatting,
+ // where we want to especially preserve any selected new line character
+ // before the selected text, as it is conventionally depicted with a highlight
+ // at the end of the previous line, which we would like to format.
+ const TRIM_ONLY_END_TYPES = ["bulleted"];
- field.setSelectionRange(range.start + left_trim_length, range.end - right_trim_length);
+ let start_trim_length;
+ if (TRIM_ONLY_END_TYPES.includes(type)) {
+ start_trim_length = 0;
+ } else {
+ start_trim_length = range.text.length - range.text.trimStart().length;
+ }
+ const end_trim_length = range.text.length - range.text.trimEnd().length;
+ field.setSelectionRange(range.start + start_trim_length, range.end - end_trim_length);
range = $textarea.range();
+ const selected_text = range.text;
const is_selection_bold = () =>
// First check if there are enough characters before/after selection.
@@ -375,6 +385,52 @@ export function format_text($textarea, type, inserted_content) {
selected_text.slice(0, bold_syntax.length) === bold_syntax &&
selected_text.slice(-bold_syntax.length) === bold_syntax;
+ const section_off_selected_lines = () => {
+ // Divide all lines of text (separated by `\n`) into those entirely or
+ // partially selected, and those before and after these selected lines.
+ const before = text.slice(0, range.start);
+ const after = text.slice(range.end);
+ let separating_new_line_before = false;
+ let closest_new_line_beginning_before_index;
+ if (before.includes("\n")) {
+ separating_new_line_before = true;
+ closest_new_line_beginning_before_index = before.lastIndexOf("\n");
+ } else {
+ separating_new_line_before = false;
+ // The beginning of the entire text acts as a new line.
+ closest_new_line_beginning_before_index = -1;
+ }
+ let separating_new_line_after = false;
+ let closest_new_line_char_after_index;
+ if (after.includes("\n")) {
+ separating_new_line_after = true;
+ closest_new_line_char_after_index =
+ after.indexOf("\n") + before.length + selected_text.length;
+ } else {
+ separating_new_line_after = false;
+ // The end of the entire text acts as a new line.
+ closest_new_line_char_after_index = text.length;
+ }
+ // selected_lines neither includes the `\n` character that marks its
+ // beginning (which exists if there are before_lines) nor the one
+ // that marks its end (which exists if there are after_lines).
+ const selected_lines = text.slice(
+ closest_new_line_beginning_before_index + 1,
+ closest_new_line_char_after_index,
+ );
+ // before_lines excludes the `\n` character that separates it from selected_lines.
+ const before_lines = text.slice(0, Math.max(0, closest_new_line_beginning_before_index));
+ // after_lines excludes the `\n` character that separates it from selected_lines.
+ const after_lines = text.slice(closest_new_line_char_after_index + 1);
+ return {
+ before_lines,
+ separating_new_line_before,
+ selected_lines,
+ separating_new_line_after,
+ after_lines,
+ };
+ };
+
switch (type) {
case "bold":
// Ctrl + B: Toggle bold syntax on selection.
@@ -495,6 +551,64 @@ export function format_text($textarea, type, inserted_content) {
wrapSelection(field, italic_syntax);
break;
+ case "bulleted": {
+ // We toggle complete lines even when they are partially selected (and just selecting the
+ // newline character after a line counts as partial selection too).
+ const sections = section_off_selected_lines();
+ let {before_lines, selected_lines, after_lines} = sections;
+ const {separating_new_line_before, separating_new_line_after} = sections;
+ // If there is even a single unbulleted line selected, we bullet all.
+ const should_bullet = selected_lines
+ .split("\n")
+ .some((line) => !bulleted_numbered_list_util.is_bulleted(line));
+ if (should_bullet) {
+ selected_lines = selected_lines
+ .split("\n")
+ .map((line) => "- " + line)
+ .join("\n");
+ // We always ensure a blank line before and after the list, as we want
+ // a clean separation between the list and the rest of the text, especially
+ // when the markdown is rendered.
+
+ // Add blank line between text before and list if not already present.
+ if (before_lines.length && before_lines.at(-1) !== "\n") {
+ before_lines += "\n";
+ }
+ // Add blank line between list and rest of text if not already present.
+ if (after_lines.length && after_lines.at(0) !== "\n") {
+ after_lines = "\n" + after_lines;
+ }
+ } else {
+ // Unbullet all bulleted lines by removing the 2 bullet syntax characters.
+ selected_lines = selected_lines
+ .split("\n")
+ .map((line) => bulleted_numbered_list_util.strip_bullet(line))
+ .join("\n");
+ }
+ // Restore the separating newlines that were removed by section_off_selected_lines.
+ if (separating_new_line_before) {
+ before_lines += "\n";
+ }
+ if (separating_new_line_after) {
+ after_lines = "\n" + after_lines;
+ }
+ text = before_lines + selected_lines + after_lines;
+ set(field, text);
+ // If no text was selected, that is, bullet was added to the line with the
+ // cursor, nothing will be selected and the cursor will remain as it was.
+ if (selected_text === "") {
+ field.setSelectionRange(
+ before_lines.length + selected_lines.length,
+ before_lines.length + selected_lines.length,
+ );
+ } else {
+ field.setSelectionRange(
+ before_lines.length,
+ before_lines.length + selected_lines.length,
+ );
+ }
+ break;
+ }
case "link": {
// Ctrl + L: Insert a link to selected text
wrapSelection(field, "[", "](url)");
diff --git a/web/styles/app_components.css b/web/styles/app_components.css
index 8d8dbff820c..fdbe3c0ec32 100644
--- a/web/styles/app_components.css
+++ b/web/styles/app_components.css
@@ -28,6 +28,26 @@ kbd {
user-select: none;
}
+@media (width < $mc_min) {
+ .hide-mc {
+ display: none !important;
+ }
+
+ .show-mc {
+ display: block !important;
+ }
+}
+
+@media (width < $md_min) {
+ .hide-md {
+ display: none !important;
+ }
+
+ .show-md {
+ display: flex !important;
+ }
+}
+
@media (width < $sm_min) {
.hide-sm {
display: none !important;
diff --git a/web/templates/compose_control_buttons.hbs b/web/templates/compose_control_buttons.hbs
index 439cea58575..f9cbbc75d90 100644
--- a/web/templates/compose_control_buttons.hbs
+++ b/web/templates/compose_control_buttons.hbs
@@ -22,14 +22,14 @@
<div class="compose_control_button_container {{#unless giphy_enabled }}hide{{/unless}} preview_mode_disabled" data-tippy-content="{{t 'Add GIF' }}">
<a role="button" class="compose_control_button compose_gif_icon zulip-icon zulip-icon-gif" aria-label="{{t 'Add GIF' }}" tabindex=0></a>
</div>
- <div class="divider hide-sm">|</div>
- <div class="{{#if message_id}}hide-lg{{else}}hide-sm{{/if}}">
+ <div class="divider hide-sm show-md hide-mc">|</div>
+ <div class="{{#if message_id}}hide-lg{{else}}hide-sm show-md hide-mc{{/if}}">
{{> compose_control_buttons_in_popover}}
</div>
<a role="button" class="compose_control_button compose_draft_button hide-sm" tabindex=0 href="#drafts" data-tooltip-template-id="compose_draft_tooltip_template">
{{t 'Drafts' }}
</a>
<div class="compose_control_menu_wrapper" role="button" tabindex=0>
- <a class="compose_control_button zulip-icon zulip-icon-more-vertical hide {{#if message_id}}show-lg{{else}}show-sm{{/if}} compose_control_menu" tabindex="-1" data-tippy-content="Compose actions"></a>
+ <a class="compose_control_button zulip-icon zulip-icon-more-vertical hide {{#if message_id}}show-lg{{else}}show-sm hide-md show-mc{{/if}} compose_control_menu" tabindex="-1" data-tippy-content="Compose actions"></a>
</div>
</div>
diff --git a/web/templates/compose_control_buttons_in_popover.hbs b/web/templates/compose_control_buttons_in_popover.hbs
index f172f550d96..eb7ec7e94ea 100644
--- a/web/templates/compose_control_buttons_in_popover.hbs
+++ b/web/templates/compose_control_buttons_in_popover.hbs
@@ -1,7 +1,8 @@
<div class="compose_control_buttons_container preview_mode_disabled">
<a role="button" data-format-type="bold" class="compose_control_button fa fa-bold formatting_button" aria-label="{{t 'Bold' }}" {{#unless preview_mode_on}} tabindex=0 {{/unless}} data-tippy-content="{{t 'Bold' }}"></a>
<a role="button" data-format-type="italic" class="compose_control_button fa fa-italic formatting_button" aria-label="{{t 'Italic' }}" {{#unless preview_mode_on}} tabindex=0 {{/unless}} data-tippy-content="{{t 'Italic' }}"></a>
+ <a role="button" data-format-type="bulleted" class="compose_control_button fa fa-list-ul formatting_button" aria-label="{{t 'Bulleted list' }}" {{#unless preview_mode_on}} tabindex=0 {{/unless}} data-tippy-content="{{t 'Bulleted list' }}"></a>
<a role="button" data-format-type="link" class="compose_control_button fa fa-link formatting_button" aria-label="{{t 'Link' }}" {{#unless preview_mode_on}} tabindex=0 {{/unless}} data-tippy-content="{{t 'Link' }}"></a>
- <div class="divider hide-sm">|</div>
+ <div class="divider hide-sm show-md hide-mc">|</div>
</div>
<a role="button" class="compose_control_button compose_help_button fa fa-question" tabindex=0 data-tippy-content="{{t 'Message formatting' }}" data-overlay-trigger="message-formatting"></a>
diff --git a/web/tests/compose_ui.test.js b/web/tests/compose_ui.test.js
index d4a626879d2..71e9e08fc0f 100644
--- a/web/tests/compose_ui.test.js
+++ b/web/tests/compose_ui.test.js
@@ -504,7 +504,14 @@ run_test("format_text", ({override}) => {
const $textarea = $("#compose-textarea");
$textarea.get = () => ({
- setSelectionRange() {},
+ setSelectionRange(start, end) {
+ $textarea.range = () => ({
+ start,
+ end,
+ text: $textarea.val().slice(start, end),
+ length: end - start,
+ });
+ },
});
function init_textarea(val, range) {
@@ -534,7 +541,7 @@ run_test("format_text", ({override}) => {
start: 2,
end: 5,
text: "abc",
- length: 7,
+ length: 3,
});
compose_ui.format_text($textarea, "bold");
assert.equal(set_text, "abc");
@@ -636,6 +643,54 @@ run_test("format_text", ({override}) => {
compose_ui.format_text($textarea, "italic");
assert.equal(set_text, "**abc**");
assert.equal(wrap_selection_called, false);
+
+ // Test bulleted list addition
+ reset_state();
+ init_textarea("first_item\nsecond_item", {
+ start: 0,
+ end: 22,
+ text: "first_item\nsecond_item",
+ length: 22,
+ });
+ compose_ui.format_text($textarea, "bulleted");
+ assert.equal(set_text, "- first_item\n- second_item");
+ assert.equal(wrap_selection_called, false);
+
+ // Test bulleted list addition with newline at start
+ reset_state();
+ init_textarea("\nfirst_item\nsecond_item", {
+ start: 0,
+ end: 23,
+ text: "\nfirst_item\nsecond_item",
+ length: 23,
+ });
+ compose_ui.format_text($textarea, "bulleted");
+ assert.equal(set_text, "- \n- first_item\n- second_item");
+ assert.equal(wrap_selection_called, false);
+
+ // Test bulleted list removal
+ reset_state();
+ init_textarea("- first_item\n- second_item", {
+ start: 0,
+ end: 26,
+ text: "- first_item\n- second_item",
+ length: 26,
+ });
+ compose_ui.format_text($textarea, "bulleted");
+ assert.equal(set_text, "first_item\nsecond_item");
+ assert.equal(wrap_selection_called, false);
+
+ // Test bulleted list removal with *s
+ reset_state();
+ init_textarea("* first_item\n* second_item", {
+ start: 0,
+ end: 26,
+ text: "* first_item\n* second_item",
+ length: 26,
+ });
+ compose_ui.format_text($textarea, "bulleted");
+ assert.equal(set_text, "first_item\nsecond_item");
+ assert.equal(wrap_selection_called, false);
});
run_test("markdown_shortcuts", ({override_rewire}) => {