@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
135 lines (133 loc) • 5.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SvgCodeConverter = void 0;
class SvgCodeConverter {
static openContentTs = `/* eslint:disable */
/**
* @file Source code generated by gulp-svg-code.
* @version 1.0
*/
export module Svg {
'use strict'
`;
static closeContentTs = `}
`;
static openContentCss = `/**
* @file Source code generated by gulp-svg-code.
* @version 1.0
*/
`;
outputCss = [];
outputTs = [];
get contentCss() {
return this.outputCss.join('');
}
get contentTs() {
return this.outputTs.join('');
}
contentReset() {
this.outputCss = [];
this.outputTs = [];
}
generate(collection, pathPrefix) {
const root = this.createStructure(collection, pathPrefix);
this.outputCss.push(SvgCodeConverter.openContentCss);
this.outputTs.push(SvgCodeConverter.openContentTs);
this.addData(root, []);
this.outputTs.push(SvgCodeConverter.closeContentTs);
}
addData(current, segments) {
const ignores = ['<?xml', '<!-- Generator:', '<!DOCTYPE'];
const nested = [];
const keys = Object.keys(current);
for (const key of keys) {
const content = current[key];
segments.push(key);
if (typeof content === 'object') {
const segs = segments.slice(0);
nested.push({ content, segs });
}
else if (typeof content === 'string') {
const cssName = '.svg-' + segments.join('--');
this.outputCss.push(cssName + ' {\r\n');
this.outputCss.push(this.indent(1) + 'background-image: url("data:image/svg+xml;');
this.outputTs.push(this.indent(segments.length) + 'export const ' + key + ' = \'');
const lines = content.split('\r');
let svg = '';
lines.forEach((value, index, array) => {
value = value.replace('\n', '');
if (value && value.length > 1) {
let skip = false;
for (const item of ignores) {
if (value.indexOf(item) >= 0) {
skip = true;
break;
}
}
if (!skip) {
svg += value;
// this.outputCss.push(value);
this.outputTs.push(value);
}
}
});
svg = this.replaceAll(svg, '"', '\'');
svg = this.replaceAll(svg, '%', '%25');
svg = this.replaceAll(svg, '#', '%23');
svg = this.replaceAll(svg, '{', '%7B');
svg = this.replaceAll(svg, '}', '%7D');
svg = this.replaceAll(svg, '<', '%3C');
svg = this.replaceAll(svg, '>', '%3E');
this.outputCss.push('charset=utf8,' + svg);
this.outputCss.push('");\r\n');
this.outputCss.push('}\r\n');
this.outputCss.push('\r\n');
this.outputTs.push('\';\r\n');
}
segments.pop();
}
for (let index = 0; index < nested.length; index++) {
const { content, segs } = nested[index];
this.outputTs.push(this.indent(segs.length) + 'export module ' + segs[segs.length - 1] + ' {\r\n');
this.addData(nested[index].content, nested[index].segs);
this.outputTs.push(this.indent(segs.length) + '}\r\n');
}
}
createStructure(collection, pathPrefix) {
const root = {};
const keys = Object.keys(collection).sort((left, right) => left.toLowerCase().localeCompare(right.toLowerCase()));
for (const key of keys) {
let shortName = key.substring(0, key.length - '.svg'.length);
shortName = this.replaceAll(shortName.substring(pathPrefix.length + 1), '-', '_').toLowerCase();
const segments = shortName.split('\\');
let current = root;
for (let index = 0; index < segments.length - 1; index++) {
const segment = segments[index];
if (current.hasOwnProperty(segment)) {
current = current[segment];
}
else {
current[segment] = {};
current = current[segment];
}
}
current[segments[segments.length - 1]] = collection[key];
}
return root;
}
regexEscape(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}
replaceAll(input, searchValue, replaceValue) {
return input.replace(new RegExp(this.regexEscape(searchValue), 'g'), replaceValue);
}
indent(count) {
let pad = '';
for (let i = 0; i < count; i++) {
pad += ' ';
}
return pad;
}
}
exports.SvgCodeConverter = SvgCodeConverter;
//# sourceMappingURL=svg-code-convert.js.map