* Prepares a shallow copy of this object for immediate serialisation or structuredCloning. * The return value should be discarded immediately. * @param options Serialise options = currently `sortNodes: boolean`, whether to sort nodes by ID. * @returns A shallow copy of parts of this graph,
(options?: { sortNodes: boolean })
| 1640 | * It is intended for use with {@link structuredClone} or {@link JSON.stringify}. |
| 1641 | */ |
| 1642 | asSerialisable(options?: { sortNodes: boolean }): SerialisableGraph & Required<Pick<SerialisableGraph, "nodes" | "groups" | "extra">> { |
| 1643 | const { id, revision, config, state } = this |
| 1644 | |
| 1645 | const nodeList = !LiteGraph.use_uuids && options?.sortNodes |
| 1646 | // @ts-expect-error If LiteGraph.use_uuids is false, ids are numbers. |
| 1647 | ? [...this._nodes].sort((a, b) => a.id - b.id) |
| 1648 | : this._nodes |
| 1649 | |
| 1650 | const nodes = nodeList.map(node => node.serialize()) |
| 1651 | const groups = this._groups.map(x => x.serialize()) |
| 1652 | |
| 1653 | const links = this._links.size ? [...this._links.values()].map(x => x.asSerialisable()) : undefined |
| 1654 | const floatingLinks = this.floatingLinks.size ? [...this.floatingLinks.values()].map(x => x.asSerialisable()) : undefined |
| 1655 | const reroutes = this.reroutes.size ? [...this.reroutes.values()].map(x => x.asSerialisable()) : undefined |
| 1656 | |
| 1657 | // Save scale and offset |
| 1658 | const extra = { ...this.extra } |
| 1659 | if (LiteGraph.saveViewportWithGraph) extra.ds = this.#getDragAndScale() |
| 1660 | if (!extra.ds) delete extra.ds |
| 1661 | |
| 1662 | const data: ReturnType<typeof this.asSerialisable> = { |
| 1663 | id, |
| 1664 | revision, |
| 1665 | version: LGraph.serialisedSchemaVersion, |
| 1666 | config, |
| 1667 | state, |
| 1668 | groups, |
| 1669 | nodes, |
| 1670 | links, |
| 1671 | floatingLinks, |
| 1672 | reroutes, |
| 1673 | extra, |
| 1674 | } |
| 1675 | |
| 1676 | if (this.isRootGraph && this._subgraphs.size) { |
| 1677 | const usedSubgraphIds = findUsedSubgraphIds(this, this._subgraphs) |
| 1678 | const usedSubgraphs = [...this._subgraphs.values()] |
| 1679 | .filter(subgraph => usedSubgraphIds.has(subgraph.id)) |
| 1680 | .map(x => x.asSerialisable()) |
| 1681 | |
| 1682 | if (usedSubgraphs.length > 0) { |
| 1683 | data.definitions = { subgraphs: usedSubgraphs } |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | this.onSerialize?.(data) |
| 1688 | return data |
| 1689 | } |
| 1690 | |
| 1691 | protected _configureBase(data: ISerialisedGraph | SerialisableGraph): void { |
| 1692 | const { id, extra } = data |
no test coverage detected