-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow to customization the connections (#29)
* feat(connection): Allow to customization connection * chore: removed mixins and replaced them with classes for easier typing * chore: cleanup connection to better DX
- Loading branch information
Showing
70 changed files
with
3,387 additions
and
1,207 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ node_modules | |
.parcel-cache | ||
dist | ||
.idea | ||
.vscode | ||
.DS_Store | ||
storybook-static | ||
.tsbuildinfo | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
96 changes: 96 additions & 0 deletions
96
src/components/canvas/EventedComponent/EventedComponent.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,96 @@ | ||
import { Component, TComponentContext, TComponentProps, TComponentState } from "../../../lib/Component"; | ||
|
||
type TEventedComponentListener = Component | ((e: Event) => void); | ||
|
||
const listeners = new WeakMap<Component, Map<string, Set<TEventedComponentListener>>>(); | ||
|
||
export class EventedComponent< | ||
Props extends TComponentProps = TComponentProps, | ||
State extends TComponentState = TComponentState, | ||
Context extends TComponentContext = TComponentContext, | ||
> extends Component<Props, State, Context> { | ||
public readonly cursor?: string; | ||
|
||
private get events() { | ||
if (!listeners.has(this)) { | ||
listeners.set(this, new Map()); | ||
} | ||
return listeners.get(this); | ||
} | ||
|
||
protected unmount() { | ||
listeners.delete(this); | ||
super.unmount(); | ||
} | ||
|
||
protected handleEvent(_: Event) { | ||
// noop | ||
} | ||
|
||
public listenEvents(events: string[], cbOrObject: TEventedComponentListener = this) { | ||
const unsubs = events.map((eventName) => { | ||
return this.addEventListener(eventName, cbOrObject); | ||
}); | ||
return unsubs; | ||
} | ||
|
||
public addEventListener(type: string, cbOrObject: TEventedComponentListener) { | ||
const cbs = this.events.get(type) || new Set(); | ||
cbs.add(cbOrObject); | ||
this.events.set(type, cbs); | ||
return () => this.removeEventListener(type, cbOrObject); | ||
} | ||
|
||
public removeEventListener(type: string, cbOrObject: TEventedComponentListener) { | ||
const cbs = this.events.get(type); | ||
if (cbs) { | ||
cbs.delete(cbOrObject); | ||
} | ||
} | ||
|
||
public _fireEvent(cmp: Component, event: Event) { | ||
const handlers = listeners.get(cmp)?.get?.(event.type); | ||
|
||
handlers?.forEach((cb) => { | ||
if (typeof cb === "function") { | ||
cb(event); | ||
} else if (cb instanceof Component && "handleEvent" in cb && typeof cb.handleEvent === "function") { | ||
cb.handleEvent?.(event); | ||
} | ||
}); | ||
} | ||
|
||
public dispatchEvent(event: Event): boolean { | ||
const bubbles = event.bubbles || false; | ||
|
||
if (bubbles) { | ||
return this._dipping(this, event); | ||
} else if (this._hasListener(this, event.type)) { | ||
this._fireEvent(this, event); | ||
return false; | ||
} | ||
return false; | ||
} | ||
|
||
public _dipping(startParent: Component, event: Event) { | ||
let stopPropagation = false; | ||
let parent: Component = startParent; | ||
event.stopPropagation = () => { | ||
stopPropagation = true; | ||
}; | ||
|
||
do { | ||
this._fireEvent(parent, event); | ||
if (stopPropagation) { | ||
return false; | ||
} | ||
parent = parent.getParent() as Component; | ||
} while (parent); | ||
|
||
return true; | ||
} | ||
|
||
public _hasListener(comp: EventedComponent, type: string) { | ||
return listeners.get(comp)?.has?.(type); | ||
} | ||
} |
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,69 @@ | ||
import { Signal } from "@preact/signals-core"; | ||
|
||
import { Graph } from "../../../graph"; | ||
import { Component } from "../../../lib"; | ||
import { TComponentContext, TComponentProps, TComponentState } from "../../../lib/Component"; | ||
import { HitBox, HitBoxData } from "../../../services/HitTest"; | ||
import { EventedComponent } from "../EventedComponent/EventedComponent"; | ||
import { TGraphLayerContext } from "../layers/graphLayer/GraphLayer"; | ||
|
||
export type GraphComponentContext = TComponentContext & | ||
TGraphLayerContext & { | ||
graph: Graph; | ||
}; | ||
|
||
export class GraphComponent< | ||
Props extends TComponentProps = TComponentProps, | ||
State extends TComponentState = TComponentState, | ||
Context extends GraphComponentContext = GraphComponentContext, | ||
> extends EventedComponent<Props, State, Context> { | ||
public hitBox: HitBox; | ||
|
||
private unsubscribe: (() => void)[] = []; | ||
|
||
constructor(props: Props, parent: Component) { | ||
super(props, parent); | ||
this.hitBox = new HitBox(this, this.context.graph.hitTest); | ||
} | ||
|
||
protected subscribeSignal<T>(signal: Signal<T>, cb: (v: T) => void) { | ||
this.unsubscribe.push(signal.subscribe(cb)); | ||
} | ||
|
||
protected unmount() { | ||
super.unmount(); | ||
this.unsubscribe.forEach((cb) => cb()); | ||
this.destroyHitBox(); | ||
} | ||
|
||
public setHitBox(minX: number, minY: number, maxX: number, maxY: number, force?: boolean) { | ||
this.hitBox.update(minX, minY, maxX, maxY, force); | ||
} | ||
|
||
protected willIterate(): void { | ||
super.willIterate(); | ||
if (!this.firstIterate) { | ||
this.shouldRender = this.isVisible(); | ||
} | ||
} | ||
|
||
protected isVisible() { | ||
return this.context.camera.isRectVisible(...this.getHitBox()); | ||
} | ||
|
||
public getHitBox() { | ||
return this.hitBox.getRect(); | ||
} | ||
|
||
public removeHitBox() { | ||
this.hitBox.remove(); | ||
} | ||
|
||
public destroyHitBox() { | ||
this.hitBox.destroy(); | ||
} | ||
|
||
public onHitBox(_: HitBoxData) { | ||
return this.isIterated(); | ||
} | ||
} |
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.