Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
composebox_typeahead: Insert complete channel links on selection.
Shubham Padia7d8ae7cdfe72
diff --git a/web/src/composebox_typeahead.ts b/web/src/composebox_typeahead.tsindex b06dd25333f..87f0d615584 100644--- a/web/src/composebox_typeahead.ts+++ b/web/src/composebox_typeahead.ts@@ -1137,15 +1137,40 @@ export function get_candidates(const topic_shortcut_regex = /#(>)([^*\n]*)$/;// Matches '[#channel](url)>some text' at the end of a split.const partial_fallback_stream_topic_regex = /(\[#)([^*>]+)]\(#[^)]*\)>([^*\n]*)$/;+ // Matches a complete '#**stream name**' link, optionally followed+ // by a topic query; reopening the typeahead here offers topic+ // completion. The query must start immediately after the link —+ // leading whitespace is rejected so an older link on the line+ // can't match across message text and suppress other typeaheads.+ // It excludes `>` to stay non-overlapping with topic_jump above.+ const complete_stream_regex = /#\*\*([^*>]+)\*\*((?!\s)[^*>\n]*)$/;+ // Matches the '[#channel](url)' fallback link for special characters.+ const complete_fallback_stream_regex = /(\[#)([^*>]+)]\(#[^)]*\)((?!\s)[^*>\n]*)$/;const partial_fallback_stream_topic_tokens = partial_fallback_stream_topic_regex.exec(split[0],);const partial_stream_topic_tokens = partial_stream_topic_regex.exec(split[0]);const topic_shortcut_tokens = topic_shortcut_regex.exec(split[0]);+ // A complete link is a valid final form, so only trigger topic+ // completion while the typeahead is already open (the reopen flow+ // after picking a channel), not when the cursor lands after an+ // existing link.+ const typeahead_already_shown = compose_ui.compose_textarea_typeahead?.shown ?? false;+ const complete_channel_link_tokens = typeahead_already_shown+ ? complete_stream_regex.exec(split[0])+ : null;+ const complete_fallback_link_tokens = typeahead_already_shown+ ? complete_fallback_stream_regex.exec(split[0])+ : null;const tokens =partial_stream_topic_tokens ?? // #**channel>topictopic_shortcut_tokens ?? // #>topic- partial_fallback_stream_topic_tokens; // [#channel](url)>topic+ partial_fallback_stream_topic_tokens ?? // [#channel](url)>topic+ complete_channel_link_tokens ?? // #**channel**+ complete_fallback_link_tokens; // [#channel](url)+ const is_complete_channel_link =+ tokens !== null &&+ (tokens === complete_channel_link_tokens || tokens === complete_fallback_link_tokens);const should_begin_typeahead = tokens !== null;if (should_begin_typeahead) {completing = "topic_list";@@ -1185,6 +1210,15 @@ export function get_candidates(}// If we aren't composing to a channel, `sub` would be undefined.if (sub !== undefined) {+ if (+ is_complete_channel_link &&+ stream_data.is_empty_topic_only_channel(sub.stream_id)+ ) {+ // This channel only allows the empty topic, so the+ // complete channel link is the final form; don't offer+ // topic completion.+ return [];+ }// We always show topic suggestions after the user types a stream, and let them// pick between just showing the stream (the first option, when nothing follows ">")// or adding a topic.@@ -1436,16 +1470,22 @@ export function content_typeahead_selected(sub && stream_data.is_empty_topic_only_channel(sub.stream_id);const is_greater_than_key_pressed = event?.type === "keydown" && event.key === ">";- // For empty topic only channel, skip showing topic typeahead and- // insert direct channel link.- if (is_empty_topic_only_channel && !is_greater_than_key_pressed) {- beginning += topic_link_util.get_stream_link_syntax(item.name);- } else if (topic_link_util.will_produce_broken_stream_topic_link(item.name)) {- // for stream links, we use markdown link syntax if the #**stream** syntax- // will generate a broken url.- beginning += topic_link_util.get_fallback_markdown_link(item.name) + ">";+ // For an empty-topic-only channel, the user pressed ">" to+ // explicitly request a topic, so insert the partial syntax to+ // open topic completion.+ if (is_empty_topic_only_channel && is_greater_than_key_pressed) {+ if (topic_link_util.will_produce_broken_stream_topic_link(item.name)) {+ // for stream links, we use markdown link syntax if the #**stream** syntax+ // will generate a broken url.+ beginning += topic_link_util.get_fallback_markdown_link(item.name) + ">";+ } else {+ beginning += "#**" + item.name + ">";+ }} else {- beginning += "#**" + item.name + ">";+ // Insert a complete channel link; the topic typeahead reopens+ // to offer topic completion, so dismissing it leaves a valid+ // link rather than broken `#**channel>` syntax.+ beginning += topic_link_util.get_stream_link_syntax(item.name);}void compose_validate.warn_if_private_stream_is_linked(item, $textbox);diff --git a/web/tests/composebox_typeahead.test.cjs b/web/tests/composebox_typeahead.test.cjsindex 01f09a51cb0..11c249161f6 100644--- a/web/tests/composebox_typeahead.test.cjs+++ b/web/tests/composebox_typeahead.test.cjs@@ -1160,34 +1160,40 @@ test("content_typeahead_selected", ({override}) => {warned_for_stream_link = true;});+ // Selecting a channel inserts a complete channel link; the topic+ // typeahead then reopens to offer topic completion (see the+ // get_candidates tests), so no broken `#**channel>` syntax is left+ // behind if the user dismisses it without picking a topic.query = "#swed";ct.get_or_set_token_for_testing("swed");actual_value = ct.content_typeahead_selected(sweden_stream, query, input_element);- expected_value = "#**Sweden>";+ expected_value = "#**Sweden**";assert.equal(actual_value, expected_value);query = "Hello #swed";ct.get_or_set_token_for_testing("swed");actual_value = ct.content_typeahead_selected(sweden_stream, query, input_element);- expected_value = "Hello #**Sweden>";+ expected_value = "Hello #**Sweden**";assert.equal(actual_value, expected_value);query = "#**swed";ct.get_or_set_token_for_testing("swed");actual_value = ct.content_typeahead_selected(sweden_stream, query, input_element);- expected_value = "#**Sweden>";+ expected_value = "#**Sweden**";assert.equal(actual_value, expected_value);+ // A channel name with special characters falls back to the markdown+ // link syntax, still as a complete link.query = "#**A* al";ct.get_or_set_token_for_testing("A* al");actual_value = ct.content_typeahead_selected(broken_link_stream, query, input_element);- expected_value = "[#A* Algorithm](#narrow/channel/6-A.2A-Algorithm)>";+ expected_value = "[#A* Algorithm](#narrow/channel/6-A.2A-Algorithm)";assert.equal(actual_value, expected_value);query = "#>";ct.get_or_set_token_for_testing("#");actual_value = ct.content_typeahead_selected(broken_link_stream, query, input_element);- expected_value = "[#A* Algorithm](#narrow/channel/6-A.2A-Algorithm)>";+ expected_value = "[#A* Algorithm](#narrow/channel/6-A.2A-Algorithm)";assert.equal(actual_value, expected_value);// A channel that only allows the empty topic gets a complete channel@@ -2968,9 +2974,35 @@ test("begins_typeahead", ({override, override_rewire}) => {// rewrites the link into the partial-then-completed form. Typing after// the link filters topics just like the `>` syntax does.compose_ui.compose_textarea_typeahead = {shown: true};+ assert_typeahead_equals(+ "#**Sweden**",+ typed_topics("Sweden", ["Sweden", ...sweden_topics_to_show]),+ );+ assert_typeahead_equals(+ "#**Sweden**more ice",+ typed_topics("Sweden", ["more ice", "even more ice"]),+ );+ assert_typeahead_equals(+ "#**Sweden**totally new topic",+ typed_topics("Sweden", ["totally new topic"], is_new_topic),+ );// A space immediately after the complete link means the user has- // moved on to the message body, so no topic typeahead is offered.+ // moved on to the message body: the complete-link pattern doesn't+ // match, and with no other token present there are no suggestions.assert_typeahead_equals("#**Sweden** more ice", []);+ // The pattern rejects leading whitespace, so tokens typed after a+ // complete channel link on the same line get their own typeaheads.+ assert.deepEqual(+ get_values("#**Sweden** see #den", "").map((item) => item.name),+ ["Denmark", "Sweden"],+ );+ // The timestamp typeahead also still runs after a channel link.+ assert_typeahead_equals("#**Sweden** at <time:something", [+ {+ message: "translated: Mention a time-zone-aware time",+ type: "time_jump",+ },+ ]);// A channel that only allows the empty topic gets a complete channel// link with no topic completion offered.assert_typeahead_equals("#**Announce**", []);