Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions packages/ui/src/components/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,36 +324,11 @@ export function useCopyPaste() {
}, [handleCopy, handlePaste, rfDomNode]);
}

function useYjsObserver() {
const store = useContext(RepoContext);
if (!store) throw new Error("Missing BearContext.Provider in the tree");
const nodesMap = useStore(store, (state) => state.getNodesMap());
const updateView = useStore(store, (state) => state.updateView);
const resetSelection = useStore(store, (state) => state.resetSelection);

useEffect(() => {
const observer = (YMapEvent: Y.YEvent<any>, transaction: Y.Transaction) => {
if (transaction.local) return;
updateView();
};

// FIXME need to observe edgesMap as well
// FIXME need to observe resultMap as well
nodesMap.observe(observer);

return () => {
nodesMap.unobserve(observer);
resetSelection();
};
}, [nodesMap, resetSelection, updateView]);
}

/**
* The ReactFlow instance keeps re-rendering when nodes change. Thus, we're
* using this wrapper component to load the useXXX functions only once.
*/
function CanvasImplWrap() {
useYjsObserver();
useCopyPaste();
useJump();
return (
Expand Down
15 changes: 15 additions & 0 deletions packages/ui/src/lib/store/yjsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@ export const createYjsSlice: StateCreator<MyState, [], [], YjsSlice> = (
});
}
);
const nodesMap = get().getNodesMap();
// FIXME do I need to unobserve it when disconnecting?
nodesMap.observe(
(YMapEvent: Y.YEvent<any>, transaction: Y.Transaction) => {
if (transaction.local) return;
get().updateView();
}
);
const edgesMap = get().getEdgesMap();
edgesMap.observe(
(YMapEvent: Y.YEvent<any>, transaction: Y.Transaction) => {
if (transaction.local) return;
get().updateView();
}
);
// Set active runtime to the first one.
const runtimeMap = get().getRuntimeMap();
if (runtimeMap.size > 0) {
Expand Down