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 Ctrl+]/[ shortcuts for list indent/unindent

sandeep8da9cbf764a3
diff --git a/starlight_help/astro.config.mjs b/starlight_help/astro.config.mjs
index 294dffe886c..1f7871092b9 100644
--- a/starlight_help/astro.config.mjs
+++ b/starlight_help/astro.config.mjs
@@ -327,6 +327,7 @@ export default defineConfig({
"paragraph-and-section-formatting",
"bulleted-lists",
"numbered-lists",
+ "indent-a-list-item",
"tables",
"code-blocks",
"latex",
diff --git a/starlight_help/src/content/docs/format-your-message-using-markdown.mdx b/starlight_help/src/content/docs/format-your-message-using-markdown.mdx
index da0e196f4df..9c0c06b31f2 100644
--- a/starlight_help/src/content/docs/format-your-message-using-markdown.mdx
+++ b/starlight_help/src/content/docs/format-your-message-using-markdown.mdx
@@ -18,6 +18,8 @@ import EmphasisExamples from "../include/_EmphasisExamples.mdx";
import EmphasisIntro from "../include/_EmphasisIntro.mdx";
import GlobalTimesExamples from "../include/_GlobalTimesExamples.mdx";
import GlobalTimesIntro from "../include/_GlobalTimesIntro.mdx";
+import IndentingListItemsExamples from "../include/_IndentingListItemsExamples.mdx";
+import IndentingListItemsIntro from "../include/_IndentingListItemsIntro.mdx";
import LatexExamples from "../include/_LatexExamples.mdx";
import LatexIntro from "../include/_LatexIntro.mdx";
import LinksExamples from "../include/_LinksExamples.mdx";
@@ -73,6 +75,7 @@ below.
* [Text emphasis](#text-emphasis)
* [Bulleted lists](#bulleted-lists)
* [Numbered lists](#numbered-lists)
+* [Indenting list items](#indenting-list-items)
* [Links](#links)
* [Code blocks](#code-blocks)
* [LaTeX](#latex)
@@ -123,6 +126,17 @@ below.
[Learn more](/help/numbered-lists).
</ZulipTip>
+## Indenting list items
+
+<IndentingListItemsIntro />
+
+<IndentingListItemsExamples />
+
+<ZulipTip>
+ You can indent bulleted and numbered list items using keyboard
+ shortcuts. [Learn more](/help/indent-a-list-item).
+</ZulipTip>
+
## Links
<LinksIntro />
diff --git a/starlight_help/src/content/docs/indent-a-list-item.mdx b/starlight_help/src/content/docs/indent-a-list-item.mdx
new file mode 100644
index 00000000000..c9f045eace4
--- /dev/null
+++ b/starlight_help/src/content/docs/indent-a-list-item.mdx
@@ -0,0 +1,31 @@
+---
+title: Indent a list item
+---
+
+import FlattenedSteps from "../../components/FlattenedSteps.astro";
+import IndentingListItemsExamples from "../include/_IndentingListItemsExamples.mdx";
+import IndentingListItemsIntro from "../include/_IndentingListItemsIntro.mdx";
+import StartComposing from "../include/_StartComposing.mdx";
+
+<IndentingListItemsIntro />
+
+## How to indent a list item
+
+<FlattenedSteps>
+ <StartComposing />
+
+ 1. Place your cursor on a list item, or select multiple list items.
+ 1. Press <kbd data-mac-key="Cmd">Ctrl</kbd> + <kbd>]</kbd> to increase
+ indentation, or <kbd data-mac-key="Cmd">Ctrl</kbd> + <kbd>\[</kbd> to
+ reduce indentation.
+</FlattenedSteps>
+
+## Examples
+
+<IndentingListItemsExamples />
+
+## Related articles
+
+* [Bulleted lists](/help/bulleted-lists)
+* [Numbered lists](/help/numbered-lists)
+* [Message formatting](/help/format-your-message-using-markdown)
diff --git a/starlight_help/src/content/include/_IndentingListItemsExamples.mdx b/starlight_help/src/content/include/_IndentingListItemsExamples.mdx
new file mode 100644
index 00000000000..c6588022e76
--- /dev/null
+++ b/starlight_help/src/content/include/_IndentingListItemsExamples.mdx
@@ -0,0 +1,4 @@
+* Press <kbd data-mac-key="Cmd">Ctrl</kbd> + <kbd>]</kbd> to increase the
+ indentation of the current list item.
+* Press <kbd data-mac-key="Cmd">Ctrl</kbd> + <kbd>\[</kbd> to reduce the
+ indentation of the current list item.
diff --git a/starlight_help/src/content/include/_IndentingListItemsIntro.mdx b/starlight_help/src/content/include/_IndentingListItemsIntro.mdx
new file mode 100644
index 00000000000..4c80d8ae4cc
--- /dev/null
+++ b/starlight_help/src/content/include/_IndentingListItemsIntro.mdx
@@ -0,0 +1,2 @@
+In Zulip, you can increase or reduce the indentation of a list item using
+keyboard shortcuts in the compose box.
diff --git a/web/src/bulleted_numbered_list_util.ts b/web/src/bulleted_numbered_list_util.ts
index b5bb35989bc..6e291853a81 100644
--- a/web/src/bulleted_numbered_list_util.ts
+++ b/web/src/bulleted_numbered_list_util.ts
@@ -12,3 +12,8 @@ export const is_numbered = (line: string): boolean => /^\d+\. /.test(line);
export const strip_bullet = (line: string): string => line.slice(2);
export const strip_numbering = (line: string): string => line.slice(line.indexOf(" ") + 1);
+
+export const is_list_item = (line: string): boolean => {
+ const trimmed = line.trimStart();
+ return is_bulleted(trimmed) || is_numbered(trimmed);
+};
diff --git a/web/src/compose_ui.ts b/web/src/compose_ui.ts
index 870c7850af1..0b21882b0d6 100644
--- a/web/src/compose_ui.ts
+++ b/web/src/compose_ui.ts
@@ -584,6 +584,67 @@ export function handle_scrolling_formatting_buttons(event: JQuery.ScrollEvent):
}
}
+export function handle_list_indent(
+ $textarea: JQuery<HTMLTextAreaElement>,
+ increase: boolean,
+): boolean {
+ const elem = $textarea[0]!;
+ const val = elem.value;
+ const start = elem.selectionStart;
+ const end = elem.selectionEnd;
+
+ const line_start = val.lastIndexOf("\n", start - 1) + 1;
+ const line_end_idx = val.indexOf("\n", end);
+ const line_end = line_end_idx === -1 ? val.length : line_end_idx;
+ const selected_block = val.slice(line_start, line_end);
+ const original_lines = selected_block.split("\n");
+ const lines = [...original_lines];
+ let changed = false;
+ let changed_count = 0;
+ let is_list_context = false;
+
+ for (let i = 0; i < lines.length; i += 1) {
+ const line = lines[i]!;
+ if (!bulleted_numbered_list_util.is_list_item(line)) {
+ continue;
+ }
+ is_list_context = true;
+ if (increase) {
+ lines[i] = " " + line;
+ changed = true;
+ changed_count += 1;
+ } else if (line.startsWith(" ")) {
+ lines[i] = line.startsWith(" ") ? line.slice(2) : line.slice(1);
+ changed = true;
+ changed_count += line.startsWith(" ") ? 2 : 1;
+ }
+ }
+
+ if (!changed) {
+ return is_list_context;
+ }
+
+ const new_block = lines.join("\n");
+ const new_val = val.slice(0, line_start) + new_block + val.slice(line_end);
+ setFieldText(elem, new_val);
+
+ let start_offset = 0;
+ if (lines[0] !== original_lines[0]) {
+ if (increase) {
+ start_offset = 2;
+ } else {
+ start_offset = original_lines[0]!.startsWith(" ") ? -2 : -1;
+ }
+ }
+ const end_offset = increase ? changed_count * 2 : -changed_count;
+
+ elem.setSelectionRange(
+ Math.max(line_start, start + start_offset),
+ Math.max(line_start, end + end_offset),
+ );
+ return true;
+}
+
export function handle_keydown(
event: JQuery.KeyboardEventBase,
$textarea: JQuery<HTMLTextAreaElement>,
@@ -615,6 +676,15 @@ export function handle_keydown(
autosize_textarea($textarea);
event.preventDefault();
}
+
+ if (isCmdOrCtrl && (key === "]" || key === "[")) {
+ if (handle_list_indent($textarea, key === "]")) {
+ event.preventDefault();
+ event.stopPropagation();
+ }
+
+ return;
+ }
}
export function handle_keyup(
diff --git a/web/src/info_overlay.ts b/web/src/info_overlay.ts
index 353e3bd5850..0ae14ac99a0 100644
--- a/web/src/info_overlay.ts
+++ b/web/src/info_overlay.ts
@@ -139,6 +139,16 @@ const markdown_help_rows = [
1. ${$t({defaultMessage: "Tea"})}
1. ${$t({defaultMessage: "Coffee"})}`,
},
+ {
+ markdown: "",
+ usage_html: `<kbd>Ctrl</kbd> + <kbd>]</kbd>`,
+ output_html: $t({defaultMessage: "Increase list indentation"}),
+ },
+ {
+ markdown: "",
+ usage_html: `<kbd>Ctrl</kbd> + <kbd>[</kbd>`,
+ output_html: $t({defaultMessage: "Reduce list indentation"}),
+ },
{
markdown: `> ${$t({defaultMessage: "Quoted"})}`,
},
diff --git a/web/tests/compose_ui.test.cjs b/web/tests/compose_ui.test.cjs
index 509c026d097..d00aeb8a1f6 100644
--- a/web/tests/compose_ui.test.cjs
+++ b/web/tests/compose_ui.test.cjs
@@ -1475,3 +1475,57 @@ run_test("get_focus_area", ({override, override_rewire}) => {
"input#stream_message_recipient_topic",
);
});
+
+run_test("handle_list_indent", ({override}) => {
+ override(text_field_edit, "setFieldText", (elt, val) => {
+ elt.value = val;
+ });
+ function make_list_textarea(val, selectionStart, selectionEnd) {
+ const elem = {
+ value: val,
+ selectionStart,
+ selectionEnd,
+ setSelectionRange(start, end) {
+ this.selectionStart = start;
+ this.selectionEnd = end;
+ },
+ };
+ return {length: 1, 0: elem, trigger: noop};
+ }
+
+ {
+ const $t = make_list_textarea("- Item 1\n- Item 2", 9, 9);
+ assert.ok(compose_ui.handle_list_indent($t, true));
+ assert.equal($t[0].value, "- Item 1\n - Item 2");
+ }
+
+ {
+ const $t = make_list_textarea("- Item 1\n - Item 2", 11, 11);
+ assert.ok(compose_ui.handle_list_indent($t, false));
+ assert.equal($t[0].value, "- Item 1\n- Item 2");
+ }
+
+ {
+ const $t = make_list_textarea("Hello world", 5, 5);
+ assert.equal(compose_ui.handle_list_indent($t, true), false);
+ assert.equal($t[0].value, "Hello world");
+ }
+
+ {
+ const $t = make_list_textarea("- Item 1", 3, 3);
+ assert.equal(compose_ui.handle_list_indent($t, false), true);
+ }
+
+ {
+ const val = "- Item 1\n- Item 2\n- Item 3";
+ const $t = make_list_textarea(val, 0, val.length);
+ assert.ok(compose_ui.handle_list_indent($t, true));
+ assert.equal($t[0].value, " - Item 1\n - Item 2\n - Item 3");
+ }
+
+ {
+ const $t = make_list_textarea("1. Item 1", 0, 0);
+ assert.ok(compose_ui.handle_list_indent($t, true));
+ assert.equal($t[0].value, " 1. Item 1");
+ }
+});