figma-transformer
Version:
```js import { processFile } from "figma-transformer";
177 lines (150 loc) • 4.85 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
function _objectWithoutPropertiesLoose(source, excluded) {
if (source == null) return {};
var target = {};
var sourceKeys = Object.keys(source);
var key, i;
for (i = 0; i < sourceKeys.length; i++) {
key = sourceKeys[i];
if (excluded.indexOf(key) >= 0) continue;
target[key] = source[key];
}
return target;
}
function uniqBy(arr, key) {
var set = new Set();
return arr.filter(function (el) {
return function (v) {
return !set.has(v) && set.add(v);
}(el[key]);
});
}
function groupBy(arr, key) {
return arr.reduce(function (rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
}
var groupNodes = function groupNodes(nodes) {
return parseShortcutKeys(groupBy(uniqBy(nodes, "id"), "type"));
};
var parseShortcutKeys = function parseShortcutKeys(obj) {
var mapKeys = {
DOCUMENT: "documents",
COMPONENT: "components",
CANVAS: "pages",
LINE: "lines",
INSTANCE: "instances",
FRAME: "frames",
GROUP: "groups",
VECTOR: "vectors",
BOOLEAN: "booleans",
STAR: "stars",
ELLIPSE: "ellipses",
REGULAR_POLYGON: "regularPolygon",
RECTANGLE: "rectangles",
TEXT: "texts",
SLICE: "slices",
STYLE: "styles"
};
return Object.fromEntries(Object.entries(obj).map(function (_ref) {
var k = _ref[0],
v = _ref[1];
return [mapKeys[k], v];
}));
};
function processFile(data, id) {
var name = data.name,
lastModified = data.lastModified,
thumbnailUrl = data.thumbnailUrl,
version = data.version,
document = data.document,
styles = data.styles,
components = data.components;
var _processNodes = processNodes(document, styles, components, id),
processedNodes = _processNodes[0],
processedShortcuts = _processNodes[1];
return {
fileId: id,
name: name,
lastModified: lastModified,
thumbnailUrl: thumbnailUrl,
version: version,
children: processedNodes[0].children,
shortcuts: groupNodes(processedShortcuts)
};
}
function processNodes(nodes, documentStyles, components, fileId) {
var parsedStyles = new Map(Object.entries(documentStyles));
var parsedComponents = new Map(Object.entries(components));
var traverseChildren = function traverseChildren(node, parentId) {
var id = node.id,
styles = node.styles,
children = node.children,
rest = _objectWithoutPropertiesLoose(node, ["id", "styles", "children"]);
var nodeStyles = []; // If node has styles definitions populate that with the actual styles
if (styles != null) {
nodeStyles = Object.entries(styles).map(function (_ref) {
var key = _ref[0],
styleId = _ref[1];
var documentStyle = parsedStyles.get(styleId);
return _extends({
id: styleId
}, documentStyle, {
styles: node[key + "s"],
textStyles: node.style,
type: "STYLE"
});
});
} // Reached a leaf so returning the simplified node
if (children == null || children.length === 0) {
return [[_extends({
id: id,
parentId: parentId,
fileId: fileId
}, rest)], nodeStyles];
} // If it gets here then it means it has children
// so we're going to recursively go through them
// and combine everything
var _children$reduce = children.reduce(function (acc, child) {
var accChildren = acc[0],
accShortcuts = acc[1];
var _traverseChildren = traverseChildren(child, id),
tChildren = _traverseChildren[0],
tShortcuts = _traverseChildren[1];
return [[].concat(accChildren, tChildren), [].concat(accShortcuts, tChildren, tShortcuts)];
}, [[], []]),
parsedChildren = _children$reduce[0],
shortcuts = _children$reduce[1];
var componentInfo = parsedComponents.get(id); // Finally we return the parsed node with the
// parsed children grouped by type
var parsedNode = _extends({
id: id,
parentId: parentId,
fileId: fileId
}, rest, componentInfo && componentInfo, {
children: parsedChildren,
shortcuts: groupNodes(shortcuts)
});
return [[parsedNode], shortcuts];
};
return traverseChildren(nodes, "0:0");
}
exports.processFile = processFile;
exports.processNodes = processNodes;
//# sourceMappingURL=figma-transformer.cjs.development.js.map