2024-09-04 21:48:42 +02:00
|
|
|
import { listen } from "rkgk/framework.js";
|
2024-09-02 22:37:50 +02:00
|
|
|
|
|
|
|
export class ResizeHandle extends HTMLElement {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback() {
|
|
|
|
this.direction = this.getAttribute("data-direction");
|
|
|
|
this.targetId = this.getAttribute("data-target");
|
|
|
|
this.target = document.getElementById(this.targetId);
|
|
|
|
this.targetProperty = this.getAttribute("data-target-property");
|
|
|
|
this.initSize = parseInt(this.getAttribute("data-init-size"));
|
2024-09-03 22:52:12 +02:00
|
|
|
this.minSize = parseInt(this.getAttribute("data-min-size"));
|
2024-09-02 22:37:50 +02:00
|
|
|
|
2024-09-03 22:52:12 +02:00
|
|
|
this.#setSize(parseInt(localStorage.getItem(this.#localStorageKey)));
|
|
|
|
this.#saveSize();
|
2024-09-02 22:37:50 +02:00
|
|
|
this.#updateTargetProperty();
|
|
|
|
|
|
|
|
this.visual = this.appendChild(document.createElement("div"));
|
|
|
|
this.visual.classList.add("visual");
|
|
|
|
|
|
|
|
this.#draggingBehaviour();
|
|
|
|
}
|
|
|
|
|
2024-09-03 22:52:12 +02:00
|
|
|
#setSize(newSize) {
|
|
|
|
if (newSize != newSize) {
|
2024-09-04 20:38:52 +02:00
|
|
|
newSize = this.initSize;
|
2024-09-03 22:52:12 +02:00
|
|
|
}
|
|
|
|
this.size = Math.max(this.minSize, newSize);
|
|
|
|
}
|
|
|
|
|
2024-09-02 22:37:50 +02:00
|
|
|
get #localStorageKey() {
|
|
|
|
return `rkgk.resizeHandle.size.${this.targetId}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
#saveSize() {
|
|
|
|
localStorage.setItem(this.#localStorageKey, this.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
#updateTargetProperty() {
|
|
|
|
this.target.style.setProperty(this.targetProperty, `${this.size}px`);
|
|
|
|
}
|
|
|
|
|
|
|
|
async #draggingBehaviour() {
|
|
|
|
while (true) {
|
|
|
|
let mouseDown = await listen([this, "mousedown"]);
|
|
|
|
let startingSize = this.size;
|
|
|
|
if (mouseDown.button == 0) {
|
2024-09-08 12:09:14 +02:00
|
|
|
mouseDown.preventDefault();
|
2024-09-02 22:37:50 +02:00
|
|
|
this.classList.add("dragging");
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
let event = await listen([window, "mousemove"], [window, "mouseup"]);
|
|
|
|
if (event.type == "mousemove") {
|
|
|
|
if (this.direction == "vertical") {
|
2024-09-03 22:52:12 +02:00
|
|
|
this.#setSize(startingSize + (mouseDown.clientX - event.clientX));
|
2024-09-02 22:37:50 +02:00
|
|
|
}
|
|
|
|
this.#updateTargetProperty();
|
|
|
|
} else if (event.type == "mouseup") {
|
|
|
|
this.classList.remove("dragging");
|
|
|
|
this.#saveSize();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define("rkgk-resize-handle", ResizeHandle);
|