summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/utils/text.js
blob: b29c97b4aee7ff4985b807482f49e1e50895d393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */

// @flow

/**
 * Utils for keyboard command strings
 * @module utils/text
 */
import Services from "devtools-services";
const { appinfo } = Services;

const isMacOS = appinfo.OS === "Darwin";

/**
 * Formats key for use in tooltips
 * For macOS we use the following unicode
 *
 * cmd ⌘ = \u2318
 * shift ⇧ – \u21E7
 * option (alt) ⌥ \u2325
 *
 * For Win/Lin this replaces CommandOrControl or CmdOrCtrl with Ctrl
 *
 * @memberof utils/text
 * @static
 */
export function formatKeyShortcut(shortcut: string): string {
  if (isMacOS) {
    return shortcut
      .replace(/Shift\+/g, "\u21E7")
      .replace(/Command\+|Cmd\+/g, "\u2318")
      .replace(/CommandOrControl\+|CmdOrCtrl\+/g, "\u2318")
      .replace(/Alt\+/g, "\u2325");
  }
  return shortcut
    .replace(/CommandOrControl\+|CmdOrCtrl\+/g, `${L10N.getStr("ctrl")}+`)
    .replace(/Shift\+/g, "Shift+");
}

/**
 * Truncates the received text to the maxLength in the format:
 * Original: 'this is a very long text and ends here'
 * Truncated: 'this is a ver...and ends here'
 * @param {String} sourceText - Source text
 * @param {Number} maxLength - Max allowed length
 * @memberof utils/text
 * @static
 */
export function truncateMiddleText(
  sourceText: string,
  maxLength: number
): string {
  let truncatedText = sourceText;
  if (sourceText.length > maxLength) {
    truncatedText = `${sourceText.substring(
      0,
      Math.round(maxLength / 2) - 2
    )}${sourceText.substring(
      sourceText.length - Math.round(maxLength / 2 - 1)
    )}`;
  }
  return truncatedText;
}