@lottiefiles/relottie-parse
Version:
Parse Lottie JSON format to LAST
97 lines (95 loc) • 3.01 kB
JavaScript
import { traverseJsonExit, traverseJsonEnter } from './chunk-UJT6FNLI.js';
import { Stack } from './chunk-WVIQP6RP.js';
import { __publicField } from './chunk-V6TY7KAL.js';
import { traverse } from '@humanwhocodes/momoa';
import { TITLES } from '@lottiefiles/last';
import { rt } from '@lottiefiles/last-builder';
var { string: ST } = TITLES;
var Slots = class {
constructor(vfile, options) {
/**
* Parser's VFile instance
*/
__publicField(this, "file");
/**
* SlotID.value to ParentTitle mapping
*/
__publicField(this, "idTitles", /* @__PURE__ */ new Map());
/**
* SlotProperty node in the JSON AST
*/
__publicField(this, "jsonNode");
/**
* Parser's options
*/
__publicField(this, "options");
/**
* Slots Parent Node
*/
__publicField(this, "parentNode");
this.file = vfile;
this.options = options;
}
/**
* If set, replaces the original Slots JSON node with a LAST node using collected SlotId parent titles.
*
* @returns void
*/
mutate() {
if (!this.jsonNode || !this.parentNode) {
return;
}
const info = {
slotIdTitles: this.idTitles
};
const stack = new Stack();
const parentPosition = this.parentNode.position ? { position: this.parentNode.position } : {};
stack.push(rt([], { ...parentPosition }));
traverse(this.jsonNode, {
enter: (currNode, parentNode) => {
traverseJsonEnter(currNode, parentNode, stack, this.file, this.options, info);
},
exit: (currNode, parentNode) => {
traverseJsonExit(currNode, parentNode, stack, this.file, this.options, info);
}
});
const tempParent = stack.pop();
if (!tempParent || tempParent.type !== "root") return;
const newSlots = tempParent.children.find((child) => child.title === "slots");
if (!newSlots) return;
const childIndex = this.parentNode.children.findIndex((child) => child.title === "slots");
this.parentNode.children[childIndex] = newSlots;
}
/**
* Collects SlotID id and its Parent's Ancestor title for future mutations
*
* @param node - SlotID Node
* @param ancestor - SlotID Parent's Ancestor Node
*/
setIdTitle(node, ancestor) {
if (node.title !== ST.slotId) return;
const child = node.children[0];
const childValue = child?.value;
if (typeof childValue !== "string") return;
const id = childValue;
const ancestorTitle = ancestor.title;
const title = this.idTitles.get(id);
if (title) return;
this.idTitles.set(id, ancestorTitle);
}
/**
* Set Slots Node info for future mutations
*
* @param parentNode - SlotProperty parent node
* @param jsonNode - SlotProperty node in the JSON AST
* @returns void
*/
setNodes(parentNode, jsonNode) {
if (this.jsonNode) return;
this.jsonNode = jsonNode;
this.parentNode = parentNode;
}
};
export { Slots };
//# sourceMappingURL=chunk-KA742OHG.js.map
//# sourceMappingURL=chunk-KA742OHG.js.map