UNPKG

4.39 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.LabelParser = exports.LabelIcon = void 0;
4const tslib_1 = require("tslib");
5// *****************************************************************************
6// Copyright (C) 2017 TypeFox and others.
7//
8// This program and the accompanying materials are made available under the
9// terms of the Eclipse Public License v. 2.0 which is available at
10// http://www.eclipse.org/legal/epl-2.0.
11//
12// This Source Code may also be made available under the following Secondary
13// Licenses when the conditions for such availability set forth in the Eclipse
14// Public License v. 2.0 are satisfied: GNU General Public License, version 2
15// with the GNU Classpath Exception which is available at
16// https://www.gnu.org/software/classpath/license.html.
17//
18// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
19// *****************************************************************************
20const inversify_1 = require("inversify");
21const common_1 = require("../common");
22var LabelIcon;
23(function (LabelIcon) {
24 function is(val) {
25 return (0, common_1.isObject)(val) && (0, common_1.isString)(val.name);
26 }
27 LabelIcon.is = is;
28})(LabelIcon = exports.LabelIcon || (exports.LabelIcon = {}));
29let LabelParser = class LabelParser {
30 /**
31 * Returns an array with parts of the given text.
32 * These parts are of type LabelPart which can be either a string or a LabelIcon.
33 * For splitting up the giving text the parser follows this rule:
34 * The text gets parsed for the following pattern: $(iconName~iconAnimation).
35 * If the parser finds such pattern a new LabelIcon object
36 * { name: 'iconName', animation: 'iconAnimation'} is added to the returned array.
37 * iconName can be for instance the name of an icon of e.g. FontAwesome and the (optional) iconAnimation
38 * the name of an animation class which must be supported by the particular icon toolkit.
39 *
40 * Every string before, between or after such icon patterns gets also added to the array
41 * before, between or after the related LabelIcon.
42 *
43 * @param text - the label text to parse
44 */
45 parse(text) {
46 const parserArray = [];
47 let arrPointer = 0;
48 let potentialIcon = '';
49 for (let idx = 0; idx < text.length; idx++) {
50 const char = text.charAt(idx);
51 parserArray[arrPointer] = parserArray[arrPointer] || '';
52 if (potentialIcon === '') {
53 if (char === '$') {
54 potentialIcon += char;
55 }
56 else {
57 parserArray[arrPointer] += char;
58 }
59 }
60 else if (potentialIcon === '$') {
61 if (char === '(') {
62 potentialIcon += char;
63 }
64 else {
65 parserArray[arrPointer] += potentialIcon + char;
66 potentialIcon = '';
67 }
68 }
69 else {
70 if (char === ')') {
71 const iconClassArr = potentialIcon.substring(2, potentialIcon.length).split('~');
72 if (parserArray[arrPointer] !== '') {
73 arrPointer++;
74 }
75 parserArray[arrPointer] = { name: iconClassArr[0], animation: iconClassArr[1] };
76 arrPointer++;
77 potentialIcon = '';
78 }
79 else {
80 potentialIcon += char;
81 }
82 }
83 }
84 if (potentialIcon !== '') {
85 parserArray[arrPointer] += potentialIcon;
86 }
87 return parserArray;
88 }
89 /**
90 * Strips icon specifiers from the given `text`, leaving only a
91 * space-separated concatenation of the non-icon segments.
92 *
93 * @param text text to be stripped of icon specifiers
94 * @returns the `text` with icon specifiers stripped out
95 */
96 stripIcons(text) {
97 return this.parse(text)
98 .filter(item => !LabelIcon.is(item))
99 .map(s => s.trim())
100 .filter(s => s.length)
101 .join(' ');
102 }
103};
104LabelParser = (0, tslib_1.__decorate)([
105 (0, inversify_1.injectable)()
106], LabelParser);
107exports.LabelParser = LabelParser;
108//# sourceMappingURL=label-parser.js.map
\No newline at end of file