monaco-editor
Version:
A browser based code editor
38 lines (35 loc) • 1.58 kB
JavaScript
import { $ } from '../../dom.js';
import { ThemeIcon } from '../../../common/themables.js';
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
const labelWithIconsRegex = new RegExp(`(\\\\)?\\$\\((${ThemeIcon.iconNameExpression}(?:${ThemeIcon.iconModifierExpression})?)\\)`, 'g');
function renderLabelWithIcons(text, renderIconsInDefaultColor) {
const elements = new Array();
let match;
let textStart = 0, textStop = 0;
while ((match = labelWithIconsRegex.exec(text)) !== null) {
textStop = match.index || 0;
if (textStart < textStop) {
elements.push(text.substring(textStart, textStop));
}
textStart = (match.index || 0) + match[0].length;
const [, escaped, codicon] = match;
elements.push(escaped ? `$(${codicon})` : renderIcon({ id: codicon }, renderIconsInDefaultColor));
}
if (textStart < text.length) {
elements.push(text.substring(textStart));
}
return elements;
}
function renderIcon(icon, renderDefaultColor) {
const node = $(`span`);
const classes = ThemeIcon.asClassNameArray(icon);
if (renderDefaultColor) {
classes.push('codicon-colored');
}
node.classList.add(...classes);
return node;
}
export { renderIcon, renderLabelWithIcons };