MovableAPI Fully Implemented!! :)
This commit is contained in:
@@ -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<MovableState>({
|
||||
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 = '';
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
+33
-63
@@ -1,12 +1,10 @@
|
||||
<script lang="ts">
|
||||
import { movable } from '$lib/movable/movable_controller.svelte';
|
||||
import type { MovableAPI } from '$lib/movable/util';
|
||||
import Input from './terminal/Input.svelte';
|
||||
|
||||
let { username }: { username: string } = $props();
|
||||
|
||||
//const clearTerminal = (): void => clear();
|
||||
|
||||
//const printOutput = (e: HTMLElement, d: PrintData): void => print(e, d);
|
||||
|
||||
export async function init(username: string, password: string) {
|
||||
try {
|
||||
//await initTerminal({ username, password }, callbackInit, 'en_US');
|
||||
@@ -48,74 +46,46 @@
|
||||
return ctx.measureText('M').width;
|
||||
}
|
||||
|
||||
/* function inputHandler(event: KeyboardEvent) {
|
||||
const terminalKeyInhibitor = terminal.getKeybindInhibitor();
|
||||
terminalKeyInhibitor.processKeystroke(event);
|
||||
|
||||
const processKeyInhibitor = terminal.getProcessKeyInhibitor();
|
||||
if (processKeyInhibitor != ExitCode.ERROR) {
|
||||
processKeyInhibitor.processKeystroke(event);
|
||||
}
|
||||
|
||||
console.log(processKeyInhibitor, terminalKeyInhibitor);
|
||||
switch (event.key) {
|
||||
case 'Enter': {
|
||||
terminal.executeCommand(inputValue);
|
||||
updateTerminal();
|
||||
break;
|
||||
}
|
||||
case 'ArrowRight': {
|
||||
//TODO: Move cursor visually
|
||||
}
|
||||
case 'ArrowLeft': {
|
||||
//TODO: Move cursor visually
|
||||
}
|
||||
case 'ArrowUp': {
|
||||
//TODO: Make a traverse history function with up/down args
|
||||
}
|
||||
case 'ArrowDown': {
|
||||
//TODO: Make a traverse history function with up/down args
|
||||
}
|
||||
}
|
||||
|
||||
const elem = document.getElementById('cout');
|
||||
if (!elem) {
|
||||
throw new Error('cant scroll to bottom, element is null');
|
||||
}
|
||||
|
||||
elem.scrollTop = elem.scrollHeight;
|
||||
} */
|
||||
|
||||
/* //Callback initializer
|
||||
const callbackInit = {
|
||||
print: (data: any) => {
|
||||
const e = document.getElementById('outputWrapper');
|
||||
if (!e) return;
|
||||
printOutput(e, data);
|
||||
},
|
||||
clear: clearTerminal,
|
||||
getWidth: getWidth,
|
||||
getFontSize: getFontSize
|
||||
}; */
|
||||
let cwd: string = $state('');
|
||||
const hostname = 'terminal'; //TODO: Change this to pull from config on db
|
||||
//let isProcessActive = $state(false);
|
||||
|
||||
let movableAPI: MovableAPI;
|
||||
let inputValue = $state('');
|
||||
</script>
|
||||
|
||||
<label for="input" class="w-11/12">
|
||||
<div
|
||||
class="terminal-wrapper relative flex h-screen w-1/2 flex-col items-center justify-center bg-red-400"
|
||||
>
|
||||
<div
|
||||
id="terminal"
|
||||
class=" terminal-window flex size-full flex-col rounded-md shadow-subtle transition-discrete"
|
||||
class="absolute flex size-full h-256 w-512 flex-col rounded-md shadow-subtle transition-discrete will-change-auto"
|
||||
use:movable={{
|
||||
wrapper: '.terminal-wrapper',
|
||||
handle: '.terminal-bar',
|
||||
baseZIndex: 10,
|
||||
resizable: { threshold: 24, minWidth: 500, minHeight: 300 },
|
||||
onAPI: (api) => (movableAPI = api)
|
||||
}}
|
||||
>
|
||||
<div
|
||||
class="terminal-bar flex h-9 w-full flex-row items-center rounded-t-md bg-surface-2 text-center font-terminal text-sm font-bold text-primary"
|
||||
class="terminal-bar flex h-9 w-full flex-row items-center rounded-t-md bg-surface-2 text-center font-terminal text-sm font-bold text-primary select-none"
|
||||
>
|
||||
<div class="dots-wrapper mx-2.5 flex h-full flex-row items-center justify-center gap-2.5">
|
||||
<button class="size-2.5 cursor-pointer rounded-full p-0" title=""></button>
|
||||
<button class="size-2.5 cursor-pointer rounded-full p-0" title=""></button>
|
||||
<button class="size-2.5 cursor-pointer rounded-full p-0" title=""></button>
|
||||
<div class="dots-wrapper mx-3 flex h-full flex-row items-center justify-center gap-3">
|
||||
<button
|
||||
class="size-3 cursor-pointer rounded-full p-0"
|
||||
title=""
|
||||
onclick={() => movableAPI.close()}
|
||||
></button>
|
||||
<button
|
||||
class="size-3 cursor-pointer rounded-full p-0"
|
||||
title=""
|
||||
onclick={() => movableAPI.maximize()}
|
||||
></button>
|
||||
<button
|
||||
class="size-3 cursor-pointer rounded-full p-0"
|
||||
title=""
|
||||
onclick={() => movableAPI.minimize()}
|
||||
></button>
|
||||
</div>
|
||||
<div class=" mr-2 flex grow text-sm">
|
||||
<p>{username}@{hostname} {cwd}</p>
|
||||
@@ -126,10 +96,10 @@
|
||||
id="cout"
|
||||
>
|
||||
<div id="outputWrapper"></div>
|
||||
<Input autofocus bind:value={inputValue} blinkDelay={500} />
|
||||
<Input bind:value={inputValue} blinkDelay={500} />
|
||||
</div>
|
||||
</div>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
* {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
let {
|
||||
value = $bindable(''),
|
||||
blinkDelay = 500,
|
||||
blinkDelay = 250,
|
||||
autofocus = true,
|
||||
oninput = () => {}
|
||||
}: {
|
||||
@@ -15,18 +15,11 @@
|
||||
|
||||
let container: HTMLDivElement | undefined;
|
||||
let cursorPos: number = $derived(value.length);
|
||||
let text: string = $derived(value);
|
||||
let text: string = $state('');
|
||||
let focused: boolean = $state(false);
|
||||
let blinkPaused: boolean = $state(false); // true → cursor steady, false → blinking
|
||||
let blinkTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
$effect(() => {
|
||||
if (value !== text) {
|
||||
text = value;
|
||||
cursorPos = Math.min(cursorPos, text.length);
|
||||
}
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
if (autofocus && container) {
|
||||
container.focus();
|
||||
@@ -52,17 +45,20 @@
|
||||
}
|
||||
|
||||
function handleFocus() {
|
||||
console.log('focused');
|
||||
focused = true;
|
||||
blinkPaused = false; // start blinking immediately
|
||||
}
|
||||
|
||||
function handleBlur() {
|
||||
console.log('blured');
|
||||
|
||||
focused = false;
|
||||
if (blinkTimer) {
|
||||
clearTimeout(blinkTimer);
|
||||
blinkTimer = null;
|
||||
}
|
||||
blinkPaused = false;
|
||||
blinkPaused = true;
|
||||
}
|
||||
|
||||
function insertChar(char: string) {
|
||||
@@ -95,7 +91,8 @@
|
||||
}
|
||||
|
||||
function handleKeydown(e: KeyboardEvent) {
|
||||
// Ctrl + A / Ctrl + E
|
||||
console.log(e.key);
|
||||
|
||||
if (e.ctrlKey) {
|
||||
if (e.key === 'a' || e.key === 'A') {
|
||||
e.preventDefault();
|
||||
@@ -109,6 +106,24 @@
|
||||
pauseBlink();
|
||||
return;
|
||||
}
|
||||
if (e.key === 'd' || e.key === 'D') {
|
||||
e.preventDefault();
|
||||
deleteCharAfter();
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.key === 'ArrowLeft') {
|
||||
e.preventDefault();
|
||||
cursorPos = 0;
|
||||
pauseBlink();
|
||||
return;
|
||||
}
|
||||
if (e.key === 'ArrowRight') {
|
||||
e.preventDefault();
|
||||
cursorPos = text.length;
|
||||
pauseBlink();
|
||||
return;
|
||||
}
|
||||
// ignore other Ctrl combos
|
||||
return;
|
||||
}
|
||||
@@ -158,7 +173,7 @@
|
||||
return;
|
||||
}
|
||||
|
||||
// Everything else (Enter, Tab, etc.) is ignored.
|
||||
// Everything else (Enter, Tab, etc.) is ignored.EEE
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
@@ -170,7 +185,7 @@
|
||||
|
||||
<div
|
||||
id="terminal-input"
|
||||
class=" relative inline-block cursor-text overflow-hidden whitespace-nowrap"
|
||||
class=" relative inline-block font-terminal whitespace-nowrap outline-none select-none"
|
||||
bind:this={container}
|
||||
tabindex="0"
|
||||
role="textbox"
|
||||
@@ -178,18 +193,22 @@
|
||||
onblur={handleBlur}
|
||||
onfocus={handleFocus}
|
||||
onkeydown={handleKeydown}
|
||||
style="--cursor-index: {cursorPos}; --pad: 8px;"
|
||||
style="--cursor-index: {cursorPos};"
|
||||
>
|
||||
<span>$ </span>
|
||||
<span class="text inline" aria-hidden="true">
|
||||
<span class="prompt inline-block w-[1ch] text-center">$</span>
|
||||
<span class="inline cursor-text" aria-hidden="true">
|
||||
{#each text.split('') as char}
|
||||
<span class="char inline-block w-[1ch] text-center">{char}</span>
|
||||
<span class="inline-block w-[1ch] text-center">{char}</span>
|
||||
{/each}
|
||||
</span>
|
||||
{#if focused}
|
||||
<span
|
||||
class="cursor absolute bottom-[0.15rem] h-0.5 w-[1ch] animate-cursor-blink bg-amber-50"
|
||||
class:blink={!blinkPaused}
|
||||
></span>
|
||||
{/if}
|
||||
</div>
|
||||
<div
|
||||
class="cursor absolute bottom-[0.2rem] h-px w-[1ch] bg-text-primary"
|
||||
class:animate-cursor-blink={!blinkPaused}
|
||||
></div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.cursor {
|
||||
left: calc(var(--cursor-index) * 1ch + 8.5px + 1ch);
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
terminalComponent.init(username, password); */
|
||||
}
|
||||
|
||||
console.log(username);
|
||||
let bound = $state<HTMLElement>();
|
||||
</script>
|
||||
|
||||
{#if MAINTENANCE}
|
||||
@@ -34,15 +34,19 @@
|
||||
<Maintenence />
|
||||
{:else}
|
||||
<Settings></Settings>
|
||||
<div id="main" class="relative flex h-dvh w-full items-center justify-center p-24">
|
||||
<div
|
||||
bind:this={bound}
|
||||
id="main"
|
||||
class="relative flex h-screen w-full items-center justify-center p-24"
|
||||
>
|
||||
<div
|
||||
id="wrapper1"
|
||||
class=" absolute z-35 flex flex-col items-center {form?.success ? 'outro' : ''}"
|
||||
>
|
||||
<Login init={initializer} {form} />
|
||||
</div>
|
||||
<div id="wrapper2" class=" absolute z-4 size-4/5 opacity-0 {form?.success ? 'intro' : ''}">
|
||||
<Terminal {username}></Terminal>
|
||||
<div id="wrapper2" class=" {form?.success ? 'intro' : ''}">
|
||||
<Terminal {username} {bound}></Terminal>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
@@ -2,14 +2,17 @@
|
||||
import { addToast } from '$lib/stores/toasts';
|
||||
import { FileSystem } from '$lib/termos/filesystem/filesystem';
|
||||
import { onMount } from 'svelte';
|
||||
import UsersTable from '../../modules/panel/users/UsersTable.svelte';
|
||||
import Settings from '../../modules/Settings.svelte';
|
||||
import Terminal from '../../modules/Terminal.svelte';
|
||||
import type { ToastMessage } from '../admin/util';
|
||||
|
||||
function bye(tm: ToastMessage): void {
|
||||
console.log('Funkcja wywolana');
|
||||
}
|
||||
|
||||
let wrapper = $state<HTMLElement>();
|
||||
|
||||
let username = 'test';
|
||||
|
||||
onMount(async () => {
|
||||
await FileSystem.init();
|
||||
|
||||
@@ -21,7 +24,4 @@
|
||||
});
|
||||
</script>
|
||||
|
||||
<Settings></Settings>
|
||||
<div class="size-full p-16">
|
||||
<UsersTable></UsersTable>
|
||||
</div>
|
||||
<Terminal {username}></Terminal>
|
||||
|
||||
Reference in New Issue
Block a user