Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix remirror toolbar position when panning on canvas #346

Merged
merged 1 commit into from
Jun 28, 2023
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
8 changes: 8 additions & 0 deletions ui/src/components/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,8 @@ function CanvasImpl() {
store,
(state) => state.helperLineVertical
);
const toggleMoved = useStore(store, (state) => state.toggleMoved);
const toggleClicked = useStore(store, (state) => state.toggleClicked);

return (
<Box
Expand All @@ -768,6 +770,12 @@ function CanvasImpl() {
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
onMove={() => {
toggleMoved();
}}
onPaneClick={() => {
toggleClicked();
}}
onNodeDragStop={(event, node) => {
removeDragHighlight();
let mousePos = project({ x: event.clientX, y: event.clientY });
Expand Down
25 changes: 23 additions & 2 deletions ui/src/components/nodes/Rich.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,23 @@ const DelayAutoFocusInput = ({
return <input ref={inputRef} {...rest} />;
};

function useUpdatePositionerOnMove() {
// Update (all) the positioners whenever there's a move (pane) on reactflow,
// so that the toolbar moves with the Rich pod and content.
const { forceUpdatePositioners, emptySelection } = useCommands();
const store = useContext(RepoContext);
if (!store) throw new Error("Missing BearContext.Provider in the tree");
const moved = useStore(store, (state) => state.moved);
const clicked = useStore(store, (state) => state.clicked);
useEffect(() => {
forceUpdatePositioners();
}, [moved]);
useEffect(() => {
emptySelection();
}, [clicked]);
return;
}

const FloatingLinkToolbar = ({ children }) => {
const {
isEditing,
Expand All @@ -254,6 +271,7 @@ const FloatingLinkToolbar = ({ children }) => {
setHref,
cancelHref,
} = useFloatingLinkState();
useUpdatePositionerOnMove();
const active = useActive();
const activeLink = active.link();
const { empty } = useCurrentSelection();
Expand Down Expand Up @@ -294,13 +312,16 @@ const FloatingLinkToolbar = ({ children }) => {
return (
<>
{!isEditing && (
<FloatingToolbar>
// By default, MUI's Popper creates a Portal, which is a ROOT html
// elements that prevents paning on reactflow canvas. Therefore, we
// disable the portal behavior.
<FloatingToolbar disablePortal>
{linkEditButtons}
{children}
</FloatingToolbar>
)}
{!isEditing && empty && (
<FloatingToolbar positioner={linkPositioner}>
<FloatingToolbar positioner={linkPositioner} disablePortal>
{linkEditButtons}
{children}
</FloatingToolbar>
Expand Down
13 changes: 13 additions & 0 deletions ui/src/lib/store/canvasSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ export interface CanvasSlice {
setPaneFocus: () => void;
setPaneBlur: () => void;

// onMove indicator
moved: boolean;
toggleMoved: () => void;
// clicked-on-canvas indicator
clicked: boolean;
toggleClicked: () => void;

addNode: (
type: "CODE" | "SCOPE" | "RICH",
position: XYPosition,
Expand Down Expand Up @@ -941,6 +948,12 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
},
setPaneFocus: () => set({ isPaneFocused: true }),
setPaneBlur: () => set({ isPaneFocused: false }),

moved: false,
toggleMoved: () => set({ moved: !get().moved }),
clicked: false,
toggleClicked: () => set({ clicked: !get().clicked }),

/**
* This node2children is maintained with the canvas reactflow states, not with
* the pods. This mapping may be used by other components, e.g. the runtime.
Expand Down