mirror of
https://github.com/wavetermdev/waveterm
synced 2026-05-24 09:18:27 +00:00
This PR is a large refactoring of the layout code to move as much of the layout state logic as possible into a unified model class, with atoms and derived atoms to notify the display logic of changes. It also fixes some latent bugs in the node resize code, significantly speeds up response times for resizing and dragging, and sets us up to fully replace the React-DnD library in the future.
52 lines
2.2 KiB
TypeScript
52 lines
2.2 KiB
TypeScript
import { WOS } from "@/app/store/global";
|
|
import { Atom, atom, Getter } from "jotai";
|
|
import { LayoutTreeState, WritableLayoutTreeStateAtom } from "./types";
|
|
|
|
const layoutStateAtomMap: WeakMap<Atom<Tab>, WritableLayoutTreeStateAtom> = new WeakMap();
|
|
// const layoutStateLoadingAtomMap: WeakMap<Atom<Tab>, Atom<boolean>> = new WeakMap();
|
|
// const layoutStateAtomMap
|
|
|
|
function getLayoutStateAtomFromTab(tabAtom: Atom<Tab>, get: Getter): WritableWaveObjectAtom<LayoutState> {
|
|
const tabData = get(tabAtom);
|
|
if (!tabData) return;
|
|
const layoutStateOref = WOS.makeORef("layout", tabData.layoutstate);
|
|
const layoutStateAtom = WOS.getWaveObjectAtom<LayoutState>(layoutStateOref);
|
|
return layoutStateAtom;
|
|
}
|
|
|
|
export function withLayoutTreeStateAtomFromTab(tabAtom: Atom<Tab>): WritableLayoutTreeStateAtom {
|
|
if (layoutStateAtomMap.has(tabAtom)) {
|
|
console.log("found atom");
|
|
return layoutStateAtomMap.get(tabAtom);
|
|
}
|
|
const generationAtom = atom(1);
|
|
const treeStateAtom: WritableLayoutTreeStateAtom = atom(
|
|
(get) => {
|
|
const stateAtom = getLayoutStateAtomFromTab(tabAtom, get);
|
|
if (!stateAtom) return;
|
|
const layoutStateData = get(stateAtom);
|
|
console.log("layoutStateData", layoutStateData);
|
|
const layoutTreeState: LayoutTreeState = {
|
|
rootNode: layoutStateData?.rootnode,
|
|
magnifiedNodeId: layoutStateData?.magnifiednodeid,
|
|
generation: get(generationAtom),
|
|
};
|
|
return layoutTreeState;
|
|
},
|
|
(get, set, value) => {
|
|
if (get(generationAtom) < value.generation) {
|
|
const stateAtom = getLayoutStateAtomFromTab(tabAtom, get);
|
|
console.log("setting new atom val", value);
|
|
if (!stateAtom) return;
|
|
const waveObjVal = get(stateAtom);
|
|
console.log("waveObjVal", waveObjVal);
|
|
waveObjVal.rootnode = value.rootNode;
|
|
waveObjVal.magnifiednodeid = value.magnifiedNodeId;
|
|
set(generationAtom, value.generation);
|
|
set(stateAtom, waveObjVal);
|
|
}
|
|
}
|
|
);
|
|
layoutStateAtomMap.set(tabAtom, treeStateAtom);
|
|
return treeStateAtom;
|
|
}
|