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.

email_pill: Allow comma into the name part of the email.

Pritesh Shetty013a8fa87e2c
diff --git a/web/src/email_pill.ts b/web/src/email_pill.ts
index c80792f4996..2d3cec86dc4 100644
--- a/web/src/email_pill.ts
+++ b/web/src/email_pill.ts
@@ -1,4 +1,4 @@
-import {parseOneAddress} from "email-addresses";
+import {parseAddressList, parseOneAddress} from "email-addresses";
import type {InputPillConfig, InputPillContainer} from "./input_pill.ts";
import * as input_pill from "./input_pill.ts";
@@ -52,6 +52,20 @@ export function get_current_email(
return null;
}
+export function split_text_to_form_email_pills(raw_emails: string): string[] {
+ const parsed_emails = parseAddressList(raw_emails);
+
+ // If the input string cannot be parsed into valid email addresses,
+ // return the string so that the pill code can handle the invalid case.
+ if (!parsed_emails) {
+ return [raw_emails];
+ }
+
+ return parsed_emails
+ .filter((email) => email.type === "mailbox")
+ .map((email) => (email.name ? `"${email.name}" <${email.address}>` : email.address));
+}
+
export function create_pills(
$pill_container: JQuery,
pill_config?: InputPillConfig,
@@ -62,6 +76,8 @@ export function create_pills(
create_item_from_text: create_item_from_email,
get_text_from_item: get_email_from_item,
get_display_value_from_item: get_email_from_item,
+ split_text_on_comma: false,
+ split_text_to_form_pills: split_text_to_form_email_pills,
});
return pill_container;
}
diff --git a/web/src/input_pill.ts b/web/src/input_pill.ts
index 69c07e76429..bf3a2a08eea 100644
--- a/web/src/input_pill.ts
+++ b/web/src/input_pill.ts
@@ -37,6 +37,7 @@ type InputPillCreateOptions<ItemType> = {
remove_pill: (pill: HTMLElement) => void,
) => void;
show_outline_on_invalid_input?: boolean;
+ split_text_to_form_pills?: (pills: string) => string[];
};
export type InputPill<ItemType> = {
@@ -63,6 +64,7 @@ type InputPillStore<ItemType> = {
split_text_on_comma: boolean;
convert_to_pill_on_enter: boolean;
show_outline_on_invalid_input: boolean;
+ split_text_to_form_pills: InputPillCreateOptions<ItemType>["split_text_to_form_pills"];
};
// These are the functions that are exposed to other modules.
@@ -111,6 +113,7 @@ export function create<ItemType extends {type: string}>(
generate_pill_html: opts.generate_pill_html,
on_pill_exit: opts.on_pill_exit,
show_outline_on_invalid_input: opts.show_outline_on_invalid_input ?? false,
+ split_text_to_form_pills: opts.split_text_to_form_pills,
};
// a dictionary of internal functions. Some of these are exposed as well,
@@ -270,7 +273,11 @@ export function create<ItemType extends {type: string}>(
insertManyPills(pills: string | string[]) {
if (typeof pills === "string") {
- pills = pills.split(/,/g).map((pill) => pill.trim());
+ if (!store.split_text_on_comma && store.split_text_to_form_pills) {
+ pills = store.split_text_to_form_pills(pills);
+ } else {
+ pills = pills.split(/,/g).map((pill) => pill.trim());
+ }
}
// this is an array to push all the errored values to, so it's drafts
@@ -358,6 +365,12 @@ export function create<ItemType extends {type: string}>(
// and append the pill, then clear the input.
const value = funcs.value(this).trim();
if (value.length > 0) {
+ // If there are multiple values separated by commas, we should use insertManyPills
+ // to handle them properly when pressing the Enter key.
+ if (!store.split_text_on_comma && value.includes(",")) {
+ funcs.insertManyPills(value);
+ return;
+ }
// append the pill and by proxy create the pill object.
const ret = funcs.appendPill(value);
@@ -403,7 +416,7 @@ export function create<ItemType extends {type: string}>(
// Typing of the comma is prevented if the last field doesn't validate,
// as well as when the new pill is created.
- if (e.key === ",") {
+ if (e.key === "," && store.split_text_on_comma) {
// if the pill is successful, it will create the pill and clear
// the input.
if (funcs.appendPill(store.$input.text().trim())) {