Skip to content

Commit

Permalink
breaking: migrate to new Scope typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ni55aN committed May 7, 2023
1 parent 8198bbe commit c6a7b0f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/extensions/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { AreaPlugin } from '..'

export function simpleNodesOrder<T>(plugin: AreaPlugin<BaseSchemes, T>) {
plugin.addPipe(context => {
if (!('type' in context)) return context
if (!context || typeof context !== 'object' || !('type' in context)) return context

if (context.type === 'nodepicked') {
const view = plugin.nodeViews.get(context.data.id)
const { content } = plugin.area
Expand Down
5 changes: 3 additions & 2 deletions src/extensions/selectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ export function selectableNodes<T>(area: AreaPlugin<Schemes, T>, core: Selectabl
}
}

// eslint-disable-next-line max-statements
// eslint-disable-next-line max-statements, complexity
area.addPipe(context => {
if (!('type' in context)) return context
if (!context || typeof context !== 'object' || !('type' in context)) return context

if (context.type === 'nodepicked') {
const pickedId = context.data.id

Expand Down
18 changes: 7 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import { BaseSchemes, ConnectionId, NodeId, Root, Scope } from 'rete'

// type BaseSchemes, ConnectionId, NodeId, Root, Scope
import { Area, TranslateEventParams, ZoomEventParams } from './area'
import { ConnectionView } from './connection-view'
import { ElementsHolder } from './elements-holder'
import { NodeResizeEventParams, NodeTranslateEventParams, NodeView } from './node-view'
import { GetRenderTypes, Position } from './types'
import { GetRenderTypes, Position, RenderMeta, RenderSignal } from './types'

export { Area } from './area'
export { Drag } from './drag'
export * as AreaExtensions from './extensions'
export { NodeView } from './node-view'
export type { RenderSignal } from './types'
export type { PointerListener } from './utils'
export { usePointerListener } from './utils'
export { Zoom } from './zoom'

export type RenderMeta = { filled?: boolean }
export type RenderData<Schemes extends BaseSchemes> =
| { element: HTMLElement, type: 'node', payload: Schemes['Node'] }
| { element: HTMLElement, type: 'connection', payload: Schemes['Connection'], start?: Position, end?: Position }

export type Area2D<Schemes extends BaseSchemes> =
| { type: 'nodepicked', data: { id: string } }
| { type: 'nodetranslate', data: { id: string } & NodeTranslateEventParams }
| { type: 'nodetranslated', data: { id: string } & NodeTranslateEventParams }
| { type: 'translate', data: { position: Position } }
| { type: 'contextmenu', data: { event: MouseEvent, context: 'root' | Schemes['Node'] | Schemes['Connection'] } }
| { type: 'pointerdown', data: { position: Position, event: PointerEvent }}
| { type: 'pointermove', data: { position: Position, event: PointerEvent }}
Expand All @@ -32,8 +28,8 @@ export type Area2D<Schemes extends BaseSchemes> =
| { type: 'translated', data: TranslateEventParams }
| { type: 'zoom', data: ZoomEventParams }
| { type: 'zoomed', data: ZoomEventParams }
| { type: 'render', data: RenderData<Schemes> & RenderMeta }
| { type: 'rendered', data: RenderData<Schemes> & RenderMeta }
| RenderSignal<'node', { payload: Schemes['Node'] }>
| RenderSignal<'connection', { payload: Schemes['Connection'], start?: Position, end?: Position }>
| { type: 'unmount', data: { element: HTMLElement } }
| { type: 'nodedragged', data: Schemes['Node'] }
| { type: 'resized', data: { event: Event } }
Expand All @@ -47,7 +43,7 @@ export class AreaPlugin<Schemes extends BaseSchemes, ExtraSignals = never> exten
public nodeViews = new Map<NodeId, NodeView>()
public connectionViews = new Map<ConnectionId, ConnectionView>()
public area: Area
private elements = new ElementsHolder<HTMLElement, RenderData<Schemes> & RenderMeta>()
private elements = new ElementsHolder<HTMLElement, Extract<Area2D<Schemes>, { type: 'render' }>['data'] & RenderMeta>()

constructor(public container: HTMLElement) {
super('area')
Expand Down Expand Up @@ -162,7 +158,7 @@ export class AreaPlugin<Schemes extends BaseSchemes, ExtraSignals = never> exten
public async update(type: GetRenderTypes<Area2D<Schemes>> | GetRenderTypes<ExtraSignals>, id: string) {
const data = this.elements.get(type, id)

if (data) await this.emit({ type: 'render', data })
if (data) await this.emit({ type: 'render', data } as Area2D<Schemes>)
}

public async resize(id: NodeId, width: number, height: number) {
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ export type GetRenderTypes<Signals> = Extract<
Signals,
{ type: 'render', data: any }
> extends { type: 'render', data: { type: infer G } } ? (G extends string ? G : string) : string

export type RenderMeta = { filled?: boolean }
export type RenderSignal<Type extends string, Data> =
| { type: 'render', data: { element: HTMLElement, type: Type } & RenderMeta & Data }
| { type: 'rendered', data: { element: HTMLElement, type: Type } & Data }

0 comments on commit c6a7b0f

Please sign in to comment.