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

fix: update cli and fix linting errors #24

Merged
merged 2 commits into from
Aug 30, 2024
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
6 changes: 0 additions & 6 deletions .eslintrc

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ node_modules
npm-debug.log
dist
docs
/coverage
.rete-cli
.sonar
14 changes: 14 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import tseslint from 'typescript-eslint';
import configs from 'rete-cli/configs/eslint.mjs';
import gloals from 'globals'

export default tseslint.config(
...configs,
{
languageOptions: {
globals: {
...gloals.browser
}
}
}
)
4,021 changes: 2,129 additions & 1,892 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
"rete": "^2.0.0"
},
"devDependencies": {
"globals": "^15.9.0",
"rete": "^2.0.0",
"rete-cli": "^1.0.2",
"rete-cli": "~2.0.1",
"rollup-plugin-sass": "^0.9.2"
},
"dependencies": {
Expand Down
12 changes: 6 additions & 6 deletions src/area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
reordered: (element: HTMLElement) => Promise<unknown>
}
type Guards = {
translate: (params: TranslateEventParams) => Promise<unknown | boolean>
zoom: (params: ZoomEventParams) => Promise<unknown | boolean>
translate: (params: TranslateEventParams) => Promise<unknown>
zoom: (params: ZoomEventParams) => Promise<unknown>
}

export class Area {
Expand Down Expand Up @@ -111,12 +111,12 @@
}

private onTranslate = (x: number, y: number) => {
if (this.zoomHandler && this.zoomHandler.isTranslating()) return // lock translation while zoom on multitouch
this.translate(x, y)
if (this.zoomHandler?.isTranslating()) return // lock translation while zoom on multitouch
void this.translate(x, y)
}

