Compare commits
3
Commits
unstable
...
pre-release
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
871c5bed86 | ||
|
|
c7d1610648 | ||
|
|
afd663b6af |
@@ -5,6 +5,7 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite dev",
|
"dev": "vite dev",
|
||||||
|
"dev-host": "vite dev --host",
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"preview": "vite preview",
|
"preview": "vite preview",
|
||||||
"prepare": "svelte-kit sync || echo ''",
|
"prepare": "svelte-kit sync || echo ''",
|
||||||
|
|||||||
@@ -0,0 +1,568 @@
|
|||||||
|
import {
|
||||||
|
getWrapperManager,
|
||||||
|
MWIN_MANAGER,
|
||||||
|
resolveElement,
|
||||||
|
type MWinAPI,
|
||||||
|
type MWinNormalizedParams,
|
||||||
|
type MWinParams,
|
||||||
|
type MWinState,
|
||||||
|
type ResizeDirection
|
||||||
|
} from './mwin_util';
|
||||||
|
|
||||||
|
export function mwin_movable(node: HTMLElement, options: MWinParams) {
|
||||||
|
const controller = new MWinController(node, options);
|
||||||
|
const api: MWinAPI = {
|
||||||
|
state: controller.state,
|
||||||
|
minimize: () => controller.minimize(),
|
||||||
|
maximize: () => controller.maximize(),
|
||||||
|
close: () => controller.close()
|
||||||
|
};
|
||||||
|
|
||||||
|
options.onAPI?.(api);
|
||||||
|
return {
|
||||||
|
destroy: () => controller.destroy()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
class MWinController {
|
||||||
|
#rafID: number | null = null;
|
||||||
|
#pendingApply = false;
|
||||||
|
|
||||||
|
#node: HTMLElement;
|
||||||
|
#wrapper: HTMLElement;
|
||||||
|
#opts: MWinNormalizedParams;
|
||||||
|
#savedTransition: string;
|
||||||
|
#savedStyles: string;
|
||||||
|
#wrapperObserver: ResizeObserver | null = null;
|
||||||
|
|
||||||
|
#wrapperWidth: number = 0;
|
||||||
|
#wrapperHeight: number = 0;
|
||||||
|
#wrapperLeft: number = 0;
|
||||||
|
#wrapperTop: number = 0;
|
||||||
|
#wrapperBorderLeft: number = 0;
|
||||||
|
#wrapperBorderTop: 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<MWinState>({
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
width: 0,
|
||||||
|
height: 0,
|
||||||
|
minimized: false,
|
||||||
|
maximized: false,
|
||||||
|
moving: false,
|
||||||
|
resizing: false,
|
||||||
|
zIndex: 0
|
||||||
|
});
|
||||||
|
|
||||||
|
constructor(node: HTMLElement, options: MWinParams) {
|
||||||
|
this.#node = node;
|
||||||
|
|
||||||
|
const wrapper = resolveElement(options.wrapper, node);
|
||||||
|
if (!wrapper) throw new Error('[Movable] - Wrapper element not found!');
|
||||||
|
this.#wrapper = wrapper;
|
||||||
|
|
||||||
|
const wrapperStyle = getComputedStyle(this.#wrapper);
|
||||||
|
this.#wrapperBorderLeft = parseFloat(wrapperStyle.borderLeftWidth);
|
||||||
|
this.#wrapperBorderTop = parseFloat(wrapperStyle.borderTopWidth);
|
||||||
|
|
||||||
|
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 ?? 24, //MinWidth should be min-content
|
||||||
|
minHeight: resizableOpts.minHeight ?? 24, //MinHeight should be min-content
|
||||||
|
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.#node.style.willChange = 'transform';
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
this.#wrapperLeft = wrapperRect.left + this.#wrapperBorderLeft;
|
||||||
|
this.#wrapperTop = wrapperRect.top + this.#wrapperBorderTop;
|
||||||
|
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 t = this.#opts.threshold;
|
||||||
|
|
||||||
|
let dirY: string = '';
|
||||||
|
if (e.clientY - this.state.y < t) dirY = 'n';
|
||||||
|
else if (this.state.y + this.state.height - e.clientY < t) dirY = 's';
|
||||||
|
|
||||||
|
let dirX: string = '';
|
||||||
|
if (e.clientX - this.state.x < t) dirX = 'w';
|
||||||
|
else if (this.state.x + this.state.width - 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(() => {
|
||||||
|
if (!this.state.moving && !this.state.resizing) {
|
||||||
|
this.#node.style.willChange = 'auto';
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = '';
|
||||||
|
this.#node.style.cursor = 'auto';
|
||||||
|
|
||||||
|
window.removeEventListener('pointermove', this.#onPointerMove);
|
||||||
|
window.removeEventListener('pointerup', this.#onPointerUp);
|
||||||
|
window.removeEventListener('pointercancel', this.#onPointerCancel);
|
||||||
|
|
||||||
|
this.#apply();
|
||||||
|
};
|
||||||
|
|
||||||
|
#onWrapperResize(): void {
|
||||||
|
const style = getComputedStyle(this.#wrapper);
|
||||||
|
this.#wrapperBorderLeft = parseFloat(style.borderLeftWidth);
|
||||||
|
this.#wrapperBorderTop = parseFloat(style.borderTopWidth);
|
||||||
|
|
||||||
|
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.#apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
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)[MWIN_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,96 @@
|
|||||||
|
export interface MWinParams {
|
||||||
|
wrapper: HTMLElement | (() => HTMLElement) | string; // changed
|
||||||
|
handle?: HTMLElement | (() => HTMLElement) | string;
|
||||||
|
initX?: number;
|
||||||
|
initY?: number;
|
||||||
|
initWidth?: number;
|
||||||
|
initHeight?: number;
|
||||||
|
baseZIndex?: number;
|
||||||
|
resizable?: boolean | MWinResizeParams;
|
||||||
|
resizeHandle?: HTMLElement | (() => HTMLElement) | string;
|
||||||
|
onClose?: () => void;
|
||||||
|
onMinimize?: () => void;
|
||||||
|
onMaximize?: () => void;
|
||||||
|
onAPI?: (api: MWinAPI) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MWinNormalizedParams {
|
||||||
|
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 MWinResizeParams {
|
||||||
|
threshold?: number;
|
||||||
|
minWidth?: number;
|
||||||
|
minHeight?: number;
|
||||||
|
maxWidth?: number;
|
||||||
|
maxHeight?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MWinState {
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
minimized: boolean;
|
||||||
|
maximized: boolean;
|
||||||
|
moving: boolean;
|
||||||
|
resizing: boolean;
|
||||||
|
zIndex: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MWinAPI {
|
||||||
|
state: MWinState;
|
||||||
|
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 MWIN_MANAGER = Symbol('mwinlib');
|
||||||
|
|
||||||
|
export function getWrapperManager(wrapper: HTMLElement): WrapperManager {
|
||||||
|
let mgr = (wrapper as any)[MWIN_MANAGER] as WrapperManager | undefined;
|
||||||
|
if (!mgr) {
|
||||||
|
mgr = { zCounter: 0, refCount: 0 };
|
||||||
|
(wrapper as any)[MWIN_MANAGER] = mgr;
|
||||||
|
}
|
||||||
|
return mgr;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveElement(
|
||||||
|
input: HTMLElement | (() => HTMLElement) | string | undefined,
|
||||||
|
relativeTo: HTMLElement
|
||||||
|
): HTMLElement | null {
|
||||||
|
if (!input) return null;
|
||||||
|
|
||||||
|
if (input instanceof HTMLElement) return input;
|
||||||
|
|
||||||
|
if (typeof input === 'function') return input();
|
||||||
|
|
||||||
|
if (typeof input === 'string') {
|
||||||
|
return relativeTo.closest(input) as HTMLElement | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
export class KeybindListener {
|
||||||
|
#abortController: AbortController;
|
||||||
|
#eventStack: KeyboardEvent[] = [];
|
||||||
|
#bindTable: Map<Keybind, KeybindOptions> = new Map();
|
||||||
|
|
||||||
|
constructor(elem: HTMLElement) {
|
||||||
|
this.#abortController = new AbortController();
|
||||||
|
|
||||||
|
elem.addEventListener('keydown', (e) => this.#captureEvent(e), {
|
||||||
|
signal: this.#abortController.signal,
|
||||||
|
passive: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#captureEvent(e: KeyboardEvent) {
|
||||||
|
if (e.altKey || e.shiftKey || e.ctrlKey) {
|
||||||
|
this.#eventStack.push(e);
|
||||||
|
}
|
||||||
|
this.#processEvent(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
#processEvent(e: KeyboardEvent) {
|
||||||
|
const keybind: Keybind = {
|
||||||
|
key: e.key,
|
||||||
|
modifiers: this.#pushModifiers()
|
||||||
|
};
|
||||||
|
|
||||||
|
const value = this.#bindTable.get(keybind);
|
||||||
|
if (!value) return;
|
||||||
|
|
||||||
|
value?.function?.(e);
|
||||||
|
console.log(`[keybind-listener] - executed a function under a keybind: ${keybind}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
#pushModifiers(): Modifier[] {
|
||||||
|
const mods: Modifier[] = [];
|
||||||
|
|
||||||
|
for (const event of this.#eventStack) {
|
||||||
|
mods.push(event.key as Modifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mods;
|
||||||
|
}
|
||||||
|
|
||||||
|
registerKeybinds(keybinds: { keybind: Keybind; options: KeybindOptions }[]): void {
|
||||||
|
for (const value of keybinds) {
|
||||||
|
this.#bindTable.set(value.keybind, {
|
||||||
|
function: value.options.function,
|
||||||
|
no_inhibit: value.options.no_inhibit ?? false
|
||||||
|
});
|
||||||
|
console.log(`[keybind-listener] - keybind registered under the key: ${value.keybind}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Keybind = {
|
||||||
|
key: string;
|
||||||
|
modifiers?: Modifier[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type KeybindOptions = {
|
||||||
|
function: (e: KeyboardEvent) => void;
|
||||||
|
no_inhibit?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Modifier = 'Control' | 'Alt' | 'Shift';
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import type { Keybind, KeybindOptions } from "../terminal/keybind_listenter";
|
||||||
|
|
||||||
|
export const SHELL_KEYBINDS: { keybind: Keybind; options: KeybindOptions }[] = [
|
||||||
|
{
|
||||||
|
keybind: {
|
||||||
|
key: 'c',
|
||||||
|
modifiers: ['Control']
|
||||||
|
}
|
||||||
|
options: {
|
||||||
|
function
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
+37
-63
@@ -1,12 +1,10 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { mwin_movable } from '$lib/mwinlib/mwin_controller.svelte';
|
||||||
|
import type { MWinAPI } from '$lib/mwinlib/mwin_util';
|
||||||
import Input from './terminal/Input.svelte';
|
import Input from './terminal/Input.svelte';
|
||||||
|
|
||||||
let { username }: { username: string } = $props();
|
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) {
|
export async function init(username: string, password: string) {
|
||||||
try {
|
try {
|
||||||
//await initTerminal({ username, password }, callbackInit, 'en_US');
|
//await initTerminal({ username, password }, callbackInit, 'en_US');
|
||||||
@@ -48,74 +46,46 @@
|
|||||||
return ctx.measureText('M').width;
|
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('');
|
let cwd: string = $state('');
|
||||||
const hostname = 'terminal'; //TODO: Change this to pull from config on db
|
const hostname = 'terminal'; //TODO: Change this to pull from config on db
|
||||||
//let isProcessActive = $state(false);
|
//let isProcessActive = $state(false);
|
||||||
|
|
||||||
|
let mwin: MWinAPI;
|
||||||
let inputValue = $state('');
|
let inputValue = $state('');
|
||||||
|
let inputComponent: any;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<label for="input" class="w-11/12">
|
<div
|
||||||
|
id="terminal-wrapper"
|
||||||
|
class=" relative flex h-screen w-full flex-col items-center justify-center"
|
||||||
|
>
|
||||||
<div
|
<div
|
||||||
id="terminal"
|
class="absolute flex size-full h-256 w-512 flex-col rounded-md shadow-subtle transition-discrete will-change-auto"
|
||||||
class=" terminal-window flex size-full flex-col rounded-md shadow-subtle transition-discrete"
|
use:mwin_movable={{
|
||||||
|
wrapper: '#terminal-wrapper',
|
||||||
|
handle: '#terminal-bar',
|
||||||
|
baseZIndex: 10,
|
||||||
|
resizable: { threshold: 24, minWidth: 500, minHeight: 300 },
|
||||||
|
onAPI: (api) => (mwin = api)
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<div
|
<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"
|
id="terminal-bar"
|
||||||
|
class=" 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">
|
<div class="dots-wrapper mx-3 flex h-full flex-row items-center justify-center gap-3">
|
||||||
<button class="size-2.5 cursor-pointer rounded-full p-0" title=""></button>
|
<button class="size-3 cursor-pointer rounded-full p-0" title="" onclick={() => mwin.close()}
|
||||||
<button class="size-2.5 cursor-pointer rounded-full p-0" title=""></button>
|
></button>
|
||||||
<button class="size-2.5 cursor-pointer rounded-full p-0" title=""></button>
|
<button
|
||||||
|
class="size-3 cursor-pointer rounded-full p-0"
|
||||||
|
title=""
|
||||||
|
onclick={() => mwin.maximize()}
|
||||||
|
></button>
|
||||||
|
<button
|
||||||
|
class="size-3 cursor-pointer rounded-full p-0"
|
||||||
|
title=""
|
||||||
|
onclick={() => mwin.minimize()}
|
||||||
|
></button>
|
||||||
</div>
|
</div>
|
||||||
<div class=" mr-2 flex grow text-sm">
|
<div class=" mr-2 flex grow text-sm">
|
||||||
<p>{username}@{hostname} {cwd}</p>
|
<p>{username}@{hostname} {cwd}</p>
|
||||||
@@ -124,12 +94,16 @@
|
|||||||
<div
|
<div
|
||||||
class="inner-content scroll-hidden grow origin-top overflow-y-auto rounded-b-md bg-surface-1 p-4 text-sm text-text-primary"
|
class="inner-content scroll-hidden grow origin-top overflow-y-auto rounded-b-md bg-surface-1 p-4 text-sm text-text-primary"
|
||||||
id="cout"
|
id="cout"
|
||||||
|
onclick={() => inputComponent?.focusInput()}
|
||||||
|
onkeydown={() => inputComponent?.focusInput()}
|
||||||
|
role="textbox"
|
||||||
|
tabindex="-1"
|
||||||
>
|
>
|
||||||
<div id="outputWrapper"></div>
|
<div id="outputWrapper"></div>
|
||||||
<Input autofocus bind:value={inputValue} blinkDelay={500} />
|
<Input bind:value={inputValue} blinkDelay={500} bind:this={inputComponent} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</label>
|
</div>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
* {
|
* {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
<div
|
<div
|
||||||
transition:fly={{ x: 20, y: 0, duration: 300 }}
|
transition:fly={{ x: 20, y: 0, duration: 300 }}
|
||||||
id={`toast-${toast.id.toString()}`}
|
id={`toast-${toast.id.toString()}`}
|
||||||
class="w-md overflow-hidden rounded-md bg-bg-lighter-dark"
|
class="w-md overflow-hidden rounded-md bg-surface-2"
|
||||||
onclick={() => toast.onClick?.(toast)}
|
onclick={() => toast.onClick?.(toast)}
|
||||||
role="button"
|
role="button"
|
||||||
tabindex={toast.onClick ? 0 : undefined}
|
tabindex={toast.onClick ? 0 : undefined}
|
||||||
@@ -30,18 +30,18 @@
|
|||||||
<div class="flex items-center justify-start">
|
<div class="flex items-center justify-start">
|
||||||
<div class="mx-4 my-8">
|
<div class="mx-4 my-8">
|
||||||
{#if toast.type === 'info'}
|
{#if toast.type === 'info'}
|
||||||
<Info class=" text-info-dark"></Info>
|
<Info class=" text-info"></Info>
|
||||||
{:else if toast.type === 'error'}
|
{:else if toast.type === 'error'}
|
||||||
<CircleX class="text-danger-dark"></CircleX>
|
<CircleX class="text-error"></CircleX>
|
||||||
{:else if toast.type === 'warn'}
|
{:else if toast.type === 'warn'}
|
||||||
<TriangleAlert class="text-warning-dark"></TriangleAlert>
|
<TriangleAlert class="text-warning"></TriangleAlert>
|
||||||
{:else}
|
{:else}
|
||||||
<CircleCheckBig class="text-success-dark"></CircleCheckBig>
|
<CircleCheckBig class="text-success"></CircleCheckBig>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
<div class="my-2 mr-auto mb-auto ml-2 flex flex-col items-start justify-start select-none">
|
<div class="my-2 mr-auto mb-auto ml-2 flex flex-col items-start justify-start select-none">
|
||||||
<h5 class=" text- text-md font-bold text-text-dark">{toast.title}</h5>
|
<h5 class=" text- text-md text-text-dark font-bold">{toast.title}</h5>
|
||||||
<p class=" text-md mb-2 overflow-x-hidden overflow-y-scroll text-text-muted-dark">
|
<p class=" text-md mb-2 overflow-x-hidden overflow-y-scroll text-muted">
|
||||||
{toast.message}
|
{toast.message}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
@@ -56,16 +56,13 @@
|
|||||||
<X
|
<X
|
||||||
stroke-width={3}
|
stroke-width={3}
|
||||||
size={18}
|
size={18}
|
||||||
class=" transition-all duration-200 hover:text-text-muted-dark active:scale-90"
|
class=" transition-all duration-200 hover:text-muted active:scale-90"
|
||||||
></X>
|
></X>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{#if !toast.hideProgress && toast.tween}
|
{#if !toast.hideProgress && toast.tween}
|
||||||
<div
|
<div style:padding-right={`${toast.tween.current}%`} class=" h-[2px] w-0 bg-primary"></div>
|
||||||
style:padding-right={`${toast.tween.current}%`}
|
|
||||||
class=" h-[2px] w-0 bg-primary-hover-dark"
|
|
||||||
></div>
|
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { KeybindListener } from '$lib/termos/terminal/keybind_listenter';
|
||||||
|
import { SHELL_KEYBINDS } from '$lib/termos/util/shell_keybinds';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
|
|
||||||
let {
|
let {
|
||||||
value = $bindable(''),
|
value = $bindable(''),
|
||||||
blinkDelay = 500,
|
blinkDelay = 250,
|
||||||
autofocus = true,
|
autofocus = true,
|
||||||
oninput = () => {}
|
oninput = () => {}
|
||||||
}: {
|
}: {
|
||||||
@@ -13,20 +15,13 @@
|
|||||||
oninput?: (value: string) => void;
|
oninput?: (value: string) => void;
|
||||||
} = $props();
|
} = $props();
|
||||||
|
|
||||||
let container: HTMLDivElement | undefined;
|
let container: HTMLElement | undefined;
|
||||||
let cursorPos: number = $derived(value.length);
|
let cursorPos: number = $derived(value.length);
|
||||||
let text: string = $derived(value);
|
let text: string = $state('');
|
||||||
let focused: boolean = $state(false);
|
let focused: boolean = $state(false);
|
||||||
let blinkPaused: boolean = $state(false); // true → cursor steady, false → blinking
|
let blinkPaused: boolean = $state(false);
|
||||||
let blinkTimer: ReturnType<typeof setTimeout> | null = null;
|
let blinkTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
|
||||||
$effect(() => {
|
|
||||||
if (value !== text) {
|
|
||||||
text = value;
|
|
||||||
cursorPos = Math.min(cursorPos, text.length);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if (autofocus && container) {
|
if (autofocus && container) {
|
||||||
container.focus();
|
container.focus();
|
||||||
@@ -42,6 +37,10 @@
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export function focusInput(): void {
|
||||||
|
container?.focus();
|
||||||
|
}
|
||||||
|
|
||||||
function pauseBlink() {
|
function pauseBlink() {
|
||||||
blinkPaused = true;
|
blinkPaused = true;
|
||||||
if (blinkTimer) clearTimeout(blinkTimer);
|
if (blinkTimer) clearTimeout(blinkTimer);
|
||||||
@@ -52,17 +51,20 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleFocus() {
|
function handleFocus() {
|
||||||
|
console.log('focused');
|
||||||
focused = true;
|
focused = true;
|
||||||
blinkPaused = false; // start blinking immediately
|
blinkPaused = false; // start blinking immediately
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleBlur() {
|
function handleBlur() {
|
||||||
|
console.log('blured');
|
||||||
|
|
||||||
focused = false;
|
focused = false;
|
||||||
if (blinkTimer) {
|
if (blinkTimer) {
|
||||||
clearTimeout(blinkTimer);
|
clearTimeout(blinkTimer);
|
||||||
blinkTimer = null;
|
blinkTimer = null;
|
||||||
}
|
}
|
||||||
blinkPaused = false;
|
blinkPaused = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function insertChar(char: string) {
|
function insertChar(char: string) {
|
||||||
@@ -95,7 +97,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleKeydown(e: KeyboardEvent) {
|
function handleKeydown(e: KeyboardEvent) {
|
||||||
// Ctrl + A / Ctrl + E
|
console.log(e.key);
|
||||||
|
|
||||||
if (e.ctrlKey) {
|
if (e.ctrlKey) {
|
||||||
if (e.key === 'a' || e.key === 'A') {
|
if (e.key === 'a' || e.key === 'A') {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@@ -109,6 +112,24 @@
|
|||||||
pauseBlink();
|
pauseBlink();
|
||||||
return;
|
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
|
// ignore other Ctrl combos
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -158,10 +179,14 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Everything else (Enter, Tab, etc.) is ignored.
|
// Everything else (Enter, Tab, etc.) is ignored.EEE
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
|
const keybindListener = new KeybindListener(container!);
|
||||||
|
|
||||||
|
keybindListener.registerKeybinds(SHELL_KEYBINDS);
|
||||||
|
|
||||||
if (autofocus && container) {
|
if (autofocus && container) {
|
||||||
container.focus();
|
container.focus();
|
||||||
}
|
}
|
||||||
@@ -170,7 +195,7 @@
|
|||||||
|
|
||||||
<div
|
<div
|
||||||
id="terminal-input"
|
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}
|
bind:this={container}
|
||||||
tabindex="0"
|
tabindex="0"
|
||||||
role="textbox"
|
role="textbox"
|
||||||
@@ -178,18 +203,22 @@
|
|||||||
onblur={handleBlur}
|
onblur={handleBlur}
|
||||||
onfocus={handleFocus}
|
onfocus={handleFocus}
|
||||||
onkeydown={handleKeydown}
|
onkeydown={handleKeydown}
|
||||||
style="--cursor-index: {cursorPos}; --pad: 8px;"
|
style="--cursor-index: {cursorPos};"
|
||||||
>
|
>
|
||||||
<span>$ </span>
|
<span class="prompt inline-block w-[1ch] text-center">$</span>
|
||||||
<span class="text inline" aria-hidden="true">
|
<span class="inline cursor-text" aria-hidden="true">
|
||||||
{#each text.split('') as char}
|
{#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}
|
{/each}
|
||||||
</span>
|
</span>
|
||||||
{#if focused}
|
<div
|
||||||
<span
|
class="cursor absolute bottom-[0.2rem] h-px w-[1ch] bg-text-primary"
|
||||||
class="cursor absolute bottom-[0.15rem] h-0.5 w-[1ch] animate-cursor-blink bg-amber-50"
|
class:animate-cursor-blink={!blinkPaused}
|
||||||
class:blink={!blinkPaused}
|
></div>
|
||||||
></span>
|
|
||||||
{/if}
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.cursor {
|
||||||
|
left: calc(var(--cursor-index) * 1ch + 8.5px + 1ch);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
terminalComponent.init(username, password); */
|
terminalComponent.init(username, password); */
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(username);
|
let bound = $state<HTMLElement>();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if MAINTENANCE}
|
{#if MAINTENANCE}
|
||||||
@@ -34,15 +34,19 @@
|
|||||||
<Maintenence />
|
<Maintenence />
|
||||||
{:else}
|
{:else}
|
||||||
<Settings></Settings>
|
<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
|
<div
|
||||||
id="wrapper1"
|
id="wrapper1"
|
||||||
class=" absolute z-35 flex flex-col items-center {form?.success ? 'outro' : ''}"
|
class=" absolute z-35 flex flex-col items-center {form?.success ? 'outro' : ''}"
|
||||||
>
|
>
|
||||||
<Login init={initializer} {form} />
|
<Login init={initializer} {form} />
|
||||||
</div>
|
</div>
|
||||||
<div id="wrapper2" class=" absolute z-4 size-4/5 opacity-0 {form?.success ? 'intro' : ''}">
|
<div id="wrapper2" class=" {form?.success ? 'intro' : ''}">
|
||||||
<Terminal {username}></Terminal>
|
<Terminal {username} {bound}></Terminal>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
@@ -1,27 +1,5 @@
|
|||||||
<script lang="ts">
|
<script>
|
||||||
import { addToast } from '$lib/stores/toasts';
|
import Terminal from '../../modules/Terminal.svelte';
|
||||||
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 type { ToastMessage } from '../admin/util';
|
|
||||||
|
|
||||||
function bye(tm: ToastMessage): void {
|
|
||||||
console.log('Funkcja wywolana');
|
|
||||||
}
|
|
||||||
|
|
||||||
onMount(async () => {
|
|
||||||
await FileSystem.init();
|
|
||||||
|
|
||||||
document.addEventListener('keydown', (e) => {
|
|
||||||
if (e.key === 'Enter') {
|
|
||||||
addToast('success', 'Hi~', '', 5000, undefined, undefined, true, false, bye, bye);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Settings></Settings>
|
<Terminal username="test"></Terminal>
|
||||||
<div class="size-full p-16">
|
|
||||||
<UsersTable></UsersTable>
|
|
||||||
</div>
|
|
||||||
|
|||||||
Reference in New Issue
Block a user