Skip to content

Commit fed8011

Browse files
lihebili-xin-yi
authored andcommitted
fix remirror toolbar position when panning on canvas (#346)
1 parent 8a970a4 commit fed8011

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

ui/src/components/Canvas.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,8 @@ function CanvasImpl() {
752752
store,
753753
(state) => state.helperLineVertical
754754
);
755+
const toggleMoved = useStore(store, (state) => state.toggleMoved);
756+
const toggleClicked = useStore(store, (state) => state.toggleClicked);
755757

756758
return (
757759
<Box
@@ -768,6 +770,12 @@ function CanvasImpl() {
768770
onNodesChange={onNodesChange}
769771
onEdgesChange={onEdgesChange}
770772
onConnect={onConnect}
773+
onMove={() => {
774+
toggleMoved();
775+
}}
776+
onPaneClick={() => {
777+
toggleClicked();
778+
}}
771779
onNodeDragStop={(event, node) => {
772780
removeDragHighlight();
773781
let mousePos = project({ x: event.clientX, y: event.clientY });

ui/src/components/nodes/Rich.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,23 @@ const DelayAutoFocusInput = ({
243243
return <input ref={inputRef} {...rest} />;
244244
};
245245

246+
function useUpdatePositionerOnMove() {
247+
// Update (all) the positioners whenever there's a move (pane) on reactflow,
248+
// so that the toolbar moves with the Rich pod and content.
249+
const { forceUpdatePositioners, emptySelection } = useCommands();
250+
const store = useContext(RepoContext);
251+
if (!store) throw new Error("Missing BearContext.Provider in the tree");
252+
const moved = useStore(store, (state) => state.moved);
253+
const clicked = useStore(store, (state) => state.clicked);
254+
useEffect(() => {
255+
forceUpdatePositioners();
256+
}, [moved]);
257+
useEffect(() => {
258+
emptySelection();
259+
}, [clicked]);
260+
return;
261+
}
262+
246263
const FloatingLinkToolbar = ({ children }) => {
247264
const {
248265
isEditing,
@@ -254,6 +271,7 @@ const FloatingLinkToolbar = ({ children }) => {
254271
setHref,
255272
cancelHref,
256273
} = useFloatingLinkState();
274+
useUpdatePositionerOnMove();
257275
const active = useActive();
258276
const activeLink = active.link();
259277
const { empty } = useCurrentSelection();
@@ -294,13 +312,16 @@ const FloatingLinkToolbar = ({ children }) => {
294312
return (
295313
<>
296314
{!isEditing && (
297-
<FloatingToolbar>
315+
// By default, MUI's Popper creates a Portal, which is a ROOT html
316+
// elements that prevents paning on reactflow canvas. Therefore, we
317+
// disable the portal behavior.
318+
<FloatingToolbar disablePortal>
298319
{linkEditButtons}
299320
{children}
300321
</FloatingToolbar>
301322
)}
302323
{!isEditing && empty && (
303-
<FloatingToolbar positioner={linkPositioner}>
324+
<FloatingToolbar positioner={linkPositioner} disablePortal>
304325
{linkEditButtons}
305326
{children}
306327
</FloatingToolbar>

ui/src/lib/store/canvasSlice.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ export interface CanvasSlice {
275275
setPaneFocus: () => void;
276276
setPaneBlur: () => void;
277277

278+
// onMove indicator
279+
moved: boolean;
280+
toggleMoved: () => void;
281+
// clicked-on-canvas indicator
282+
clicked: boolean;
283+
toggleClicked: () => void;
284+
278285
addNode: (
279286
type: "CODE" | "SCOPE" | "RICH",
280287
position: XYPosition,
@@ -941,6 +948,12 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
941948
},
942949
setPaneFocus: () => set({ isPaneFocused: true }),
943950
setPaneBlur: () => set({ isPaneFocused: false }),
951+
952+
moved: false,
953+
toggleMoved: () => set({ moved: !get().moved }),
954+
clicked: false,
955+
toggleClicked: () => set({ clicked: !get().clicked }),
956+
944957
/**
945958
* This node2children is maintained with the canvas reactflow states, not with
946959
* the pods. This mapping may be used by other components, e.g. the runtime.

0 commit comments

Comments
 (0)