UNPKG

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