-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Optimized reassign and create connection
- Loading branch information
1 parent
200071b
commit ec697c3
Showing
19 changed files
with
107 additions
and
184 deletions.
There are no files selected for viewing
5 changes: 4 additions & 1 deletion
5
...ctions/assign-node-to-connection-on-drop/assign-node-to-connection-on-drop.component.html
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
37 changes: 24 additions & 13 deletions
37
...reate-connection/create-connection-preparation/create-connection-preparation.execution.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 |
---|---|---|
@@ -1,43 +1,54 @@ | ||
import { IHandler } from '@foblex/mediator'; | ||
import { IPointerEvent } from '@foblex/drag-toolkit'; | ||
import { Injectable } from '@angular/core'; | ||
import { inject, Injectable } from '@angular/core'; | ||
import { FComponentsStore } from '../../../../f-storage'; | ||
import { FConnectorBase, isNodeOutlet, isNodeOutput } from '../../../../f-connectors'; | ||
import { FNodeBase } from '../../../../f-node'; | ||
import { CreateConnectionPreparationRequest } from './create-connection-preparation.request'; | ||
import { FExecutionRegister, FMediator } from '@foblex/mediator'; | ||
import { CreateConnectionFromOutletPreparationRequest } from './create-connection-from-outlet-preparation'; | ||
import { CreateConnectionFromOutputPreparationRequest } from './create-connection-from-output-preparation'; | ||
import { FDraggableDataContext } from '../../../f-draggable-data-context'; | ||
|
||
@Injectable() | ||
@FExecutionRegister(CreateConnectionPreparationRequest) | ||
export class CreateConnectionPreparationExecution | ||
implements IHandler<CreateConnectionPreparationRequest, void> { | ||
|
||
constructor( | ||
private fComponentsStore: FComponentsStore, | ||
private fMediator: FMediator, | ||
) { | ||
} | ||
private _fMediator = inject(FMediator); | ||
private _fComponentsStore = inject(FComponentsStore); | ||
private _fDraggableDataContext = inject(FDraggableDataContext); | ||
|
||
public handle(request: CreateConnectionPreparationRequest): void { | ||
if (!this._isValid(request)) { | ||
return; | ||
} | ||
|
||
if (isNodeOutlet(request.event.targetElement)) { | ||
this.fMediator.send<void>(new CreateConnectionFromOutletPreparationRequest(request.event)); | ||
} else if (this.isNodeOutput(request.event.targetElement, this.getNode(request.event))) { | ||
this.fMediator.send<void>(new CreateConnectionFromOutputPreparationRequest(request.event)); | ||
this._fMediator.send<void>(new CreateConnectionFromOutletPreparationRequest(request.event)); | ||
} else if (this.isNodeOutput(request.event.targetElement, this._getNode(request.event)!)) { | ||
this._fMediator.send<void>(new CreateConnectionFromOutputPreparationRequest(request.event)); | ||
} | ||
} | ||
|
||
private getNode(event: IPointerEvent): FNodeBase { | ||
return this.fComponentsStore | ||
.fNodes.find(n => n.isContains(event.targetElement))!; | ||
private _isValid(request: CreateConnectionPreparationRequest): boolean { | ||
return !!this._getNode(request.event) && this._isValidConditions(); | ||
} | ||
|
||
private _getNode(event: IPointerEvent): FNodeBase | undefined { | ||
return this._fComponentsStore | ||
.fNodes.find(n => n.isContains(event.targetElement)); | ||
} | ||
|
||
private _isValidConditions(): boolean { | ||
return !this._fDraggableDataContext.draggableItems.length && !!this._fComponentsStore.fTempConnection; | ||
} | ||
|
||
private isNodeOutput(targetElement: HTMLElement, node: FNodeBase): boolean { | ||
return isNodeOutput(targetElement) && !this.getOutlets(node).length; | ||
} | ||
|
||
private getOutlets(node: FNodeBase): FConnectorBase[] { | ||
return this.fComponentsStore.fOutlets.filter((x) => node.isContains(x.hostElement)); | ||
return this._fComponentsStore.fOutlets.filter((x) => node.isContains(x.hostElement)); | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
...reate-connection/create-connection-preparation/create-connection-preparation.validator.ts
This file was deleted.
Oops, something went wrong.
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
2 changes: 0 additions & 2 deletions
2
.../src/f-draggable/connections/reassign-connection/reassign-connection-preparation/index.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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
export * from './reassign-connection-preparation.execution'; | ||
|
||
export * from './reassign-connection-preparation.request'; | ||
|
||
export * from './reassign-connection-preparation.validator'; |
87 changes: 48 additions & 39 deletions
87
...n-connection/reassign-connection-preparation/reassign-connection-preparation.execution.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 |
---|---|---|
@@ -1,73 +1,82 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { inject, Injectable } from '@angular/core'; | ||
import { ReassignConnectionPreparationRequest } from './reassign-connection-preparation.request'; | ||
import { IPoint, ITransformModel, Point } from '@foblex/2d'; | ||
import { FComponentsStore } from '../../../../f-storage'; | ||
import { FDraggableDataContext } from '../../../f-draggable-data-context'; | ||
import { UpdateItemAndChildrenLayersRequest } from '../../../../domain'; | ||
import { FExecutionRegister, FMediator, IExecution } from '@foblex/mediator'; | ||
import { F_CONNECTION_DRAG_HANDLE_CLASS, FConnectionBase } from '../../../../f-connection'; | ||
import { FConnectionBase } from '../../../../f-connection'; | ||
import { ReassignConnectionDragHandler } from '../reassign-connection.drag-handler'; | ||
import { BrowserService } from '@foblex/platform'; | ||
|
||
@Injectable() | ||
@FExecutionRegister(ReassignConnectionPreparationRequest) | ||
export class ReassignConnectionPreparationExecution implements IExecution<ReassignConnectionPreparationRequest, void> { | ||
|
||
private get transform(): ITransformModel { | ||
return this.fComponentsStore.fCanvas!.transform; | ||
} | ||
private _fMediator = inject(FMediator); | ||
private _fComponentsStore = inject(FComponentsStore); | ||
private _fDraggableDataContext = inject(FDraggableDataContext); | ||
|
||
private _fConnection: FConnectionBase | undefined; | ||
|
||
private get flowHost(): HTMLElement { | ||
return this.fComponentsStore.fFlow!.hostElement; | ||
private get _transform(): ITransformModel { | ||
return this._fComponentsStore.fCanvas!.transform; | ||
} | ||
|
||
private get fConnections(): FConnectionBase[] { | ||
return this.fComponentsStore.fConnections; | ||
private get _fHost(): HTMLElement { | ||
return this._fComponentsStore.fFlow!.hostElement; | ||
} | ||
|
||
constructor( | ||
private fComponentsStore: FComponentsStore, | ||
private fDraggableDataContext: FDraggableDataContext, | ||
private fMediator: FMediator, | ||
private fBrowser: BrowserService | ||
) { | ||
private get _fConnections(): FConnectionBase[] { | ||
return this._fComponentsStore.fConnections; | ||
} | ||
|
||
public handle(request: ReassignConnectionPreparationRequest): void { | ||
const connectionToReassign = this.getConnectionHandler( | ||
this.getDragHandleElement(request.event.getPosition()) | ||
)!; | ||
if (connectionToReassign.fDraggingDisabled) { | ||
if (!this._isValid(request)) { | ||
return; | ||
} | ||
|
||
this.fMediator.send<void>( | ||
new UpdateItemAndChildrenLayersRequest( | ||
connectionToReassign, this.fComponentsStore.fCanvas!.fConnectionsContainer.nativeElement | ||
) | ||
); | ||
this._fDraggableDataContext.onPointerDownScale = this._transform.scale; | ||
this._fDraggableDataContext.onPointerDownPosition = Point.fromPoint(request.event.getPosition()) | ||
.elementTransform(this._fHost).div(this._transform.scale); | ||
this._fDraggableDataContext.draggableItems = [ | ||
new ReassignConnectionDragHandler(this._fMediator, this._fComponentsStore, this._fConnection!) | ||
]; | ||
|
||
setTimeout(() => this._updateConnectionLayer()); | ||
} | ||
|
||
this.fDraggableDataContext.onPointerDownScale = this.transform.scale; | ||
private _isValid(request: ReassignConnectionPreparationRequest): boolean { | ||
this._fConnection = this._getConnectionToReassign(this._getPointInFlow(request)); | ||
return !!this._fConnection && !this._fDraggableDataContext.draggableItems.length; | ||
} | ||
|
||
this.fDraggableDataContext.onPointerDownPosition = Point.fromPoint(request.event.getPosition()) | ||
.elementTransform(this.flowHost).div(this.transform.scale); | ||
private _getPointInFlow(request: ReassignConnectionPreparationRequest): IPoint { | ||
return Point.fromPoint(request.event.getPosition()) | ||
.elementTransform(this._fHost) | ||
.sub(this._transform.scaledPosition).sub(this._transform.position) | ||
.div(this._transform.scale); | ||
} | ||
|
||
this.fDraggableDataContext.draggableItems = [ | ||
new ReassignConnectionDragHandler(this.fMediator, this.fComponentsStore, connectionToReassign) | ||
]; | ||
private _getConnectionToReassign(position: IPoint): FConnectionBase | undefined { | ||
const connections = this._getConnectionsFromPoint(position); | ||
return connections.length ? connections[0] : undefined; | ||
} | ||
|
||
private getDragHandleElement(position: IPoint): HTMLElement { | ||
return this.getElementsFromPoint(position).filter((x) => { | ||
return !!this.getConnectionHandler(x as HTMLElement) && x.classList.contains(F_CONNECTION_DRAG_HANDLE_CLASS); | ||
}).find((x) => !!x)!; | ||
private _getConnectionsFromPoint(position: IPoint): FConnectionBase[] { | ||
return this._fConnections.filter((x) => { | ||
return this._isPointInsideCircle(position, x.fDragHandle.point) && !x.fDraggingDisabled; | ||
}); | ||
} | ||
|
||
private getElementsFromPoint(position: IPoint): HTMLElement[] { | ||
return this.fBrowser.document.elementsFromPoint(position.x, position.y) as HTMLElement[]; | ||
private _isPointInsideCircle(point: IPoint, circleCenter: IPoint): boolean { | ||
return (point.x - circleCenter.x) ** 2 + (point.y - circleCenter.y) ** 2 <= 8 ** 2; | ||
} | ||
|
||
public getConnectionHandler(element: HTMLElement | SVGElement): FConnectionBase | undefined { | ||
return this.fConnections.find(c => c.isContains(element)); | ||
private _updateConnectionLayer(): void { | ||
this._fMediator.execute<void>( | ||
new UpdateItemAndChildrenLayersRequest( | ||
this._fConnection!, this._fComponentsStore.fCanvas!.fConnectionsContainer.nativeElement | ||
) | ||
); | ||
} | ||
} |
43 changes: 0 additions & 43 deletions
43
...n-connection/reassign-connection-preparation/reassign-connection-preparation.validator.ts
This file was deleted.
Oops, something went wrong.
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.