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

Feature/PSYNEU-86 #55

Merged
merged 2 commits into from
Jul 5, 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: 3 additions & 5 deletions src/components/CanvasWidget.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { makeStyles } from '@mui/styles';
import { Box } from '@mui/system';
import {
CanvasEngine,
CanvasWidget as Canvas,
} from '@projectstorm/react-canvas-core';
import { CanvasWidget as Canvas } from '@projectstorm/react-canvas-core';
import React, { useCallback } from 'react';
import { DropTargetMonitor, useDrop } from 'react-dnd';
import { CanvasDropTypes } from '../constants';
import vars from './assets/styles/variables';
import { INode, ISidebarNodeProps } from '../types/sidebar';
import { DiagramEngine } from '@projectstorm/react-diagrams';

const useStyles = makeStyles(_ => ({
container: {
Expand All @@ -25,7 +23,7 @@
const { canvasBg } = vars;

interface ICanvasWidgetProps {
engine: CanvasEngine;
engine: DiagramEngine;
className?: string;
}

Expand All @@ -42,11 +40,11 @@

if (!!node.onNodeDrop) node?.onNodeDrop(monitor, node, engine);
},
[]

Check warning on line 43 in src/components/CanvasWidget.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

React Hook useCallback has a missing dependency: 'engine'. Either include it or remove the dependency array

Check warning on line 43 in src/components/CanvasWidget.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and windows-latest

React Hook useCallback has a missing dependency: 'engine'. Either include it or remove the dependency array

Check warning on line 43 in src/components/CanvasWidget.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and macOS-latest

React Hook useCallback has a missing dependency: 'engine'. Either include it or remove the dependency array
);

// can drop custom hook
const [{}, dropRef] = useDrop({

Check warning on line 47 in src/components/CanvasWidget.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

Unexpected empty object pattern

Check warning on line 47 in src/components/CanvasWidget.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and windows-latest

Unexpected empty object pattern

Check warning on line 47 in src/components/CanvasWidget.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and macOS-latest

Unexpected empty object pattern
accept: CanvasDropTypes.CANVAS_NODE,
// canDrop: () => true,
drop: onDrop,
Expand Down
4 changes: 3 additions & 1 deletion src/components/sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import { CreateLinkState } from './states/CreateLinkState';

const Sidebar = ({ engine, sidebarNodes, updateSelection }: ISidebarProps) => {
const [currentState, setCurrentState] = useState<string | null>(null);
const [currentState, setCurrentState] = useState<string | null>(
() => DefaultSidebarNodeTypes.CREATE_LINK
);

const reactDiagramsState = engine
.getStateMachine()
Expand All @@ -35,7 +37,7 @@

useEffect(() => {
handleSelection(DefaultSidebarNodeTypes.PANNING);
}, []);

Check warning on line 40 in src/components/sidebar/Sidebar.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

React Hook useEffect has a missing dependency: 'handleSelection'. Either include it or remove the dependency array

Check warning on line 40 in src/components/sidebar/Sidebar.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and windows-latest

React Hook useEffect has a missing dependency: 'handleSelection'. Either include it or remove the dependency array

Check warning on line 40 in src/components/sidebar/Sidebar.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and macOS-latest

React Hook useEffect has a missing dependency: 'handleSelection'. Either include it or remove the dependency array

return (
<>
Expand Down
5 changes: 5 additions & 0 deletions src/components/sidebar/states/CreateLinkState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { State } from './State';
import { updateCanvasMouseCursor } from '../../../utils';
import { CursorTypes } from '../../../constants';
import { DefaultState } from '../../../react-diagrams/state/DefaultState';
import { DefaultDiagramState } from '@projectstorm/react-diagrams';

export class CreateLinkState extends State {
onExit() {
if (this.state instanceof DefaultState) {
this.state.createLink.config.allowCreate = false;
} else if (this.state instanceof DefaultDiagramState) {
this.state.dragNewLink.config.allowLinksFromLockedPorts = false;
} else {
this.state.config.allowCreate = false;
}
Expand All @@ -16,6 +19,8 @@ export class CreateLinkState extends State {
onEnter() {
if (this.state instanceof DefaultState) {
this.state.createLink.config.allowCreate = true;
} else if (this.state instanceof DefaultDiagramState) {
this.state.dragNewLink.config.allowLinksFromLockedPorts = true;
} else {
this.state.config.allowCreate = true;
}
Expand Down
11 changes: 9 additions & 2 deletions src/components/sidebar/states/PanningState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import { State } from './State';
import { updateCanvasMouseCursor } from '../../../utils';
import { CursorTypes } from '../../../constants';
import { DefaultState } from '../../../react-diagrams/state/DefaultState';
import { DefaultDiagramState } from '@projectstorm/react-diagrams';

export class PanningState extends State {
onExit() {
if (this.state instanceof DefaultState) {
if (
this.state instanceof DefaultState ||
this.state instanceof DefaultDiagramState
) {
this.state.dragCanvas.config.allowDrag = false;
} else {
this.state.config.allowDrag = false;
Expand All @@ -14,7 +18,10 @@ export class PanningState extends State {
}

onEnter() {
if (this.state instanceof DefaultState) {
if (
this.state instanceof DefaultState ||
this.state instanceof DefaultDiagramState
) {
this.state.dragCanvas.config.allowDrag = true;
} else {
this.state.config.allowDrag = true;
Expand Down
7 changes: 5 additions & 2 deletions src/components/sidebar/states/State.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { DefaultDiagramState } from '@projectstorm/react-diagrams';
import { CreateLinkState } from '../../../react-diagrams/state/CreateLinkState';
import { DefaultState } from '../../../react-diagrams/state/DefaultState';

export class State {
protected state: DefaultState | CreateLinkState;
protected state: DefaultState | CreateLinkState | DefaultDiagramState;

constructor(reactDiagramsState: DefaultState | CreateLinkState) {
constructor(
reactDiagramsState: DefaultState | CreateLinkState | DefaultDiagramState
) {
this.state = reactDiagramsState;
}

Expand Down
20 changes: 15 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@
const linkRef = React.useRef<any>();

// initialize custom diagram state
const state = new DefaultState(globalProps?.createLink);
let state = new DefaultState(globalProps?.createLink);
state.isSelection = false;

// Sets up the diagram engine
// By using useMemo, we ensure that the createEngine() function is only called when the component mounts,
// and the same engine instance is reused on subsequent re-renders.
const engine = useMemo(() => createEngine(), [metaNodes, metaLinks]);

Check warning on line 80 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

React Hook useMemo has unnecessary dependencies: 'metaLinks' and 'metaNodes'. Either exclude them or remove the dependency array

Check warning on line 80 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and windows-latest

React Hook useMemo has unnecessary dependencies: 'metaLinks' and 'metaNodes'. Either exclude them or remove the dependency array

Check warning on line 80 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and macOS-latest

React Hook useMemo has unnecessary dependencies: 'metaLinks' and 'metaNodes'. Either exclude them or remove the dependency array

if (metaCallback === undefined) {
metaCallback = (node: any) => {
Expand Down Expand Up @@ -144,9 +145,10 @@
const sourcePort = link.getSourcePort();
const targetPort = link.getTargetPort();
if (sourcePort && !targetPort) {
model.removeLink(link);
engine.getModel().removeLink(link);
}
linkRef.current = null;
engine.getStateMachine().popState();
};

let registerNodeListeners = (node: any) => {
Expand Down Expand Up @@ -181,18 +183,26 @@
.toLowerCase()
.startsWith(DefaultSidebarNodeTypes.SELECT);

if (startsWithSelect && Boolean(state.isSelection)) {
return;
}

if (id !== DefaultSidebarNodeTypes.CREATE_LINK && !!linkRef.current) {
removeNotValidLink();
}

if (startsWithSelect && !Boolean(state.isSelection)) {
state.isSelection = true;
} else if (startsWithSelect && Boolean(state.isSelection)) {
return;
} else if (state.isSelection) {
} else if (
state.isSelection ||
(!startsWithSelect && Boolean(state.isSelection))
) {
clearSelection();
state.isSelection = false;
}
if (engine) {
repaintCanvas();
}
};

// load model into engine
Expand All @@ -218,7 +228,7 @@

useEffect(() => {
if (onMount === undefined) {
onMount = (engine: any) => {

Check warning on line 231 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

Assignments to the 'onMount' variable from inside React Hook useEffect will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside useEffect

Check warning on line 231 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and windows-latest

Assignments to the 'onMount' variable from inside React Hook useEffect will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside useEffect

Check warning on line 231 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and macOS-latest

Assignments to the 'onMount' variable from inside React Hook useEffect will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside useEffect
console.log(engine);
};
}
Expand Down
6 changes: 5 additions & 1 deletion src/react-diagrams/state/DefaultState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ export class DefaultState extends State<DiagramEngine> {
.getActionEventBus()
.getModelForEvent(event);

if (element instanceof PortModel || element instanceof MetaNodeModel)
if (
element instanceof PortModel ||
element instanceof MetaNodeModel
) {
this.transitionWithEvent(this.createLink, event);
}
},
})
);
Expand Down
Loading