Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
linkifiers: Support dragging and reordering linkifiers.
Zixuan James Li5301060954d3
diff --git a/help/add-a-custom-linkifier.md b/help/add-a-custom-linkifier.mdindex 4e7332b1bea..ccc8a7e99f3 100644--- a/help/add-a-custom-linkifier.md+++ b/help/add-a-custom-linkifier.md@@ -27,6 +27,23 @@ support](/help/contact-support) with details on what you're trying to do.{end_tabs}+### Reorder linkifiers++Linkifiers are processed in order, and will not apply to text that+already is linkified. One can thus configure multiple linkifiers with+overlapping syntax, and only the first one whose regular expression+matches a given part of a message will take effect. See the+[overlapping patterns section](#overlapping-patterns) for examples.++{start_tabs}++{settings_tab|linkifier-settings}++1. Under **Linkifiers**, click and drag existing linkifiers into+ the desired order.++{end_tabs}+## Common linkifier patternsThe following examples cover the most common types of linkifiers, with a focus@@ -155,3 +172,40 @@ This example pattern allows linking to Google searches.This pattern uses the `{?var}` expression type. With the default expressiontype (`{q}`), there would be no way to only include the `?` in the URLif the optional `q` is present.++### Overlapping patterns++In this example, a general linkifier is configured to make GitHub+repository references like `zulip-desktop#123` link to issues in that+repository within the `zulip` GitHub organization. A more specific+linkifier overrides that linkifier for a specific repository of+interest (`django/django`) that is in a different organization.++{start_tabs}++* Specific linkifier (ordered before the general linkifier)+ * Pattern: `django#(?P<id>[0-9]+)`+ * URL template: `https://github.com/django/django/pull/{id}`++* General linkifier+ * Pattern: `(?P<repo>[a-zA-Z0-9_-]+)#(?P<id>[0-9]+)`+ * URL template: `https://github.com/zulip/{repo}/pull/{id}`++* Example matching both linkifiers; specific linkifier takes precedence:+ * Original text: `django#123`+ * Automatically links to: `https://github.com/django/django/pull/123`++* Example matching only the general linkifier:+ * Original text: `zulip-desktop#123`+ * Automatically links to: `https://github.com/zulip/zulip-desktop/pull/123`++{end_tabs}++!!! tip ""++ This set of patterns has overlapping regular expressions. Note that+ the general linkifier pattern would match `lorem#123` too. The specific+ linkifier will only get prioritized over the general linkifier if it is+ ordered before the more general pattern. This can be customized by+ dragging and dropping existing linkifiers into the desired order. New+ linkifiers will automatically be ordered last.diff --git a/web/e2e-tests/realm-linkifier.test.ts b/web/e2e-tests/realm-linkifier.test.tsindex cdb74550941..121fa1f6e33 100644--- a/web/e2e-tests/realm-linkifier.test.ts+++ b/web/e2e-tests/realm-linkifier.test.ts@@ -20,29 +20,30 @@ async function test_add_linkifier(page: Page): Promise<void> {);assert.strictEqual(admin_linkifier_status, "Custom linkifier added!");- await page.waitForSelector(".linkifier_row", {visible: true});+ await page.waitForSelector(".linkifier_row:nth-child(4)", {visible: true});assert.strictEqual(await common.get_text_from_selector(page,- ".linkifier_row:nth-child(1) span.linkifier_pattern",+ ".linkifier_row:nth-child(4) span.linkifier_pattern",),"#(?P<id>[0-9]+)",);assert.strictEqual(await common.get_text_from_selector(page,- ".linkifier_row:nth-child(1) span.linkifier_url_template",+ ".linkifier_row:nth-child(4) span.linkifier_url_template",),"https://trac.example.com/ticket/{id}",);}async function test_delete_linkifier(page: Page): Promise<void> {- await page.click(".linkifier_row:nth-child(1) .delete");+ await page.waitForFunction(() => document.querySelectorAll(".linkifier_row").length === 4);+ await page.click(".linkifier_row:nth-last-child(1) .delete");await common.wait_for_micromodal_to_open(page);await page.click("#confirm_delete_linkifiers_modal .dialog_submit_button");await common.wait_for_micromodal_to_close(page);- await page.waitForFunction(() => document.querySelectorAll(".linkifier_row").length === 0);+ await page.waitForFunction(() => document.querySelectorAll(".linkifier_row").length === 3);}async function test_add_invalid_linkifier_pattern(page: Page): Promise<void> {@@ -61,7 +62,7 @@ async function test_add_invalid_linkifier_pattern(page: Page): Promise<void> {}async function test_edit_linkifier(page: Page): Promise<void> {- await page.click(".linkifier_row:nth-child(1) .edit");+ await page.click(".linkifier_row:nth-last-child(1) .edit");await common.wait_for_micromodal_to_open(page);await common.fill_form(page, "form.linkifier-edit-form", {pattern: "(?P<num>[0-9a-f]{40})",@@ -72,23 +73,23 @@ async function test_edit_linkifier(page: Page): Promise<void> {await page.waitForSelector("#dialog_widget_modal", {hidden: true});await common.wait_for_micromodal_to_close(page);- await page.waitForSelector(".linkifier_row:nth-child(1)", {visible: true});+ await page.waitForSelector(".linkifier_row:nth-last-child(1)", {visible: true});await page.waitForFunction(() =>- document.querySelector(".linkifier_row:nth-child(1) span.linkifier_pattern")+ document.querySelector(".linkifier_row:nth-last-child(1) span.linkifier_pattern")?.textContent === "(?P<num>[0-9a-f]{40})",);assert.strictEqual(await common.get_text_from_selector(page,- ".linkifier_row:nth-child(1) span.linkifier_url_template",+ ".linkifier_row:nth-last-child(1) span.linkifier_url_template",),"https://trac.example.com/commit/{num}",);}async function test_edit_invalid_linkifier(page: Page): Promise<void> {- await page.click(".linkifier_row:nth-child(1) .edit");+ await page.click(".linkifier_row:nth-last-child(1) .edit");await common.wait_for_micromodal_to_open(page);await common.fill_form(page, "form.linkifier-edit-form", {pattern: "#(?P<id>d????)",@@ -118,18 +119,18 @@ async function test_edit_invalid_linkifier(page: Page): Promise<void> {await page.click(".dialog_exit_button");await page.waitForSelector("#dialog_widget_modal", {hidden: true});- await page.waitForSelector(".linkifier_row:nth-child(1)", {visible: true});+ await page.waitForSelector(".linkifier_row:nth-last-child(1)", {visible: true});assert.strictEqual(await common.get_text_from_selector(page,- ".linkifier_row:nth-child(1) span.linkifier_pattern",+ ".linkifier_row:nth-last-child(1) span.linkifier_pattern",),"(?P<num>[0-9a-f]{40})",);assert.strictEqual(await common.get_text_from_selector(page,- ".linkifier_row:nth-child(1) span.linkifier_url_template",+ ".linkifier_row:nth-last-child(1) span.linkifier_url_template",),"https://trac.example.com/commit/{num}",);diff --git a/web/src/settings_linkifiers.js b/web/src/settings_linkifiers.jsindex 242cad24061..8309453afe9 100644--- a/web/src/settings_linkifiers.js+++ b/web/src/settings_linkifiers.js@@ -1,4 +1,5 @@import $ from "jquery";+import {Sortable} from "sortablejs";import render_confirm_delete_linkifier from "../templates/confirm_dialog/confirm_delete_linkifier.hbs";import render_admin_linkifier_edit_form from "../templates/settings/admin_linkifier_edit_form.hbs";@@ -28,23 +29,6 @@ export function maybe_disable_widgets() {}}-function compare_values(x, y) {- if (x > y) {- return 1;- } else if (x === y) {- return 0;- }- return -1;-}--function sort_pattern(a, b) {- return compare_values(a.pattern, b.pattern);-}--function sort_url(a, b) {- return compare_values(a.url_template, b.url_template);-}-function open_linkifier_edit_form(linkifier_id) {const linkifiers_list = page_params.realm_linkifiers;const linkifier = linkifiers_list.find((linkifier) => linkifier.id === linkifier_id);@@ -108,6 +92,19 @@ function open_linkifier_edit_form(linkifier_id) {});}+function update_linkifiers_order() {+ const order = [];+ $(".linkifier_row").each(function () {+ order.push(Number.parseInt($(this).attr("data-linkifier-id"), 10));+ });+ settings_ui.do_settings_change(+ channel.patch,+ "/json/realm/linkifiers",+ {ordered_linkifier_ids: JSON.stringify(order)},+ $("#linkifier-field-status").expectOne(),+ );+}+function handle_linkifier_api_error(xhr, pattern_status, template_status, linkifier_status) {// The endpoint uses the Django ValidationError system for error// handling, which returns somewhat complicated error@@ -159,13 +156,16 @@ export function populate_linkifiers(linkifiers_data) {},},$parent_container: $("#linkifier-settings").expectOne(),- init_sort: sort_pattern,- sort_fields: {- pattern: sort_pattern,- url: sort_url,- },$simplebar_container: $("#linkifier-settings .progressive-table-wrapper"),});++ if (page_params.is_admin) {+ Sortable.create($linkifiers_table[0], {+ onUpdate: update_linkifiers_order,+ filter: "input",+ preventOnFilter: false,+ });+ }}export function set_up() {diff --git a/web/styles/settings.css b/web/styles/settings.cssindex 536b79bdd2b..d5201cf8f43 100644--- a/web/styles/settings.css+++ b/web/styles/settings.css@@ -726,6 +726,7 @@ input[type="checkbox"] {.admin_profile_fields_table,.edit_profile_field_choices_container,+.admin_linkifiers_table,.profile_field_choices_table {.movable-row {cursor: move;diff --git a/web/templates/settings/admin_linkifier_list.hbs b/web/templates/settings/admin_linkifier_list.hbsindex d8ba6d4efe0..e39a55506c4 100644--- a/web/templates/settings/admin_linkifier_list.hbs+++ b/web/templates/settings/admin_linkifier_list.hbs@@ -1,6 +1,10 @@{{#with linkifier}}-<tr class="linkifier_row">+<tr class="linkifier_row{{#if ../can_modify}} movable-row{{/if}}" data-linkifier-id="{{id}}"><td>+ {{#if ../can_modify}}+ <i class="fa fa-ellipsis-v" aria-hidden="true"></i>+ <i class="fa fa-ellipsis-v" aria-hidden="true"></i>+ {{/if}}<span class="linkifier_pattern">{{pattern}}</span></td><td>diff --git a/web/templates/settings/linkifier_settings_admin.hbs b/web/templates/settings/linkifier_settings_admin.hbsindex 860c40d68d1..73025089239 100644--- a/web/templates/settings/linkifier_settings_admin.hbs+++ b/web/templates/settings/linkifier_settings_admin.hbs@@ -65,8 +65,8 @@<div class="progressive-table-wrapper" data-simplebar><table class="table table-condensed table-striped wrapped-table admin_linkifiers_table"><thead class="table-sticky-headers">- <th class="active" data-sort="pattern">{{t "Pattern" }}</th>- <th data-sort="url">{{t "URL template" }}</th>+ <th>{{t "Pattern" }}</th>+ <th>{{t "URL template" }}</th>{{#if is_admin}}<th class="actions">{{t "Actions" }}</th>{{/if}}diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex ab2286b27d3..8121958f088 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -3205,15 +3205,12 @@ paths:realm_linkifiers:type: arraydescription: |- An ordered array of dictionaries where each- dictionary contains details about a single- realm linkifier.-- The order of the array reflects the order- that each linkifier should be processed when- linkifying messages and topics. By default,- new linkifiers are ordered last. This order- can be modified with [`PATCH+ An ordered array of dictionaries where each dictionary contains+ details about a single linkifier.++ Clients should always process linkifiers in the order given;+ this is important if the realm has linkifiers with overlapping+ patterns. The order can be modified using [`PATCH/realm/linkifiers`](/api/reorder-linkifiers).items:type: object@@ -10617,10 +10614,9 @@ paths:An ordered array of objects, where each objectdescribes a linkifier.- The order of the array reflects the order that each- linkifier should be processed when linkifying messages- and topics. By default, new linkifiers are ordered- last. This order can be modified with [`PATCH+ Clients should always process linkifiers in the order given;+ this is important if the realm has linkifiers with overlapping+ patterns. The order can be modified using [`PATCH/realm/linkifiers`](/api/reorder-linkifiers).items:type: object