private onZoom = (delta: number, ox: number, oy: number, source?: ZoomSource) => {
this.zoom(this.transform.k * (1 + delta), ox, oy, source)
void this.zoom(this.transform.k * (1 + delta), ox, oy, source)

this.update()
}
Expand All @@ -132,7 +132,7 @@
public async translate(x: number, y: number) {
type T = undefined | { data: TranslateEventParams }
const position = { x, y }
const result = await this.guards.translate({ previous: this.transform, position }) as T

Check warning on line 135 in src/area.ts

View workflow job for this annotation

GitHub Actions / ci / ci

The 'await this.guards.translate({ previous: this.transform, position }) as T' has unsafe 'as' type assertion

Check warning on line 135 in src/area.ts

View workflow job for this annotation

GitHub Actions / ci / ci

The 'await this.guards.translate({ previous: this.transform, position }) as T' has unsafe 'as' type assertion

if (!result) return false

Expand All @@ -158,11 +158,11 @@
public async zoom(zoom: number, ox = 0, oy = 0, source?: ZoomSource) {
type T = undefined | { data: ZoomEventParams }
const k = this.transform.k
const result = await this.guards.zoom({ previous: this.transform, zoom, source }) as T

Check warning on line 161 in src/area.ts

View workflow job for this annotation

GitHub Actions / ci / ci

The 'await this.guards.zoom({ previous: this.transform, zoom, source }) as T' has unsafe 'as' type assertion

Check warning on line 161 in src/area.ts

View workflow job for this annotation

GitHub Actions / ci / ci

The 'await this.guards.zoom({ previous: this.transform, zoom, source }) as T' has unsafe 'as' type assertion

if (!result) return true

const d = (k - result.data.zoom) / ((k - zoom) || 1)
const d = (k - result.data.zoom) / (k - zoom || 1)

this.transform.k = result.data.zoom || 1
this.transform.x += ox * d
Expand Down
1 change: 1 addition & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-invalid-void-type */
import { BaseSchemes, ConnectionId, NodeId, Root, Scope } from 'rete'

import { NodeResizeEventParams, NodeTranslateEventParams } from './node-view'
Expand Down
4 changes: 2 additions & 2 deletions src/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

type Events = {
start: (e: PointerEvent) => void
translate: (x: number, y: number, e: PointerEvent) => void
translate: (x: number, y: number, e: PointerEvent) => unknown
drag: (e: PointerEvent) => void
}

Expand All @@ -29,7 +29,7 @@
protected guards: Guards

constructor(guards?: Guards) {
this.guards = guards || {

Check warning on line 32 in src/drag.ts

View workflow job for this annotation

GitHub Actions / ci / ci

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 32 in src/drag.ts

View workflow job for this annotation

GitHub Actions / ci / ci

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
down: e => !(e.pointerType === 'mouse' && e.button !== 0),
move: () => true
}
Expand Down Expand Up @@ -69,7 +69,7 @@
const x = this.startPosition.x + delta.x / zoom
const y = this.startPosition.y + delta.y / zoom

this.events.translate(x, y, e)
void this.events.translate(x, y, e)
}

private up = (e: PointerEvent) => {
Expand Down
4 changes: 3 additions & 1 deletion src/extensions/bounding-box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
* @param nodes The nodes to get the bounding box of
* @returns The bounding box
*/
export function getBoundingBox<Schemes extends BaseSchemes, K>(plugin: BaseAreaPlugin<Schemes, K>, nodes: NodeRef<Schemes>[]) {

Check warning on line 14 in src/extensions/bounding-box.ts

View workflow job for this annotation

GitHub Actions / ci / ci

This line has a length of 127. Maximum allowed is 120

Check warning on line 14 in src/extensions/bounding-box.ts

View workflow job for this annotation

GitHub Actions / ci / ci

This line has a length of 127. Maximum allowed is 120
const editor = plugin.parentScope<NodeEditor<Schemes>>(NodeEditor)
const list = nodes.map(node => typeof node === 'object' ? node : editor.getNode(node))
const list = nodes.map(node => typeof node === 'object'
? node
: editor.getNode(node))
const rects = getNodesRect(list, plugin.nodeViews)

return getBBox(rects)
Expand Down
14 changes: 10 additions & 4 deletions src/extensions/restrictor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,21 @@ export type Params = {
*/
export function restrictor<Schemes extends BaseSchemes, K>(plugin: AreaPlugin<Schemes, K>, params?: Params) {
const scaling = params?.scaling
? params.scaling === true ? { min: 0.1, max: 1 } : params.scaling
? params.scaling === true
? { min: 0.1, max: 1 }
: params.scaling
: false
const translation = params?.translation
? params.translation === true ? { left: 0, top: 0, right: 1000, bottom: 1000 } : params.translation
? params.translation === true
? { left: 0, top: 0, right: 1000, bottom: 1000 }
: params.translation
: false

function restrictZoom(zoom: number) {
if (!scaling) throw new Error('scaling param isnt defined')
const { min, max } = typeof scaling === 'function' ? scaling() : scaling
const { min, max } = typeof scaling === 'function'
? scaling()
: scaling

if (zoom < min) {
return min
Expand Down Expand Up @@ -82,7 +88,7 @@ export function restrictor<Schemes extends BaseSchemes, K>(plugin: AreaPlugin<Sc
if (translation && context.type === 'zoomed') {
const position = restrictPosition(plugin.area.transform)

plugin.area.translate(position.x, position.y)
void plugin.area.translate(position.x, position.y)
}
if (translation && context.type === 'translate') {
return {
Expand Down
6 changes: 3 additions & 3 deletions src/extensions/selectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { BaseArea, BaseAreaPlugin } from '../base'

type Schemes = GetSchemes<BaseSchemes['Node'] & { selected?: boolean }, any>

Check warning on line 5 in src/extensions/selectable.ts

View workflow job for this annotation

GitHub Actions / ci / ci

Unexpected any. Specify a different type

Check warning on line 5 in src/extensions/selectable.ts

View workflow job for this annotation

GitHub Actions / ci / ci

Unexpected any. Specify a different type

/**
* Selector's accumulate function, activated when the ctrl key is pressed
Expand Down Expand Up @@ -61,11 +61,11 @@
}

unselectAll() {
[...Array.from(this.entities.values())].forEach(item => this.remove(item))

Check warning on line 64 in src/extensions/selectable.ts

View workflow job for this annotation

GitHub Actions / ci / ci

Returning a void expression from an arrow function shorthand is forbidden. Please add braces to the arrow function

Check warning on line 64 in src/extensions/selectable.ts

View workflow job for this annotation

GitHub Actions / ci / ci

Returning a void expression from an arrow function shorthand is forbidden. Please add braces to the arrow function
}

translate(dx: number, dy: number) {
this.entities.forEach(item => !this.isPicked(item) && item.translate(dx, dy))

Check warning on line 68 in src/extensions/selectable.ts

View workflow job for this annotation

GitHub Actions / ci / ci

Returning a void expression from an arrow function shorthand is forbidden. Please add braces to the arrow function

Check warning on line 68 in src/extensions/selectable.ts

View workflow job for this annotation

GitHub Actions / ci / ci

Returning a void expression from an arrow function shorthand is forbidden. Please add braces to the arrow function
}

pick(entity: Pick<E, 'label' | 'id'>) {
Expand Down Expand Up @@ -119,14 +119,14 @@
function selectNode(node: Schemes['Node']) {
if (!node.selected) {
node.selected = true
area.update('node', node.id)
void area.update('node', node.id)
}
}

function unselectNode(node: Schemes['Node']) {
if (node.selected) {
node.selected = false
area.update('node', node.id)
void area.update('node', node.id)
}
}
/**
Expand All @@ -147,7 +147,7 @@
const current = view?.position

if (current) {
view.translate(current.x + dx, current.y + dy)
void view.translate(current.x + dx, current.y + dy)
}
},
unselect() {
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BaseSchemes, GetSchemes } from 'rete'

export type NodeRef<Schemes extends BaseSchemes> = Schemes['Node'] | Schemes['Node']['id']
export type SchemesWithSizes = GetSchemes<
BaseSchemes['Node'] & { width?: number, height?: number },
BaseSchemes['Connection']
BaseSchemes['Node'] & { width?: number, height?: number },
BaseSchemes['Connection']
>

6 changes: 4 additions & 2 deletions src/extensions/show-input-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ export function showInputControl<S extends Scheme>(area: BaseAreaPlugin<BaseSche
return connection.target === target && connection.targetInput === targetInput
}))

input.showControl = visible ? visible({ hasAnyConnection, input }) : !hasAnyConnection
input.showControl = visible
? visible({ hasAnyConnection, input })
: !hasAnyConnection

if (input.showControl !== previous) {
area.update('node', node.id)
void area.update('node', node.id)
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/extensions/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ export type Params = {
*/
export function snapGrid<Schemes extends BaseSchemes, K>(base: BaseAreaPlugin<Schemes, K>, params?: Params) {
const area = base as BaseAreaPlugin<Schemes, BaseArea<Schemes>>
const size = typeof params?.size === 'undefined' ? 16 : params.size
const dynamic = typeof params?.dynamic === 'undefined' ? true : params.dynamic
const size = typeof params?.size === 'undefined'
? 16
: params.size
const dynamic = typeof params?.dynamic === 'undefined'
? true
: params.dynamic

function snap(value: number) {
return Math.round(value / size) * size
Expand All @@ -49,7 +53,7 @@ export function snapGrid<Schemes extends BaseSchemes, K>(base: BaseAreaPlugin<Sc
if (view) {
const { x, y } = view.position

view.translate(snap(x), snap(y))
void view.translate(snap(x), snap(y))
}
}
return context
Expand Down
4 changes: 3 additions & 1 deletion src/extensions/zoom-at.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export type Params = {
export async function zoomAt<Schemes extends SchemesWithSizes, K>(plugin: AreaPlugin<Schemes, K>, nodes: NodeRef<Schemes>[], params?: Params) {
const { scale = 0.9 } = params || {}
const editor = plugin.parentScope<NodeEditor<Schemes>>(NodeEditor)
const list = nodes.map(node => typeof node === 'object' ? node : editor.getNode(node))
const list = nodes.map(node => typeof node === 'object'
? node
: editor.getNode(node))
const rects = getNodesRect(list, plugin.nodeViews)
const boundingBox = getBoundingBox(rects)
const [w, h] = [plugin.container.clientWidth, plugin.container.clientHeight]
Expand Down
26 changes: 13 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ export class AreaPlugin<Schemes extends BaseSchemes, ExtraSignals = never> exten
container,
{
zoomed: params => this.emit({ type: 'zoomed', data: params }),
pointerDown: (position, event) => this.emit({ type: 'pointerdown', data: { position, event } }),
pointerMove: (position, event) => this.emit({ type: 'pointermove', data: { position, event } }),
pointerUp: (position, event) => this.emit({ type: 'pointerup', data: { position, event } }),
resize: event => this.emit({ type: 'resized', data: { event } }),
pointerDown: (position, event) => void this.emit({ type: 'pointerdown', data: { position, event } }),
pointerMove: (position, event) => void this.emit({ type: 'pointermove', data: { position, event } }),
pointerUp: (position, event) => void this.emit({ type: 'pointerup', data: { position, event } }),
resize: event => void this.emit({ type: 'resized', data: { event } }),
translated: params => this.emit({ type: 'translated', data: params }),
reordered: element => this.emit({ type: 'reordered', data: { element } })
},
Expand All @@ -96,18 +96,18 @@ export class AreaPlugin<Schemes extends BaseSchemes, ExtraSignals = never> exten
}

private onContextMenu = (event: MouseEvent) => {
this.emit({ type: 'contextmenu', data: { event, context: 'root' } })
void this.emit({ type: 'contextmenu', data: { event, context: 'root' } })
}

public addNodeView(node: Schemes['Node']) {
const { id } = node
const view = new NodeView(
() => this.area.transform.k,
{
picked: () => this.emit({ type: 'nodepicked', data: { id } }),
picked: () => void this.emit({ type: 'nodepicked', data: { id } }),
translated: data => this.emit({ type: 'nodetranslated', data: { id, ...data } }),
dragged: () => this.emit({ type: 'nodedragged', data: node }),
contextmenu: event => this.emit({ type: 'contextmenu', data: { event, context: node } }),
dragged: () => void this.emit({ type: 'nodedragged', data: node }),
contextmenu: event => void this.emit({ type: 'contextmenu', data: { event, context: node } }),
resized: ({ size }) => this.emit({ type: 'noderesized', data: { id: node.id, size } })
},
{
Expand All @@ -119,7 +119,7 @@ export class AreaPlugin<Schemes extends BaseSchemes, ExtraSignals = never> exten
this.nodeViews.set(id, view)
this.area.content.add(view.element)

this.emit({
void this.emit({
type: 'render',
data: { element: view.element, type: 'node', payload: node }
})
Expand All @@ -131,21 +131,21 @@ export class AreaPlugin<Schemes extends BaseSchemes, ExtraSignals = never> exten
const view = this.nodeViews.get(id)

if (view) {
this.emit({ type: 'unmount', data: { element: view.element } })
void this.emit({ type: 'unmount', data: { element: view.element } })
this.nodeViews.delete(id)
this.area.content.remove(view.element)
}
}

public addConnectionView(connection: Schemes['Connection']) {
const view = new ConnectionView({
contextmenu: event => this.emit({ type: 'contextmenu', data: { event, context: connection } })
contextmenu: event => void this.emit({ type: 'contextmenu', data: { event, context: connection } })
})

this.connectionViews.set(connection.id, view)
this.area.content.add(view.element)

this.emit({
void this.emit({
type: 'render',
data: { element: view.element, type: 'connection', payload: connection }
})
Expand All @@ -157,7 +157,7 @@ export class AreaPlugin<Schemes extends BaseSchemes, ExtraSignals = never> exten
const view = this.connectionViews.get(id)

if (view) {
this.emit({ type: 'unmount', data: { element: view.element } })
void this.emit({ type: 'unmount', data: { element: view.element } })
this.connectionViews.delete(id)
this.area.content.remove(view.element)
}
Expand Down
14 changes: 6 additions & 8 deletions src/node-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export type NodeResizeEventParams = { size: Size }

type Events = {
picked: () => void
translated: (params: NodeTranslateEventParams) => Promise<unknown | boolean>
translated: (params: NodeTranslateEventParams) => Promise<unknown>
dragged: () => void
contextmenu: (event: MouseEvent) => void
resized: (params: NodeResizeEventParams) => Promise<unknown | boolean>
resized: (params: NodeResizeEventParams) => Promise<unknown>
}
type Guards = {
resize: (params: NodeResizeEventParams) => Promise<unknown | boolean>
translate: (params: NodeTranslateEventParams) => Promise<unknown | boolean>
resize: (params: NodeResizeEventParams) => Promise<unknown>
translate: (params: NodeTranslateEventParams) => Promise<unknown>
}

export class NodeView {
Expand All @@ -25,7 +25,7 @@ export class NodeView {
this.element = document.createElement('div')
this.element.style.position = 'absolute'
this.position = { x: 0, y: 0 }
this.translate(0, 0)
void this.translate(0, 0)

this.element.addEventListener('contextmenu', event => this.events.contextmenu(event))

Expand All @@ -43,7 +43,6 @@ export class NodeView {
}
)
}

public translate = async (x: number, y: number) => {
type Params = undefined | { data: NodeTranslateEventParams }
const previous = { ...this.position }
Expand All @@ -58,11 +57,10 @@ export class NodeView {

return true
}

public resize = async (width: number, height: number) => {
const size = { width, height }

if (!(await this.guards.resize({ size }))) return false
if (!await this.guards.resize({ size })) return false

const el = this.element.querySelector('*:not(span):not([fragment])')

Expand Down
Loading
Loading