zp-figma-converter
Version:
Convert Figma designs to various code formats
120 lines • 4.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseParser = void 0;
const querystring_1 = require("querystring");
const xml_utils_1 = require("../../../utils/xml-utils");
/**
* Base class for parsers
*/
class BaseParser {
/**
* Convert common attributes
* @param nodeData JSON node data
* @returns Base node data for CSD
*/
parseBaseAttributes(nodeData) {
const result = {
'@_Name': (0, querystring_1.escape)(nodeData.Name),
'@_ctype': nodeData.ctype
};
// Common attributes
if (nodeData.ActionTag !== undefined)
result['@_ActionTag'] = (0, xml_utils_1.toString)(nodeData.ActionTag);
if (nodeData.Tag !== undefined)
result['@_Tag'] = (0, xml_utils_1.toString)(nodeData.Tag);
if (nodeData.IconVisible !== undefined)
result['@_IconVisible'] = (0, xml_utils_1.boolToString)(nodeData.IconVisible);
// Rotation
if (nodeData.RotationSkewX !== undefined)
result['@_RotationSkewX'] = (0, xml_utils_1.toString)(nodeData.RotationSkewX);
if (nodeData.RotationSkewY !== undefined)
result['@_RotationSkewY'] = (0, xml_utils_1.toString)(nodeData.RotationSkewY);
// Alpha
if (nodeData.Alpha !== undefined)
result['@_Alpha'] = (0, xml_utils_1.toString)(nodeData.Alpha);
// Flip
if (nodeData.FlipX !== undefined)
result['@_FlipX'] = (0, xml_utils_1.boolToString)(nodeData.FlipX);
if (nodeData.FlipY !== undefined)
result['@_FlipY'] = (0, xml_utils_1.boolToString)(nodeData.FlipY);
// Margin
if (nodeData.LeftMargin !== undefined)
result['@_LeftMargin'] = (0, xml_utils_1.toString)(nodeData.LeftMargin);
if (nodeData.RightMargin !== undefined)
result['@_RightMargin'] = (0, xml_utils_1.toString)(nodeData.RightMargin);
if (nodeData.TopMargin !== undefined)
result['@_TopMargin'] = (0, xml_utils_1.toString)(nodeData.TopMargin);
if (nodeData.BottomMargin !== undefined)
result['@_BottomMargin'] = (0, xml_utils_1.toString)(nodeData.BottomMargin);
// Edge
if (nodeData.HorizontalEdge !== undefined)
result['@_HorizontalEdge'] = nodeData.HorizontalEdge;
if (nodeData.VerticalEdge !== undefined)
result['@_VerticalEdge'] = nodeData.VerticalEdge;
// Size
if (nodeData.Size) {
result.Size = {
'@_X': (0, xml_utils_1.toString)(nodeData.Size.X),
'@_Y': (0, xml_utils_1.toString)(nodeData.Size.Y)
};
}
// AnchorPoint
if (nodeData.AnchorPoint) {
result.AnchorPoint = {};
if (nodeData.AnchorPoint.ScaleX !== undefined)
result.AnchorPoint['@_ScaleX'] = (0, xml_utils_1.toString)(nodeData.AnchorPoint.ScaleX);
if (nodeData.AnchorPoint.ScaleY !== undefined)
result.AnchorPoint['@_ScaleY'] = (0, xml_utils_1.toString)(nodeData.AnchorPoint.ScaleY);
}
// Position
if (nodeData.Position) {
result.Position = {
'@_X': (0, xml_utils_1.toString)(nodeData.Position.X),
'@_Y': (0, xml_utils_1.toString)(nodeData.Position.Y)
};
}
// Scale
if (nodeData.Scale) {
result.Scale = {
'@_ScaleX': (0, xml_utils_1.toString)(nodeData.Scale.ScaleX),
'@_ScaleY': (0, xml_utils_1.toString)(nodeData.Scale.ScaleY)
};
}
// CColor
if (nodeData.CColor) {
const color = {
'@_A': '255'
};
if (nodeData.CColor.R !== undefined)
color['@_R'] = (0, xml_utils_1.toString)(nodeData.CColor.R);
else
color['@_R'] = '255';
if (nodeData.CColor.G !== undefined)
color['@_G'] = (0, xml_utils_1.toString)(nodeData.CColor.G);
else
color['@_G'] = '255';
if (nodeData.CColor.B !== undefined)
color['@_B'] = (0, xml_utils_1.toString)(nodeData.CColor.B);
else
color['@_B'] = '255';
result.CColor = color;
}
// PrePosition
if (nodeData.PrePosition) {
result.PrePosition = {
'@_X': (0, xml_utils_1.toString)(nodeData.PrePosition.X),
'@_Y': (0, xml_utils_1.toString)(nodeData.PrePosition.Y)
};
}
// PreSize
if (nodeData.PreSize) {
result.PreSize = {
'@_X': (0, xml_utils_1.toString)(nodeData.PreSize.X),
'@_Y': (0, xml_utils_1.toString)(nodeData.PreSize.Y)
};
}
return result;
}
}
exports.BaseParser = BaseParser;
//# sourceMappingURL=base-parser.js.map