Skip to content
Merged
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
28 changes: 14 additions & 14 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 @@ -31,7 +31,7 @@
}
}

export type SelectorEntity = { label: string, id: string, unselect(): void, translate(dx: number, dy: number): void }
export type SelectorEntity = { label: string, id: string, unselect(): void | Promise<void>, translate(dx: number, dy: number): void }

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

View workflow job for this annotation

GitHub Actions / ci / ci

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

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

View workflow job for this annotation

GitHub Actions / ci / ci

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

/**
* Selector class. Used to collect selected entities (nodes, connections, etc.) and synchronize them (select, unselect, translate, etc.).
Expand All @@ -45,27 +45,27 @@
return this.entities.has(`${entity.label}_${entity.id}`)
}

add(entity: E, accumulate: boolean) {
if (!accumulate) this.unselectAll()
async add(entity: E, accumulate: boolean) {
if (!accumulate) await this.unselectAll()
this.entities.set(`${entity.label}_${entity.id}`, entity)
}

remove(entity: Pick<E, 'label' | 'id'>) {
async remove(entity: Pick<E, 'label' | 'id'>) {
const id = `${entity.label}_${entity.id}`
const item = this.entities.get(id)

if (item) {
this.entities.delete(id)
item.unselect()
await item.unselect()
}
}

unselectAll() {
[...Array.from(this.entities.values())].forEach(item => this.remove(item))
async unselectAll() {
await Promise.all([...Array.from(this.entities.values())].map(item => this.remove(item)))
}

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 @@ -134,12 +134,12 @@
* @param nodeId Node id
* @param accumulate Whether to accumulate nodes on selection
*/
function add(nodeId: NodeId, accumulate: boolean) {
async function add(nodeId: NodeId, accumulate: boolean) {
const node = getEditor().getNode(nodeId)

if (!node) return

core.add({
await core.add({
label: 'node',
id: node.id,
translate(dx, dy) {
Expand All @@ -160,12 +160,12 @@
* Unselect node programmatically
* @param nodeId Node id
*/
function remove(nodeId: NodeId) {
core.remove({ id: nodeId, label: 'node' })
async function remove(nodeId: NodeId) {
await core.remove({ id: nodeId, label: 'node' })
}

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

if (context.type === 'nodepicked') {
Expand All @@ -174,7 +174,7 @@

core.pick({ id: pickedId, label: 'node' })
twitch = null
add(pickedId, accumulate)
await add(pickedId, accumulate)
} else if (context.type === 'nodetranslated') {
const { id, position, previous } = context.data
const dx = position.x - previous.x
Expand All @@ -187,7 +187,7 @@
if (twitch !== null) twitch++
} else if (context.type === 'pointerup') {
if (twitch !== null && twitch < 4) {
core.unselectAll()
await core.unselectAll()
}
twitch = null
}
Expand Down
Loading