From f5bedd9501ead52b6af5f99d65d35df5cb993d92 Mon Sep 17 00:00:00 2001 From: Sayam Samal Date: Thu, 21 Mar 2024 03:47:45 +0530 Subject: [PATCH] hotkeys: Show Level 2 Shift key symbol for Shift key in popovers. --- web/src/common.ts | 13 +++++++++++++ web/src/templates.js | 11 +++++++++-- web/src/tippyjs.ts | 15 +++++++++++++++ web/tests/templates.test.js | 16 ++++++++++++++-- 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/web/src/common.ts b/web/src/common.ts index ebb6f18e9c..6f6f3f808c 100644 --- a/web/src/common.ts +++ b/web/src/common.ts @@ -113,6 +113,19 @@ export function adjust_mac_hotkey_hints(hotkeys: string[]): void { } } +// We convert the Shift key with ⇧ (Level 2 Select Symbol) in the +// popover menu hotkey hints. This helps us reduce the width of +// the popover menus. +export function adjust_shift_hotkey(hotkeys: string[]): boolean { + for (const [index, hotkey] of hotkeys.entries()) { + if (hotkey === "Shift") { + hotkeys[index] = "⇧"; + return true; + } + } + return false; +} + // See https://zulip.readthedocs.io/en/latest/development/authentication.html#password-form-implementation // for design details on this feature. function set_password_toggle_label( diff --git a/web/src/templates.js b/web/src/templates.js index b78563943e..b672ab7bb1 100644 --- a/web/src/templates.js +++ b/web/src/templates.js @@ -132,9 +132,16 @@ Handlebars.registerHelper("popover_hotkey_hints", (...hotkeys) => { hotkeys.pop(); // Handlebars options let hotkey_hints = ""; common.adjust_mac_hotkey_hints(hotkeys); + const shift_hotkey_exists = common.adjust_shift_hotkey(hotkeys); for (const hotkey of hotkeys) { hotkey_hints += `${hotkey}`; } - const result = `${hotkey_hints}`; - return new Handlebars.SafeString(result); + if (shift_hotkey_exists) { + return new Handlebars.SafeString( + `${hotkey_hints}`, + ); + } + return new Handlebars.SafeString( + `${hotkey_hints}`, + ); }); diff --git a/web/src/tippyjs.ts b/web/src/tippyjs.ts index 9b244f4516..e6f76ff959 100644 --- a/web/src/tippyjs.ts +++ b/web/src/tippyjs.ts @@ -621,4 +621,19 @@ export function initialize(): void { instance.destroy(); }, }); + + delegate("body", { + target: ".popover-contains-shift-hotkey", + trigger: "mouseenter", + placement: "top", + appendTo: () => document.body, + onShow(instance) { + const hotkey_hints = $(instance.reference).attr("data-hotkey-hints"); + if (hotkey_hints) { + instance.setContent(hotkey_hints.replace("⇧", "Shift").replaceAll(",", " + ")); + return undefined; + } + return false; + }, + }); } diff --git a/web/tests/templates.test.js b/web/tests/templates.test.js index 6cd87a31a6..36ee1c7c79 100644 --- a/web/tests/templates.test.js +++ b/web/tests/templates.test.js @@ -56,11 +56,23 @@ run_test("tooltip_hotkey_hints", () => { run_test("popover_hotkey_hints", () => { const args = { - hotkey_one: "Shift", - hotkey_two: "V", + hotkey_one: "Ctrl", + hotkey_two: "[", }; const html = require("./templates/popover_hotkey_hints.hbs")(args); const expected_html = `${args.hotkey_one}${args.hotkey_two}\n`; assert.equal(html, expected_html); }); + +run_test("popover_hotkey_hints_shift_hotkey", () => { + const args = { + hotkey_one: "Shift", + hotkey_two: "V", + }; + + const html = require("./templates/popover_hotkey_hints.hbs")(args); + args.hotkey_one = "⇧"; // adjust_shift_hotkey + const expected_html = `${args.hotkey_one}${args.hotkey_two}\n`; + assert.equal(html, expected_html); +});