Avoid using arrays when unnecessary

This commit is contained in:
barsdeveloper
2023-01-04 20:52:09 +01:00
parent 39cade4879
commit 3c5017de91
29 changed files with 570 additions and 426 deletions

View File

@@ -30,6 +30,7 @@ export default class Paste extends IInput {
window.removeEventListener("paste", this.#pasteHandle)
}
/** @param {String} value */
pasted(value) {
let top = 0
let left = 0
@@ -50,11 +51,7 @@ export default class Paste extends IInput {
}
let mousePosition = this.blueprint.mousePosition
nodes.forEach(node => {
const locationOffset = [
mousePosition[0] - left,
mousePosition[1] - top,
]
node.addLocation(locationOffset)
node.addLocation(mousePosition[0] - left, mousePosition[1] - top)
node.snapToGrid()
node.setSelected(true)
})

View File

@@ -14,96 +14,92 @@ import Utility from "../../Utility"
*/
export default class IMouseClickDrag extends IPointing {
#mouseDownHandler =
/** @param {MouseEvent} e */
e => {
this.blueprint.setFocused(true)
switch (e.button) {
case this.options.clickButton:
// Either doesn't matter or consider the click only when clicking on the parent, not descandants
if (!this.options.strictTarget || e.target == e.currentTarget) {
if (this.options.consumeEvent) {
e.stopImmediatePropagation() // Captured, don't call anyone else
}
// Attach the listeners
this.#movementListenedElement.addEventListener("mousemove", this.#mouseStartedMovingHandler)
document.addEventListener("mouseup", this.#mouseUpHandler)
this.clickedPosition = this.locationFromEvent(e)
this.blueprint.mousePosition[0] = this.clickedPosition[0]
this.blueprint.mousePosition[1] = this.clickedPosition[1]
if (this.target instanceof IDraggableElement) {
this.clickedOffset = [
this.clickedPosition[0] - this.target.locationX,
this.clickedPosition[1] - this.target.locationY,
]
}
this.clicked(this.clickedPosition)
/** @param {MouseEvent} e */
#mouseDownHandler = e => {
this.blueprint.setFocused(true)
switch (e.button) {
case this.options.clickButton:
// Either doesn't matter or consider the click only when clicking on the parent, not descandants
if (!this.options.strictTarget || e.target == e.currentTarget) {
if (this.options.consumeEvent) {
e.stopImmediatePropagation() // Captured, don't call anyone else
}
break
default:
if (!this.options.exitAnyButton) {
this.#mouseUpHandler(e)
// Attach the listeners
this.#movementListenedElement.addEventListener("mousemove", this.#mouseStartedMovingHandler)
document.addEventListener("mouseup", this.#mouseUpHandler)
this.clickedPosition = this.locationFromEvent(e)
this.blueprint.mousePosition[0] = this.clickedPosition[0]
this.blueprint.mousePosition[1] = this.clickedPosition[1]
if (this.target instanceof IDraggableElement) {
this.clickedOffset = [
this.clickedPosition[0] - this.target.locationX,
this.clickedPosition[1] - this.target.locationY,
]
}
break
}
this.clicked(this.clickedPosition)
}
break
default:
if (!this.options.exitAnyButton) {
this.#mouseUpHandler(e)
}
break
}
}
#mouseStartedMovingHandler =
/** @param {MouseEvent} e */
e => {
/** @param {MouseEvent} e */
#mouseStartedMovingHandler = e => {
if (this.options.consumeEvent) {
e.stopImmediatePropagation() // Captured, don't call anyone else
}
// Delegate from now on to this.#mouseMoveHandler
this.#movementListenedElement.removeEventListener("mousemove", this.#mouseStartedMovingHandler)
this.#movementListenedElement.addEventListener("mousemove", this.#mouseMoveHandler)
// Handler calls e.preventDefault() when it receives the event, this means dispatchEvent returns false
const dragEvent = this.getEvent(Configuration.trackingMouseEventName.begin)
this.#trackingMouse = this.target.dispatchEvent(dragEvent) == false
const location = this.locationFromEvent(e)
// Do actual actions
this.lastLocation = Utility.snapToGrid(this.clickedPosition[0], this.clickedPosition[1], this.stepSize)
this.startDrag(location)
this.started = true
}
/** @param {MouseEvent} e */
#mouseMoveHandler = e => {
if (this.options.consumeEvent) {
e.stopImmediatePropagation() // Captured, don't call anyone else
}
const location = this.locationFromEvent(e)
const movement = [e.movementX, e.movementY]
this.dragTo(location, movement)
if (this.#trackingMouse) {
this.blueprint.mousePosition = this.locationFromEvent(e)
}
}
/** @param {MouseEvent} e */
#mouseUpHandler = e => {
if (!this.options.exitAnyButton || e.button == this.options.clickButton) {
if (this.options.consumeEvent) {
e.stopImmediatePropagation() // Captured, don't call anyone else
}
// Delegate from now on to this.#mouseMoveHandler
// Remove the handlers of "mousemove" and "mouseup"
this.#movementListenedElement.removeEventListener("mousemove", this.#mouseStartedMovingHandler)
this.#movementListenedElement.addEventListener("mousemove", this.#mouseMoveHandler)
// Handler calls e.preventDefault() when it receives the event, this means dispatchEvent returns false
const dragEvent = this.getEvent(Configuration.trackingMouseEventName.begin)
this.#trackingMouse = this.target.dispatchEvent(dragEvent) == false
const location = this.locationFromEvent(e)
// Do actual actions
this.lastLocation = Utility.snapToGrid(this.clickedPosition, this.stepSize)
this.startDrag(location)
this.started = true
}
#mouseMoveHandler =
/** @param {MouseEvent} e */
e => {
if (this.options.consumeEvent) {
e.stopImmediatePropagation() // Captured, don't call anyone else
this.#movementListenedElement.removeEventListener("mousemove", this.#mouseMoveHandler)
document.removeEventListener("mouseup", this.#mouseUpHandler)
if (this.started) {
this.endDrag()
}
const location = this.locationFromEvent(e)
const movement = [e.movementX, e.movementY]
this.dragTo(location, movement)
this.unclicked()
if (this.#trackingMouse) {
this.blueprint.mousePosition = this.locationFromEvent(e)
}
}
#mouseUpHandler =
/** @param {MouseEvent} e */
e => {
if (!this.options.exitAnyButton || e.button == this.options.clickButton) {
if (this.options.consumeEvent) {
e.stopImmediatePropagation() // Captured, don't call anyone else
}
// Remove the handlers of "mousemove" and "mouseup"
this.#movementListenedElement.removeEventListener("mousemove", this.#mouseStartedMovingHandler)
this.#movementListenedElement.removeEventListener("mousemove", this.#mouseMoveHandler)
document.removeEventListener("mouseup", this.#mouseUpHandler)
if (this.started) {
this.endDrag()
}
this.unclicked()
if (this.#trackingMouse) {
const dragEvent = this.getEvent(Configuration.trackingMouseEventName.end)
this.target.dispatchEvent(dragEvent)
this.#trackingMouse = false
}
this.started = false
const dragEvent = this.getEvent(Configuration.trackingMouseEventName.end)
this.target.dispatchEvent(dragEvent)
this.#trackingMouse = false
}
this.started = false
}
}
#trackingMouse = false
#movementListenedElement

View File

@@ -25,6 +25,6 @@ export default class IPointing extends IInput {
)
return this.options.ignoreTranslateCompensate
? location
: this.blueprint.compensateTranslation(location)
: this.blueprint.compensateTranslation(location[0], location[1])
}
}

View File

@@ -30,8 +30,9 @@ export default class MouseClickDrag extends MouseMoveDraggable {
}
}
clicked() {
super.clicked()
/** @param {[Number, Number]} location */
clicked(location) {
super.clicked(location)
this.#onClicked?.()
}

View File

@@ -39,7 +39,7 @@ export default class MouseCreateLink extends IMouseClickDrag {
this.link.setMessageDirectionsIncompatible()
} else if (a.isOutput() == b.isOutput()) {
this.link.setMessageDirectionsIncompatible()
} else if (this.blueprint.getLinks([a, b]).length) {
} else if (this.blueprint.getLinks(a, b).length) {
this.link.setMessageReplaceLink()
this.linkValid = true
} else {

View File

@@ -12,23 +12,31 @@ import Utility from "../../Utility"
*/
export default class MouseMoveDraggable extends IMouseClickDrag {
/** @param {[Number, Number]} location */
clicked(location) {
if (this.options.repositionOnClick) {
this.target.setLocation(this.stepSize > 1
? Utility.snapToGrid(location, this.stepSize)
this.target.setLocation(...(this.stepSize > 1
? Utility.snapToGrid(location[0], location[1], this.stepSize)
: location
)
))
this.clickedOffset = [0, 0]
}
}
/**
* @param {Number[]} location
* @param {Number[]} offset
*/
dragTo(location, offset) {
const targetLocation = [
this.target.locationX ?? this.lastLocation[0],
this.target.locationY ?? this.lastLocation[1],
]
const [adjustedLocation, adjustedTargetLocation] = this.stepSize > 1
? [Utility.snapToGrid(location, this.stepSize), Utility.snapToGrid(targetLocation, this.stepSize)]
? [
Utility.snapToGrid(location[0], location[1], this.stepSize),
Utility.snapToGrid(targetLocation[0], targetLocation[1], this.stepSize)
]
: [location, targetLocation]
offset = [
adjustedLocation[0] - this.lastLocation[0],
@@ -45,10 +53,11 @@ export default class MouseMoveDraggable extends IMouseClickDrag {
this.lastLocation = adjustedLocation
}
/**
* @param {Number[]} location
* @param {Number[]} offset
*/
dragAction(location, offset) {
this.target.setLocation([
location[0] - this.clickedOffset[0],
location[1] - this.clickedOffset[1],
])
this.target.setLocation(location[0] - this.clickedOffset[0], location[1] - this.clickedOffset[1])
}
}

View File

@@ -7,7 +7,7 @@ export default class MouseScrollGraph extends IMouseClickDrag {
}
dragTo(location, movement) {
this.blueprint.scrollDelta([-movement[0], -movement[1]])
this.blueprint.scrollDelta(-movement[0], -movement[1])
}
endDrag() {