Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
compose: Prompt topic typeahead after selecting stream.
apoorvapendseee104ab14151
diff --git a/web/src/bootstrap_typeahead.ts b/web/src/bootstrap_typeahead.tsindex ca3bb00414b..0421fd26769 100644--- a/web/src/bootstrap_typeahead.ts+++ b/web/src/bootstrap_typeahead.ts@@ -263,6 +263,8 @@ export class Typeahead<ItemType extends string | object> {// after selecting an option, instead of the default call to lookup().hideAfterSelect: () => boolean;hideOnEmptyAfterBackspace: boolean;+ // Used for adding a custom classname to the typeahead link.+ getCustomItemClassname: ((item: ItemType) => string) | undefined;constructor(input_element: TypeaheadInputElement, options: TypeaheadOptions<ItemType>) {this.input_element = input_element;@@ -304,7 +306,7 @@ export class Typeahead<ItemType extends string | object> {this.updateElementContent = options.updateElementContent ?? true;this.hideAfterSelect = options.hideAfterSelect ?? (() => true);this.hideOnEmptyAfterBackspace = options.hideOnEmptyAfterBackspace ?? false;-+ this.getCustomItemClassname = options.getCustomItemClassname;this.listen();}@@ -547,6 +549,9 @@ export class Typeahead<ItemType extends string | object> {const $item_html = $i.find("a").html(item_html);const option_label_html = this.option_label(matching_items, item);+ if (this.getCustomItemClassname) {+ $item_html.addClass(this.getCustomItemClassname(item));+ }if (option_label_html) {$item_html@@ -887,4 +892,5 @@ type TypeaheadOptions<ItemType> = {shouldHighlightFirstResult?: () => boolean;updateElementContent?: boolean;hideAfterSelect?: () => boolean;+ getCustomItemClassname?: (item: ItemType) => string;};diff --git a/web/src/composebox_typeahead.ts b/web/src/composebox_typeahead.tsindex 27660b11dae..ef8cb766e37 100644--- a/web/src/composebox_typeahead.ts+++ b/web/src/composebox_typeahead.ts@@ -79,9 +79,13 @@ export type LanguageSuggestion = {type: "syntax";};-type TopicSuggestion = {+export type TopicSuggestion = {topic: string;type: "topic_list";+ // is_channel_link will be used when we want to only render the stream as an+ // option in the topic typeahead while having #**stream_name> as the token.+ is_channel_link: boolean;+ stream_data: StreamPillData;};type TimeJumpSuggestion = {@@ -456,22 +460,6 @@ export function tokenize_compose_str(s: string): string {return s.slice(i);}break;- case ">":- // topic_jump- //- // If you hit `>` immediately after completing the typeahead for mentioning a stream,- // this will reposition the user from. If | is the cursor, implements:- //- // `#**stream name** >|` => `#**stream name>|`.- if (- s.slice(Math.max(0, i - 2), i) === "**" ||- s.slice(Math.max(0, i - 3), i) === "** "- ) {- // return any string as long as its not ''.- return ">topic_jump";- }- // maybe topic_list; let's let the stream_topic_regex decide later.- return ">topic_list";}}@@ -955,7 +943,13 @@ export function get_candidates(}const stream_id = stream_data.get_stream_id(stream_name);+ const sub = stream_data.get_sub_by_name(stream_name);+ assert(sub !== undefined);+ // 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.const topic_list = topics_seen_for(stream_id);+if (should_show_custom_query(token, topic_list)) {topic_list.push(token);}@@ -964,8 +958,35 @@ export function get_candidates(const matches_list: TopicSuggestion[] = matches.map((topic) => ({topic,type: "topic_list",+ is_channel_link: false,+ stream_data: {+ ...sub,+ type: "stream",+ // The channel description is only rendered for the channel mention+ // itself, not topic rows, so we leave this blank.+ rendered_description: "",+ },}));- return typeahead_helper.sorter(token, matches_list, (x) => x.topic);+ const topic_suggestion_candidates = typeahead_helper.sorter(+ token,+ matches_list,+ (x) => x.topic,+ );++ // Add link to channel if and only if nothing is typed after '>'+ if (token.length === 0) {+ topic_suggestion_candidates.unshift({+ topic: stream_name,+ type: "topic_list",+ is_channel_link: true,+ stream_data: {+ ...sub,+ type: "stream",+ rendered_description: "",+ },+ });+ }+ return topic_suggestion_candidates;}}}@@ -1031,12 +1052,10 @@ export function content_highlighter_html(item: TypeaheadSuggestion): string | uncase "topic_jump":return typeahead_helper.render_typeahead_item({primary: item.message});case "topic_list": {- const topic_display_name = util.get_final_topic_display_name(item.topic);- const is_empty_string_topic = item.topic === "";- return typeahead_helper.render_typeahead_item({- primary: topic_display_name,- is_empty_string_topic,- });+ if (item.is_channel_link) {+ return typeahead_helper.render_stream(item.stream_data);+ }+ return typeahead_helper.render_stream_topic(item);}case "time_jump":return typeahead_helper.render_typeahead_item({primary: item.message});@@ -1049,7 +1068,6 @@ export function content_typeahead_selected(item: TypeaheadSuggestion,query: string,input_element: TypeaheadInputElement,- event?: JQuery.ClickEvent | JQuery.KeyUpEvent | JQuery.KeyDownEvent,): string {const pieces = split_at_cursor(query, input_element.$element);let beginning = pieces[0];@@ -1142,24 +1160,16 @@ export function content_typeahead_selected(if (beginning.endsWith("#*")) {beginning = beginning.slice(0, -2);}- if (event && event.key === ">") {- // Normally, one accepts typeahead with `Tab` or `Enter`, but when completing- // stream typeahead, we allow `>`, the delimiter for stream+topic mentions,- // as a completion that automatically sets up stream+topic typeahead for you.- // Even if the stream name produces a broken link, we'll go with the #** syntax- // here since we are dealing with it along with the topic name later.- beginning += "#**" + item.name + ">";+ // for stream links, we use markdown link syntax if the #**stream** syntax+ // will generate a broken url.+ if (topic_link_util.will_produce_broken_stream_topic_link(item.name)) {+ // use markdown link syntax+ beginning += topic_link_util.get_fallback_markdown_link(item.name);} else {- // for stream links, we use markdown link syntax if the #**stream** syntax- // will generate a broken url.- if (topic_link_util.will_produce_broken_stream_topic_link(item.name)) {- // use markdown link syntax- beginning += topic_link_util.get_fallback_markdown_link(item.name);- } else {- beginning += "#**" + item.name + "** ";- }+ beginning += "#**" + item.name + ">";}+compose_validate.warn_if_private_stream_is_linked(item, $textbox);break;case "syntax": {@@ -1200,12 +1210,16 @@ export function content_typeahead_selected(// will cause encoding issues.// "beginning" contains all the text before the cursor, so we use lastIndexOf to// avoid any other stream+topic mentions in the message.- const syntax = "#**";- const syntax_start_index = beginning.lastIndexOf(syntax);- const stream_name = beginning.slice(- syntax_start_index + syntax.length,- beginning.lastIndexOf(">"),- );+ const syntax_start_index = beginning.lastIndexOf("#**");+ // Get the stream name, not including "#**" at the start or ">" at the end.+ const topic_start_index = beginning.lastIndexOf(">");+ const stream_name = beginning.slice(syntax_start_index + 3, topic_start_index);++ if (item.is_channel_link) {+ // The user opted to select only the stream and not specify a topic.+ beginning = beginning.slice(0, syntax_start_index) + "#**" + stream_name + "** ";+ break;+ }beginning =beginning.slice(0, syntax_start_index) +topic_link_util.get_stream_topic_link_syntax(stream_name, item.topic) +@@ -1305,9 +1319,6 @@ export function initialize_topic_edit_typeahead(function get_header_html(): string | false {let tip_text = "";switch (completing) {- case "stream":- tip_text = $t({defaultMessage: "Press > for list of topics"});- break;case "silent_mention":tip_text = $t({defaultMessage: "Silent mentions do not trigger notifications."});break;@@ -1351,8 +1362,26 @@ export function initialize_compose_typeahead($element: JQuery<HTMLTextAreaElemenupdater: content_typeahead_selected,stopAdvance: true, // Do not advance to the next field on a Tab or Enterautomated: compose_automated_selection,+ option_label(_matching_items, item): string | false {+ if (item.type === "topic_list" && item.is_channel_link) {+ return `<em>${$t({defaultMessage: "(link to channel)"})}</em>`;+ }+ return false;+ },trigger_selection: compose_trigger_selection,header_html: get_header_html,+ hideAfterSelect() {+ // After selecting a stream, we immediately show topic options,+ // so we don't want to hide the typeahead.+ return completing !== "stream";+ },+ getCustomItemClassname(item) {+ // Inject this class for non stream items in the typeahead menu to remove extra+ // gap between the stream name, chevron and the the topic name.+ return item.type === "topic_list" && !item.is_channel_link+ ? "topic-typeahead-link"+ : "";+ },}),);}diff --git a/web/src/typeahead_helper.ts b/web/src/typeahead_helper.tsindex 00e769e8fcc..9d79d00ca43 100644--- a/web/src/typeahead_helper.ts+++ b/web/src/typeahead_helper.ts@@ -9,7 +9,11 @@ import render_typeahead_list_item from "../templates/typeahead_list_item.hbs";import {MAX_ITEMS} from "./bootstrap_typeahead.ts";import * as buddy_data from "./buddy_data.ts";import * as compose_state from "./compose_state.ts";-import type {LanguageSuggestion, SlashCommandSuggestion} from "./composebox_typeahead.ts";+import type {+ LanguageSuggestion,+ SlashCommandSuggestion,+ TopicSuggestion,+} from "./composebox_typeahead.ts";import type {InputPillContainer} from "./input_pill.ts";import * as people from "./people.ts";import type {PseudoMentionUser, User} from "./people.ts";@@ -105,6 +109,8 @@ export let render_typeahead_item = (args: {stream?: StreamData;emoji_code?: string | undefined;is_empty_string_topic?: boolean;+ topic_object?: TopicSuggestion;+ is_stream_topic?: boolean;}): string => {const has_image = args.img_src !== undefined;const has_status = args.status_emoji_info !== undefined;@@ -113,6 +119,7 @@ export let render_typeahead_item = (args: {const has_pronouns = args.pronouns !== undefined;return render_typeahead_list_item({...args,+ ...args.topic_object,has_image,has_status,has_secondary,@@ -192,6 +199,12 @@ export let render_stream = (stream: StreamData): string =>stream,});+export const render_stream_topic = (topic_object: TopicSuggestion): string =>+ render_typeahead_item({+ topic_object,+ is_stream_topic: true,+ });+export function rewire_render_stream(value: typeof render_stream): void {render_stream = value;}diff --git a/web/styles/typeahead.css b/web/styles/typeahead.cssindex c3baf60181e..94b7c7cf32b 100644--- a/web/styles/typeahead.css+++ b/web/styles/typeahead.css@@ -62,6 +62,10 @@margin-left: -2px;}+ &.topic-typeahead-link {+ gap: 0;+ }+.typeahead-text-container {display: flex;align-self: center;@@ -70,6 +74,17 @@white-space: nowrap;gap: 3px;}++ .compose-stream-name {+ overflow: visible;+ gap: 0;+ }+ }++ .stream-to-topic-arrow {+ cursor: default;+ color: var(--color-compose-chevron-arrow);+ text-decoration: none;}}diff --git a/web/templates/typeahead_list_item.hbs b/web/templates/typeahead_list_item.hbsindex e3b3d48b446..8ed963a8158 100644--- a/web/templates/typeahead_list_item.hbs+++ b/web/templates/typeahead_list_item.hbs@@ -16,6 +16,19 @@{{else if is_user_group}}<i class="typeahead-image zulip-icon zulip-icon-triple-users no-presence-circle" aria-hidden="true"></i>{{/if}}+{{#if is_stream_topic}}+<div class="typeahead-text-container compose-stream-name">+ <strong class="typeahead-strong-section">+ {{~> inline_decorated_stream_name stream=stream_data ~}}+ </strong>+ <span role="button" class="conversation-arrow zulip-icon zulip-icon-chevron-right stream-to-topic-arrow"></span>+</div>+<div class="typeahead-text-container">+ <strong class="typeahead-strong-section">+ {{~ topic ~}}+ </strong>+</div>+{{else}}{{!-- Separate container to ensure overflowing text remains in this container. --}}<div class="typeahead-text-container{{#if has_secondary_html}} has_secondary_html{{/if}}"><strong class="typeahead-strong-section{{#if is_empty_string_topic}} empty-topic-display{{/if}}">@@ -45,3 +58,4 @@</span>{{~/if}}</div>+{{/if}}diff --git a/web/tests/composebox_typeahead.test.cjs b/web/tests/composebox_typeahead.test.cjsindex 2767a05b1cb..0169b69ae97 100644--- a/web/tests/composebox_typeahead.test.cjs+++ b/web/tests/composebox_typeahead.test.cjs@@ -831,19 +831,19 @@ test("content_typeahead_selected", ({override}) => {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);// topic_list@@ -875,6 +875,32 @@ test("content_typeahead_selected", ({override}) => {expected_value = "Hello #**Sweden>testing** ";assert.equal(actual_value, expected_value);+ query = "Hello #**Sweden>";+ ct.get_or_set_token_for_testing("");+ actual_value = ct.content_typeahead_selected(+ {+ topic: "Sweden",+ type: "topic_list",+ is_channel_link: false,+ },+ query,+ input_element,+ );+ expected_value = "Hello #**Sweden>Sweden** ";+ assert.equal(actual_value, expected_value);++ ct.get_or_set_token_for_testing("");+ actual_value = ct.content_typeahead_selected(+ {+ topic: "Sweden",+ type: "topic_list",+ is_channel_link: true,+ },+ query,+ input_element,+ );+ expected_value = "Hello #**Sweden** ";+ assert.equal(actual_value, expected_value);// syntaxct.get_or_set_completing_for_tests("syntax");@@ -1898,10 +1924,16 @@ test("begins_typeahead", ({override, override_rewire}) => {// topic_list// includes "more ice"function typed_topics(topics) {- return topics.map((topic) => ({- type: "topic_list",+ const matches_list = topics.map((topic) => ({+ is_channel_link: false,+ stream_data: {+ ...stream_data.get_sub_by_name("Sweden"),+ rendered_description: "",+ },topic,+ type: "topic_list",}));+ return matches_list;}assert_typeahead_equals("#**Sweden>more ice", typed_topics(["more ice", "even more ice"]));assert_typeahead_equals("#**Sweden>totally new topic", typed_topics(["totally new topic"]));diff --git a/web/tests/typeahead_helper.test.cjs b/web/tests/typeahead_helper.test.cjsindex 99d399d4d23..ef7a4b9d968 100644--- a/web/tests/typeahead_helper.test.cjs+++ b/web/tests/typeahead_helper.test.cjs@@ -956,6 +956,46 @@ test("render_stream", ({mock_template}) => {assert.ok(rendered);});+test("render_stream_topic", ({mock_template}) => {+ let rendered = false;+ const streamData = {+ invite_only: true,+ is_web_public: false,+ color: "blue",+ name: "Design",+ description: "Design related discussions.",+ rendered_description: "",+ subscribed: true,+ };++ const topic_object = {+ topic: "Test topic title",+ stream_data: {+ invite_only: true,+ is_web_public: false,+ color: "blue",+ name: "Design",+ description: "Design related discussions.",+ rendered_description: "",+ subscribed: true,+ },+ type: "topic_list",+ is_stream_only: false,+ };++ mock_template("typeahead_list_item.hbs", false, (args) => {+ assert.equal(args.topic, "Test topic title");+ assert.equal(args.type, "topic_list");+ assert.equal(args.is_stream_only, false);+ assert.equal(args.is_stream_topic, true);+ assert.deepEqual(args.stream_data, streamData);+ rendered = true;+ return "typeahead-item-stub";+ });+ assert.equal(th.render_stream_topic(topic_object), "typeahead-item-stub");+ assert.ok(rendered);+});+test("render_emoji", ({mock_template}) => {// Test render_emoji with normal emoji.let expected_template_data = {