Update editor, workspace, Git services, appearance, and project guides
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
<script lang="ts">
|
||||
import { onMount, untrack, tick } from 'svelte';
|
||||
import IconButton from '$lib/ui/IconButton.svelte';
|
||||
import { state as browserState, pageErrors, request, frames } from './client';
|
||||
import { TabSession, type TabStatus } from './tabSession';
|
||||
import { AutoScroll } from './autoScroll';
|
||||
let { initial, onSessionChange, newTab }: { initial: { tabId?: string; profileId?: string; url?: string }; onSessionChange: (value: TabStatus) => void; newTab: (profileId: string) => Promise<void> } = $props();
|
||||
let status = $state<TabStatus>({ tabId: '', phase: 'connecting', message: 'Opening browser…' });
|
||||
const connection = new TabSession(request, untrack(() => initial), value => { status = value; onSessionChange(value); });
|
||||
const tabId = $derived(status.tabId);
|
||||
const ready = $derived(status.phase === 'ready');
|
||||
const current = $derived($browserState?.tabs.find(tab => tab.id === tabId));
|
||||
let address = $state(''); let editing = $state(false);
|
||||
let image = $state(''); let surface: HTMLDivElement; let addressField: HTMLInputElement;
|
||||
let width = 1000, height = 700;
|
||||
let opening = $state(false), openingError = $state(''), mounted = $state(false);
|
||||
let findOpen = $state(false), findText = $state('');
|
||||
let findField: HTMLInputElement | undefined = $state();
|
||||
let findTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
const autoScroll = new AutoScroll();
|
||||
let scrollAnchor = $state<{ x: number; y: number } | null>(null);
|
||||
let scrollFrame = 0, moveFrame = 0, wheelPending = false, movePending = false;
|
||||
let pendingMove: Record<string, unknown> | undefined;
|
||||
let suppressedRelease = -1;
|
||||
const result = $derived(current?.find?.text === findText ? current.find : null);
|
||||
const documentKey = $derived(`${tabId}:${current?.url}:${!!current?.loading}`);
|
||||
$effect(() => {
|
||||
void documentKey;
|
||||
untrack(stopScroll);
|
||||
if (ready && findOpen) untrack(() => { if (findText && !current?.loading) search(); });
|
||||
});
|
||||
export async function find() {
|
||||
findOpen = true;
|
||||
await tick(); findField?.focus(); findField?.select();
|
||||
}
|
||||
export function detach() { connection.detach(); }
|
||||
export function action(command: string, args: Record<string, unknown> = {}) { return send(command, args); }
|
||||
function search(next = false, forward = true) {
|
||||
clearTimeout(findTimer);
|
||||
void send('find', { text: findText, next, forward });
|
||||
}
|
||||
function closeFind() { clearTimeout(findTimer); findOpen = false; void send('stopFind'); surface.focus(); }
|
||||
function findKey(event: KeyboardEvent) {
|
||||
if (event.key === 'Escape') { event.preventDefault(); event.stopPropagation(); closeFind(); }
|
||||
else if (event.key === 'Enter' && event.target === findField) { event.preventDefault(); event.stopPropagation(); search(true, !event.shiftKey); }
|
||||
}
|
||||
$effect(() => { if (!editing && current?.url) address = current.url === 'about:blank' ? '' : current.url; });
|
||||
$effect(() => {
|
||||
const id = tabId;
|
||||
image = '';
|
||||
if (!id) return;
|
||||
frames.set(id, frame => {
|
||||
if (frame.width > 0 && frame.height > 0 && frame.png.startsWith('iVBORw0KGgo')) image = `data:image/png;base64,${frame.png}`;
|
||||
});
|
||||
return () => { frames.delete(id); };
|
||||
});
|
||||
$effect(() => { if (mounted && ready && tabId) untrack(syncSize); });
|
||||
async function send(command: string, args = {}) {
|
||||
if (command === 'navigate') pageErrors.update(errors => { const next = { ...errors }; delete next[tabId]; return next; });
|
||||
await connection.send(command, args);
|
||||
}
|
||||
async function openTab(profileId = current?.profileId || $browserState?.profiles[0]?.id || 'user') {
|
||||
if (opening) return;
|
||||
opening = true;
|
||||
try { openingError = ''; await newTab(profileId); } catch (error) { openingError = `Could not open a browser tab: ${String(error)}`; }
|
||||
finally { opening = false; }
|
||||
}
|
||||
onMount(() => {
|
||||
mounted = true;
|
||||
const offState = browserState.subscribe(value => { if (value) connection.observe(value); });
|
||||
void connection.connect();
|
||||
let timer: ReturnType<typeof setTimeout>;
|
||||
const resize = new ResizeObserver(entries => {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(syncSize, 100);
|
||||
});
|
||||
resize.observe(surface);
|
||||
return () => { mounted = false; stopScroll(); cancelAnimationFrame(moveFrame); clearTimeout(timer); clearTimeout(findTimer); resize.disconnect(); offState(); void connection.close(); };
|
||||
});
|
||||
function syncSize() {
|
||||
const rect = surface.getBoundingClientRect();
|
||||
if (!rect.width || !rect.height) { stopScroll(); void send('visible', { visible: false }); return; }
|
||||
width = Math.max(100, Math.min(2560, Math.round(rect.width)));
|
||||
height = Math.max(100, Math.min(1600, Math.round(rect.height)));
|
||||
void send('resize', { width, height }); void send('visible', { visible: true });
|
||||
}
|
||||
const modifiers = (event: MouseEvent | KeyboardEvent) => [event.shiftKey && 'shift', event.ctrlKey && 'control', event.altKey && 'alt', event.metaKey && 'meta'].filter(Boolean);
|
||||
function stopScroll() { autoScroll.stop(); scrollAnchor = null; cancelAnimationFrame(scrollFrame); }
|
||||
function scrollStep(time: number) {
|
||||
if (!autoScroll.anchor || !ready) { stopScroll(); return; }
|
||||
const delta = autoScroll.delta(time);
|
||||
if (delta && !wheelPending && (delta.x || delta.y)) {
|
||||
wheelPending = true;
|
||||
const rect = surface.getBoundingClientRect();
|
||||
if (!rect.width || !rect.height) { wheelPending = false; stopScroll(); return; }
|
||||
const anchor = autoScroll.anchor;
|
||||
void send('input', { input: { type: 'mouseWheel', x: Math.min(width - 1, Math.round(anchor.x * width / rect.width)), y: Math.min(height - 1, Math.round(anchor.y * height / rect.height)), deltaX: -delta.x, deltaY: -delta.y } }).finally(() => wheelPending = false);
|
||||
}
|
||||
scrollFrame = requestAnimationFrame(scrollStep);
|
||||
}
|
||||
function flushMove() {
|
||||
moveFrame = 0;
|
||||
if (!pendingMove || movePending) return;
|
||||
const input = pendingMove; pendingMove = undefined; movePending = true;
|
||||
void send('input', { input }).finally(() => {
|
||||
movePending = false;
|
||||
if (mounted && pendingMove) moveFrame = requestAnimationFrame(flushMove);
|
||||
});
|
||||
}
|
||||
function pointer(event: MouseEvent | WheelEvent, type: string) {
|
||||
event.preventDefault(); event.stopPropagation(); if (type === 'mouseDown') surface.focus();
|
||||
const rect = surface.getBoundingClientRect();
|
||||
const point = { x: event.clientX - rect.left, y: event.clientY - rect.top };
|
||||
if (type === 'mouseUp' && event.button === suppressedRelease) { suppressedRelease = -1; return; }
|
||||
if (type === 'mouseDown' && autoScroll.anchor) { suppressedRelease = event.button; stopScroll(); return; }
|
||||
if (type === 'mouseDown' && event.button === 1 && ready) {
|
||||
pendingMove = undefined; cancelAnimationFrame(moveFrame); moveFrame = 0;
|
||||
autoScroll.start(point); scrollAnchor = point; scrollFrame = requestAnimationFrame(scrollStep); return;
|
||||
}
|
||||
if (autoScroll.anchor) {
|
||||
if (type === 'mouseMove') autoScroll.move(point);
|
||||
if (type === 'mouseUp' && event.button === 1) { autoScroll.release(); if (!autoScroll.anchor) stopScroll(); }
|
||||
if (type === 'mouseWheel') stopScroll(); else return;
|
||||
}
|
||||
const input = { type, x: Math.max(0, Math.min(width - 1, Math.round(point.x * width / rect.width))), y: Math.max(0, Math.min(height - 1, Math.round(point.y * height / rect.height))),
|
||||
button: ['left', 'middle', 'right'][event.button] || 'left', clickCount: event.detail, modifiers: modifiers(event),
|
||||
...('deltaY' in event ? { deltaY: -event.deltaY, deltaX: -event.deltaX } : {}) };
|
||||
if (type === 'mouseMove') {
|
||||
pendingMove = input;
|
||||
if (!moveFrame && !movePending) moveFrame = requestAnimationFrame(flushMove);
|
||||
} else { pendingMove = undefined; void send('input', { input }); }
|
||||
}
|
||||
function key(event: KeyboardEvent, type: string) {
|
||||
if (autoScroll.anchor) { stopScroll(); if (event.key === 'Escape') { event.preventDefault(); event.stopPropagation(); return; } }
|
||||
if ((event.ctrlKey || event.metaKey) && !event.shiftKey && !event.altKey && event.key.toLowerCase() === 'f') { event.preventDefault(); event.stopPropagation(); if (type === 'keyDown') void find(); return; }
|
||||
if (event.key === 'Escape' && findOpen) { event.preventDefault(); event.stopPropagation(); if (type === 'keyDown') closeFind(); return; }
|
||||
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 'l') { event.preventDefault(); addressField.focus(); addressField.select(); return; }
|
||||
if ((event.ctrlKey || event.metaKey) && [',', 'w', 't'].includes(event.key.toLowerCase())) return;
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
const names: Record<string, string> = { ArrowLeft: 'LEFT', ArrowRight: 'RIGHT', ArrowUp: 'UP', ArrowDown: 'DOWN', Enter: 'ENTER', Backspace: 'BACKSPACE', Escape: 'ESCAPE', Delete: 'DELETE', Tab: 'TAB', Home: 'HOME', End: 'END', PageUp: 'PAGEUP', PageDown: 'PAGEDOWN', ' ': 'SPACE' };
|
||||
void send('input', { input: { type, keyCode: names[event.key] || event.key, modifiers: modifiers(event) } });
|
||||
if (type === 'keyDown' && event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey) void send('input', { input: { type: 'char', keyCode: event.key } });
|
||||
}
|
||||
</script>
|
||||
<svelte:window onblur={stopScroll} />
|
||||
<svelte:document onvisibilitychange={() => { if (document.hidden) stopScroll(); }} />
|
||||
<div class="browser" data-browser-tab={tabId} data-browser-state={status.phase}>
|
||||
<form class="toolbar" onsubmit={event => { event.preventDefault(); editing = false; void send('navigate', { input: address }); surface.focus(); }}>
|
||||
<IconButton icon="arrow-left" label="Back" disabled={!ready || !current?.canBack} onclick={() => void send('back')} />
|
||||
<IconButton icon="arrow-right" label="Forward" disabled={!ready || !current?.canForward} onclick={() => void send('forward')} />
|
||||
<IconButton icon={current?.loading ? 'stop' : 'refresh'} label={current?.loading ? 'Stop loading' : 'Reload'} disabled={!ready} onclick={() => void send(current?.loading ? 'stop' : 'reload')} />
|
||||
<input bind:this={addressField} bind:value={address} disabled={!ready} onfocus={() => editing = true} onblur={() => editing = false} aria-label="Address or search" placeholder="Search or enter an address" spellcheck="false" autocomplete="off">
|
||||
<IconButton icon="bookmark" label="Bookmark this page" disabled={!ready || !current?.url.startsWith('http')} onclick={() => void send('bookmark')} />
|
||||
<select aria-label="Browser profile" value={current?.profileId} disabled={opening} onchange={event => void openTab(event.currentTarget.value)}>
|
||||
{#each $browserState?.profiles || [] as profile}<option value={profile.id}>{profile.name}</option>{/each}
|
||||
</select>
|
||||
</form>
|
||||
{#if findOpen}
|
||||
<!-- svelte-ignore a11y_no_noninteractive_element_interactions (Find controls share the Escape shortcut.) -->
|
||||
<form class="findbar" role="search" aria-label="Find in page" onkeydown={findKey} onsubmit={event => { event.preventDefault(); search(true); }}>
|
||||
<input bind:this={findField} bind:value={findText} aria-label="Find in page" placeholder="Find in page" maxlength="1000" autocomplete="off" spellcheck="false"
|
||||
disabled={!ready} oninput={() => { clearTimeout(findTimer); findTimer = setTimeout(() => search(), 120); }}>
|
||||
<span class="find-count" role="status" aria-live="polite">{!findText ? '' : !result || result.pending ? 'Searching…' : result.matches ? `${result.activeMatch} of ${result.matches}` : 'No matches'}</span>
|
||||
<IconButton icon="up" label="Previous match (Shift+Enter)" disabled={!ready || !findText} onclick={() => search(true, false)} />
|
||||
<IconButton icon="down" label="Next match (Enter)" disabled={!ready || !findText} onclick={() => search(true)} />
|
||||
<IconButton icon="close" label="Close find (Escape)" onclick={closeFind} />
|
||||
</form>
|
||||
{/if}
|
||||
{#if status.phase === 'connecting'}<p class="status" role="status">{status.message}</p>{/if}
|
||||
{#if ready && status.message}<p class="error" role="alert">{status.message}</p>{/if}
|
||||
{#if status.phase === 'disconnected'}
|
||||
<div class="error" role="alert"><p>{status.message}</p><button class="reconnect" onclick={() => void connection.connect()}>Reconnect</button></div>
|
||||
{/if}
|
||||
{#if status.detail}
|
||||
<details class="diagnostics"><summary>Browser error details</summary><dl>
|
||||
<dt>Operation</dt><dd>{status.detail.operation}</dd><dt>Reason</dt><dd>{status.detail.reason}</dd>
|
||||
<dt>Tab</dt><dd>{status.detail.tabId || 'Not created'}</dd><dt>Time</dt><dd>{status.detail.time}</dd>
|
||||
<dt>Browser</dt><dd>Included with RuneStone</dd>
|
||||
</dl></details>
|
||||
{/if}
|
||||
{#if openingError || $pageErrors[tabId]}<p class="error" role="alert">{openingError || $pageErrors[tabId]}</p>{/if}
|
||||
<!-- svelte-ignore a11y_no_noninteractive_tabindex, a11y_no_noninteractive_element_interactions (This surface forwards native browser input.) -->
|
||||
<div class="surface" class:autoscroll={!!scrollAnchor} bind:this={surface} role="application" aria-label="Web page" tabindex="0"
|
||||
onmousedown={event => pointer(event, 'mouseDown')} onmouseup={event => pointer(event, 'mouseUp')}
|
||||
onmousemove={event => pointer(event, 'mouseMove')} onwheel={event => pointer(event, 'mouseWheel')}
|
||||
onkeydown={event => key(event, 'keyDown')} onkeyup={event => key(event, 'keyUp')} onblur={stopScroll} onmouseleave={stopScroll} onauxclick={event => event.preventDefault()} oncontextmenu={event => event.preventDefault()}>
|
||||
{#if image && ready}<img src={image} alt="Web page rendered by the browser" draggable="false">{/if}
|
||||
{#if scrollAnchor}<span class="scroll-anchor" style:left={`${scrollAnchor.x}px`} style:top={`${scrollAnchor.y}px`} aria-hidden="true">↕</span>{/if}
|
||||
</div>
|
||||
</div>
|
||||
<style>
|
||||
.browser{display:flex;flex-direction:column;width:100%;height:100%;min-height:200px;background:var(--rs-bg);color:var(--rs-text);font-family:var(--rs-font-ui)}
|
||||
.toolbar,.findbar{display:flex;align-items:center;gap:4px;padding:5px 8px;border-bottom:1px solid var(--rs-border);background:var(--rs-bg-elevated)}
|
||||
.toolbar{flex-wrap:wrap}.toolbar input{flex:1 1 120px}.findbar input{max-width:320px}.find-count{font-size:12px;white-space:nowrap;color:var(--rs-text-dim);min-width:66px}
|
||||
input,select{min-width:0;border:1px solid var(--rs-border);border-radius:var(--rs-control-radius);background:var(--rs-bg);color:var(--rs-text);font:inherit;padding:5px 8px}
|
||||
input{flex:1}select{max-width:160px}input:focus-visible,select:focus-visible{outline:2px solid var(--rs-accent)}
|
||||
.surface{position:relative;flex:1;min-height:0;overflow:hidden;outline:none;background:var(--rs-bg)}.surface:focus-visible{box-shadow:inset 0 0 0 1px var(--rs-accent)}
|
||||
.surface.autoscroll{cursor:all-scroll}.scroll-anchor{position:absolute;transform:translate(-50%,-50%);display:grid;place-items:center;width:24px;height:24px;border:1px solid var(--rs-text);border-radius:50%;background:var(--rs-bg-elevated);color:var(--rs-text);font-size:20px;pointer-events:none;box-shadow:0 1px 4px #0006}
|
||||
img{display:block;width:100%;height:100%;pointer-events:none;user-select:none}.error{margin:0;padding:5px 10px;color:var(--rs-error);font-size:12px}
|
||||
.error p{margin:4px 0}.status,.diagnostics{margin:0;padding:8px 10px;font-size:12px;color:var(--rs-text-dim)}
|
||||
.reconnect{padding:6px 12px;margin:4px 0;border:1px solid var(--rs-border);border-radius:var(--rs-control-radius);background:var(--rs-bg-elevated);color:var(--rs-text);cursor:pointer}
|
||||
.reconnect:focus-visible,summary:focus-visible{outline:2px solid var(--rs-accent);outline-offset:2px}summary{cursor:pointer}
|
||||
dl{display:grid;grid-template-columns:auto 1fr;gap:4px 12px}dd{margin:0;overflow-wrap:anywhere;user-select:text}
|
||||
</style>
|
||||
Reference in New Issue
Block a user