mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-03-02 05:04:43 +08:00
Refactoring
This commit is contained in:
62
js/input/Context.js
Normal file → Executable file
62
js/input/Context.js
Normal file → Executable file
@@ -1,31 +1,31 @@
|
||||
export default class Context {
|
||||
|
||||
constructor(target, blueprint, options) {
|
||||
/** @type {HTMLElement} */
|
||||
this.target = target
|
||||
/** @type {import("../Blueprint").default}" */
|
||||
this.blueprint = blueprint
|
||||
this.options = options
|
||||
if (options?.wantsFocusCallback ?? false) {
|
||||
let self = this
|
||||
this.blueprintfocusHandler = _ => self.blueprintFocused()
|
||||
this.blueprintunfocusHandler = _ => self.blueprintUnfocused()
|
||||
this.blueprint.addEventListener("blueprintfocus", this.blueprintfocusHandler)
|
||||
this.blueprint.addEventListener("blueprintunfocus", this.blueprintunfocusHandler)
|
||||
}
|
||||
}
|
||||
|
||||
unlistenDOMElement() {
|
||||
this.blueprintUnfocused()
|
||||
this.blueprint.removeEventListener("blueprintfocus", this.blueprintfocusHandler)
|
||||
this.blueprint.removeEventListener("blueprintunfocus", this.blueprintunfocusHandler)
|
||||
}
|
||||
|
||||
|
||||
/* Subclasses will probabily override the following methods */
|
||||
blueprintFocused() {
|
||||
}
|
||||
|
||||
blueprintUnfocused() {
|
||||
}
|
||||
}
|
||||
export default class Context {
|
||||
|
||||
constructor(target, blueprint, options) {
|
||||
/** @type {HTMLElement} */
|
||||
this.target = target
|
||||
/** @type {import("../Blueprint").default}" */
|
||||
this.blueprint = blueprint
|
||||
this.options = options
|
||||
let self = this
|
||||
this.blueprintFocusHandler = _ => self.blueprintFocused()
|
||||
this.blueprintUnfocusHandler = _ => self.blueprintUnfocused()
|
||||
if (options?.wantsFocusCallback ?? false) {
|
||||
this.blueprint.addEventListener("blueprint-focus", this.blueprintFocusHandler)
|
||||
this.blueprint.addEventListener("blueprint-unfocus", this.blueprintUnfocusHandler)
|
||||
}
|
||||
}
|
||||
|
||||
unlistenDOMElement() {
|
||||
this.blueprintUnfocused()
|
||||
this.blueprint.removeEventListener("blueprint-focus", this.blueprintFocusHandler)
|
||||
this.blueprint.removeEventListener("blueprint-unfocus", this.blueprintUnfocusHandler)
|
||||
}
|
||||
|
||||
|
||||
/* Subclasses will probabily override the following methods */
|
||||
blueprintFocused() {
|
||||
}
|
||||
|
||||
blueprintUnfocused() {
|
||||
}
|
||||
}
|
||||
|
||||
52
js/input/Copy.js
Normal file → Executable file
52
js/input/Copy.js
Normal file → Executable file
@@ -1,26 +1,26 @@
|
||||
import Context from "./Context"
|
||||
import ObjectSerializer from "../serialization/ObjectSerializer"
|
||||
|
||||
export default class Copy extends Context {
|
||||
|
||||
constructor(target, blueprint, options = {}) {
|
||||
options.wantsFocusCallback = true
|
||||
super(target, blueprint, options)
|
||||
this.serializer = new ObjectSerializer()
|
||||
let self = this
|
||||
this.copyHandle = _ => self.copied()
|
||||
}
|
||||
|
||||
blueprintFocused() {
|
||||
document.body.addEventListener("copy", this.copyHandle)
|
||||
}
|
||||
|
||||
blueprintUnfocused() {
|
||||
document.body.removeEventListener("copy", this.copyHandle)
|
||||
}
|
||||
|
||||
copied() {
|
||||
const value = this.blueprint.getNodes(true).map(node => this.serializer.write(node.entity)).join("\n")
|
||||
navigator.clipboard.writeText(value)
|
||||
}
|
||||
}
|
||||
import Context from "./Context"
|
||||
import ObjectSerializer from "../serialization/ObjectSerializer"
|
||||
|
||||
export default class Copy extends Context {
|
||||
|
||||
constructor(target, blueprint, options = {}) {
|
||||
options.wantsFocusCallback = true
|
||||
super(target, blueprint, options)
|
||||
this.serializer = new ObjectSerializer()
|
||||
let self = this
|
||||
this.copyHandler = _ => self.copied()
|
||||
}
|
||||
|
||||
blueprintFocused() {
|
||||
document.body.addEventListener("copy", this.copyHandler)
|
||||
}
|
||||
|
||||
blueprintUnfocused() {
|
||||
document.body.removeEventListener("copy", this.copyHandler)
|
||||
}
|
||||
|
||||
copied() {
|
||||
const value = this.blueprint.getNodes(true).map(node => this.serializer.write(node.entity)).join("\n")
|
||||
navigator.clipboard.writeText(value)
|
||||
}
|
||||
}
|
||||
|
||||
40
js/input/KeyboardCanc.js
Normal file → Executable file
40
js/input/KeyboardCanc.js
Normal file → Executable file
@@ -1,21 +1,21 @@
|
||||
import KeyboardShortcut from "./KeyboardShortcut"
|
||||
import Configuration from "../Configuration"
|
||||
|
||||
|
||||
export default class KeyvoardCanc extends KeyboardShortcut {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {HTMLElement} target
|
||||
* @param {import("../Blueprint").default} blueprint
|
||||
* @param {OBject} options
|
||||
*/
|
||||
constructor(target, blueprint, options = {}) {
|
||||
options = KeyboardShortcut.keyOptionsParse(options, Configuration.deleteNodesKeyboardKey)
|
||||
super(target, blueprint, options)
|
||||
}
|
||||
|
||||
fire() {
|
||||
this.blueprint.removeGraphElement(...this.blueprint.getNodes(true))
|
||||
}
|
||||
import KeyboardShortcut from "./KeyboardShortcut"
|
||||
import Configuration from "../Configuration"
|
||||
|
||||
|
||||
export default class KeyvoardCanc extends KeyboardShortcut {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {HTMLElement} target
|
||||
* @param {import("../Blueprint").default} blueprint
|
||||
* @param {OBject} options
|
||||
*/
|
||||
constructor(target, blueprint, options = {}) {
|
||||
options = KeyboardShortcut.keyOptionsParse(options, Configuration.deleteNodesKeyboardKey)
|
||||
super(target, blueprint, options)
|
||||
}
|
||||
|
||||
fire() {
|
||||
this.blueprint.removeGraphElement(...this.blueprint.getNodes(true))
|
||||
}
|
||||
}
|
||||
100
js/input/KeyboardShortcut.js
Normal file → Executable file
100
js/input/KeyboardShortcut.js
Normal file → Executable file
@@ -1,50 +1,50 @@
|
||||
import Context from "./Context"
|
||||
|
||||
export default class KeyboardShortcut extends Context {
|
||||
|
||||
constructor(target, blueprint, options = {}) {
|
||||
options.wantsFocusCallback = true
|
||||
super(target, blueprint, options)
|
||||
|
||||
/** @type {String[]} */
|
||||
this.key = this.options.key
|
||||
this.ctrlKey = options.ctrlKey ?? false
|
||||
this.shiftKey = options.shiftKey ?? false
|
||||
this.altKey = options.altKey ?? false
|
||||
this.metaKey = options.metaKey ?? false
|
||||
|
||||
let self = this
|
||||
this.keyDownHandler = e => {
|
||||
if (
|
||||
e.code == self.key
|
||||
&& e.ctrlKey === self.ctrlKey
|
||||
&& e.shiftKey === self.shiftKey
|
||||
&& e.altKey === self.altKey
|
||||
&& e.metaKey === self.metaKey
|
||||
) {
|
||||
self.fire()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {String} keyString
|
||||
* @returns {Object}
|
||||
*/
|
||||
static keyOptionsParse(options, keyString) {
|
||||
options.key = keyString
|
||||
return options
|
||||
}
|
||||
|
||||
blueprintFocused() {
|
||||
document.addEventListener("keydown", this.keyDownHandler)
|
||||
}
|
||||
|
||||
blueprintUnfocused() {
|
||||
document.removeEventListener("keydown", this.keyDownHandler)
|
||||
}
|
||||
|
||||
fire() {
|
||||
}
|
||||
}
|
||||
import Context from "./Context"
|
||||
|
||||
export default class KeyboardShortcut extends Context {
|
||||
|
||||
constructor(target, blueprint, options = {}) {
|
||||
options.wantsFocusCallback = true
|
||||
super(target, blueprint, options)
|
||||
|
||||
/** @type {String[]} */
|
||||
this.key = this.options.key
|
||||
this.ctrlKey = options.ctrlKey ?? false
|
||||
this.shiftKey = options.shiftKey ?? false
|
||||
this.altKey = options.altKey ?? false
|
||||
this.metaKey = options.metaKey ?? false
|
||||
|
||||
let self = this
|
||||
this.keyDownHandler = e => {
|
||||
if (
|
||||
e.code == self.key
|
||||
&& e.ctrlKey === self.ctrlKey
|
||||
&& e.shiftKey === self.shiftKey
|
||||
&& e.altKey === self.altKey
|
||||
&& e.metaKey === self.metaKey
|
||||
) {
|
||||
self.fire()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {String} keyString
|
||||
* @returns {Object}
|
||||
*/
|
||||
static keyOptionsParse(options, keyString) {
|
||||
options.key = keyString
|
||||
return options
|
||||
}
|
||||
|
||||
blueprintFocused() {
|
||||
document.addEventListener("keydown", this.keyDownHandler)
|
||||
}
|
||||
|
||||
blueprintUnfocused() {
|
||||
document.removeEventListener("keydown", this.keyDownHandler)
|
||||
}
|
||||
|
||||
fire() {
|
||||
}
|
||||
}
|
||||
|
||||
44
js/input/MouseTracking.js
Normal file → Executable file
44
js/input/MouseTracking.js
Normal file → Executable file
@@ -1,22 +1,22 @@
|
||||
import Pointing from "./Pointing"
|
||||
|
||||
export default class MouseTracking extends Pointing {
|
||||
|
||||
constructor(target, blueprint, options = {}) {
|
||||
options.wantsFocusCallback = true
|
||||
super(target, blueprint, options)
|
||||
|
||||
let self = this
|
||||
this.mousemoveHandler = e => {
|
||||
self.blueprint.entity.mousePosition = self.getLocation(e)
|
||||
}
|
||||
}
|
||||
|
||||
blueprintFocused() {
|
||||
this.target.addEventListener("mousemove", this.mousemoveHandler)
|
||||
}
|
||||
|
||||
blueprintUnfocused() {
|
||||
this.target.removeEventListener("mousemove", this.mousemoveHandler)
|
||||
}
|
||||
}
|
||||
import Pointing from "./Pointing"
|
||||
|
||||
export default class MouseTracking extends Pointing {
|
||||
|
||||
constructor(target, blueprint, options = {}) {
|
||||
options.wantsFocusCallback = true
|
||||
super(target, blueprint, options)
|
||||
|
||||
let self = this
|
||||
this.mousemoveHandler = e => {
|
||||
self.blueprint.entity.mousePosition = self.getLocation(e)
|
||||
}
|
||||
}
|
||||
|
||||
blueprintFocused() {
|
||||
this.target.addEventListener("mousemove", this.mousemoveHandler)
|
||||
}
|
||||
|
||||
blueprintUnfocused() {
|
||||
this.target.removeEventListener("mousemove", this.mousemoveHandler)
|
||||
}
|
||||
}
|
||||
|
||||
100
js/input/Paste.js
Normal file → Executable file
100
js/input/Paste.js
Normal file → Executable file
@@ -1,50 +1,50 @@
|
||||
import GraphNode from "../graph/GraphNode"
|
||||
import ObjectSerializer from "../serialization/ObjectSerializer"
|
||||
import Context from "./Context"
|
||||
|
||||
export default class Paste extends Context {
|
||||
|
||||
constructor(target, blueprint, options = {}) {
|
||||
options.wantsFocusCallback = true
|
||||
super(target, blueprint, options)
|
||||
this.serializer = new ObjectSerializer()
|
||||
let self = this
|
||||
this.pasteHandle = e => self.pasted(e.clipboardData.getData("Text"))
|
||||
}
|
||||
|
||||
blueprintFocused() {
|
||||
document.body.addEventListener("paste", this.pasteHandle)
|
||||
}
|
||||
|
||||
blueprintUnfocused() {
|
||||
document.body.removeEventListener("paste", this.pasteHandle)
|
||||
}
|
||||
|
||||
pasted(value) {
|
||||
let top = 0
|
||||
let left = 0
|
||||
let count = 0
|
||||
let nodes = this.serializer.readMultiple(value).map(entity => {
|
||||
let node = new GraphNode(entity)
|
||||
top += node.location[1]
|
||||
left += node.location[0]
|
||||
++count
|
||||
return node
|
||||
})
|
||||
top /= count
|
||||
left /= count
|
||||
if (nodes.length > 0) {
|
||||
this.blueprint.unselectAll()
|
||||
}
|
||||
let mousePosition = this.blueprint.entity.mousePosition
|
||||
this.blueprint.addGraphElement(...nodes)
|
||||
nodes.forEach(node => {
|
||||
const locationOffset = [
|
||||
mousePosition[0] - left,
|
||||
mousePosition[1] - top,
|
||||
]
|
||||
node.addLocation(this.blueprint.compensateTranslation(locationOffset))
|
||||
node.setSelected(true)
|
||||
})
|
||||
}
|
||||
}
|
||||
import GraphNode from "../graph/GraphNode"
|
||||
import ObjectSerializer from "../serialization/ObjectSerializer"
|
||||
import Context from "./Context"
|
||||
|
||||
export default class Paste extends Context {
|
||||
|
||||
constructor(target, blueprint, options = {}) {
|
||||
options.wantsFocusCallback = true
|
||||
super(target, blueprint, options)
|
||||
this.serializer = new ObjectSerializer()
|
||||
let self = this
|
||||
this.pasteHandle = e => self.pasted(e.clipboardData.getData("Text"))
|
||||
}
|
||||
|
||||
blueprintFocused() {
|
||||
document.body.addEventListener("paste", this.pasteHandle)
|
||||
}
|
||||
|
||||
blueprintUnfocused() {
|
||||
document.body.removeEventListener("paste", this.pasteHandle)
|
||||
}
|
||||
|
||||
pasted(value) {
|
||||
let top = 0
|
||||
let left = 0
|
||||
let count = 0
|
||||
let nodes = this.serializer.readMultiple(value).map(entity => {
|
||||
let node = new GraphNode(entity)
|
||||
top += node.location[1]
|
||||
left += node.location[0]
|
||||
++count
|
||||
return node
|
||||
})
|
||||
top /= count
|
||||
left /= count
|
||||
if (nodes.length > 0) {
|
||||
this.blueprint.unselectAll()
|
||||
}
|
||||
let mousePosition = this.blueprint.entity.mousePosition
|
||||
this.blueprint.addGraphElement(...nodes)
|
||||
nodes.forEach(node => {
|
||||
const locationOffset = [
|
||||
mousePosition[0] - left,
|
||||
mousePosition[1] - top,
|
||||
]
|
||||
node.addLocation(this.blueprint.compensateTranslation(locationOffset))
|
||||
node.setSelected(true)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
70
js/input/Unfocus.js
Normal file → Executable file
70
js/input/Unfocus.js
Normal file → Executable file
@@ -1,35 +1,35 @@
|
||||
import Context from "./Context"
|
||||
|
||||
export default class Unfocus extends Context {
|
||||
|
||||
constructor(target, blueprint, options = {}) {
|
||||
options.wantsFocusCallback = true
|
||||
super(target, blueprint, options)
|
||||
|
||||
let self = this
|
||||
this.clickHandler = e => self.clickedSomewhere(e)
|
||||
if (this.blueprint.focuse) {
|
||||
document.addEventListener("click", this.clickHandler)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {MouseEvent} e
|
||||
*/
|
||||
clickedSomewhere(e) {
|
||||
// If target is inside the blueprint grid
|
||||
if (e.target.closest("ueb-blueprint")) {
|
||||
return
|
||||
}
|
||||
this.blueprint.setFocused(false)
|
||||
}
|
||||
|
||||
blueprintFocused() {
|
||||
document.addEventListener("click", this.clickHandler)
|
||||
}
|
||||
|
||||
blueprintUnfocused() {
|
||||
document.removeEventListener("click", this.clickHandler)
|
||||
}
|
||||
}
|
||||
import Context from "./Context"
|
||||
|
||||
export default class Unfocus extends Context {
|
||||
|
||||
constructor(target, blueprint, options = {}) {
|
||||
options.wantsFocusCallback = true
|
||||
super(target, blueprint, options)
|
||||
|
||||
let self = this
|
||||
this.clickHandler = e => self.clickedSomewhere(e)
|
||||
if (this.blueprint.focuse) {
|
||||
document.addEventListener("click", this.clickHandler)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {MouseEvent} e
|
||||
*/
|
||||
clickedSomewhere(e) {
|
||||
// If target is inside the blueprint grid
|
||||
if (e.target.closest("ueb-blueprint")) {
|
||||
return
|
||||
}
|
||||
this.blueprint.setFocused(false)
|
||||
}
|
||||
|
||||
blueprintFocused() {
|
||||
document.addEventListener("click", this.clickHandler)
|
||||
}
|
||||
|
||||
blueprintUnfocused() {
|
||||
document.removeEventListener("click", this.clickHandler)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user