mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-04 08:50:33 +08:00
Input system cleanup
This commit is contained in:
@@ -401,9 +401,10 @@ export default class Blueprint extends IElement {
|
||||
this.dispatchEvent(event)
|
||||
}
|
||||
|
||||
dispatchEditTextEvent(beginning) {
|
||||
/** @param {Boolean} begin */
|
||||
dispatchEditTextEvent(begin) {
|
||||
const event = new CustomEvent(
|
||||
beginning
|
||||
begin
|
||||
? Configuration.editTextEventName.begin
|
||||
: Configuration.editTextEventName.end
|
||||
)
|
||||
|
||||
@@ -5,6 +5,7 @@ import { LitElement } from "lit"
|
||||
* @typedef {import("../entity/IEntity").default} IEntity
|
||||
* @typedef {import("../input/IInput").default} IInput
|
||||
* @typedef {import("../template/ITemplate").default} ITemplate
|
||||
* @typedef {import("lit").PropertyDeclarations} PropertyDeclarations
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -13,7 +14,7 @@ import { LitElement } from "lit"
|
||||
*/
|
||||
export default class IElement extends LitElement {
|
||||
|
||||
/** @type {import("lit").PropertyDeclarations} */
|
||||
/** @type {PropertyDeclarations} */
|
||||
static properties = {
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,28 @@
|
||||
import InputTemplate from "../template/InputTemplate"
|
||||
import Utility from "../Utility"
|
||||
import IElement from "./IElement"
|
||||
|
||||
export default class InputElement extends IElement {
|
||||
|
||||
static properties = {
|
||||
...super.properties,
|
||||
singleLine: {
|
||||
type: Boolean,
|
||||
attribute: "data-single-line",
|
||||
converter: Utility.booleanConverter,
|
||||
reflect: true,
|
||||
},
|
||||
blurOnEnter: {
|
||||
type: Boolean,
|
||||
attribute: "data-blur-enter",
|
||||
converter: Utility.booleanConverter,
|
||||
reflect: true,
|
||||
},
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super({}, new InputTemplate())
|
||||
this.singleLine = false
|
||||
this.blurOnEnter = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
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() {
|
||||
}
|
||||
}
|
||||
@@ -73,8 +73,8 @@ export default class ColorPickerWindowTemplate extends WindowTemplate {
|
||||
/** @param {KeyboardEvent} e */
|
||||
e => {
|
||||
if (e.code == "Enter") {
|
||||
action(e)
|
||||
e.preventDefault()
|
||||
action(e)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -341,21 +341,21 @@ export default class ColorPickerWindowTemplate extends WindowTemplate {
|
||||
<div class="ueb-color-control">
|
||||
<span class="ueb-color-control-label">Hex Linear</span>
|
||||
<div class="ueb-color-picker-hex-linear ueb-text-input">
|
||||
<span class="ueb-pin-input-content" role="textbox" contenteditable="true"
|
||||
<ueb-input
|
||||
.innerText="${colorRGB}"
|
||||
@focusout="${this.#hexRGBHandler}"
|
||||
@keydown="${this.#doOnEnter(this.#hexRGBHandler)}">
|
||||
</span>
|
||||
</ueb-input>
|
||||
</div>
|
||||
</div>
|
||||
<div class="ueb-color-control">
|
||||
<span class="ueb-color-control-label">Hex sRGB</span>
|
||||
<div class="ueb-color-picker-hex-srgb ueb-text-input">
|
||||
<span class="ueb-pin-input-content" role="textbox" contenteditable="true"
|
||||
<ueb-input
|
||||
.innerText="${colorSRGB}"
|
||||
@focusout="${this.#hexSRGBHandler}"
|
||||
@keydown="${this.#doOnEnter(this.#hexSRGBHandler)}">
|
||||
</span>
|
||||
</ueb-input>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { html } from "lit"
|
||||
import { html, nothing } from "lit"
|
||||
import MouseIgnore from "../input/mouse/MouseIgnore"
|
||||
import PinTemplate from "./PinTemplate"
|
||||
import Utility from "../Utility"
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {import("../element/PinElement").default<T>} PinElement
|
||||
@@ -34,19 +33,16 @@ export default class IInputPinTemplate extends PinTemplate {
|
||||
.replace(/(?<=\n\s*)$/, "\n") // Put back trailing double newline
|
||||
}
|
||||
|
||||
#onFocusOutHandler = () => this.setInputs(this.getInputs(), true)
|
||||
|
||||
/** @param {Map} changedProperties */
|
||||
firstUpdated(changedProperties) {
|
||||
super.firstUpdated(changedProperties)
|
||||
this.#inputContentElements = /** @type {HTMLElement[]} */([...this.element.querySelectorAll("ueb-input")])
|
||||
if (this.#inputContentElements.length) {
|
||||
this.setInputs(this.getInputs(), false)
|
||||
let self = this
|
||||
this.onFocusOutHandler = e => {
|
||||
e.preventDefault()
|
||||
self.setInputs(this.getInputs(), true)
|
||||
}
|
||||
this.#inputContentElements.forEach(element => {
|
||||
element.addEventListener("focusout", this.onFocusOutHandler)
|
||||
element.addEventListener("focusout", this.#onFocusOutHandler)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -54,7 +50,7 @@ export default class IInputPinTemplate extends PinTemplate {
|
||||
cleanup() {
|
||||
super.cleanup()
|
||||
this.#inputContentElements.forEach(element => {
|
||||
element.removeEventListener("focusout", this.onFocusOutHandler)
|
||||
element.removeEventListener("focusout", this.#onFocusOutHandler)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -100,6 +96,6 @@ export default class IInputPinTemplate extends PinTemplate {
|
||||
</div>
|
||||
`
|
||||
}
|
||||
return html``
|
||||
return nothing
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { css, nothing } from "lit"
|
||||
/**
|
||||
* @typedef {import("../element/IElement").default} IElement
|
||||
* @typedef {import("../input/IInput").default} IInput
|
||||
* @typedef {import("lit").TemplateResult<1>} TemplateResult
|
||||
*/
|
||||
|
||||
/** @template {IElement} T */
|
||||
@@ -40,6 +41,7 @@ export default class ITemplate {
|
||||
update(changedProperties) {
|
||||
}
|
||||
|
||||
/** @returns {TemplateResult | symbol} */
|
||||
render() {
|
||||
return nothing
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import FocusTextEdit from "../input/common/FocusTextEdit"
|
||||
import ITemplate from "./ITemplate"
|
||||
|
||||
/** @typedef {import ("../element/InputElement").default} InputElement */
|
||||
@@ -6,18 +5,49 @@ import ITemplate from "./ITemplate"
|
||||
/** @extends {ITemplate<InputElement>} */
|
||||
export default class InputTemplate extends ITemplate {
|
||||
|
||||
createInputObjects() {
|
||||
return [
|
||||
...super.createInputObjects(),
|
||||
new FocusTextEdit(this.element, this.element.blueprint),
|
||||
]
|
||||
#focusHandler = () => this.element.blueprint.dispatchEditTextEvent(true)
|
||||
#focusoutHandler = () => {
|
||||
this.element.blueprint.dispatchEditTextEvent(false)
|
||||
document.getSelection()?.removeAllRanges() // Deselect eventually selected text inside the input
|
||||
}
|
||||
#inputSingleLineHandler =
|
||||
/** @param {InputEvent} e */
|
||||
e => /** @type {HTMLElement} */(e.target).querySelectorAll("br").forEach(br => br.remove())
|
||||
#onKeydownBlurOnEnterHandler =
|
||||
/** @param {KeyboardEvent} e */
|
||||
e => {
|
||||
if (e.code == "Enter" && !e.shiftKey) {
|
||||
/** @type {HTMLElement} */(e.target).blur()
|
||||
}
|
||||
}
|
||||
|
||||
/** @param {Map} changedProperties */
|
||||
firstUpdated(changedProperties) {
|
||||
super.firstUpdated(changedProperties)
|
||||
/** @param {InputElement} element */
|
||||
constructed(element) {
|
||||
super.constructed(element)
|
||||
this.element.classList.add("ueb-pin-input-content")
|
||||
this.element.setAttribute("role", "textbox")
|
||||
this.element.contentEditable = "true"
|
||||
}
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.element.addEventListener("focus", this.#focusHandler)
|
||||
this.element.addEventListener("focusout", this.#focusoutHandler)
|
||||
if (this.element.singleLine) {
|
||||
this.element.addEventListener("input", this.#inputSingleLineHandler)
|
||||
}
|
||||
if (this.element.blurOnEnter) {
|
||||
this.element.addEventListener("keydown", this.#onKeydownBlurOnEnterHandler)
|
||||
}
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
this.element.removeEventListener("focus", this.#focusHandler)
|
||||
this.element.removeEventListener("focusout", this.#focusoutHandler)
|
||||
if (this.element.singleLine) {
|
||||
this.element.removeEventListener("input", this.#inputSingleLineHandler)
|
||||
}
|
||||
if (this.element.blurOnEnter) {
|
||||
this.element.removeEventListener("keydown", this.#onKeydownBlurOnEnterHandler)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { html } from "lit"
|
||||
import { html, nothing } from "lit"
|
||||
import ITemplate from "./ITemplate"
|
||||
import MouseCreateLink from "../input/mouse/MouseCreateLink"
|
||||
import Utility from "../Utility"
|
||||
|
||||
/**
|
||||
* @typedef {import("../input/IInput").default} IInput
|
||||
* @typedef {import("lit").TemplateResult} TemplateResult
|
||||
*/
|
||||
/**
|
||||
* @template T
|
||||
@@ -50,6 +51,7 @@ export default class PinTemplate extends ITemplate {
|
||||
`
|
||||
}
|
||||
|
||||
/** @returns {TemplateResult | symbol} */
|
||||
renderIcon() {
|
||||
return html`
|
||||
<svg viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg">
|
||||
@@ -59,8 +61,9 @@ export default class PinTemplate extends ITemplate {
|
||||
`
|
||||
}
|
||||
|
||||
/** @returns {TemplateResult | symbol} */
|
||||
renderInput() {
|
||||
return html``
|
||||
return nothing
|
||||
}
|
||||
|
||||
/** @param {Map} changedProperties */
|
||||
|
||||
@@ -17,7 +17,7 @@ export default class RealPinTemplate extends INumericPinTemplate {
|
||||
if (this.element.isInput()) {
|
||||
return html`
|
||||
<div class="ueb-pin-input">
|
||||
<ueb-input
|
||||
<ueb-input .singleLine="${true}"
|
||||
.innerText="${IInputPinTemplate.stringFromUEToInput(Utility.minDecimals(this.element.entity.DefaultValue))}">
|
||||
</ueb-input>
|
||||
</div>
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import { html } from "lit"
|
||||
import { html, nothing } from "lit"
|
||||
import Configuration from "../Configuration"
|
||||
import IDraggablePositionedTemplate from "./IDraggablePositionedTemplate"
|
||||
import MouseMoveDraggable from "../input/mouse/MouseMoveDraggable"
|
||||
|
||||
/** @typedef {import("../element/WindowElement").default} WindowElement */
|
||||
/**
|
||||
* @typedef {import("../element/WindowElement").default} WindowElement
|
||||
* @typedef {import("lit").TemplateResult<1>} TemplateResult
|
||||
*/
|
||||
|
||||
/** @extends {IDraggablePositionedTemplate<WindowElement>} */
|
||||
export default class WindowTemplate extends IDraggablePositionedTemplate {
|
||||
@@ -46,8 +49,9 @@ export default class WindowTemplate extends IDraggablePositionedTemplate {
|
||||
return html`Window`
|
||||
}
|
||||
|
||||
/** @returns {TemplateResult | symbol} */
|
||||
renderContent() {
|
||||
return html``
|
||||
return nothing
|
||||
}
|
||||
|
||||
apply() {
|
||||
|
||||
Reference in New Issue
Block a user