mirror of
				https://github.com/zulip/zulip.git
				synced 2025-10-31 12:03:46 +00:00 
			
		
		
		
	We scan a tooltip for any required windows-to-mac hotkey conversions from the list of attributes supplied to the hotkey_hints helper. If we find any, we add/modify the hotkyes in the hotkey hints list to match the mac-style key combinations and then return back the modified list of hotkey hints to be displayed in the tooltip. We also rename the "adjust_mac_shortcuts" function, used for the keyboard shortcuts menu and help center documnets, to "adjust_mac_kbd_tags" to avoid any ambiguity with the adjust_mac_tooltip_keys funtion which is used for tooltip hotkeys.
		
			
				
	
	
		
			119 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			119 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| import Handlebars from "handlebars/runtime";
 | |
| 
 | |
| import * as common from "./common";
 | |
| import {default_html_elements, intl} from "./i18n";
 | |
| import * as util from "./util";
 | |
| 
 | |
| // Below, we register Zulip-specific extensions to the Handlebars API.
 | |
| //
 | |
| // IMPORTANT: When adding a new Handlebars helper, update the
 | |
| // knownHelpers array in the webpack config so that webpack knows your
 | |
| // helper is registered at runtime and don't try to require them when
 | |
| // bundling.
 | |
| 
 | |
| // We don't want to wait for DOM ready to register the Handlebars helpers
 | |
| // below. There's no need to, as they do not access the DOM.
 | |
| // Furthermore, waiting for DOM ready would introduce race conditions with
 | |
| // other DOM-ready callbacks that attempt to render templates.
 | |
| 
 | |
| Handlebars.registerHelper({
 | |
|     eq(a, b) {
 | |
|         return a === b;
 | |
|     },
 | |
|     and(...args) {
 | |
|         args.pop(); // Handlebars options
 | |
|         if (args.length === 0) {
 | |
|             return true;
 | |
|         }
 | |
|         const last = args.pop();
 | |
|         for (const arg of args) {
 | |
|             if (!arg || Handlebars.Utils.isEmpty(arg)) {
 | |
|                 return arg;
 | |
|             }
 | |
|         }
 | |
|         return last;
 | |
|     },
 | |
|     or(...args) {
 | |
|         args.pop(); // Handlebars options
 | |
|         if (args.length === 0) {
 | |
|             return false;
 | |
|         }
 | |
|         const last = args.pop();
 | |
|         for (const arg of args) {
 | |
|             if (arg && !Handlebars.Utils.isEmpty(arg)) {
 | |
|                 return arg;
 | |
|             }
 | |
|         }
 | |
|         return last;
 | |
|     },
 | |
|     not(a) {
 | |
|         return !a || Handlebars.Utils.isEmpty(a);
 | |
|     },
 | |
| });
 | |
| 
 | |
| Handlebars.registerHelper("t", (message) => {
 | |
|     // Marks a string for translation.
 | |
|     // Example usage:
 | |
|     //     {{t "some English text"}}
 | |
| 
 | |
|     const descriptor = {id: message, defaultMessage: message};
 | |
|     return intl.formatMessage(descriptor);
 | |
| });
 | |
| 
 | |
| Handlebars.registerHelper("tr", function (options) {
 | |
|     // Marks a block for translation.
 | |
|     // Example usage 1:
 | |
|     //     {{#tr}}
 | |
|     //         <p>some English text</p>
 | |
|     //     {{/tr}}
 | |
|     //
 | |
|     // Example usage 2:
 | |
|     //     {{#tr}}
 | |
|     //         <p>This {variable} will get value from the current context</p>
 | |
|     //     {{/tr}}
 | |
|     //
 | |
|     // Note: use `{` and `}` instead of `{{` and `}}` to declare
 | |
|     // variables.
 | |
|     const message = options
 | |
|         .fn(this)
 | |
|         .trim()
 | |
|         .split("\n")
 | |
|         .map((s) => s.trim())
 | |
|         .join(" ");
 | |
|     const descriptor = {id: message, defaultMessage: message};
 | |
|     const result = intl.formatMessage(descriptor, {
 | |
|         ...default_html_elements,
 | |
|         ...Object.fromEntries(
 | |
|             Object.entries(options.fn.partials ?? {}).map(([name, value]) => [
 | |
|                 name,
 | |
|                 (s) => value(this, {data: {"partial-block": () => s.join("")}}),
 | |
|             ]),
 | |
|         ),
 | |
|         ...Object.fromEntries(
 | |
|             Object.entries(this ?? {}).map(([key, value]) => [
 | |
|                 key,
 | |
|                 Handlebars.Utils.escapeExpression(value),
 | |
|             ]),
 | |
|         ),
 | |
|     });
 | |
|     return new Handlebars.SafeString(result);
 | |
| });
 | |
| 
 | |
| Handlebars.registerHelper(
 | |
|     "rendered_markdown",
 | |
|     (content) => new Handlebars.SafeString(util.clean_user_content_links(content)),
 | |
| );
 | |
| 
 | |
| Handlebars.registerHelper("numberFormat", (number) => number.toLocaleString());
 | |
| 
 | |
| Handlebars.registerHelper("hotkey_hints", (...hotkeys) => {
 | |
|     hotkeys.pop(); // Handlebars options
 | |
|     let hotkey_hints = "";
 | |
|     common.adjust_mac_tooltip_keys(hotkeys);
 | |
|     for (const hotkey of hotkeys) {
 | |
|         hotkey_hints += `<span class="hotkey-hint">${hotkey}</span>`;
 | |
|     }
 | |
|     const result = `<span class="hotkey-hints">${hotkey_hints}</span>`;
 | |
|     return new Handlebars.SafeString(result);
 | |
| });
 |