-
Notifications
You must be signed in to change notification settings - Fork 9
feat(NewBlockLayer, ConnectionLayer): added validation and duplication of blocks by a group #62
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -33,13 +33,15 @@ | |
| createIcon?: TIcon; | ||
| point?: TIcon; | ||
| drawLine?: DrawLineFunction; | ||
| isConnectionAllowed?: (sourceComponent: BlockState | AnchorState) => boolean; | ||
| }; | ||
|
|
||
| declare module "../../../../graphEvents" { | ||
| interface GraphEventsDefinitions { | ||
| /** | ||
| * Event reporting on connection pull out of a block/block's anchor. | ||
| * Preventing this event will prevent the connection from being created. | ||
| * Fired when a user initiates a connection from a block or anchor. | ||
| * This happens when dragging starts from a block (with Shift key) or an anchor. | ||
| * Preventing this event will prevent the selection of the source component. | ||
| */ | ||
| "connection-create-start": ( | ||
| event: CustomEvent<{ | ||
|
|
@@ -49,8 +51,8 @@ | |
| ) => void; | ||
|
|
||
| /** | ||
| * Event fires on pulled out connection hover on block or anchor | ||
| * Preventing prevent connection creation. | ||
| * Fired when the dragged connection endpoint hovers over a potential target block or anchor. | ||
| * Preventing this event will prevent the selection of the target component. | ||
| */ | ||
| "connection-create-hover": ( | ||
| event: CustomEvent<{ | ||
|
|
@@ -62,9 +64,9 @@ | |
| ) => void; | ||
|
|
||
| /** | ||
| * Event fires when a connection is successfully created | ||
| * By default, this adds the connection to connectionsList | ||
| * Preventing this event will prevent the connection from being added | ||
| * Fired when a connection is successfully created between two elements. | ||
| * By default, this adds the connection to the connectionsList in the store. | ||
| * Preventing this event will prevent the connection from being added to the store. | ||
| */ | ||
| "connection-created": ( | ||
| event: CustomEvent<{ | ||
|
|
@@ -76,8 +78,9 @@ | |
| ) => void; | ||
|
|
||
| /** | ||
| * Event fires when the user drops the connection endpoint | ||
| * This happens regardless of whether a connection was created | ||
| * Fired when the user releases the mouse button to complete the connection process. | ||
| * This event fires regardless of whether a valid connection was established. | ||
| * Can be used for cleanup or to handle custom connection drop behavior. | ||
| */ | ||
| "connection-create-drop": ( | ||
| event: CustomEvent<{ | ||
|
|
@@ -91,19 +94,23 @@ | |
| } | ||
| } | ||
|
|
||
| /** | ||
|
Check warning on line 97 in src/components/canvas/layers/connectionLayer/ConnectionLayer.ts
|
||
| * ConnectionLayer provides functionality for creating and visualizing connections | ||
| * between blocks and anchors in a graph. | ||
| * ConnectionLayer manages the creation process of connections between blocks and anchors in the graph. | ||
| * It handles the temporary visualization during connection creation but does not render existing connections. | ||
| * | ||
| * Features: | ||
| * - Create connections through an intuitive drag and drop interface | ||
| * - Customize connection appearance with icons and line styles | ||
| * - Automatic selection of connected elements | ||
| * - Rich event system for connection lifecycle | ||
| * - Interactive connection creation through drag and drop | ||
| * - Temporary visualization during connection creation with configurable icons and line styles | ||
| * - Automatic selection handling of source and target elements | ||
| * - Comprehensive event system for the connection creation lifecycle | ||
| * - Optional connection validation through isConnectionAllowed prop | ||
| * | ||
| * Connection types: | ||
| * - Block-to-Block: Hold Shift and drag from one block to another | ||
| * - Anchor-to-Anchor: Drag from one anchor to another | ||
| * - Block-to-Block: Hold Shift key and drag from one block to another | ||
| * - Anchor-to-Anchor: Drag from one anchor to another (must be on different blocks) | ||
| * | ||
| * The layer renders on a separate canvas with a higher z-index and handles | ||
| * all mouse interactions for connection creation. | ||
| */ | ||
| export class ConnectionLayer extends Layer< | ||
| ConnectionLayerProps, | ||
|
|
@@ -117,8 +124,8 @@ | |
| }; | ||
| protected target?: Block | Anchor; | ||
| protected sourceComponent?: BlockState | AnchorState; | ||
|
|
||
| protected enabled: boolean; | ||
| private declare eventAborter: AbortController; | ||
|
|
||
| constructor(props: ConnectionLayerProps) { | ||
| super({ | ||
|
|
@@ -141,9 +148,10 @@ | |
|
|
||
| this.enabled = Boolean(this.props.graph.rootStore.settings.getConfigFlag("canCreateNewConnections")); | ||
|
|
||
| this.eventAborter = new AbortController(); | ||
| this.performRender = this.performRender.bind(this); | ||
| this.context.graph.on("camera-change", this.performRender); | ||
| this.context.graph.on("mousedown", this.handleMouseDown, { capture: true }); | ||
| this.context.graph.on("camera-change", this.performRender, { signal: this.eventAborter.signal }); | ||
| this.context.graph.on("mousedown", this.handleMouseDown, { capture: true, signal: this.eventAborter.signal }); | ||
| } | ||
|
|
||
| public enable = () => { | ||
|
|
@@ -169,6 +177,14 @@ | |
| ((this.context.graph.rootStore.settings.getConfigFlag("useBlocksAnchors") && target instanceof Anchor) || | ||
| (isShiftKeyEvent(event) && isBlock(target))) | ||
| ) { | ||
| // Get the source component state | ||
| const sourceComponent = target.connectedState; | ||
|
|
||
| // Check if connection is allowed using the validation function if provided | ||
| if (this.props.isConnectionAllowed && !this.props.isConnectionAllowed(sourceComponent)) { | ||
| return; | ||
| } | ||
|
|
||
| nativeEvent.preventDefault(); | ||
| nativeEvent.stopPropagation(); | ||
| dragListener(this.getOwnedDocument()) | ||
|
|
@@ -249,8 +265,9 @@ | |
| } | ||
|
|
||
| protected unmount(): void { | ||
| this.context.graph.off("camera-change", this.performRender); | ||
| this.context.graph.off("mousedown", this.handleMouseDown); | ||
| this.eventAborter.abort(); | ||
|
|
||
| super.unmount(); | ||
| } | ||
|
|
||
| private getBlockId(component: BlockState | AnchorState) { | ||
|
|
@@ -274,6 +291,15 @@ | |
| return; | ||
| } | ||
|
|
||
| this.sourceComponent = sourceComponent.connectedState; | ||
|
|
||
| const xy = getXY(this.context.graphCanvas, event); | ||
| this.connectionState = { | ||
| ...this.connectionState, | ||
| sx: xy[0], | ||
| sy: xy[1], | ||
| }; | ||
|
|
||
| this.context.graph.executеDefaultEventAction( | ||
| "connection-create-start", | ||
| { | ||
|
|
@@ -284,29 +310,21 @@ | |
| anchorId: sourceComponent instanceof Anchor ? sourceComponent.connectedState.id : undefined, | ||
| }, | ||
| () => { | ||
| this.sourceComponent = sourceComponent.connectedState; | ||
|
|
||
| if (sourceComponent instanceof Block) { | ||
| this.context.graph.api.selectBlocks([this.sourceComponent.id], true, ESelectionStrategy.REPLACE); | ||
| } else if (sourceComponent instanceof Anchor) { | ||
| this.context.graph.api.setAnchorSelection(sourceComponent.props.blockId, sourceComponent.props.id, true); | ||
| } | ||
|
|
||
| const xy = getXY(this.context.graphCanvas, event); | ||
| this.connectionState = { | ||
| ...this.connectionState, | ||
| sx: xy[0], | ||
| sy: xy[1], | ||
| }; | ||
| this.performRender(); | ||
| } | ||
| ); | ||
|
|
||
| this.performRender(); | ||
| } | ||
|
|
||
| private onMoveNewConnection(event: MouseEvent, point: Point) { | ||
| const newTargetComponent = this.context.graph.getElementOverPoint(point, [Block, Anchor]); | ||
| const xy = getXY(this.context.graphCanvas, event); | ||
| this.target = newTargetComponent; | ||
|
|
||
| this.connectionState = { | ||
| ...this.connectionState, | ||
| tx: xy[0], | ||
|
|
@@ -320,14 +338,17 @@ | |
| return; | ||
| } | ||
|
|
||
| // Only process if the target has changed or if there was no previous target | ||
| if ( | ||
| this.target?.connectedState !== newTargetComponent.connectedState && | ||
| (!this.target || this.target.connectedState !== newTargetComponent.connectedState) && | ||
| newTargetComponent.connectedState !== this.sourceComponent | ||
| ) { | ||
| this.target?.connectedState?.setSelection(false); | ||
|
|
||
| const target = newTargetComponent.connectedState; | ||
|
|
||
| this.target = newTargetComponent; | ||
|
|
||
| this.context.graph.executеDefaultEventAction( | ||
| "connection-create-hover", | ||
| { | ||
|
|
@@ -338,7 +359,6 @@ | |
| targetBlockId: target instanceof AnchorState ? target.blockId : target.id, | ||
| }, | ||
| () => { | ||
| this.target = newTargetComponent; | ||
| this.target.connectedState.setSelection(true); | ||
| } | ||
| ); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.