From afd663b6afc4ac11c12438c533d640d512ec2066 Mon Sep 17 00:00:00 2001 From: gornodox Date: Tue, 30 Jun 2026 05:59:02 +0200 Subject: [PATCH] MovableAPI Fully Implemented!! :) --- package.json | 2 +- src/lib/movable/movable_controller.svelte.ts | 557 +++++++++++++++++++ src/lib/movable/util.ts | 89 +++ src/modules/Terminal.svelte | 96 ++-- src/modules/terminal/Input.svelte | 67 ++- src/routes/+page.svelte | 12 +- src/routes/test/+page.svelte | 12 +- 7 files changed, 737 insertions(+), 98 deletions(-) create mode 100644 src/lib/movable/movable_controller.svelte.ts create mode 100644 src/lib/movable/util.ts diff --git a/package.json b/package.json index e3e443e..159b479 100755 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "version": "0.0.69", "type": "module", "scripts": { - "dev": "vite dev", + "dev": "vite dev --host", "build": "vite build", "preview": "vite preview", "prepare": "svelte-kit sync || echo ''", diff --git a/src/lib/movable/movable_controller.svelte.ts b/src/lib/movable/movable_controller.svelte.ts new file mode 100644 index 0000000..ef69510 --- /dev/null +++ b/src/lib/movable/movable_controller.svelte.ts @@ -0,0 +1,557 @@ +import { + getWrapperManager, + MOVABLE_MANAGER, + resolveElement, + type MovableAPI, + type MovableOptions, + type MovableState, + type NormalisedOptions, + type ResizeDirection +} from './util'; + +export function movable(node: HTMLElement, options: MovableOptions) { + const controller = new MovableController(node, options); + const api: MovableAPI = { + state: controller.state, + minimize: () => controller.minimize(), + maximize: () => controller.maximize(), + close: () => controller.close() + }; + + options.onAPI?.(api); + return { + destroy: () => controller.destroy() + }; +} + +class MovableController { + #rafID: number | null = null; + #pendingApply = false; + + #node: HTMLElement; + #wrapper: HTMLElement; + #opts: NormalisedOptions; + #savedTransition: string; + #savedStyles: string; + #wrapperObserver: ResizeObserver | null = null; + + #wrapperWidth: number = 0; + #wrapperHeight: number = 0; + #wrapperLeft: number = 0; + #wrapperTop: number = 0; + + #dragOffsetX: number = 0; + #dragOffsetY: number = 0; + #resizeDirection: ResizeDirection = null; + #resizeStartRect = { x: 0, y: 0, width: 0, height: 0 }; + #resizeStartPos = { x: 0, y: 0 }; + #restoreRect: { x: number; y: number; width: number; height: number } | null = null; + + state = $state({ + x: 0, + y: 0, + width: 0, + height: 0, + minimized: false, + maximized: false, + moving: false, + resizing: false, + zIndex: 0 + }); + + constructor(node: HTMLElement, options: MovableOptions) { + this.#node = node; + + const wrapper = resolveElement(options.wrapper, node); + if (!wrapper) throw new Error('[Movable] - Wrapper element not found!'); + this.#wrapper = wrapper; + + const resizableOpts = typeof options.resizable === 'object' ? options.resizable : {}; + + this.#opts = { + handle: resolveElement(options.handle, node), + initX: options.initX, + initY: options.initY, + initWidth: options.initWidth, + initHeight: options.initHeight, + baseZIndex: options.baseZIndex ?? 1, + resizeHandle: resolveElement(options.resizeHandle, node), + resizable: !!options.resizable, + threshold: resizableOpts.threshold ?? 8, + minWidth: resizableOpts.minWidth ?? 0, + minHeight: resizableOpts.minHeight ?? 0, + maxWidth: resizableOpts.maxWidth ?? Infinity, + maxHeight: resizableOpts.maxHeight ?? Infinity, + onClose: options.onClose, + onMinimize: options.onMinimize, + onMaximize: options.onMaximize + }; + + this.#savedStyles = this.#node.style.cssText; + + this.#savedTransition = this.#node.style.transition; + this.#node.style.transition = 'none'; + + this.#wrapper.style.position = 'relative'; + this.#node.style.position = 'absolute'; + + const wRect = this.#wrapper.getBoundingClientRect(); + const nRect = this.#node.getBoundingClientRect(); + + this.state.x = this.#opts.initX ?? nRect.left - wRect.left; + this.state.y = this.#opts.initY ?? nRect.top - wRect.top; + this.state.width = this.#opts.initWidth ?? nRect.width; + this.state.height = this.#opts.initHeight ?? nRect.height; + + this.#node.style.left = '0'; + this.#node.style.top = '0'; + + const mgr = getWrapperManager(this.#wrapper); + mgr.refCount++; + + if (!node.hasAttribute('tabindex')) { + node.setAttribute('tabindex', '0'); + } + + node.setAttribute('role', 'region'); + node.setAttribute('aria-label', 'Movable window'); + + this.#cacheWrapperRect(); + + node.addEventListener('pointerdown', this.#onPointerDown); + node.addEventListener('pointermove', this.#updateCursor); + node.addEventListener('pointerleave', this.#resetCursor); + //node.addEventListener('keydown', this.#onKeyDown); + + this.#clampAndApply(); + this.#bringToFront(); + + this.#wrapperObserver = new ResizeObserver(() => { + this.#wrapperWidth = this.#wrapper.clientWidth; + this.#wrapperHeight = this.#wrapper.clientHeight; + this.#onWrapperResize(); + }); + this.#wrapperObserver.observe(this.#wrapper); + } + + #cacheWrapperRect() { + const wrapperRect = this.#wrapper.getBoundingClientRect(); + const style = getComputedStyle(this.#wrapper); + + this.#wrapperLeft = wrapperRect.left + parseFloat(style.borderLeftWidth); + this.#wrapperTop = wrapperRect.top + parseFloat(style.borderTopWidth); + this.#wrapperWidth = this.#wrapper.clientWidth; + this.#wrapperHeight = this.#wrapper.clientHeight; + } + + #bringToFront() { + const mgr = getWrapperManager(this.#wrapper); + mgr.zCounter++; + + this.state.zIndex = this.#opts.baseZIndex + mgr.zCounter; + this.#node.style.zIndex = String(this.state.zIndex); + } + + #clampAndApply() { + this.#clampRect(); + this.#apply(); + } + + #clampRect() { + const bw = this.#wrapperWidth; + const bh = this.#wrapperHeight; + + let w = Math.min(this.state.width, bw, this.#opts.maxWidth); + w = Math.max(w, this.#opts.minWidth, 0); + let h = Math.min(this.state.height, bh, this.#opts.maxHeight); + h = Math.max(h, this.#opts.minHeight, 0); + + this.state.width = w; + this.state.height = h; + + const maxX = Math.max(0, bw - w); + const maxY = Math.max(0, bh - h); + this.state.x = Math.min(Math.max(this.state.x, 0), maxX); + this.state.y = Math.min(Math.max(this.state.y, 0), maxY); + } + + #apply() { + this.#node.style.transform = `translate(${this.state.x}px, ${this.state.y}px)`; + this.#node.style.width = this.state.width + 'px'; + this.#node.style.height = this.state.height + 'px'; + } + + #getCornerDirection(e: PointerEvent): ResizeDirection { + if (!this.#opts.resizable) return null; + + const rect = this.#node.getBoundingClientRect(); + const t = this.#opts.threshold; + + let dirY: string = ''; + if (e.clientY - rect.top < t) dirY = 'n'; + else if (rect.bottom - e.clientY < t) dirY = 's'; + + let dirX: string = ''; + if (e.clientX - rect.left < t) dirX = 'w'; + else if (rect.right - e.clientX < t) dirX = 'e'; + + if (dirY && dirX) { + return (dirY + dirX) as ResizeDirection; + } + return null; + } + + #onPointerCancel = () => this.#onPointerUp(); + + #onPointerDown = (e: PointerEvent): void => { + if (this.#isInteractive(e.target as HTMLElement)) return; + if (this.state.minimized) return; + + if (this.state.maximized) { + const target = e.target as HTMLElement; + const canDrag = this.#opts.handle?.contains(target) || !this.#opts.handle; + if (canDrag) this.#unmaximizeAtCursor(e); + return; + } + + if (this.#opts.resizeHandle?.contains(e.target as HTMLElement)) { + this.#startResize(e, 'se'); + return; + } + + const dir = this.#getCornerDirection(e); + if (dir) { + this.#startResize(e, dir); + return; + } + + if (this.#opts.handle?.contains(e.target as HTMLElement)) { + this.#startMove(e); + return; + } + + if (!this.#opts.handle) { + this.#startMove(e); + } + }; + + #startMove(e: PointerEvent, skipOffset = false): void { + e.preventDefault(); + this.#bringToFront(); + this.state.moving = true; + this.#node.style.cursor = 'grabbing'; + + this.#cacheWrapperRect(); + + if (!skipOffset) { + const rect = this.#node.getBoundingClientRect(); + this.#dragOffsetX = e.clientX - rect.left; + this.#dragOffsetY = e.clientY - rect.top; + } + + this.#prepareInteraction(); + } + + #startResize(e: PointerEvent, direction: ResizeDirection): void { + e.preventDefault(); + e.stopPropagation(); + this.#bringToFront(); + this.state.resizing = true; + + this.#cacheWrapperRect(); + + this.#resizeDirection = direction; + this.#resizeStartRect = { + x: this.state.x, + y: this.state.y, + width: this.state.width, + height: this.state.height + }; + + this.#resizeStartPos = { x: e.clientX - this.#wrapperLeft, y: e.clientY - this.#wrapperTop }; + + this.#prepareInteraction(); + } + + #unmaximizeAtCursor(e: PointerEvent): void { + this.#cacheWrapperRect(); + + const cursorX = e.clientX - this.#wrapperLeft; + const cursorY = e.clientY - this.#wrapperTop; + + const fracX = this.#wrapperWidth > 0 ? cursorX / this.#wrapperWidth : 0; + const fracY = this.#wrapperHeight > 0 ? cursorY / this.#wrapperHeight : 0; + + if (this.#restoreRect) { + this.state.width = this.#restoreRect.width; + this.state.height = this.#restoreRect.height; + this.state.maximized = false; + this.#restoreRect = null; + } else { + // fallback: no saved rect, just keep current size + this.state.maximized = false; + } + + // Position so the same relative point stays under the cursor + let newX = cursorX - fracX * this.state.width; + let newY = cursorY - fracY * this.state.height; + + // Clamp inside the boundary + const maxX = Math.max(0, this.#wrapperWidth - this.state.width); + const maxY = Math.max(0, this.#wrapperHeight - this.state.height); + newX = Math.min(Math.max(newX, 0), maxX); + newY = Math.min(Math.max(newY, 0), maxY); + + this.state.x = newX; + this.state.y = newY; + this.#scheduleApply(); + + this.#dragOffsetX = cursorX - newX; + this.#dragOffsetY = cursorY - newY; + + this.#bringToFront(); + this.state.moving = true; + this.#node.style.cursor = 'grabbing'; + this.#prepareInteraction(); + return; + } + + #prepareInteraction(): void { + document.body.style.userSelect = 'none'; + this.#node.style.transition = 'none'; + this.#node.style.willChange = 'transform, width, height'; + this.#node.style.touchAction = 'none'; + + window.addEventListener('pointermove', this.#onPointerMove, { passive: true }); + window.addEventListener('pointerup', this.#onPointerUp, { passive: true }); + window.addEventListener('pointercancel', this.#onPointerCancel, { passive: true }); + } + + #onPointerMove = (e: PointerEvent): void => { + this.#cacheWrapperRect(); + const events = e.getCoalescedEvents?.() ?? [e]; + + for (const event of events) { + if (this.state.moving) { + this.#updateMove(event); + } else if (this.state.resizing) { + this.#updateResize(event); + } + } + + this.#scheduleApply(); + }; + + #scheduleApply(): void { + if (!this.#pendingApply) { + this.#pendingApply = true; + this.#rafID = requestAnimationFrame(() => { + this.#rafID = null; + this.#pendingApply = false; + this.#apply(); + }); + } + } + + #updateMove(e: PointerEvent): void { + let newX = e.clientX - this.#wrapperLeft - this.#dragOffsetX; + let newY = e.clientY - this.#wrapperTop - this.#dragOffsetY; + + newX = Math.max(0, Math.min(newX, this.#wrapperWidth - this.state.width)); + newY = Math.max(0, Math.min(newY, this.#wrapperHeight - this.state.height)); + + this.state.x = newX; + this.state.y = newY; + } + + #updateResize(e: PointerEvent): void { + const dir = this.#resizeDirection!; + const start = this.#resizeStartRect; + const dx = e.clientX - this.#wrapperLeft - this.#resizeStartPos.x; + const dy = e.clientY - this.#wrapperTop - this.#resizeStartPos.y; + + const anchorLeft = dir.includes('e') ? start.x : start.x + start.width; + const anchorTop = dir.includes('s') ? start.y : start.y + start.height; + + let movingX = anchorLeft; + let movingY = anchorTop; + + if (dir.includes('e')) movingX = start.x + start.width + dx; + else movingX = start.x + dx; + + if (dir.includes('s')) movingY = start.y + start.height + dy; + else movingY = start.y + dy; + + const minWidth = this.#opts.minWidth; + const minHeight = this.#opts.minHeight; + const maxWidth = Math.min(this.#opts.maxWidth, this.#wrapperWidth); + const maxHeight = Math.min(this.#opts.maxHeight, this.#wrapperHeight); + + if (dir.includes('e')) { + movingX = Math.max(anchorLeft + minWidth, Math.min(movingX, anchorLeft + maxWidth)); + movingX = Math.min(movingX, this.#wrapperWidth); + } else { + movingX = Math.min(anchorLeft - minWidth, Math.max(movingX, anchorLeft - maxWidth)); + movingX = Math.max(movingX, 0); + } + + if (dir.includes('s')) { + movingY = Math.max(anchorTop + minHeight, Math.min(movingY, anchorTop + maxHeight)); + movingY = Math.min(movingY, this.#wrapperHeight); + } else { + movingY = Math.min(anchorTop - minHeight, Math.max(movingY, anchorTop - maxHeight)); + movingY = Math.max(movingY, 0); + } + + const newX = Math.min(anchorLeft, movingX); + const newY = Math.min(anchorTop, movingY); + const newWidth = Math.abs(movingX - anchorLeft); + const newHeight = Math.abs(movingY - anchorTop); + + this.state.x = newX; + this.state.y = newY; + this.state.width = newWidth; + this.state.height = newHeight; + } + + #onPointerUp = (): void => { + if (this.#rafID !== null) { + cancelAnimationFrame(this.#rafID); + this.#rafID = null; + this.#pendingApply = false; + } + + this.state.moving = false; + this.state.resizing = false; + this.#resizeDirection = null; + + document.body.style.userSelect = ''; + this.#node.style.transition = this.#savedTransition; + this.#node.style.willChange = 'auto'; + this.#node.style.touchAction = ''; + + window.removeEventListener('pointermove', this.#onPointerMove); + window.removeEventListener('pointerup', this.#onPointerUp); + window.removeEventListener('pointercancel', this.#onPointerCancel); + + this.#apply(); + }; + + #onWrapperResize(): void { + if (this.state.maximized) { + this.state.width = this.#wrapperWidth; + this.state.height = this.#wrapperHeight; + this.state.x = 0; + this.state.y = 0; + } else { + this.#clampRect(); + } + + this.#apply(); + } + + #updateCursor = (e: PointerEvent): void => { + if (this.state.moving || this.state.resizing) return; + const target = e.target as HTMLElement; + + if (this.#isInteractive(target)) { + this.#resetCursor(); + return; + } + + if (this.#opts.resizeHandle?.contains(target)) { + this.#node.style.cursor = 'se-resize'; + return; + } + + const dir = this.#getCornerDirection(e); + if (dir) { + this.#node.style.cursor = dir === 'nw' || dir === 'se' ? 'nwse-resize' : 'nesw-resize'; + return; + } + + if (this.#opts.handle) { + if (this.#opts.handle.contains(target)) { + this.#node.style.cursor = 'grab'; + } else { + this.#node.style.cursor = ''; + } + } else { + this.#node.style.cursor = ''; + } + }; + + #resetCursor = (): void => { + this.#node.style.cursor = ''; + }; + + #isInteractive(target: HTMLElement): boolean { + const tag = target.tagName; + if (['INPUT', 'BUTTON', 'TEXTAREA', 'SELECT', 'A'].includes(tag)) return true; + if (target.isContentEditable) return true; + return !!target.closest('[role="button"], [role="textbox"]'); + } + + minimize(): void { + this.state.minimized = !this.state.minimized; + this.#opts.onMinimize?.(); + } + + maximize(): void { + if (this.state.maximized) { + if (this.#restoreRect) { + Object.assign(this.state, this.#restoreRect); + this.#restoreRect = null; + } + this.state.maximized = false; + } else { + this.#restoreRect = { + x: this.state.x, + y: this.state.y, + width: this.state.width, + height: this.state.height + }; + this.state.x = 0; + this.state.y = 0; + this.state.width = this.#wrapperWidth; + this.state.height = this.#wrapperHeight; + this.state.maximized = true; + } + this.#scheduleApply(); + } + + close(): void { + this.#opts.onClose?.(); + } + + destroy(): void { + if (this.#rafID !== null) { + cancelAnimationFrame(this.#rafID); + } + + this.#node.removeEventListener('pointerdown', this.#onPointerDown); + this.#node.removeEventListener('pointermove', this.#updateCursor); + this.#node.removeEventListener('pointerleave', this.#resetCursor); + //this.#node.removeEventListener('keydown', this.#onKeyDown); + + window.removeEventListener('pointermove', this.#onPointerMove); + window.removeEventListener('pointerup', this.#onPointerUp); + window.removeEventListener('pointercancel', this.#onPointerCancel); + + if (this.#wrapperObserver) { + this.#wrapperObserver.disconnect(); + } + + const mgr = getWrapperManager(this.#wrapper); + mgr.refCount--; + if (mgr.refCount === 0) { + delete (this.#wrapper as any)[MOVABLE_MANAGER]; + } + + document.body.style.userSelect = ''; + this.#node.style.cssText = this.#savedStyles; + this.#node.style.transition = this.#savedTransition; + this.#node.style.willChange = 'auto'; + this.#node.style.touchAction = ''; + } +} diff --git a/src/lib/movable/util.ts b/src/lib/movable/util.ts new file mode 100644 index 0000000..f096db2 --- /dev/null +++ b/src/lib/movable/util.ts @@ -0,0 +1,89 @@ +export interface MovableOptions { + wrapper: HTMLElement | (() => HTMLElement) | string; // changed + handle?: HTMLElement | (() => HTMLElement) | string; + initX?: number; + initY?: number; + initWidth?: number; + initHeight?: number; + baseZIndex?: number; + resizable?: boolean | ResizeOptions; + resizeHandle?: HTMLElement | (() => HTMLElement) | string; + onClose?: () => void; + onMinimize?: () => void; + onMaximize?: () => void; + onAPI?: (api: MovableAPI) => void; +} + +export interface NormalisedOptions { + handle: HTMLElement | null; + initX?: number; + initY?: number; + initWidth?: number; + initHeight?: number; + baseZIndex: number; + resizeHandle: HTMLElement | null; + resizable: boolean; + threshold: number; + minWidth: number; + minHeight: number; + maxWidth: number; + maxHeight: number; + onClose?: () => void; + onMinimize?: () => void; + onMaximize?: () => void; +} + +export interface ResizeOptions { + threshold?: number; + minWidth?: number; + minHeight?: number; + maxWidth?: number; + maxHeight?: number; +} + +export interface MovableState { + x: number; + y: number; + width: number; + height: number; + minimized: boolean; + maximized: boolean; + moving: boolean; + resizing: boolean; + zIndex: number; +} + +export interface MovableAPI { + state: MovableState; + minimize: () => void; + maximize: () => void; + close: () => void; +} + +export class WrapperManager { + zCounter: number = 0; + refCount: number = 0; +} + +export type ResizeDirection = 'ne' | 'se' | 'sw' | 'nw' | null; + +export const MOVABLE_MANAGER = Symbol('movable'); + +export function getWrapperManager(wrapper: HTMLElement): WrapperManager { + let mgr = (wrapper as any)[MOVABLE_MANAGER] as WrapperManager | undefined; + if (!mgr) { + mgr = { zCounter: 0, refCount: 0 }; + (wrapper as any)[MOVABLE_MANAGER] = mgr; + } + return mgr; +} + +export function resolveElement( + input: HTMLElement | (() => HTMLElement) | string | undefined, + fallback?: HTMLElement +): HTMLElement | null { + if (!input) return null; + if (typeof input === 'function') return input(); + if (typeof input === 'string') return fallback?.closest(input) ?? document.querySelector(input); + return input; +} diff --git a/src/modules/Terminal.svelte b/src/modules/Terminal.svelte index 155aeba..912c28c 100644 --- a/src/modules/Terminal.svelte +++ b/src/modules/Terminal.svelte @@ -1,12 +1,10 @@ -