Improve editor, plugin updates, and window transfers for 0.1.41
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
import { t } from "$lib/i18n";
|
||||
import { pluginHost } from "$lib/plugins/registry";
|
||||
import type { GitLineInfo } from "$lib/git/contracts";
|
||||
import FindPanel from "$lib/editor/FindPanel.svelte";
|
||||
import { readingSearch } from "$lib/editor/readingSearch";
|
||||
import type { FindTarget } from "$lib/editor/findTarget";
|
||||
import EditorHost from "$lib/editor/EditorHost.svelte";
|
||||
import LiveWriter from "$lib/editor/LiveWriter.svelte";
|
||||
import { tick } from "svelte";
|
||||
@@ -69,6 +72,32 @@
|
||||
let element: HTMLDivElement | undefined = $state();
|
||||
let writerEditor: LiveWriter | undefined = $state();
|
||||
let sourceEditor: EditorHost | undefined = $state();
|
||||
let readingFind = $state.raw<FindTarget>();
|
||||
let readingFindPanel = $state<FindPanel>();
|
||||
$effect(() => { void buffer?.id; void reading; void previewMode; readingFind = undefined; });
|
||||
async function documentSearch(event: KeyboardEvent) {
|
||||
if (event.target instanceof Element && event.target.closest('[role="menu"], dialog, [role="dialog"]')) return;
|
||||
const key = event.key.toLowerCase();
|
||||
const open = (event.ctrlKey || event.metaKey) && !event.altKey && !event.shiftKey && (key === "f" || key === "h");
|
||||
const advance = key === "f3" && !event.ctrlKey && !event.metaKey && !event.altKey;
|
||||
if ((!open && !advance) || buffer?.kind !== "text") return;
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
const inReading = reading && (previewMode === "preview" || event.target instanceof Element && (!!event.target.closest(".md-page") || !!readingFind && !!event.target.closest(".rs-find-bar")));
|
||||
if (inReading) {
|
||||
const root = element?.querySelector<HTMLElement>(".reading");
|
||||
if (!root) return;
|
||||
sourceEditor?.closeFind();
|
||||
writerEditor?.closeFind();
|
||||
readingFind ??= readingSearch(root, () => { readingFind = undefined; });
|
||||
await tick();
|
||||
if (advance) readingFind.next(event.shiftKey);
|
||||
else readingFindPanel?.focus();
|
||||
} else {
|
||||
readingFind = undefined;
|
||||
if (writerEditor) { if (advance) writerEditor.nextMatch(event.shiftKey); else writerEditor.openFind(key === "h"); }
|
||||
else if (sourceEditor) { if (advance) sourceEditor.nextMatch(event.shiftKey); else await sourceEditor.openFind(key === "h"); }
|
||||
}
|
||||
}
|
||||
const shouldFocusOnMount = () => active && !modeControls?.contains(document.activeElement);
|
||||
export function focusEditor() {
|
||||
if (reading) element?.querySelector<HTMLElement>(".reading")?.focus({ preventScroll: true });
|
||||
@@ -145,7 +174,8 @@
|
||||
</div>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="editor" class:pane-active={active} bind:this={element}>
|
||||
<!-- svelte-ignore a11y_no_static_element_interactions (Capture document shortcuts before nested editor handlers.) -->
|
||||
<div class="editor" class:pane-active={active} bind:this={element} onkeydowncapture={documentSearch}>
|
||||
<div class="chrome-bar">
|
||||
<span class="breadcrumb" title={buffer.relPath}>{buffer.relPath}</span>
|
||||
{#if lineInfo}
|
||||
@@ -179,6 +209,7 @@
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
{#if readingFind}<FindPanel bind:this={readingFindPanel} target={readingFind} />{/if}
|
||||
<div class="body" class:split={reading && canPreview && previewMode === "split"}>
|
||||
{#if isImage && buffer.imageUrl}
|
||||
<ImageViewer src={buffer.imageUrl} name={buffer.name} />
|
||||
|
||||
@@ -702,14 +702,16 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function createTreeNote(parent: string, template?: NoteTemplate) {
|
||||
try {
|
||||
const path = await createNote(parent, template);
|
||||
await load(parent);
|
||||
onCreated?.({ name: path.split("/").pop()!, rel_path: path, kind: "file" });
|
||||
} catch (reason) { error = String(reason); }
|
||||
}
|
||||
|
||||
function paneMenuItems(parent: string): MenuItem[] {
|
||||
const fromTemplate = async (template?: NoteTemplate) => {
|
||||
try {
|
||||
const path = await createNote(parent, template);
|
||||
await load(parent);
|
||||
onCreated?.({ name: path.split("/").pop()!, rel_path: path, kind: "file" });
|
||||
} catch (reason) { error = String(reason); }
|
||||
};
|
||||
const fromTemplate = (template?: NoteTemplate) => createTreeNote(parent, template);
|
||||
return [
|
||||
{
|
||||
kind: "action",
|
||||
@@ -881,7 +883,7 @@
|
||||
|
||||
<div class="wrap">
|
||||
<div class="ribbon tree-tools">
|
||||
<IconButton icon="file-plus" label={$t("New file")} onclick={() => void createIn("", "file")} />
|
||||
<IconButton icon="file-plus" label={$t("New note")} onclick={() => void createTreeNote("")} />
|
||||
<IconButton icon="folder-plus" label={$t("New folder")} onclick={() => void createIn("", "folder")} />
|
||||
<IconButton
|
||||
icon="sort"
|
||||
|
||||
@@ -4,6 +4,13 @@
|
||||
import { actionMenu, type ActionMenuItem } from "$lib/ui/actionMenu";
|
||||
import { actionIcons } from "$lib/ui/actionIcons";
|
||||
import TabIcon from "$lib/ui/TabIcon.svelte";
|
||||
import { attachTooltip } from "$lib/ui/tooltip";
|
||||
|
||||
function tabTooltip(node: HTMLElement, label: string) {
|
||||
let text = label;
|
||||
const dispose = attachTooltip(node, () => text);
|
||||
return { update(value: string) { text = value; }, destroy: dispose };
|
||||
}
|
||||
import { tabsToClose, type TabCloseScope } from "$lib/workspace/tabActions";
|
||||
import type { WorkbenchTab } from "$lib/workspace/workbenchTabs";
|
||||
|
||||
@@ -35,6 +42,16 @@
|
||||
let menuAnchor: HTMLButtonElement | undefined;
|
||||
let menuPath = $state<string | null>(null);
|
||||
let receiving = $state(false);
|
||||
let transferFrame = 0;
|
||||
let transferX = 0;
|
||||
|
||||
function scrollTransfer() {
|
||||
if (!receiving) { transferFrame = 0; return; }
|
||||
const bounds = bar.getBoundingClientRect();
|
||||
const speed = transferX < bounds.left + 28 ? -10 : transferX > bounds.right - 28 ? 10 : 0;
|
||||
if (speed) { bar.scrollLeft += speed; transferIndex(transferX); }
|
||||
transferFrame = requestAnimationFrame(scrollTransfer);
|
||||
}
|
||||
|
||||
function startTabTransfer(event: DragEvent, tab: WorkbenchTab) {
|
||||
const data = tabDragData(tab);
|
||||
@@ -59,13 +76,18 @@
|
||||
event.preventDefault();
|
||||
event.dataTransfer.dropEffect = "move";
|
||||
receiving = true;
|
||||
transferX = event.clientX;
|
||||
if (!transferFrame) transferFrame = requestAnimationFrame(scrollTransfer);
|
||||
const bounds = bar.getBoundingClientRect();
|
||||
if (event.clientX < bounds.left + 28) bar.scrollLeft -= 10;
|
||||
else if (event.clientX > bounds.right - 28) bar.scrollLeft += 10;
|
||||
transferIndex(event.clientX);
|
||||
}
|
||||
|
||||
function finishTabTransfer() { dragged = dropBefore = dropAfter = null; receiving = false; suppressClick = false; }
|
||||
function finishTabTransfer() {
|
||||
dragged = dropBefore = dropAfter = null; receiving = false; suppressClick = false;
|
||||
cancelAnimationFrame(transferFrame); transferFrame = 0;
|
||||
}
|
||||
|
||||
function dropTab(event: DragEvent) {
|
||||
const data = event.dataTransfer?.getData(transferType);
|
||||
@@ -237,7 +259,7 @@
|
||||
window.addEventListener("keydown", onEscape, true);
|
||||
}
|
||||
|
||||
onDestroy(() => { cancelDrag(); closeMenu(); });
|
||||
onDestroy(() => { cancelDrag(); finishTabTransfer(); closeMenu(); });
|
||||
|
||||
function onMiddleDown(event: MouseEvent) {
|
||||
if (event.button === 1) {
|
||||
@@ -271,7 +293,7 @@
|
||||
aria-controls={tab.panelId}
|
||||
aria-describedby={tab.pinned ? `tab-pin-${tab.id}` : undefined}
|
||||
aria-keyshortcuts="Alt+Shift+ArrowLeft Alt+Shift+ArrowRight"
|
||||
title={`${tab.title}\n${tab.draggable ? $t("Drag to reorder or move to another RuneStone window.") : $t("Drag to reorder.")} ${$t("Right-click for tab actions.")}`}
|
||||
use:tabTooltip={`${tab.title}\n${tab.draggable ? $t("Drag to reorder or move to another RuneStone window.") : $t("Drag to reorder.")} ${$t("Right-click for tab actions.")}`}
|
||||
onclick={() => { if (suppressClick) { suppressClick = false; return; } onSelect(tab.relPath); }}
|
||||
onpointerdown={(event) => { if (!tab.draggable) startDrag(event, tab.relPath); else suppressClick = false; }}
|
||||
draggable={!!tab.draggable}
|
||||
@@ -295,7 +317,7 @@
|
||||
class="close"
|
||||
type="button"
|
||||
aria-label={$t("Close {name}", { name: tab.name })}
|
||||
title={$t("Close {name}", { name: tab.name })}
|
||||
use:tabTooltip={$t("Close {name}", { name: tab.name })}
|
||||
onclick={() => onClose(tab.relPath)}
|
||||
onmousedown={onMiddleDown}
|
||||
onmouseup={(event) => onMiddleUp(event, tab.relPath)}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import PluginTools from "$lib/plugins/PluginTools.svelte";
|
||||
import PluginAccessMenu from "$lib/plugins/PluginAccessMenu.svelte";
|
||||
import PluginEditor from "$lib/plugins/PluginEditor.svelte";
|
||||
import { pluginSplits, pinPluginEditor, pluginTabActions, pluginTabState, restorePluginTab, pluginTabDragData, dropPluginTab, findPluginEditor } from "$lib/plugins/external";
|
||||
import { pluginSplits, pinPluginEditor, pluginTabActions, pluginTabState, restorePluginTab, pluginTabDragData, dropPluginTab } from "$lib/plugins/external";
|
||||
import { containsPane, paneBounds } from "$lib/workspace/splitLayout";
|
||||
import { pluginSidebar, closePluginSidebar, pluginNavigation, openPluginView, pluginView, pluginEditors, pluginEditorActive, selectPluginEditor, closePluginEditor, openPluginDocument, renamePluginDocuments, removePluginDocuments, notifyPluginWorkspace, guardPluginWrites, handlePluginFileOpen, handlePluginDocumentRename, cancelPluginDocumentOpen, type OpenPluginView } from "$lib/plugins/external";
|
||||
import { onMount, onDestroy, tick, untrack } from "svelte";
|
||||
@@ -146,6 +146,7 @@
|
||||
let workbenchReady = $state(false);
|
||||
let documents: Documents | null = null;
|
||||
let paneComponents = $state<Record<string, EditorPane | undefined>>({});
|
||||
let pluginPaneComponents = $state<Record<string, PluginEditor | undefined>>({});
|
||||
function activeEditor() { return paneComponents[documentPanes.active]; }
|
||||
let fileTree: FileTree | undefined = $state();
|
||||
let closingTabs = false;
|
||||
@@ -216,7 +217,7 @@
|
||||
...tabs.map(tab => ({
|
||||
id: tab.tabId ?? tab.id, relPath: tab.relPath, name: tab.name,
|
||||
title: tab.kind === "empty" ? tab.name : tab.relPath,
|
||||
dirty: tab.contents !== tab.saved, pinned: !!tab.pinned, buffer: tab,
|
||||
dirty: tab.contents !== tab.saved, pinned: !!tab.pinned, draggable: desktop, buffer: tab,
|
||||
})),
|
||||
...$pluginEditors.map(plugin => ({
|
||||
id: plugin.key, relPath: `plugin:${plugin.key}`, name: plugin.title,
|
||||
@@ -236,10 +237,35 @@
|
||||
});
|
||||
function workingTabDragData(tab: WorkbenchTab) {
|
||||
const view = $pluginEditors.find(view => view.key === tab.id);
|
||||
return view ? pluginTabDragData(view) : undefined;
|
||||
if (view) return pluginTabDragData(view);
|
||||
const item = workingTabs.find(item => item.id === tab.id);
|
||||
if (!item || !workspace || !desktop) return;
|
||||
try { return JSON.stringify({ tabTransfer: prepareTabTransfer(item).token }); }
|
||||
catch (reason) { error = String(reason); return; }
|
||||
}
|
||||
async function receiveTab(data: string, index: number) {
|
||||
try {
|
||||
const payload = JSON.parse(data);
|
||||
if (typeof payload?.tabTransfer === "string") {
|
||||
if (!/^[0-9a-f-]{36}$/i.test(payload.tabTransfer)) throw new Error("Invalid tab transfer.");
|
||||
const key = `runestone.tab-transfer.${payload.tabTransfer}`;
|
||||
const raw = localStorage.getItem(key);
|
||||
if (!raw) throw new Error("This tab transfer expired. Drag the tab again.");
|
||||
const transfer = JSON.parse(raw);
|
||||
if (!workspace || switching || !documents || transfer.root !== workspace.root) throw new Error("Move tabs only between windows of the same workspace.");
|
||||
if (!Number.isFinite(transfer.created) || transfer.created < Date.now() - 120000
|
||||
|| !transfer.buffer || typeof transfer.buffer.relPath !== "string" || typeof transfer.buffer.contents !== "string"
|
||||
|| typeof transfer.buffer.saved !== "string") throw new Error("Invalid tab transfer.");
|
||||
if (!documents.importTab(transfer.buffer)) throw new Error("This tab is already open in this window. The source tab remains open.");
|
||||
selectTab(transfer.buffer.relPath);
|
||||
setReading(transfer.reading === true);
|
||||
await tick();
|
||||
moveWorkingTab(transfer.buffer.relPath, index);
|
||||
localStorage.setItem(key + ".accepted", "true");
|
||||
localStorage.removeItem(key);
|
||||
error = null;
|
||||
return;
|
||||
}
|
||||
await dropPluginTab(data);
|
||||
if ($pluginView?.placement === "editor") moveWorkingTab(`plugin:${$pluginView.key}`, index);
|
||||
error = null;
|
||||
@@ -350,7 +376,9 @@
|
||||
const transfer = JSON.parse(raw);
|
||||
if (transfer.root === workspace.root && transfer.created > Date.now() - 120000
|
||||
&& (transfer.buffer || transfer.plugin)) {
|
||||
if (transfer.buffer && typeof transfer.buffer.relPath === "string" && typeof transfer.buffer.contents === "string") documents.importTab(transfer.buffer);
|
||||
if (transfer.buffer && typeof transfer.buffer.relPath === "string" && typeof transfer.buffer.contents === "string") {
|
||||
if (!documents.importTab(transfer.buffer)) throw new Error("This tab is already open in this window.");
|
||||
}
|
||||
else if (transfer.plugin) {
|
||||
const deadline = Date.now() + 15000;
|
||||
while (!await restorePluginTab(transfer.plugin)) {
|
||||
@@ -413,6 +441,13 @@
|
||||
const action = pluginTabActions(tab.plugin).find(action => "label" in action && /(?:move|open).*new window/i.test(action.label));
|
||||
if (action && "run" in action && !action.disabled) { action.run?.(); return; }
|
||||
}
|
||||
let transfer: ReturnType<typeof prepareTabTransfer> | undefined;
|
||||
try {
|
||||
transfer = prepareTabTransfer(tab, keepSource, true);
|
||||
await workspaceOpenWindow(workspace.root, transfer.token);
|
||||
} catch (reason) { transfer?.finish(); error = String(reason); }
|
||||
}
|
||||
function prepareTabTransfer(tab: EditorTab, keepSource = false, reportTimeout = false) {
|
||||
const current = documents;
|
||||
const bufferSnapshot = (buffer?: OpenBuffer) => JSON.stringify(buffer ? { ...buffer, diskError: buffer.diskError ?? null } : undefined);
|
||||
const snapshot = "buffer" in tab ? bufferSnapshot(tab.buffer) : JSON.stringify(pluginTabState(tab.plugin));
|
||||
@@ -432,11 +467,11 @@
|
||||
finish();
|
||||
};
|
||||
try {
|
||||
localStorage.setItem(key, JSON.stringify({ root: workspace.root, created: Date.now(), ...("buffer" in tab ? { buffer: tab.buffer } : { plugin: pluginTabState(tab.plugin) }), reading }));
|
||||
localStorage.setItem(key, JSON.stringify({ root: workspace!.root, created: Date.now(), ...("buffer" in tab ? { buffer: tab.buffer } : { plugin: pluginTabState(tab.plugin) }), reading }));
|
||||
window.addEventListener("storage", accepted);
|
||||
timer = setTimeout(() => { finish(); error = "The new window did not accept the tab. The source tab remains open."; }, 30000);
|
||||
await workspaceOpenWindow(workspace.root, token);
|
||||
} catch (reason) { finish(); error = String(reason); }
|
||||
timer = setTimeout(() => { finish(); if (reportTimeout) error = "The new window did not accept the tab. The source tab remains open."; }, 30000);
|
||||
return { token, finish };
|
||||
} catch (reason) { finish(); throw reason; }
|
||||
}
|
||||
function pinWorkingTab(path: string) {
|
||||
const tab = workingTabs.find(tab => tab.relPath === path);
|
||||
@@ -726,7 +761,9 @@
|
||||
if (event.defaultPrevented) return;
|
||||
if (event.target instanceof Element && event.target.closest("dialog, [role=dialog], [role=menu]")) return;
|
||||
if ((event.ctrlKey || event.metaKey) && !event.shiftKey && !event.altKey && event.key.toLowerCase() === "f" && editorPluginVisible && $pluginView) {
|
||||
if (findPluginEditor($pluginView)) { event.preventDefault(); return; }
|
||||
event.preventDefault();
|
||||
void pluginPaneComponents[$pluginView.key]?.openFind();
|
||||
return;
|
||||
}
|
||||
const action = pluginHost.get("terminal")?.shortcut(event);
|
||||
const terminalTarget = event.target instanceof Element && !!event.target.closest(".dock");
|
||||
@@ -1218,7 +1255,7 @@
|
||||
onCloseTabs={(path, scope) => void closeTabs(path, scope)} fileActions={workingTabFileActions} tabActions={workingTabActions}
|
||||
tabDragData={workingTabDragData} onDropTab={(data, index) => void receiveTab(data, index)} />
|
||||
{/if}
|
||||
<PluginEditor view={pane} />
|
||||
<PluginEditor bind:this={pluginPaneComponents[pane.key]} view={pane} />
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user