Skip to content

Commit

Permalink
[2614] Remember the last tool invoked in a tool section
Browse files Browse the repository at this point in the history
Bug: #2614
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
  • Loading branch information
pcdavid committed Nov 23, 2023
1 parent 591ddb4 commit 6213729
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ It uses our algorithm instead of elk.js to perform the arrange-all.
- https://github.com/eclipse-sirius/sirius-web/issues/2601[#2601] [emf] Fix an issue in `EditingContextCrossReferenceAdapter` where references to moved objects were lost.
- https://github.com/eclipse-sirius/sirius-web/issues/2582[#2582] [diagram] Fix a delay in renderer of palette tools between the one from _getPalette_ backend call and the one declared in frontend.
- https://github.com/eclipse-sirius/sirius-web/issues/2615[#2615] [diagram] Fix the `useDropNode` lifecycle.
- https://github.com/eclipse-sirius/sirius-web/issues/2614[#2614] [diagram] Tool sections now correctly remember the last of their tool invoked and default to that.

=== New Features

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ import {
DiagramPaletteContextProviderProps,
DiagramPaletteContextProviderState,
DiagramPaletteContextValue,
ToolSectionWithLastTool,
} from './DiagramPaletteContext.types';
import { GQLTool } from './Palette.types';

const defaultValue: DiagramPaletteContextValue = {
x: null,
y: null,
isOpened: false,
hideDiagramPalette: () => {},
showDiagramPalette: () => {},
getLastToolInvoked: () => null,
setLastToolInvoked: () => {},
};

export const DiagramPaletteContext = React.createContext<DiagramPaletteContextValue>(defaultValue);
Expand All @@ -33,6 +37,7 @@ export const DiagramPaletteContextProvider = ({ children }: DiagramPaletteContex
x: null,
y: null,
isOpened: false,
lastToolsInvoked: [],
});

const showPalette = useCallback((x: number, y: number) => {
Expand All @@ -43,6 +48,27 @@ export const DiagramPaletteContextProvider = ({ children }: DiagramPaletteContex
setState((prevState) => ({ ...prevState, x: null, y: null, isOpened: false }));
}, []);

const getLastToolInvoked = (toolSectionId: string): GQLTool | null => {
return (
state.lastToolsInvoked.find(
(toolSectionWithDefaultTool) => toolSectionWithDefaultTool.toolSectionId === toolSectionId
)?.lastTool || null
);
};

const setLastToolInvoked = (toolSectionId: string, tool: GQLTool) => {
const lastToolsInvoked: ToolSectionWithLastTool[] = state.lastToolsInvoked;
if (lastToolsInvoked.some((toolSectionWithLastTool) => toolSectionWithLastTool.toolSectionId === toolSectionId)) {
lastToolsInvoked.splice(
lastToolsInvoked.findIndex(
(toolSectionWithLastTool) => toolSectionWithLastTool.toolSectionId === toolSectionId
),
1
);
}
lastToolsInvoked.push({ toolSectionId: toolSectionId, lastTool: tool });
};

return (
<DiagramPaletteContext.Provider
value={{
Expand All @@ -51,6 +77,8 @@ export const DiagramPaletteContextProvider = ({ children }: DiagramPaletteContex
isOpened: state.isOpened,
showDiagramPalette: showPalette,
hideDiagramPalette: hidePalette,
getLastToolInvoked,
setLastToolInvoked,
}}>
{children}
</DiagramPaletteContext.Provider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
* Obeo - initial API and implementation
*******************************************************************************/

import { GQLTool } from './Palette.types';

export interface DiagramPaletteContextValue {
x: number | null;
y: number | null;
isOpened: boolean;
showDiagramPalette: (x: number, y: number) => void;
hideDiagramPalette: () => void;
getLastToolInvoked: (toolSectionId: string) => GQLTool | null;
setLastToolInvoked: (toolSectionId: string, tool: GQLTool) => void;
}

export interface DiagramPaletteContextProviderProps {
Expand All @@ -27,4 +31,10 @@ export interface DiagramPaletteContextProviderState {
x: number | null;
y: number | null;
isOpened: boolean;
lastToolsInvoked: ToolSectionWithLastTool[];
}

export interface ToolSectionWithLastTool {
toolSectionId: string;
lastTool: GQLTool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
import { useCallback, useRef } from 'react';
import { Tool } from '../../Tool';
import { GQLSingleClickOnDiagramElementTool, GQLTool } from '../Palette.types';
import { useDiagramPalette } from '../useDiagramPalette';
import { ToolSectionProps } from './ToolSection.types';

const useToolSectionStyles = makeStyles(() => ({
Expand Down Expand Up @@ -46,14 +47,16 @@ const isSingleClickOnDiagramElementTool = (tool: GQLTool): tool is GQLSingleClic

export const ToolSection = ({ toolSection, onToolClick, toolSectionExpandId, onExpand }: ToolSectionProps) => {
const tools = toolSection.tools.filter(isSingleClickOnDiagramElementTool);
const { getLastToolInvoked, setLastToolInvoked } = useDiagramPalette();

const classes = useToolSectionStyles();

const onActiveTool = useCallback(
(tool) => {
onToolClick(tool);
setLastToolInvoked(toolSection.id, tool);
},
[onToolClick]
[onToolClick, setLastToolInvoked, toolSection.id]
);

let caretContent: JSX.Element | undefined;
Expand All @@ -67,7 +70,9 @@ export const ToolSection = ({ toolSection, onToolClick, toolSectionExpandId, onE
/>
);
}
const defaultTool: GQLTool | undefined = tools[0];

const defaultTool: GQLTool | undefined = getLastToolInvoked(toolSection.id) || tools[0];

const anchorRef = useRef<HTMLDivElement | null>(null);
return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const computePalettePosition = (event: MouseEvent | React.MouseEvent, bounds?: D
};

export const useDiagramPalette = (): UseDiagramPaletteValue => {
const { x, y, isOpened, hideDiagramPalette, showDiagramPalette } =
const { x, y, isOpened, hideDiagramPalette, showDiagramPalette, getLastToolInvoked, setLastToolInvoked } =
useContext<DiagramPaletteContextValue>(DiagramPaletteContext);
const { domNode } = useStoreApi().getState();
const element = domNode?.getBoundingClientRect();
Expand All @@ -45,5 +45,7 @@ export const useDiagramPalette = (): UseDiagramPaletteValue => {
hideDiagramPalette,
showDiagramPalette,
onDiagramBackgroundClick,
getLastToolInvoked,
setLastToolInvoked,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
* Obeo - initial API and implementation
*******************************************************************************/

import { GQLTool } from './Palette.types';

export interface UseDiagramPaletteValue {
x: number | null;
y: number | null;
isOpened: boolean;
hideDiagramPalette: () => void;
showDiagramPalette: (x: number, y: number) => void;
onDiagramBackgroundClick: (event: React.MouseEvent<Element, MouseEvent>) => void;
getLastToolInvoked: (toolSectionId: string) => GQLTool | null;
setLastToolInvoked: (toolSectionId: string, tool: GQLTool) => void;
}

0 comments on commit 6213729

Please sign in to comment.