Update the browser, editor, settings, and installer
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
<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 { state as browserState, pageErrors, request, frames, pageEvents, type PageMenu, type BrowserFrame } from './client';
|
||||
import type { ActionMenuItem } from '$lib/ui/actionMenu';
|
||||
import { showActionMenu } from '$lib/ui/managedActionMenu';
|
||||
import { WheelQueue } from './inputQueue';
|
||||
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 { initial, onSessionChange, newTab }: { initial: { tabId?: string; profileId?: string; url?: string }; onSessionChange: (value: TabStatus) => void; newTab: (profileId: string, url?: 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);
|
||||
@@ -12,14 +15,19 @@
|
||||
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 width = 1000, height = 700, textInput: HTMLTextAreaElement, composing = false;
|
||||
let inputQueue = Promise.resolve();
|
||||
const orderedInput = (command: string, args: Record<string, unknown>) => inputQueue = inputQueue.then(() => send(command, args));
|
||||
const wheels = new WheelQueue(input => send('input', { input }));
|
||||
let displayedFrame: BrowserFrame | undefined;
|
||||
let closeMenu: (() => void) | undefined;
|
||||
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 scrollFrame = 0, moveFrame = 0, movePending = false;
|
||||
let pendingMove: Record<string, unknown> | undefined;
|
||||
let suppressedRelease = -1;
|
||||
const result = $derived(current?.find?.text === findText ? current.find : null);
|
||||
@@ -50,10 +58,53 @@
|
||||
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}`;
|
||||
if (displayedFrame) acknowledgeFrame();
|
||||
if (frame.width > 0 && frame.height > 0 && ['image/png', 'image/jpeg'].includes(frame.mime)) {
|
||||
displayedFrame = frame; image = `data:${frame.mime};base64,${frame.data}`;
|
||||
}
|
||||
});
|
||||
return () => { frames.delete(id); };
|
||||
pageEvents.set(id, event => {
|
||||
if (event.event === 'context-menu') showPageMenu(event);
|
||||
else if (event.event === 'open-link' && event.url) void newTab(current?.profileId || 'user', event.url);
|
||||
});
|
||||
return () => { acknowledgeFrame(); frames.delete(id); pageEvents.delete(id); closeMenu?.(); };
|
||||
});
|
||||
function acknowledgeFrame() {
|
||||
const frame = displayedFrame; displayedFrame = undefined;
|
||||
if (frame) void request('frameAck', { tabId: frame.tabId, frameId: frame.frameId }).catch(() => {});
|
||||
}
|
||||
function showPageMenu(context: PageMenu) {
|
||||
stopScroll(); closeMenu?.();
|
||||
const action = (name: string) => () => { void send('contextAction', { token: context.token, action: name }); };
|
||||
const edit = (name: string) => () => { void orderedInput('edit', { action: name }); };
|
||||
const items: ActionMenuItem[] = [];
|
||||
if (context.linkURL) items.push(
|
||||
{ label: 'Open link in new tab', icon: 'file-plus', run: () => void newTab(current?.profileId || 'user', context.linkURL) },
|
||||
{ label: 'Open link in default browser', icon: 'external', run: action('openExternal') },
|
||||
{ label: 'Copy link address', icon: 'copy', run: action('copyLink') },
|
||||
{ label: 'Save link as…', icon: 'save', run: action('saveLink') }, { separator: true });
|
||||
if (context.hasImageContents) items.push(
|
||||
{ label: 'Save image as…', icon: 'save', disabled: !context.srcURL, run: action('saveImage') },
|
||||
{ label: 'Copy image', icon: 'copy', run: action('copyImage') },
|
||||
{ label: 'Copy image address', icon: 'copy', disabled: !context.srcURL, run: action('copyImageAddress') }, { separator: true });
|
||||
if (context.isEditable) items.push(
|
||||
{ label: 'Undo', icon: 'undo', shortcut: 'Ctrl+Z', disabled: !context.editFlags.canUndo, run: edit('undo') },
|
||||
{ label: 'Redo', icon: 'redo', shortcut: 'Ctrl+Shift+Z', disabled: !context.editFlags.canRedo, run: edit('redo') }, { separator: true },
|
||||
{ label: 'Cut', icon: 'cut', shortcut: 'Ctrl+X', disabled: !context.editFlags.canCut, run: edit('cut') });
|
||||
if (context.isEditable || context.selectionText) items.push({ label: 'Copy', icon: 'copy', shortcut: 'Ctrl+C', disabled: !context.editFlags.canCopy, run: edit('copy') });
|
||||
if (context.isEditable) items.push(
|
||||
{ label: 'Paste', icon: 'paste', shortcut: 'Ctrl+V', disabled: !context.editFlags.canPaste, run: edit('paste') },
|
||||
{ label: 'Paste as plain text', icon: 'paste-text', shortcut: 'Ctrl+Shift+V', disabled: !context.editFlags.canPaste, run: edit('pasteAndMatchStyle') },
|
||||
{ label: 'Select all', icon: 'task', shortcut: 'Ctrl+A', run: edit('selectAll') }, { separator: true });
|
||||
items.push({ label: 'Back', icon: 'arrow-left', disabled: !current?.canBack, run: () => void send('back') },
|
||||
{ label: 'Forward', icon: 'arrow-right', disabled: !current?.canForward, run: () => void send('forward') },
|
||||
{ label: 'Reload', icon: 'refresh', run: () => void send('reload') },
|
||||
{ label: 'Find in page', icon: 'search', shortcut: 'Ctrl+F', run: () => void find() });
|
||||
const rect = surface.getBoundingClientRect();
|
||||
const menu = showActionMenu({ x: rect.left + context.x * rect.width / width, y: rect.top + context.y * rect.height / height,
|
||||
items, label: 'Browser page actions' });
|
||||
closeMenu = menu.close;
|
||||
}
|
||||
$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; });
|
||||
@@ -75,26 +126,25 @@
|
||||
timer = setTimeout(syncSize, 100);
|
||||
});
|
||||
resize.observe(surface);
|
||||
return () => { mounted = false; stopScroll(); cancelAnimationFrame(moveFrame); clearTimeout(timer); clearTimeout(findTimer); resize.disconnect(); offState(); void connection.close(); };
|
||||
return () => { mounted = false; stopScroll(); wheels.close(); closeMenu?.(); 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 });
|
||||
width = Math.max(100, Math.min(5120, Math.round(rect.width)));
|
||||
height = Math.max(100, Math.min(3200, Math.round(rect.height)));
|
||||
void send('resize', { width, height, scale: Math.max(1, Math.min(3, devicePixelRatio || 1)) }); 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 stopScroll() { autoScroll.stop(); wheels.clear(); 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;
|
||||
if (delta && (delta.x || delta.y)) {
|
||||
const rect = surface.getBoundingClientRect();
|
||||
if (!rect.width || !rect.height) { wheelPending = false; stopScroll(); return; }
|
||||
if (!rect.width || !rect.height) { 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);
|
||||
wheels.push({ 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 });
|
||||
}
|
||||
scrollFrame = requestAnimationFrame(scrollStep);
|
||||
}
|
||||
@@ -108,7 +158,7 @@
|
||||
});
|
||||
}
|
||||
function pointer(event: MouseEvent | WheelEvent, type: string) {
|
||||
event.preventDefault(); event.stopPropagation(); if (type === 'mouseDown') surface.focus();
|
||||
event.preventDefault(); event.stopPropagation(); if (type === 'mouseDown') textInput.focus({ preventScroll: true });
|
||||
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; }
|
||||
@@ -124,22 +174,29 @@
|
||||
}
|
||||
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 } : {}) };
|
||||
...('deltaY' in event ? { deltaY: -event.deltaY * (event.deltaMode === 1 ? 40 : event.deltaMode === 2 ? height : 1), deltaX: -event.deltaX * (event.deltaMode === 1 ? 40 : event.deltaMode === 2 ? width : 1) } : {}) };
|
||||
if (type === 'mouseWheel') { wheels.push(input as Parameters<WheelQueue['push']>[0]); return; }
|
||||
if (type === 'mouseMove') {
|
||||
pendingMove = input;
|
||||
if (!moveFrame && !movePending) moveFrame = requestAnimationFrame(flushMove);
|
||||
} else { pendingMove = undefined; void send('input', { input }); }
|
||||
} else { pendingMove = undefined; void orderedInput('input', { input }); }
|
||||
}
|
||||
function key(event: KeyboardEvent, type: string) {
|
||||
if (event.isComposing || composing || event.key === 'Process' || event.key === 'Dead') return;
|
||||
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 editActions: Record<string, string> = { a: 'selectAll', c: 'copy', x: 'cut', v: event.shiftKey ? 'pasteAndMatchStyle' : 'paste', z: event.shiftKey ? 'redo' : 'undo', y: 'redo' };
|
||||
if ((event.ctrlKey || event.metaKey) && editActions[event.key.toLowerCase()]) {
|
||||
if (type === 'keyDown') void orderedInput('edit', { action: editActions[event.key.toLowerCase()] });
|
||||
return;
|
||||
}
|
||||
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 } });
|
||||
void orderedInput('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 orderedInput('insertText', { text: event.key });
|
||||
}
|
||||
</script>
|
||||
<svelte:window onblur={stopScroll} />
|
||||
@@ -184,11 +241,17 @@
|
||||
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}
|
||||
<textarea class="text-input" bind:this={textInput} aria-label="Web page text input" autocomplete="off" spellcheck="false"
|
||||
oncompositionstart={() => composing = true}
|
||||
oncompositionend={event => { composing = false; if (event.data) void orderedInput('insertText', { text: event.data }); textInput.value = ''; }}
|
||||
oninput={() => { if (!composing && textInput.value) { void orderedInput('insertText', { text: textInput.value }); textInput.value = ''; } }}
|
||||
onblur={stopScroll}></textarea>
|
||||
{#if image && ready}<img src={image} alt="Web page rendered by the browser" draggable="false" onload={acknowledgeFrame} onerror={acknowledgeFrame}>{/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>
|
||||
.text-input{position:absolute;left:0;top:0;width:1px;height:1px;opacity:0;pointer-events:none;padding:0;border:0;}
|
||||
.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}
|
||||
|
||||
Reference in New Issue
Block a user