Files
ueblueprint/js/input/common/Paste.js
barsdeveloper a5813d0b4d Niagara and Metasound nodes WIP
* Keep track of entities

* Fix renaming

* Niagara variables wip

* Several niagara decode and test

* Move nodeTemplate code to dedicated file, self node added

* Move node decoding functions to dedicated files

* Move pin decoding logic to dedicated files

* Accept space separated keys in objects

* Build

* Prevent a crash in case of incomplete object

* Avoid creating objects unnecessarily

* types formatting

* Initial metasound style

* Common pcg nodes colors

* Fix string serialization

* Metasound new styles and fixes

* More metasound styles and colors

* WIP

* Several fixes

* More tests and fixes

* Clean gitignore
2024-05-20 12:56:36 +02:00

68 lines
2.0 KiB
JavaScript
Executable File

import ElementFactory from "../../element/ElementFactory.js"
import IInput from "../IInput.js"
import ObjectSerializer from "../../serialization/ObjectSerializer.js"
/**
* @typedef {import("../IInput.js").Options & {
* listenOnFocus?: Boolean,
* unlistenOnTextEdit?: Boolean,
* }} Options
*/
export default class Paste extends IInput {
static #serializer = new ObjectSerializer()
/** @type {(e: ClipboardEvent) => void} */
#pasteHandle
/**
* @param {Element} target
* @param {Blueprint} blueprint
* @param {Options} options
*/
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() {
window.addEventListener("paste", this.#pasteHandle)
}
unlistenEvents() {
window.removeEventListener("paste", this.#pasteHandle)
}
/** @param {String} value */
pasted(value) {
let top = 0
let left = 0
let count = 0
let nodes = Paste.#serializer.readMultiple(value).map(entity => {
let node = /** @type {NodeElementConstructor} */(ElementFactory.getConstructor("ueb-node"))
.newObject(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 => {
node.addLocation(mousePosition[0] - left, mousePosition[1] - top)
node.snapToGrid()
node.setSelected(true)
})
this.blueprint.addGraphElement(...nodes)
return nodes
}
}