Improve editor, plugin updates, and window transfers for 0.1.41
This commit is contained in:
@@ -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