Serialize layout state

This commit is contained in:
space-nuko
2023-05-03 23:19:15 -05:00
parent e9326e50b5
commit 35d4761fa9
2 changed files with 57 additions and 6 deletions

View File

@@ -32,8 +32,8 @@
&& !$layoutState.isMenuOpen && !$layoutState.isMenuOpen
$: if ($queueState && dragItem) { $: if ($queueState && widget) {
dragItem.isNodeExecuting = $queueState.runningNodeId === dragItem.nodeId; dragItem.isNodeExecuting = $queueState.runningNodeId === widget.node.id;
} }
</script> </script>

View File

@@ -2,7 +2,7 @@ import { get, writable } from 'svelte/store';
import type { Readable, Writable } from 'svelte/store'; import type { Readable, Writable } from 'svelte/store';
import type ComfyApp from "$lib/components/ComfyApp" import type ComfyApp from "$lib/components/ComfyApp"
import type { LGraphNode, IWidget, LGraph } from "@litegraph-ts/core" import type { LGraphNode, IWidget, LGraph } from "@litegraph-ts/core"
import { dndzone, SHADOW_PLACEHOLDER_ITEM_ID } from 'svelte-dnd-action'; import { dndzone, SHADOW_PLACEHOLDER_ITEM_ID } from 'svelte-dnd-action';
import type { ComfyWidgetNode } from '$lib/nodes'; import type { ComfyWidgetNode } from '$lib/nodes';
type DragItemEntry = { type DragItemEntry = {
@@ -100,6 +100,7 @@ type LayoutStateOps = {
getCurrentSelection: () => IDragItem[], getCurrentSelection: () => IDragItem[],
findLayoutForNode: (nodeId: number) => IDragItem | null; findLayoutForNode: (nodeId: number) => IDragItem | null;
clear: (state?: Partial<LayoutState>) => void, clear: (state?: Partial<LayoutState>) => void,
serialize(): SerializedLayoutState,
resetLayout: () => void, resetLayout: () => void,
} }
@@ -125,7 +126,7 @@ function findDefaultContainerForInsertion(): ContainerLayout | null {
if (state.root.type === "container") { if (state.root.type === "container") {
const container = state.root as ContainerLayout; const container = state.root as ContainerLayout;
const children: IDragItem[] = state.allItems[container.id]?.children || [] const children: IDragItem[] = state.allItems[container.id]?.children || []
const found = children.find((di) => di.type === "container" ) const found = children.find((di) => di.type === "container")
if (found && found.type === "container") if (found && found.type === "container")
return found as ContainerLayout; return found as ContainerLayout;
return container; return container;
@@ -251,7 +252,7 @@ function nodeRemoved(node: LGraphNode) {
function configureFinished(graph: LGraph) { function configureFinished(graph: LGraph) {
const id = 0; const id = 0;
clear({isConfiguring: false}) clear({ isConfiguring: false })
const root = addContainer(null, { direction: "horizontal", showTitle: false }); const root = addContainer(null, { direction: "horizontal", showTitle: false });
const left = addContainer(root, { direction: "vertical", showTitle: false }); const left = addContainer(root, { direction: "vertical", showTitle: false });
@@ -389,6 +390,54 @@ function resetLayout() {
// TODO // TODO
} }
export type SerializedLayoutState = {
root: DragItemID | null,
allItems: Record<DragItemID, SerializedDragEntry>,
currentId: number,
}
export type SerializedDragEntry = {
dragItem: SerializedDragItem,
children: DragItemID[],
parent: DragItemID | null
}
export type SerializedDragItem = {
type: string,
id: DragItemID,
nodeId: number | null,
attrs: Attributes
}
function serialize(): SerializedLayoutState {
const state = get(store)
const allItems: Record<DragItemID, SerializedDragEntry> = {}
for (const pair of Object.entries(state.allItems)) {
const [id, entry] = pair;
allItems[id] = {
dragItem: {
type: entry.dragItem.type,
id: entry.dragItem.id,
nodeId: (entry.dragItem as any).node?.id,
attrs: entry.dragItem.attrs
},
children: entry.children.map((di) => di.id),
parent: entry.parent?.id
}
}
return {
root: state.root?.id,
allItems,
currentId: state.currentId,
}
}
// function deserialize(data: any): LayoutState {
// }
const layoutStateStore: WritableLayoutStateStore = const layoutStateStore: WritableLayoutStateStore =
{ {
...store, ...store,
@@ -404,6 +453,8 @@ const layoutStateStore: WritableLayoutStateStore =
findLayoutForNode, findLayoutForNode,
ungroup, ungroup,
clear, clear,
resetLayout resetLayout,
serialize,
// deserialize
} }
export default layoutStateStore; export default layoutStateStore;