-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[2313] Support Palette on Edges on react-flow diagrams
Bug: #2313 Signed-off-by: Axel RICHARD <axel.richard@obeo.fr>
- Loading branch information
1 parent
65ebe0f
commit 8953322
Showing
15 changed files
with
306 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...agrams/frontend/sirius-components-diagrams-reactflow/src/renderer/palette/EdgePalette.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Obeo. | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
import { makeStyles } from '@material-ui/core/styles'; | ||
import { useDiagramDirectEdit } from '../direct-edit/useDiagramDirectEdit'; | ||
import { EdgePaletteProps } from './EdgePalette.types'; | ||
import { EdgePalettePortal } from './EdgePalettePortal'; | ||
import { Palette } from './Palette'; | ||
import { useEdgePalette } from './useEdgePalette'; | ||
|
||
const useEdgePaletteStyle = makeStyles((theme) => ({ | ||
toolbar: { | ||
background: theme.palette.background.paper, | ||
border: '1px solid #d1dadb', | ||
boxShadow: '0px 2px 5px #002b3c40', | ||
borderRadius: '2px', | ||
zIndex: 2, | ||
position: 'fixed', | ||
display: 'flex', | ||
alignItems: 'center', | ||
}, | ||
})); | ||
|
||
export const EdgePalette = ({ edgeId, labelId }: EdgePaletteProps) => { | ||
const { setCurrentlyEditedLabelId } = useDiagramDirectEdit(); | ||
const { x, y, isOpened } = useEdgePalette(); | ||
const classes = useEdgePaletteStyle(); | ||
|
||
const handleDirectEditClick = () => { | ||
if (labelId) { | ||
setCurrentlyEditedLabelId('palette', labelId, null); | ||
} | ||
}; | ||
|
||
return isOpened && x && y ? ( | ||
<EdgePalettePortal> | ||
<div className={classes.toolbar} style={{ position: 'absolute', left: x, top: y }}> | ||
<Palette diagramElementId={edgeId} onDirectEditClick={handleDirectEditClick} isDiagramElementPalette={true} /> | ||
</div> | ||
</EdgePalettePortal> | ||
) : null; | ||
}; |
17 changes: 17 additions & 0 deletions
17
...s/frontend/sirius-components-diagrams-reactflow/src/renderer/palette/EdgePalette.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Obeo. | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
export interface EdgePaletteProps { | ||
edgeId: string; | ||
labelId: string | null; | ||
} |
58 changes: 58 additions & 0 deletions
58
...frontend/sirius-components-diagrams-reactflow/src/renderer/palette/EdgePaletteContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Obeo. | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
import React, { useCallback, useState } from 'react'; | ||
import { | ||
EdgePaletteContextProviderProps, | ||
EdgePaletteContextProviderState, | ||
EdgePaletteContextValue, | ||
} from './EdgePaletteContext.types'; | ||
|
||
const defaultValue: EdgePaletteContextValue = { | ||
x: null, | ||
y: null, | ||
isOpened: false, | ||
hideEdgePalette: () => {}, | ||
showEdgePalette: () => {}, | ||
}; | ||
|
||
export const EdgePaletteContext = React.createContext<EdgePaletteContextValue>(defaultValue); | ||
|
||
export const EdgePaletteContextProvider = ({ children }: EdgePaletteContextProviderProps) => { | ||
const [state, setState] = useState<EdgePaletteContextProviderState>({ | ||
x: null, | ||
y: null, | ||
isOpened: false, | ||
}); | ||
|
||
const showPalette = useCallback((x: number, y: number) => { | ||
setState((prevState) => ({ ...prevState, x, y, isOpened: true })); | ||
}, []); | ||
|
||
const hidePalette = useCallback(() => { | ||
setState((prevState) => ({ ...prevState, x: null, y: null, isOpened: false })); | ||
}, []); | ||
|
||
return ( | ||
<EdgePaletteContext.Provider | ||
value={{ | ||
x: state.x, | ||
y: state.y, | ||
isOpened: state.isOpened, | ||
showEdgePalette: showPalette, | ||
hideEdgePalette: hidePalette, | ||
}}> | ||
{children} | ||
</EdgePaletteContext.Provider> | ||
); | ||
}; |
30 changes: 30 additions & 0 deletions
30
...end/sirius-components-diagrams-reactflow/src/renderer/palette/EdgePaletteContext.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Obeo. | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
export interface EdgePaletteContextValue { | ||
x: number | null; | ||
y: number | null; | ||
isOpened: boolean; | ||
showEdgePalette: (x: number, y: number) => void; | ||
hideEdgePalette: () => void; | ||
} | ||
|
||
export interface EdgePaletteContextProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
export interface EdgePaletteContextProviderState { | ||
x: number | null; | ||
y: number | null; | ||
isOpened: boolean; | ||
} |
27 changes: 27 additions & 0 deletions
27
.../frontend/sirius-components-diagrams-reactflow/src/renderer/palette/EdgePalettePortal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Obeo. | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
import { ReactNode } from 'react'; | ||
import { createPortal } from 'react-dom'; | ||
import { ReactFlowState, useStore } from 'reactflow'; | ||
|
||
const selector = (state: ReactFlowState) => state.domNode?.querySelector('.react-flow__renderer'); | ||
|
||
export const EdgePalettePortal = ({ children }: { children: ReactNode }) => { | ||
const wrapperRef = useStore(selector); | ||
|
||
if (!wrapperRef) { | ||
return null; | ||
} | ||
|
||
return createPortal(children, wrapperRef); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.