Moved to a new clean project on pnpm
This commit is contained in:
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="107" height="128" viewBox="0 0 107 128"><title>svelte-logo</title><path d="M94.157 22.819c-10.4-14.885-30.94-19.297-45.792-9.835L22.282 29.608A29.92 29.92 0 0 0 8.764 49.65a31.5 31.5 0 0 0 3.108 20.231 30 30 0 0 0-4.477 11.183 31.9 31.9 0 0 0 5.448 24.116c10.402 14.887 30.942 19.297 45.791 9.835l26.083-16.624A29.92 29.92 0 0 0 98.235 78.35a31.53 31.53 0 0 0-3.105-20.232 30 30 0 0 0 4.474-11.182 31.88 31.88 0 0 0-5.447-24.116" style="fill:#ff3e00"/><path d="M45.817 106.582a20.72 20.72 0 0 1-22.237-8.243 19.17 19.17 0 0 1-3.277-14.503 18 18 0 0 1 .624-2.435l.49-1.498 1.337.981a33.6 33.6 0 0 0 10.203 5.098l.97.294-.09.968a5.85 5.85 0 0 0 1.052 3.878 6.24 6.24 0 0 0 6.695 2.485 5.8 5.8 0 0 0 1.603-.704L69.27 76.28a5.43 5.43 0 0 0 2.45-3.631 5.8 5.8 0 0 0-.987-4.371 6.24 6.24 0 0 0-6.698-2.487 5.7 5.7 0 0 0-1.6.704l-9.953 6.345a19 19 0 0 1-5.296 2.326 20.72 20.72 0 0 1-22.237-8.243 19.17 19.17 0 0 1-3.277-14.502 17.99 17.99 0 0 1 8.13-12.052l26.081-16.623a19 19 0 0 1 5.3-2.329 20.72 20.72 0 0 1 22.237 8.243 19.17 19.17 0 0 1 3.277 14.503 18 18 0 0 1-.624 2.435l-.49 1.498-1.337-.98a33.6 33.6 0 0 0-10.203-5.1l-.97-.294.09-.968a5.86 5.86 0 0 0-1.052-3.878 6.24 6.24 0 0 0-6.696-2.485 5.8 5.8 0 0 0-1.602.704L37.73 51.72a5.42 5.42 0 0 0-2.449 3.63 5.79 5.79 0 0 0 .986 4.372 6.24 6.24 0 0 0 6.698 2.486 5.8 5.8 0 0 0 1.602-.704l9.952-6.342a19 19 0 0 1 5.295-2.328 20.72 20.72 0 0 1 22.237 8.242 19.17 19.17 0 0 1 3.277 14.503 18 18 0 0 1-8.13 12.053l-26.081 16.622a19 19 0 0 1-5.3 2.328" style="fill:#fff"/></svg>
|
||||
|
After Width: | Height: | Size: 1.5 KiB |
@@ -0,0 +1 @@
|
||||
// place files you want to import through the `#lib` alias in this folder.
|
||||
@@ -0,0 +1,43 @@
|
||||
<script lang="ts">
|
||||
import { onMount, type SvelteComponent } from 'svelte';
|
||||
|
||||
let {
|
||||
node,
|
||||
callbacks
|
||||
}: {
|
||||
node: HTMLElement | SvelteComponent | Document;
|
||||
callbacks: { title: string; fn: (e: MouseEvent) => void }[];
|
||||
} = $props();
|
||||
|
||||
let contextMenu: HTMLElement;
|
||||
|
||||
onMount(() => {
|
||||
node.oncontextmenu = showContextMenu;
|
||||
node.onclick = hideContextMenu;
|
||||
contextMenu.onclick = hideContextMenu;
|
||||
});
|
||||
|
||||
function showContextMenu(e: PointerEvent) {
|
||||
e.preventDefault();
|
||||
|
||||
console.log('im called');
|
||||
|
||||
contextMenu.style.display = 'block';
|
||||
contextMenu.style.top = e.pageY.toString() + 'px';
|
||||
contextMenu.style.left = e.pageX.toString() + 'px';
|
||||
}
|
||||
|
||||
function hideContextMenu() {
|
||||
contextMenu.style.display = 'none';
|
||||
}
|
||||
</script>
|
||||
|
||||
<div bind:this={contextMenu} class=" bg-surface-3 absolute z-200 hidden rounded-md p-2">
|
||||
<ul>
|
||||
{#each callbacks as item}
|
||||
<li class=" border-b-px border-text-primary">
|
||||
<button aria-label={item.title} onclick={(e) => item.fn(e)}>{item.title}</button>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
</div>
|
||||
@@ -0,0 +1,350 @@
|
||||
<script lang="ts">
|
||||
import { petals } from '#lib/stores/petals.ts';
|
||||
import { onMount } from 'svelte';
|
||||
|
||||
let {
|
||||
value = $bindable(''),
|
||||
blinkDelay = 250,
|
||||
autofocus = true,
|
||||
oninput = () => {}
|
||||
}: {
|
||||
value?: string;
|
||||
blinkDelay?: number;
|
||||
autofocus?: boolean;
|
||||
oninput?: (value: string) => void;
|
||||
} = $props();
|
||||
|
||||
let container: HTMLElement;
|
||||
let cursorPos: number = $state(value.length);
|
||||
let anchorPos: number | null = $state(null);
|
||||
let isDragging: boolean = $state(false);
|
||||
let focused: boolean = $state(false);
|
||||
let blinkPaused: boolean = $state(false);
|
||||
let blinkTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
const mods: Record<string, (e: KeyboardEvent) => void> = {
|
||||
a: () => {
|
||||
anchorPos = 0;
|
||||
cursorPos = value.length;
|
||||
pauseBlink();
|
||||
},
|
||||
c: () => copySelection(),
|
||||
x: () => cutSelection(),
|
||||
e: () => {
|
||||
anchorPos = null;
|
||||
cursorPos = value.length;
|
||||
pauseBlink();
|
||||
},
|
||||
d: () => deleteCharAfter()
|
||||
};
|
||||
const base: Record<string, (e: KeyboardEvent) => void> = {
|
||||
Home: (e) => moveTo(0, e.shiftKey),
|
||||
End: (e) => moveTo(value.length, e.shiftKey),
|
||||
ArrowLeft: (e) =>
|
||||
e.ctrlKey ? moveTo(prevWordBoundary(cursorPos), e.shiftKey) : moveCursor(-1, e.shiftKey),
|
||||
ArrowRight: (e) =>
|
||||
e.ctrlKey ? moveTo(nextWordBoundary(cursorPos), e.shiftKey) : moveCursor(1, e.shiftKey),
|
||||
Backspace: () => deleteCharBefore(),
|
||||
Delete: () => deleteCharAfter(),
|
||||
Enter: () => gust()
|
||||
};
|
||||
|
||||
let selection = $derived.by(() => {
|
||||
if (anchorPos === null || anchorPos === cursorPos) {
|
||||
return { active: false, start: -1, end: -1 };
|
||||
}
|
||||
return {
|
||||
active: true,
|
||||
start: Math.min(anchorPos, cursorPos),
|
||||
end: Math.max(anchorPos, cursorPos)
|
||||
};
|
||||
});
|
||||
|
||||
function isSelected(i: number): boolean {
|
||||
return selection.active && i >= selection.start && i < selection.end;
|
||||
}
|
||||
|
||||
function gust(): void {
|
||||
if (!container) return;
|
||||
petals(container, { duration: 4000, swayAmp: 26, hideOriginals: false });
|
||||
value = '';
|
||||
cursorPos = 0;
|
||||
anchorPos = null;
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (autofocus && container) {
|
||||
container.focus();
|
||||
}
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
return () => {
|
||||
if (blinkTimer) {
|
||||
clearTimeout(blinkTimer);
|
||||
blinkTimer = null;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
if (cursorPos > value.length) cursorPos = value.length;
|
||||
});
|
||||
|
||||
// Global mousemove / mouseup during drag.
|
||||
$effect(() => {
|
||||
if (!isDragging) return;
|
||||
|
||||
const onMove = (e: MouseEvent) => {
|
||||
const index = charIndexFromEvent(e);
|
||||
if (index === null) return;
|
||||
cursorPos = index;
|
||||
pauseBlink();
|
||||
};
|
||||
|
||||
window.addEventListener('mousemove', onMove);
|
||||
window.addEventListener('mouseup', () => (isDragging = false));
|
||||
return () => {
|
||||
window.removeEventListener('mousemove', onMove);
|
||||
window.removeEventListener('mouseup', () => (isDragging = false));
|
||||
};
|
||||
});
|
||||
|
||||
export function focusInput(): void {
|
||||
container?.focus();
|
||||
}
|
||||
|
||||
function pauseBlink() {
|
||||
blinkPaused = true;
|
||||
if (blinkTimer) clearTimeout(blinkTimer);
|
||||
blinkTimer = setTimeout(() => {
|
||||
blinkPaused = false;
|
||||
blinkTimer = null;
|
||||
}, blinkDelay);
|
||||
}
|
||||
|
||||
function handleFocus() {
|
||||
focused = true;
|
||||
blinkPaused = false;
|
||||
}
|
||||
|
||||
function handleBlur() {
|
||||
focused = false;
|
||||
if (blinkTimer) {
|
||||
clearTimeout(blinkTimer);
|
||||
blinkTimer = null;
|
||||
}
|
||||
blinkPaused = true;
|
||||
}
|
||||
|
||||
function charIndexFromEvent(e: MouseEvent): number | null {
|
||||
const elem = (e.target as HTMLElement | null)?.closest?.(
|
||||
'[data-char-index]'
|
||||
) as HTMLElement | null;
|
||||
if (!elem) return null;
|
||||
const raw = elem.getAttribute('data-char-index');
|
||||
return raw === null ? null : Number(raw);
|
||||
}
|
||||
|
||||
function getSelectedText(): string {
|
||||
if (!selection.active) return '';
|
||||
return value.slice(selection.start, selection.end);
|
||||
}
|
||||
|
||||
function deleteSelection(): void {
|
||||
if (!selection.active) return;
|
||||
value = value.slice(0, selection.start) + value.slice(selection.end);
|
||||
cursorPos = selection.start;
|
||||
anchorPos = null;
|
||||
oninput(value);
|
||||
}
|
||||
|
||||
function insertChar(char: string): void {
|
||||
if (selection.active) {
|
||||
value = value.slice(0, selection.start) + char + value.slice(selection.end);
|
||||
cursorPos = selection.start + char.length;
|
||||
anchorPos = null;
|
||||
} else {
|
||||
value = value.slice(0, cursorPos) + char + value.slice(cursorPos);
|
||||
cursorPos += char.length;
|
||||
}
|
||||
oninput(value);
|
||||
pauseBlink();
|
||||
}
|
||||
|
||||
function deleteCharBefore() {
|
||||
if (selection.active) {
|
||||
deleteSelection();
|
||||
pauseBlink();
|
||||
return;
|
||||
}
|
||||
if (cursorPos > 0) {
|
||||
value = value.slice(0, cursorPos - 1) + value.slice(cursorPos);
|
||||
oninput(value);
|
||||
cursorPos--;
|
||||
pauseBlink();
|
||||
}
|
||||
}
|
||||
|
||||
function deleteCharAfter() {
|
||||
if (selection.active) {
|
||||
deleteSelection();
|
||||
pauseBlink();
|
||||
return;
|
||||
}
|
||||
if (cursorPos < value.length) {
|
||||
value = value.slice(0, cursorPos) + value.slice(cursorPos + 1);
|
||||
oninput(value);
|
||||
pauseBlink();
|
||||
}
|
||||
}
|
||||
|
||||
function moveCursor(delta: number, extend: boolean) {
|
||||
const newPos = cursorPos + delta;
|
||||
if (newPos < 0 || newPos > value.length) return;
|
||||
if (extend) {
|
||||
if (anchorPos === null) anchorPos = cursorPos;
|
||||
} else {
|
||||
anchorPos = null;
|
||||
}
|
||||
cursorPos = newPos;
|
||||
pauseBlink();
|
||||
}
|
||||
|
||||
function moveTo(pos: number, extend: boolean) {
|
||||
if (extend) {
|
||||
if (anchorPos === null) anchorPos = cursorPos;
|
||||
} else {
|
||||
anchorPos = null;
|
||||
}
|
||||
cursorPos = pos;
|
||||
pauseBlink();
|
||||
}
|
||||
|
||||
function prevWordBoundary(pos: number): number {
|
||||
let i = pos;
|
||||
while (i > 0 && !/\s/.test(value[i - 1])) i--;
|
||||
while (i > 0 && /\s/.test(value[i - 1])) i--;
|
||||
return i;
|
||||
}
|
||||
|
||||
function nextWordBoundary(pos: number): number {
|
||||
let i = pos;
|
||||
while (i < value.length && !/\s/.test(value[i])) i++;
|
||||
while (i < value.length && /\s/.test(value[i])) i++;
|
||||
return i;
|
||||
}
|
||||
|
||||
function handleMouseDown(e: MouseEvent): void {
|
||||
e.preventDefault();
|
||||
const idx = charIndexFromEvent(e);
|
||||
const clickPos = idx ?? value.length;
|
||||
|
||||
if (e.shiftKey) {
|
||||
if (anchorPos === null) anchorPos = cursorPos;
|
||||
cursorPos = clickPos;
|
||||
} else {
|
||||
anchorPos = clickPos;
|
||||
cursorPos = clickPos;
|
||||
}
|
||||
isDragging = true;
|
||||
pauseBlink();
|
||||
}
|
||||
|
||||
function handlePaste(e: ClipboardEvent): void {
|
||||
e.preventDefault();
|
||||
const text = e.clipboardData?.getData('text/plain') ?? '';
|
||||
if (text) insertChar(text);
|
||||
}
|
||||
|
||||
function copySelection(): void {
|
||||
if (!selection.active) return;
|
||||
void navigator.clipboard.writeText(getSelectedText());
|
||||
}
|
||||
|
||||
function cutSelection(): void {
|
||||
if (!selection.active) return;
|
||||
void navigator.clipboard.writeText(getSelectedText());
|
||||
deleteSelection();
|
||||
pauseBlink();
|
||||
}
|
||||
|
||||
function handleKeydown(e: KeyboardEvent) {
|
||||
const isMod = e.ctrlKey || e.metaKey;
|
||||
const key = e.key.length === 1 ? e.key.toLowerCase() : e.key;
|
||||
|
||||
function insertIfChar(e: KeyboardEvent): ((e: KeyboardEvent) => void) | undefined {
|
||||
if (key.length !== 1 || e.altKey || e.metaKey) return undefined;
|
||||
return (e) => insertChar(e.key);
|
||||
}
|
||||
|
||||
const handler = isMod ? mods[key] : (base[key] ?? insertIfChar(e));
|
||||
if (!handler) return;
|
||||
|
||||
e.preventDefault();
|
||||
handler(e);
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
if (autofocus && container) {
|
||||
container.focus();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<div
|
||||
id="terminal-input"
|
||||
class="font-terminal relative inline-block whitespace-nowrap outline-none select-none"
|
||||
bind:this={container}
|
||||
tabindex="1"
|
||||
role="textbox"
|
||||
aria-label="terminal input"
|
||||
onblur={handleBlur}
|
||||
onfocus={handleFocus}
|
||||
onkeydown={handleKeydown}
|
||||
onpaste={handlePaste}
|
||||
onmousedown={handleMouseDown}
|
||||
style="--cursor-index: {cursorPos};"
|
||||
>
|
||||
<span class="prompt inline-block w-[1ch] text-center">$</span>
|
||||
<span class="inline cursor-text" aria-hidden="true">
|
||||
{#each value.split('') as char, i}
|
||||
<span
|
||||
class="char inline-block w-[1ch] text-center whitespace-pre"
|
||||
class:selected={isSelected(i)}
|
||||
data-char-index={i}>{char}</span
|
||||
>
|
||||
{/each}
|
||||
</span>
|
||||
<div
|
||||
id="curs"
|
||||
class="cursor bg-text-primary absolute bottom-[0.2rem] h-px w-[1ch]"
|
||||
class:animate-cursor-blink={!blinkPaused && !selection.active}
|
||||
></div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.cursor {
|
||||
left: calc(var(--cursor-index) * 1ch + 8.5px + 1ch);
|
||||
transition: all 0.1s ease;
|
||||
}
|
||||
.char {
|
||||
transition: all linear 0.1s;
|
||||
}
|
||||
|
||||
.char.selected {
|
||||
transition: none;
|
||||
background-color: var(--color-highlight);
|
||||
border-radius: 2px;
|
||||
}
|
||||
|
||||
.char.selected + .char.selected {
|
||||
border-top-left-radius: 0;
|
||||
border-bottom-left-radius: 0;
|
||||
}
|
||||
|
||||
.char.selected:has(+ .char.selected) {
|
||||
border-top-right-radius: 0;
|
||||
border-bottom-right-radius: 0;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,32 @@
|
||||
<script lang="ts">
|
||||
import { mwin_movable } from '#lib/mwinlib/mwin_controller.svelte.ts';
|
||||
import type { MWinParams } from '#lib/mwinlib/mwin_util.ts';
|
||||
|
||||
let {
|
||||
children,
|
||||
wrapper,
|
||||
mwinoptions,
|
||||
handle = $bindable(),
|
||||
class: className = '',
|
||||
...props
|
||||
}: {
|
||||
wrapper?: HTMLElement | string;
|
||||
children?: any;
|
||||
mwinoptions: Partial<MWinParams>;
|
||||
handle?: HTMLElement | string;
|
||||
class?: string;
|
||||
[x: string]: any;
|
||||
} = $props();
|
||||
</script>
|
||||
|
||||
<div
|
||||
{...props}
|
||||
class=" transition-discrete will-change-auto {className}"
|
||||
use:mwin_movable={{
|
||||
wrapper,
|
||||
handle,
|
||||
...mwinoptions
|
||||
}}
|
||||
>
|
||||
{@render children()}
|
||||
</div>
|
||||
@@ -0,0 +1,86 @@
|
||||
<script lang="ts">
|
||||
import { type MWinAPI, type MWinParams } from '#lib/mwinlib/mwin_util.ts';
|
||||
import Input from './Input.svelte';
|
||||
import Movable from './Movable.svelte';
|
||||
|
||||
let {
|
||||
id,
|
||||
wrapper,
|
||||
username = $bindable(),
|
||||
hostname = $bindable(),
|
||||
working_dir = $bindable(),
|
||||
mwin = $bindable(),
|
||||
close
|
||||
}: {
|
||||
id: number;
|
||||
wrapper: HTMLElement | string;
|
||||
username: string;
|
||||
hostname: string;
|
||||
working_dir: string;
|
||||
mwin: MWinAPI | undefined;
|
||||
close?: (id: number) => void;
|
||||
} = $props();
|
||||
|
||||
//let isProcessActive = $state(false);
|
||||
|
||||
let inputValue = $state('');
|
||||
let inputComponent: any;
|
||||
|
||||
const mwinoptions: Partial<MWinParams> = {
|
||||
handle: '#terminal-bar',
|
||||
resizable: { threshold: 10, minWidth: 300, minHeight: 200 },
|
||||
baseZIndex: 300,
|
||||
resizeHandle: '#terminal',
|
||||
onAPI: (api: MWinAPI) => {
|
||||
mwin = api;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<Movable
|
||||
{wrapper}
|
||||
{mwinoptions}
|
||||
class="shadow-subtle flex size-full h-256 w-420 flex-col rounded-lg"
|
||||
>
|
||||
<div
|
||||
id="terminal-bar"
|
||||
class=" bg-surface-2 grid grid-cols-[1fr_auto_1fr] rounded-t-md py-3 select-none"
|
||||
>
|
||||
<div class="dots-wrapper m-auto mx-3 flex size-max flex-row items-center justify-center gap-3">
|
||||
<button
|
||||
data-close-btn
|
||||
class="bg-term-close size-3 cursor-pointer rounded-full active:brightness-85"
|
||||
type="button"
|
||||
aria-label="Close terminal"
|
||||
onclick={() => close?.(id)}
|
||||
></button>
|
||||
<button
|
||||
class="bg-term-maximize size-3 cursor-pointer rounded-full active:brightness-85"
|
||||
type="button"
|
||||
aria-label="Maximize terminal"
|
||||
onclick={() => mwin?.maximize()}
|
||||
></button>
|
||||
<button
|
||||
class="bg-term-minimize size-3 cursor-pointer rounded-2xl active:brightness-85"
|
||||
type="button"
|
||||
aria-label="Minimize terminal"
|
||||
onclick={() => mwin?.minimize()}
|
||||
></button>
|
||||
</div>
|
||||
<span class=" text-primary mx-auto text-sm font-bold"
|
||||
>{username}@{hostname} — {working_dir} {mwin?.state.width}x{mwin?.state.height}</span
|
||||
>
|
||||
<div></div>
|
||||
</div>
|
||||
<div
|
||||
class="inner-content scroll-hidden bg-surface-1 text-text-primary grow origin-top overflow-y-auto rounded-b-md p-4 text-sm"
|
||||
id="cout"
|
||||
onclick={() => inputComponent?.focusInput()}
|
||||
onkeydown={() => inputComponent?.focusInput()}
|
||||
role="textbox"
|
||||
tabindex="-1"
|
||||
>
|
||||
<div id="outputWrapper"></div>
|
||||
<Input bind:value={inputValue} blinkDelay={500} bind:this={inputComponent} />
|
||||
</div>
|
||||
</Movable>
|
||||
@@ -0,0 +1,69 @@
|
||||
<script lang="ts">
|
||||
import { removeToast, toasts } from '#lib/stores/toasts.ts';
|
||||
import { CircleCheckBig, CircleX, Info, TriangleAlert, X } from '@lucide/svelte';
|
||||
import { onMount } from 'svelte';
|
||||
import { fly } from 'svelte/transition';
|
||||
|
||||
onMount(() => {
|
||||
toasts.set([]);
|
||||
});
|
||||
</script>
|
||||
|
||||
<div class="fixed top-4 right-4 z-50 flex h-full flex-col gap-2 overflow-visible">
|
||||
{#each $toasts as toast (toast.id)}
|
||||
<div
|
||||
transition:fly={{ x: 20, y: 0, duration: 300 }}
|
||||
id={`toast-${toast.id.toString()}`}
|
||||
class="bg-surface-2 w-md overflow-hidden rounded-md"
|
||||
onclick={() => toast.onClick?.(toast)}
|
||||
role="button"
|
||||
tabindex={toast.onClick ? 0 : undefined}
|
||||
onkeydown={toast.onClick
|
||||
? (e) => {
|
||||
if (e.key === 'Enter' || e.key === ' ') {
|
||||
e.preventDefault();
|
||||
toast.onClick?.(toast);
|
||||
}
|
||||
}
|
||||
: undefined}
|
||||
>
|
||||
<div class="flex items-center justify-start">
|
||||
<div class="mx-4 my-8">
|
||||
{#if toast.type === 'info'}
|
||||
<Info class=" text-info"></Info>
|
||||
{:else if toast.type === 'error'}
|
||||
<CircleX class="text-error"></CircleX>
|
||||
{:else if toast.type === 'warn'}
|
||||
<TriangleAlert class="text-warning"></TriangleAlert>
|
||||
{:else}
|
||||
<CircleCheckBig class="text-success"></CircleCheckBig>
|
||||
{/if}
|
||||
</div>
|
||||
<div class="my-2 mr-auto mb-auto ml-2 flex flex-col items-start justify-start select-none">
|
||||
<h5 class=" text- text-md text-text-primary font-bold">{toast.title}</h5>
|
||||
<p class=" text-md text-text-secondary mb-2 overflow-x-hidden overflow-y-scroll">
|
||||
{toast.message}
|
||||
</p>
|
||||
</div>
|
||||
<div class=" m-2 self-start">
|
||||
<button
|
||||
class:hidden={toast.hideDismiss}
|
||||
onclick={(e) => {
|
||||
e.stopPropagation();
|
||||
removeToast(toast.id);
|
||||
}}
|
||||
>
|
||||
<X
|
||||
stroke-width={3}
|
||||
size={18}
|
||||
class=" hover:text-text-secondary transition-all duration-200 active:scale-90"
|
||||
></X>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{#if !toast.hideProgress && toast.tween}
|
||||
<div style:padding-right={`${toast.tween.current}%`} class=" bg-primary h-[2px] w-0"></div>
|
||||
{/if}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
@@ -0,0 +1,27 @@
|
||||
<script lang="ts">
|
||||
let state = $state(false);
|
||||
|
||||
export function focus() {
|
||||
console.log('called');
|
||||
state = true;
|
||||
}
|
||||
|
||||
let { children, onclick }: { children?: any; onclick?: ({}?: unknown) => unknown } = $props();
|
||||
</script>
|
||||
|
||||
<button
|
||||
onclick={() => {
|
||||
onclick?.();
|
||||
console.log('im called');
|
||||
}}
|
||||
class="
|
||||
|
||||
cursor-pointer rounded-full
|
||||
transition-all duration-200 active:translate-y-0.5
|
||||
active:brightness-90 disabled:cursor-not-allowed disabled:opacity-50 disabled:hover:brightness-100
|
||||
"
|
||||
>
|
||||
<div class="">
|
||||
{@render children()}
|
||||
</div>
|
||||
</button>
|
||||
@@ -0,0 +1,580 @@
|
||||
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: Partial<MWinParams>) {
|
||||
const controller = new MWinController(node, options);
|
||||
const api: MWinAPI = {
|
||||
state: controller.state,
|
||||
minimize: () => controller.minimize(),
|
||||
maximize: () => controller.maximize(),
|
||||
close: () => controller.close(),
|
||||
focus: () => controller.focus()
|
||||
};
|
||||
|
||||
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: Partial<MWinParams>) {
|
||||
this.#node = node;
|
||||
|
||||
const wrapper = resolveElement(options.wrapper, node);
|
||||
if (!wrapper) throw new Error('[Movable] - Wrapper element not found!');
|
||||
this.#wrapper = wrapper ?? document;
|
||||
|
||||
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,
|
||||
onFocus: options.onFocus
|
||||
};
|
||||
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';
|
||||
this.#node.classList.add('[Movable]');
|
||||
|
||||
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.#opts.onFocus?.();
|
||||
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.setProperty('--x', this.state.x.toString());
|
||||
this.#node.style.setProperty('--y', this.state.y.toString());
|
||||
this.#node.style.setProperty('--w', this.state.width.toString());
|
||||
this.#node.style.setProperty('--h', this.state.height.toString());
|
||||
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?.();
|
||||
}
|
||||
|
||||
focus(): void {
|
||||
this.#bringToFront();
|
||||
}
|
||||
|
||||
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,99 @@
|
||||
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;
|
||||
onFocus?: () => 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;
|
||||
onFocus?: () => 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;
|
||||
focus: () => 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,191 @@
|
||||
// $lib/stores/petals.ts
|
||||
|
||||
function mulberry32(seed: number): () => number {
|
||||
let a = seed >>> 0;
|
||||
return () => {
|
||||
a = (a + 0x6d2b79f5) | 0;
|
||||
let t = Math.imul(a ^ (a >>> 15), 1 | a);
|
||||
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
||||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
||||
};
|
||||
}
|
||||
|
||||
function accelPosition(u: number, k: number): number {
|
||||
if (k < 0.01) return u * u;
|
||||
if (k > 50) return u;
|
||||
const denom = k - 1 + Math.exp(-k);
|
||||
return (k * u - 1 + Math.exp(-k * u)) / denom;
|
||||
}
|
||||
|
||||
export interface PetalsOptions {
|
||||
duration?: number;
|
||||
fallAccel?: number;
|
||||
fallDistance?: number;
|
||||
leadIn?: number;
|
||||
swayAmp?: number;
|
||||
swayCycles?: number;
|
||||
windX?: number;
|
||||
spin?: number;
|
||||
maxKeyframes?: number;
|
||||
seed?: number;
|
||||
hideOriginals?: boolean;
|
||||
}
|
||||
|
||||
export type PetalsCleanup = () => void;
|
||||
|
||||
let callCounter = 0;
|
||||
|
||||
function viewportBottom(): number {
|
||||
const vv = window.visualViewport;
|
||||
if (vv) return vv.height + vv.offsetTop;
|
||||
return Math.max(document.documentElement.clientHeight, window.innerHeight);
|
||||
}
|
||||
|
||||
export function petals(
|
||||
node: HTMLElement,
|
||||
{
|
||||
duration = 15000,
|
||||
fallAccel = 4,
|
||||
fallDistance,
|
||||
leadIn = 0.18,
|
||||
swayAmp = 22,
|
||||
swayCycles = 2.0,
|
||||
windX = 60,
|
||||
spin = 300,
|
||||
maxKeyframes = 90,
|
||||
seed,
|
||||
hideOriginals = true
|
||||
}: PetalsOptions = {}
|
||||
): PetalsCleanup {
|
||||
const chars = Array.from(node.querySelectorAll<HTMLElement>('.char')).filter(
|
||||
(el) => el.textContent?.trim() !== ''
|
||||
);
|
||||
|
||||
if (chars.length === 0) return () => {};
|
||||
|
||||
const effectiveSeed = seed ?? Date.now() ^ (callCounter++ * 0x9e3779b9);
|
||||
|
||||
const rect = node.getBoundingClientRect();
|
||||
const vb = viewportBottom();
|
||||
|
||||
// ── Clone + fixed overlay ────────────────────────────────────────────
|
||||
const clone = node.cloneNode(true) as HTMLElement;
|
||||
clone.removeAttribute('id');
|
||||
clone.querySelectorAll<HTMLElement>('[id]').forEach((n) => n.removeAttribute('id'));
|
||||
|
||||
clone.querySelectorAll<HTMLElement>('.prompt, .cursor').forEach((n) => {
|
||||
n.style.visibility = 'hidden';
|
||||
});
|
||||
|
||||
clone.style.cssText = `
|
||||
position: fixed;
|
||||
top: ${rect.top}px;
|
||||
left: ${rect.left}px;
|
||||
margin: 0;
|
||||
overflow: visible;
|
||||
pointer-events: none;
|
||||
z-index: 9999;
|
||||
contain: layout style;
|
||||
`;
|
||||
|
||||
const origFont = getComputedStyle(node);
|
||||
clone.style.fontSize = origFont.fontSize;
|
||||
clone.style.fontFamily = origFont.fontFamily;
|
||||
clone.style.fontWeight = origFont.fontWeight;
|
||||
clone.style.fontStyle = origFont.fontStyle;
|
||||
clone.style.fontVariant = origFont.fontVariant;
|
||||
clone.style.fontStretch = origFont.fontStretch;
|
||||
clone.style.lineHeight = origFont.lineHeight;
|
||||
clone.style.letterSpacing = origFont.letterSpacing;
|
||||
clone.style.wordSpacing = origFont.wordSpacing;
|
||||
clone.style.color = 'var(--color-text-primary)';
|
||||
clone.style.transition = 'var(--transition-standard)';
|
||||
|
||||
document.body.appendChild(clone);
|
||||
|
||||
if (hideOriginals) {
|
||||
chars.forEach((el) => {
|
||||
el.style.visibility = 'hidden';
|
||||
});
|
||||
}
|
||||
|
||||
const cloneChars = Array.from(clone.querySelectorAll<HTMLElement>('.char'));
|
||||
const petalRects: DOMRect[] = [];
|
||||
for (let i = 0; i < cloneChars.length; i++) {
|
||||
petalRects.push(cloneChars[i].getBoundingClientRect());
|
||||
}
|
||||
|
||||
const anims: Animation[] = [];
|
||||
|
||||
cloneChars.forEach((el, i) => {
|
||||
const rand = mulberry32(effectiveSeed + i * 100003);
|
||||
|
||||
const charDuration = duration * (1.85 + rand() * 1.43);
|
||||
const charAccel = fallAccel * (0.9 + rand() * 0.2);
|
||||
const charSwayCycles = swayCycles * (0.7 + rand() * 0.7);
|
||||
const charLeadIn = leadIn * (0.85 + rand() * 0.3);
|
||||
|
||||
const phase = rand() * Math.PI * 2;
|
||||
const dir: 1 | -1 = rand() < 0.5 ? -1 : 1;
|
||||
const amp = swayAmp * (0.5 + rand() * 0.9);
|
||||
const spinTotal = spin * (0.7 + rand() * 0.6) * dir;
|
||||
const charWindX = windX * (0.5 + rand() * 1.0);
|
||||
|
||||
let charFall: number;
|
||||
if (fallDistance != null) {
|
||||
charFall = fallDistance * (0.95 + rand() * 0.15);
|
||||
} else {
|
||||
const petalRect = petalRects[i];
|
||||
const neededFall = vb - petalRect.top + petalRect.height + 40;
|
||||
charFall = neededFall * (1 + rand() * 0.35);
|
||||
}
|
||||
|
||||
const N = Math.min(maxKeyframes, Math.max(40, Math.round(charDuration / 200)));
|
||||
const frames: Keyframe[] = [];
|
||||
|
||||
for (let k = 0; k <= N; k++) {
|
||||
const t = k / N;
|
||||
const y = charFall * accelPosition(t, charAccel);
|
||||
|
||||
const lead = Math.min(1, t / charLeadIn);
|
||||
const leadSmooth = lead * lead * (3 - 2 * lead);
|
||||
|
||||
const sway = amp * leadSmooth * Math.sin(2 * Math.PI * charSwayCycles * t + phase);
|
||||
const wind = charWindX * t;
|
||||
const x = sway + wind;
|
||||
|
||||
const r = spinTotal * t;
|
||||
|
||||
frames.push({
|
||||
transform: `translate(${x.toFixed(2)}px, ${y.toFixed(2)}px) rotate(${r.toFixed(2)}deg)`,
|
||||
offset: t
|
||||
});
|
||||
}
|
||||
|
||||
anims.push(
|
||||
el.animate(frames, {
|
||||
duration: charDuration,
|
||||
delay: 0,
|
||||
easing: 'linear',
|
||||
fill: 'forwards'
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
let cleaned = false;
|
||||
const doCleanup = (): void => {
|
||||
if (cleaned) return;
|
||||
cleaned = true;
|
||||
for (const a of anims) a.cancel();
|
||||
clone.remove();
|
||||
if (hideOriginals) {
|
||||
chars.forEach((el) => {
|
||||
el.style.visibility = '';
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
void Promise.allSettled(anims.map((a) => a.finished)).then(doCleanup);
|
||||
|
||||
return doCleanup;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
function getInitialTheme(): string {
|
||||
// On client, read from the data-theme attribute set by the blocking script
|
||||
if (typeof document !== 'undefined') {
|
||||
const theme = document.documentElement.getAttribute('data-theme');
|
||||
if (theme === 'dark' || theme === 'light') return theme;
|
||||
}
|
||||
// Fallback (SSR or if script failed)
|
||||
if (typeof window !== 'undefined') {
|
||||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
||||
}
|
||||
return 'dark';
|
||||
}
|
||||
|
||||
export const theme = writable(getInitialTheme());
|
||||
|
||||
theme.subscribe((value) => {
|
||||
if (typeof document !== 'undefined') {
|
||||
document.documentElement.setAttribute('data-theme', value);
|
||||
localStorage.setItem('theme', value);
|
||||
}
|
||||
});
|
||||
|
||||
// Optional: listen to system preference changes
|
||||
if (typeof window !== 'undefined') {
|
||||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
|
||||
theme.set(e.matches ? 'dark' : 'light');
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
import { Tween } from 'svelte/motion';
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
export interface ToastMessage {
|
||||
id: number;
|
||||
type: 'success' | 'error' | 'info' | 'warn';
|
||||
title: string;
|
||||
message: string;
|
||||
duration: number;
|
||||
tween?: Tween<number>;
|
||||
initial: number;
|
||||
target: number;
|
||||
hideProgress: boolean;
|
||||
hideDismiss: boolean;
|
||||
onClose?: (tm: ToastMessage) => void;
|
||||
onClick?: (tm: ToastMessage) => void;
|
||||
}
|
||||
|
||||
let toastId = 0;
|
||||
export const toasts = writable<ToastMessage[]>([]);
|
||||
|
||||
export function addToast({
|
||||
type,
|
||||
title,
|
||||
message,
|
||||
duration,
|
||||
initial,
|
||||
target,
|
||||
hideProgress,
|
||||
hideDismiss,
|
||||
onClose,
|
||||
onClick
|
||||
}: {
|
||||
type: ToastMessage['type'];
|
||||
title?: string;
|
||||
message?: string;
|
||||
duration?: number;
|
||||
initial?: number;
|
||||
target?: number;
|
||||
hideProgress?: boolean;
|
||||
hideDismiss?: boolean;
|
||||
onClose?: (tm: ToastMessage) => void;
|
||||
onClick?: (tm: ToastMessage) => void;
|
||||
}) {
|
||||
const id = ++toastId;
|
||||
title = title ?? '';
|
||||
message = message ?? '';
|
||||
duration = duration ?? 3000;
|
||||
initial = initial ?? 100;
|
||||
target = target ?? 0;
|
||||
hideProgress = hideProgress ?? false;
|
||||
hideDismiss = hideDismiss ?? false;
|
||||
|
||||
let isPermament: boolean = duration === 0 || duration === Infinity;
|
||||
|
||||
if (hideDismiss) isPermament = false;
|
||||
if (isPermament) hideProgress = true;
|
||||
|
||||
let tween: Tween<number> | undefined;
|
||||
|
||||
if (!isPermament) {
|
||||
tween = new Tween(initial, { duration: duration });
|
||||
tween.set(target).then(() => {
|
||||
removeToast(id);
|
||||
});
|
||||
}
|
||||
|
||||
toasts.update((t) => [
|
||||
...t,
|
||||
{
|
||||
id,
|
||||
type,
|
||||
title,
|
||||
message,
|
||||
duration,
|
||||
tween,
|
||||
initial,
|
||||
target,
|
||||
hideProgress,
|
||||
hideDismiss,
|
||||
onClose,
|
||||
onClick
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
export function removeToast(id: number) {
|
||||
let closedToast: ToastMessage | undefined;
|
||||
toasts.update((ts) => {
|
||||
closedToast = ts.find((t) => t.id === id);
|
||||
return ts.filter((t) => t.id !== id);
|
||||
});
|
||||
|
||||
if (closedToast?.onClose) closedToast.onClose(closedToast);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export function capitalize(word: string): string {
|
||||
if (!word) '';
|
||||
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
|
||||
}
|
||||
Reference in New Issue
Block a user