{
  "version": 3,
  "sources": ["../../src/document/partialDocument.ts"],
  "sourcesContent": ["import type { DocumentParserInterface } from \"@framerjs/document-snapshotter\"\nimport type { AnyComponentLoader, ComponentLoader } from \"@framerjs/framer-runtime\"\nimport { assert, getLogger } from \"@framerjs/shared\"\nimport { markLoadingPerf } from \"utils/performanceTracker.ts\"\nimport { repairCycleInNode, repairPage } from \"./models/CanvasTree/TreeRepair.ts\"\nimport type { TreeServices } from \"./models/CanvasTree/TreeServices.ts\"\nimport type { CanvasNode, NodeID } from \"./models/CanvasTree/index.ts\"\nimport { CanvasTree } from \"./models/CanvasTree/index.ts\"\nimport { canvasNodeFromValue } from \"./models/CanvasTree/nodes/canvasNodeFromValue.ts\"\nimport type { DetectedCycles } from \"./models/CanvasTree/traits/utils/checkTreeForCycles.ts\"\nimport { checkTreeForCycles } from \"./models/CanvasTree/traits/utils/checkTreeForCycles.ts\"\n\nconst verifyLog = getLogger(\"remote:verify\")\n\n/**\n * Loads a validated document with a root node containing the active page node and empty page siblings.\n * @param parser\n * @param pageIds\n * @param treeServices\n * @returns CanvasTree\n */\nexport function loadPartialDocument(\n\tparser: DocumentParserInterface,\n\tcomponentLoader: AnyComponentLoader,\n\tpageIds: NodeID[],\n\ttreeServices?: TreeServices,\n): CanvasTree {\n\tconst root = parser.getPartialDocumentForPageIds(pageIds)\n\tconst errors: string[] = []\n\tconst warnings: string[] | undefined = verifyLog.isLoggingTraceMessages() ? [] : undefined\n\tconst rootNode = canvasNodeFromValue(root, null, { extraChecksAndFixes: true, errors, warnings })\n\tmarkLoadingPerf(\"parsingRootNode\")\n\tassert(rootNode, \"error loading root node:\", root.id, errors)\n\n\tfor (const page of rootNode.children ?? []) {\n\t\tif (!page.children) continue\n\t\tif (page.children.length > 0) {\n\t\t\trepairPage(page, errors)\n\t\t}\n\t}\n\tif (errors.length > 0) {\n\t\tverifyLog.warn(\"errors loading server tree: \" + errors.join(\"\\n\"))\n\t}\n\tif (warnings && warnings.length > 0) {\n\t\tverifyLog.trace(\"warnings loading server tree: \" + warnings.join(\"\\n\"))\n\t}\n\n\tlet canvasTree = CanvasTree.createByAdoptingRoot(rootNode, treeServices)\n\tcanvasTree.loadReplicasAndCodeComponents(canvasTree.root)\n\tmarkLoadingPerf(\"parsingReplicasExpansion\")\n\tif (canvasTree.hasUncommittedChanges()) {\n\t\tcanvasTree = canvasTree.commit(componentLoader)\n\t}\n\n\treturn canvasTree\n}\n\n/**\n * It collects the passed children recursively until its done.\n * The children are later used for the complete tree assembly.\n */\nexport function loadOneRootChild(parser: DocumentParserInterface, loadedPages: Map<string, CanvasNode>): boolean {\n\tconst nextPage = parser.parseNextPage()\n\tif (!nextPage) return true\n\n\tconst errors: string[] = []\n\tconst pageNode = canvasNodeFromValue(nextPage, parser.root.id, { extraChecksAndFixes: true, errors })\n\tassert(pageNode, \"error loading page node:\", nextPage.id, errors)\n\trepairPage(pageNode, errors)\n\tif (errors.length > 0) {\n\t\tverifyLog.warn(\"warnings loading server tree: \" + errors.join(\"\\n\"))\n\t}\n\tloadedPages.set(pageNode.id, pageNode)\n\n\treturn false\n}\n\n/**\n * It assembles a new verified and committed tree based on the passed\n * CanvasNode array.\n */\nexport function assembleTree(tree: CanvasTree, componentLoader: ComponentLoader, loadedPages: Map<string, CanvasNode>) {\n\ttree.makeLatest()\n\n\t// Make sure we can write the tree.\n\ttree.editClosed = false\n\ttree.isViewOnly = false\n\n\ttree.root.children.forEach((child, index) => {\n\t\tconst loaded = loadedPages.get(child.id)\n\t\tif (!loaded) return\n\n\t\ttree.remove(loaded.id)\n\t\ttree.insertNode(loaded, tree.root.id, index)\n\t})\n\n\t// Just like while loading, mark the tree as not existing in the editor. Helps preserve\n\t// contentHashes and not recaculate graphics bounding boxes, otherwise any results won't be send\n\t// to the server. Also commit the tree via TemplateHelper.treeDidLoad() to ensure all replica's\n\t// are hooked up. Same as we do in other loading routines.\n\ttree.inEditor = false\n\ttree.loadReplicasAndCodeComponents(tree.root)\n\tif (tree.hasUncommittedChanges()) {\n\t\ttree = tree.commit(componentLoader)\n\t}\n\ttree.inEditor = true\n\treturn tree\n}\n\nexport class TreeFlags {\n\tprivate editClosed = false\n\tprivate isViewOnly = false\n\tprivate inEditor = false\n\tprivate processingLocalUserEdits = false\n\n\tstatic from(tree: CanvasTree): TreeFlags {\n\t\tconst flags = new TreeFlags()\n\t\tflags.editClosed = tree.editClosed\n\t\tflags.isViewOnly = tree.isViewOnly\n\t\tflags.inEditor = tree.inEditor\n\t\tflags.processingLocalUserEdits = tree.processingLocalUserEdits\n\t\treturn flags\n\t}\n\n\trestore(tree: CanvasTree): void {\n\t\ttree.editClosed = this.editClosed\n\t\ttree.isViewOnly = this.isViewOnly\n\t\ttree.inEditor = this.inEditor\n\t\ttree.processingLocalUserEdits = this.processingLocalUserEdits\n\t}\n\n\tstatic setForAssembly(tree: CanvasTree): void {\n\t\ttree.editClosed = false\n\t\ttree.isViewOnly = false\n\t\ttree.inEditor = false\n\t\ttree.processingLocalUserEdits = false\n\t}\n}\n\n/** A class that can assemble the tree step by step, so we can run that potential CPU heavy\n * operation in small chunks, not blocking the browser for multiple seconds. */\nexport class TreeAssembler {\n\tprivate pagesToAssemble: CanvasNode[]\n\n\tprivate isSetupNeeded = true\n\tprivate engineTreeFlags = new TreeFlags()\n\n\tconstructor(\n\t\tprivate engine: { componentLoader: AnyComponentLoader; tree: CanvasTree },\n\t\t/** Think of the treeToAssemble as a branch in git, forked off from the moment we loaded the\n\t\t * document, adding a page per commit, later we will rebase it on master, then force push\n\t\t * this branch as the master branch. Sort-of. */\n\t\tprivate treeToAssemble: CanvasTree,\n\t\tloadedPages: Map<string, CanvasNode>,\n\t) {\n\t\tthis.pagesToAssemble = Array.from(loadedPages.values())\n\t}\n\n\tprivate setupAssemblerTreeStateIfNeeded() {\n\t\tif (!this.isSetupNeeded) return\n\t\tthis.isSetupNeeded = false\n\n\t\tthis.engineTreeFlags = TreeFlags.from(this.engine.tree)\n\n\t\tif (!this.treeToAssemble.isLatest()) {\n\t\t\tthis.treeToAssemble.makeLatest()\n\t\t}\n\n\t\tTreeFlags.setForAssembly(this.treeToAssemble)\n\t}\n\n\t/** Restores the engine tree as latest and restores any edit flags. */\n\trestoreEngineTreeState() {\n\t\tif (this.isSetupNeeded) return\n\t\tthis.isSetupNeeded = true\n\n\t\tif (this.treeToAssemble.hasUncommittedChanges()) {\n\t\t\tthis.treeToAssemble = this.treeToAssemble.commit(this.engine.componentLoader)\n\t\t}\n\n\t\tif (!this.engine.tree.isLatest()) {\n\t\t\tthis.engine.tree.makeLatest()\n\t\t}\n\n\t\tthis.engineTreeFlags.restore(this.engine.tree)\n\t}\n\n\t/** Returns true when done or false if there are more pages to assemble. Might mess with the\n\t * main engine tree, call restoreEngineTreeState to restore.  */\n\tassembleOnePage(): boolean {\n\t\tif (this.pagesToAssemble.length === 0) return true\n\n\t\tthis.setupAssemblerTreeStateIfNeeded()\n\t\tconst page = this.pagesToAssemble.pop()\n\t\tassert(page)\n\n\t\tconst index = this.treeToAssemble.root.children.findIndex(n => n.id === page.id)\n\t\tif (index < 0) return false\n\n\t\t// Remove the shallow loaded placeholder, then insert the full page node, hook up master\n\t\t// replicas and code component links.\n\t\tthis.treeToAssemble.remove(page.id)\n\t\tthis.treeToAssemble.insertNode(page, this.treeToAssemble.root.id, index)\n\t\tthis.treeToAssemble.loadReplicasAndCodeComponents(page)\n\n\t\t// Then we commit the tree. We could postpone committing to restoreEngineTreeState() for a\n\t\t// bit more performance. But often commit is the most expensive task, and a 0.1 seconds of\n\t\t// assembleOnePage() might result in 1 or 2 seconds of commit(), still hogging the browser\n\t\t// despite time boxing the work.\n\t\tthis.treeToAssemble = this.treeToAssemble.commit(this.engine.componentLoader)\n\t\treturn this.pagesToAssemble.length === 0\n\t}\n\n\t/** Returns the assembled and committed tree. Might mess with the engine tree, call\n\t * restoreEngineTreeState to restore, or immediately load the returned tree. */\n\tbuildCompleteTree(): CanvasTree {\n\t\tassert(this.pagesToAssemble.length === 0, \"must be done with assembleOnePage\")\n\t\tthis.setupAssemblerTreeStateIfNeeded()\n\n\t\t// We are done. But there might still be cross page master replicas or code component\n\t\t// links, we need to hook them up as well.\n\t\tthis.treeToAssemble.loadReplicasAndCodeComponents(this.treeToAssemble.root)\n\t\tif (this.treeToAssemble.hasUncommittedChanges()) {\n\t\t\tthis.treeToAssemble = this.treeToAssemble.commit(this.engine.componentLoader)\n\t\t}\n\n\t\tthis.treeToAssemble.verify()\n\n\t\t// After all is loaded, we check for cycles.\n\t\tconst errors: string[] = []\n\t\tconst cycles: DetectedCycles = []\n\t\tif (checkTreeForCycles(this.treeToAssemble, cycles)) {\n\t\t\tcycles.forEach(cycle => {\n\t\t\t\terrors.push(`${cycle.id}: code component links itself via ${cycle.stack}`)\n\t\t\t\trepairCycleInNode(this.treeToAssemble, cycle.id, cycle.stack)\n\t\t\t})\n\t\t\tthis.treeToAssemble = this.treeToAssemble.commit(this.engine.componentLoader)\n\t\t}\n\t\tif (errors.length > 0) {\n\t\t\tverifyLog.warn(\"warnings loading server tree: \" + errors.join(\"\\n\"))\n\t\t}\n\n\t\t// Set the tree flags the same as the engine.\n\t\tthis.engineTreeFlags.restore(this.treeToAssemble)\n\n\t\treturn this.treeToAssemble\n\t}\n}\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;;AAYA,IAAM,YAAY,UAAU,eAAe;AASpC,SAAS,oBACf,QACA,iBACA,SACA,cACa;AACb,QAAM,OAAO,OAAO,6BAA6B,OAAO;AACxD,QAAM,SAAmB,CAAC;AAC1B,QAAM,WAAiC,UAAU,uBAAuB,IAAI,CAAC,IAAI;AACjF,QAAM,WAAW,oBAAoB,MAAM,MAAM,EAAE,qBAAqB,MAAM,QAAQ,SAAS,CAAC;AAChG,kBAAgB,iBAAiB;AACjC,SAAO,UAAU,4BAA4B,KAAK,IAAI,MAAM;AAE5D,aAAW,QAAQ,SAAS,YAAY,CAAC,GAAG;AAC3C,QAAI,CAAC,KAAK,SAAU;AACpB,QAAI,KAAK,SAAS,SAAS,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