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: Allow un-toggle for links.

Julia Bichlerf74721d92640
diff --git a/web/src/compose_ui.js b/web/src/compose_ui.js
index 671c247ceb5..d44e1975fc2 100644
--- a/web/src/compose_ui.js
+++ b/web/src/compose_ui.js
@@ -725,6 +725,93 @@ export function format_text($textarea, type, inserted_content) {
);
};
+ // Links have to be formatted differently because formatting is not only
+ // at the beginning and end of the text, but also in the middle
+ // Therefore more checks are necessary if selected text is already formatted
+ const format_link = () => {
+ const link_syntax_start = "[";
+ const link_syntax_end = "](url)";
+
+ // Captures:
+ // [<description>](<url>)
+ // with just <url> selected
+ const is_selection_url = () =>
+ range.start >= "[](".length &&
+ text.length - range.end >= ")".length &&
+ text.slice(range.start - 2, range.start) === "](" &&
+ text[range.end] === ")" &&
+ text.lastIndexOf("[", range.start - 3) < text.lastIndexOf("]", range.start - 2);
+
+ if (is_selection_url()) {
+ const beginning = text.lastIndexOf("[", range.start);
+ const url = selected_text === "url" ? "" : " " + selected_text;
+ text =
+ text.slice(0, beginning) +
+ text.slice(beginning + 1, text.indexOf("]", beginning)) +
+ url +
+ text.slice(range.end + 1);
+ set(field, text);
+ field.setSelectionRange(range.start - 2, range.start - 3 + url.length);
+ return;
+ }
+
+ // Captures:
+ // [<description>](<url>)
+ // with just <description> selected
+ const is_selection_description_of_link = () =>
+ range.start >= "[".length &&
+ text.length - range.end >= "]()".length &&
+ text.slice(range.start - 1, range.start) === "[" &&
+ text.slice(range.end, range.end + 2) === "](" &&
+ text.includes(")", range.end + 2) &&
+ (text.includes("(", range.end + 2)
+ ? text.indexOf(")", range.end + 2) < text.indexOf("(", range.end + 2)
+ : true);
+
+ if (is_selection_description_of_link()) {
+ let url = text.slice(range.end + 2, text.indexOf(")", range.end));
+ url = url === "url" ? "" : " " + url;
+ text =
+ text.slice(0, range.start - 1) +
+ text.slice(range.start, range.end) +
+ url +
+ text.slice(text.indexOf(")", range.end) + 1);
+ set(field, text);
+ field.setSelectionRange(range.start - 1, range.end - 1);
+ return;
+ }
+
+ // Captures:
+ // [<description>](<url>)
+ // with [<description>](<url>) selected
+ const is_selection_link = () =>
+ range.length >= "[]()".length &&
+ text[range.start] === "[" &&
+ text[range.end - 1] === ")" &&
+ text.slice(range.start + 1, range.end - 1).includes("](");
+
+ if (is_selection_link()) {
+ const description = selected_text.split("](")[0].slice(1);
+ let url = selected_text.split("](")[1].slice(0, -1);
+ url = url === "url" ? "" : " " + url;
+ text = text.slice(0, range.start) + description + url + text.slice(range.end);
+ set(field, text);
+ const new_range_end = url === "" ? range.end - "url".length : range.end;
+ field.setSelectionRange(range.start, new_range_end - "[](".length);
+ return;
+ }
+
+ // Otherwise, we don't have link syntax, so we add it.
+ wrapSelection(field, link_syntax_start, link_syntax_end);
+
+ // Highlight the new `url` part of the syntax.
+ // If <text> marks the selected region, we're mapping:
+ // <text> => [text](<url>).
+ const new_start = range.end + "[](".length;
+ const new_end = new_start + "url".length;
+ field.setSelectionRange(new_start, new_end);
+ };
+
switch (type) {
case "bold":
// Ctrl + B: Toggle bold syntax on selection.
@@ -853,14 +940,7 @@ export function format_text($textarea, type, inserted_content) {
}
case "link": {
// Ctrl + L: Insert a link to selected text
- wrapSelection(field, "[", "](url)");
-
- // Change selected text to `url` part of the syntax.
- // If <text> marks the selected region, we're mapping:
- // <text> => [text](<url>).
- const new_start = range.end + "[](".length;
- const new_end = new_start + "url".length;
- field.setSelectionRange(new_start, new_end);
+ format_link();
break;
}
case "linked": {
diff --git a/web/tests/compose_ui.test.js b/web/tests/compose_ui.test.js
index 48fbe4e482b..cbd325cd53e 100644
--- a/web/tests/compose_ui.test.js
+++ b/web/tests/compose_ui.test.js
@@ -1298,6 +1298,93 @@ run_test("format_text - spoiler", ({override}) => {
assert.equal(wrap_selection_called, false);
});
+run_test("format_text - link", ({override}) => {
+ override(text_field_edit, "set", (_field, text) => {
+ set_text = text;
+ });
+ override(text_field_edit, "wrapSelection", (_field, syntax_start, syntax_end) => {
+ wrap_selection_called = true;
+ wrap_syntax_start = syntax_start;
+ wrap_syntax_end = syntax_end;
+ });
+
+ const link_syntax_start = "[";
+ const link_syntax_end = "](url)";
+
+ // Link selected text
+ reset_state();
+ init_textarea("abc", {
+ start: 0,
+ end: 3,
+ text: "abc",
+ length: 3,
+ });
+ compose_ui.format_text($textarea, "link");
+ assert.equal(set_text, "");
+ assert.equal(wrap_selection_called, true);
+ assert.equal(wrap_syntax_start, link_syntax_start);
+ assert.equal(wrap_syntax_end, link_syntax_end);
+
+ // Undo link selected text, url selected
+ reset_state();
+ init_textarea("[abc](def)", {
+ start: 6,
+ end: 9,
+ text: "def",
+ length: 3,
+ });
+ compose_ui.format_text($textarea, "link");
+ assert.equal(set_text, "abc def");
+ assert.equal(wrap_selection_called, false);
+
+ reset_state();
+ init_textarea("[abc](url)", {
+ start: 6,
+ end: 9,
+ text: "url",
+ length: 3,
+ });
+ compose_ui.format_text($textarea, "link");
+ assert.equal(set_text, "abc");
+ assert.equal(wrap_selection_called, false);
+
+ // Undo link selected text, description selected
+ reset_state();
+ init_textarea("[abc](def)", {
+ start: 1,
+ end: 4,
+ text: "abc",
+ length: 3,
+ });
+ compose_ui.format_text($textarea, "link");
+ assert.equal(set_text, "abc def");
+ assert.equal(wrap_selection_called, false);
+
+ // Undo link selected text, description selected, without disturbing other links
+ reset_state();
+ init_textarea("[xyz](uvw) [abc](def)", {
+ start: 12,
+ end: 15,
+ text: "abc",
+ length: 3,
+ });
+ compose_ui.format_text($textarea, "link");
+ assert.equal(set_text, "[xyz](uvw) abc def");
+ assert.equal(wrap_selection_called, false);
+
+ // Undo link selected text, syntax selected
+ reset_state();
+ init_textarea("[abc](def)", {
+ start: 0,
+ end: 10,
+ text: "[abc](def)",
+ length: 10,
+ });
+ compose_ui.format_text($textarea, "link");
+ assert.equal(set_text, "abc def");
+ assert.equal(wrap_selection_called, false);
+});
+
run_test("markdown_shortcuts", ({override_rewire}) => {
let format_text_type;
override_rewire(compose_ui, "format_text", (_$textarea, type) => {