@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
142 lines • 5.32 kB
JavaScript
import * as U from "@aurigma/design-atoms-model/Utils/Utils";
import { TextAlignment } from "@aurigma/design-atoms-model/Product/Items";
export class TreeConverter {
constructor() { }
convert(markup) {
const body = this._parse(markup);
const result = this._convert(body, null);
return this._serialize(result);
}
_convert(originaNode, convertedParent, customData = null) {
const convertedNode = this._convertNode(originaNode, convertedParent);
if (convertedNode != null && convertedParent != null)
convertedParent.appendChild(convertedNode);
for (let i = 0; i < originaNode.childNodes.length; ++i) {
const node = originaNode.childNodes[i];
if (this._isAccepted(node) == NodeFilter.FILTER_ACCEPT)
this._convert(node, convertedNode ? convertedNode : convertedParent);
}
return convertedNode;
}
getNodeName(node) {
return node.nodeName.toLowerCase();
}
static stringToBoolean(value) {
return value === "true" ? true : false;
}
static stringToNumber(value, units = "", defaultValue = 0) {
if (value == null)
return null;
const result = !U.isNullOrEmptyOrWhiteSpace(units) ? +value.replace(units, "") : +value;
return !isNaN(result) ? result : defaultValue;
}
static letterSpacingToTracking(letterSpacing) {
const value = +letterSpacing.replace("em;", "").replace("em", "") * 1000;
return !isNaN(value) ? value : 0;
}
static trackingToLetterSpacing(tracking) {
const value = !isNaN(tracking) ? tracking / 1000 : 0;
return `${value}em`;
}
static stringToTextAlignmentForHtml(value) {
switch (value.toLocaleLowerCase()) {
case "left":
return TextAlignment.Left;
case "center":
return TextAlignment.Center;
case "right":
return TextAlignment.Right;
case "justify":
case "justify-all":
case "justification":
case "justification-last-left":
case "justification-last-center":
case "justification-last-right":
return TextAlignment.Justify;
default:
return TextAlignment.Left;
}
}
static stringToTextAlignment(value) {
switch (value.toLocaleLowerCase()) {
case "left":
return TextAlignment.Left;
case "center":
return TextAlignment.Center;
case "right":
return TextAlignment.Right;
case "justify":
case "justify-all":
case "justification":
return TextAlignment.Justify;
case "justification-last-left":
return TextAlignment.LastLeft;
case "justification-last-center":
return TextAlignment.LastCenter;
case "justification-last-right":
return TextAlignment.LastRight;
default:
return TextAlignment.Left;
}
}
static textAlignmentToXmlString(value) {
switch (value) {
case TextAlignment.Left:
return "left";
case TextAlignment.Center:
return "center";
case TextAlignment.Right:
return "right";
case TextAlignment.Justify:
return "justification-last-left";
/*case TextAlignment.LastLeft:
return "justification-last-left";
case TextAlignment.LastCenter:
return "justification-last-center";
case TextAlignment.LastRight:
return "justification-last-right";*/
}
}
static textAlignmentToHtmlString(value) {
switch (value) {
case TextAlignment.Left:
return "left";
case TextAlignment.Center:
return "center";
case TextAlignment.Right:
return "right";
case TextAlignment.Justify:
return "justify";
}
}
static _styleToDict(xmlStyle) {
//"key: value;..." -> styles[key] = value
let result = {};
if (xmlStyle != null) {
const styles = xmlStyle.split(";");
styles.forEach((style) => {
if (style) {
var content = style.split(":");
if (content.length === 2) {
var name = content[0].trim();
var value = content[1].trim();
result[name] = value;
}
}
});
return result;
}
return null;
}
static _dictToStyle(styles) {
//styles[key] = value -> "key: value;..."
let styleStr = "";
for (const key in styles) {
const value = styles[key];
styleStr += `${key}: ${value}; `;
}
return styleStr;
}
}
TreeConverter._defaultSupSubSize = 0.583;
//# sourceMappingURL=TreeConverter.js.map