mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-14 00:54:48 +08:00
58 lines
1.8 KiB
JavaScript
Executable File
58 lines
1.8 KiB
JavaScript
Executable File
import IInput from "../IInput"
|
|
import NodeElement from "../../element/NodeElement"
|
|
import ObjectSerializer from "../../serialization/ObjectSerializer"
|
|
|
|
export default class Paste extends IInput {
|
|
|
|
static #serializer = new ObjectSerializer()
|
|
|
|
/** @type {(e: ClipboardEvent) => void} */
|
|
#pasteHandle
|
|
|
|
constructor(target, blueprint, options = {}) {
|
|
options.listenOnFocus ??= true
|
|
options.unlistenOnTextEdit ??= true // No nodes paste if inside a text field, just text (default behavior)
|
|
super(target, blueprint, options)
|
|
let self = this
|
|
this.#pasteHandle = e => self.pasted(e.clipboardData.getData("Text"))
|
|
}
|
|
|
|
listenEvents() {
|
|
document.body.addEventListener("paste", this.#pasteHandle)
|
|
}
|
|
|
|
unlistenEvents() {
|
|
document.body.removeEventListener("paste", this.#pasteHandle)
|
|
}
|
|
|
|
pasted(value) {
|
|
let top = 0
|
|
let left = 0
|
|
let count = 0
|
|
let nodes = Paste.#serializer.readMultiple(value).map(entity => {
|
|
let node = new NodeElement(entity)
|
|
top += node.locationY
|
|
left += node.locationX
|
|
++count
|
|
return node
|
|
})
|
|
top /= count
|
|
left /= count
|
|
if (nodes.length > 0) {
|
|
this.blueprint.unselectAll()
|
|
}
|
|
let mousePosition = this.blueprint.mousePosition
|
|
nodes.forEach(node => {
|
|
const locationOffset = [
|
|
mousePosition[0] - left,
|
|
mousePosition[1] - top,
|
|
]
|
|
node.addLocation(locationOffset)
|
|
node.snapToGrid()
|
|
node.setSelected(true)
|
|
})
|
|
this.blueprint.addGraphElement(...nodes)
|
|
return true
|
|
}
|
|
}
|