Various improvements

This commit is contained in:
barsdeveloper
2022-11-09 22:14:30 +01:00
parent 97a9c99732
commit aa7c9932f0
26 changed files with 293 additions and 103 deletions

View File

@@ -0,0 +1,13 @@
import IFocus from "./IFocus"
export default class FocusTextEdit extends IFocus {
focused() {
this.blueprint.dispatchEditTextEvent(true)
}
unfocused() {
document.getSelection()?.removeAllRanges() // Deselect eventually selected text inside the input
this.blueprint.dispatchEditTextEvent(false)
}
}

40
js/input/common/IFocus.js Normal file
View File

@@ -0,0 +1,40 @@
import IInput from "../IInput"
export default class IFocus extends IInput {
/** @type {(e: FocusEvent) => void} */
#focusHandler
/** @type {(e: FocusEvent) => void} */
#focusoutHandler
constructor(target, blueprint, options = {}) {
options.listenOnFocus ??= true
super(target, blueprint, options)
const self = this
this.#focusHandler = e => {
e.preventDefault()
this.focused()
}
this.#focusoutHandler = e => {
e.preventDefault()
this.unfocused()
}
}
listenEvents() {
this.target.addEventListener("focus", this.#focusHandler)
this.target.addEventListener("focusout", this.#focusoutHandler)
}
unlistenEvents() {
this.target.removeEventListener("focus", this.#focusHandler)
this.target.removeEventListener("focusout", this.#focusoutHandler)
}
focused() {
}
unfocused() {
}
}

View File

@@ -3,11 +3,22 @@ import IInput from "../IInput"
import ISerializer from "../../serialization/ISerializer"
import KeyBindingEntity from "../../entity/KeyBindingEntity"
/** @typedef {import("../../Blueprint").default} Blueprint */
/**
* @template {HTMLElement} T
* @extends IInput<T>
*/
export default class IKeyboardShortcut extends IInput {
/** @type {KeyBindingEntity[]} */
#activationKeys
/**
* @param {T} target
* @param {Blueprint} blueprint
* @param {Object} options
*/
constructor(target, blueprint, options = {}) {
options.activateAnyKey ??= false
options.activationKeys ??= []

View File

@@ -0,0 +1,41 @@
import IKeyboardShortcut from "./IKeyboardShortcut"
/** @typedef {import("../../Blueprint").default} Blueprint */
/**
* @template {HTMLElement} T
* @extends IKeyboardShortcut<T>
*/
export default class KeyboardShortcutAction extends IKeyboardShortcut {
static #ignoreEvent =
/** @param {KeyboardShortcutAction} self */
self => { }
/**
* @param {T} target
* @param {Blueprint} blueprint
* @param {Object} options
* @param {(self: KeyboardShortcutAction<T>) => void} onKeyDown
* @param {(self: KeyboardShortcutAction<T>) => void} onKeyUp
*/
constructor(
target,
blueprint,
options,
onKeyDown = KeyboardShortcutAction.#ignoreEvent,
onKeyUp = KeyboardShortcutAction.#ignoreEvent
) {
super(target, blueprint, options)
this.onKeyDown = onKeyDown
this.onKeyUp = onKeyUp
}
fire() {
this.onKeyDown(this)
}
unfire() {
this.onKeyUp(this)
}
}