Show different tabs side by side in split view
Each tab menu has "Open to the right". It shows the tab in a new pane to the right of the active pane. A pane shows a document or a plugin tab, for example a browser tab. - A plugin view stays in the plugin layer above the panes, so a move between panes does not mount it again. - A browser tab that covers the area moves to the left pane when a tab opens beside it. The last pane gives its plugin tab the whole area. - The "Split" toggle of a document is now "Read beside". Card 565874b2.
This commit is contained in:
@@ -1020,10 +1020,10 @@ export const messageRows: [string, string, string, string][] = [
|
||||
"阅读"
|
||||
],
|
||||
[
|
||||
"Split",
|
||||
"Diviser",
|
||||
"分割",
|
||||
"分栏"
|
||||
"Read beside",
|
||||
"Lire à côté",
|
||||
"横に閲覧",
|
||||
"并排阅读"
|
||||
],
|
||||
[
|
||||
"Editor",
|
||||
@@ -3376,7 +3376,7 @@ export const messageRows: [string, string, string, string][] = [
|
||||
["Show the file name above each Markdown note in Write and Read.", "Afficher le nom du fichier au-dessus de chaque note Markdown dans Écrire et Lire.", "「書く」と「閲覧」で、各 Markdown ノートの上にファイル名を表示します。", "在“写作”和“阅读”中,在每篇 Markdown 笔记上方显示文件名。"],
|
||||
["Source", "Source", "ソース", "源码"],
|
||||
["View", "Affichage", "表示", "视图"],
|
||||
["Split: Read beside the editor", "Diviser : Lire à côté de l’éditeur", "分割: エディターの横に閲覧を表示", "分栏:在编辑器旁显示阅读"],
|
||||
["Read beside: show the Read view beside the editor", "Lire à côté : afficher Lire à côté de l’éditeur", "横に閲覧: エディターの横に閲覧を表示", "并排阅读:在编辑器旁显示阅读"],
|
||||
["Switch between Write and Source", "Alterner entre Écrire et Source", "「書く」と「ソース」を切り替え", "在写作和源码之间切换"],
|
||||
["Switch between Read and the editor", "Alterner entre Lire et l’éditeur", "「閲覧」とエディターを切り替え", "在阅读和编辑器之间切换"],
|
||||
["Unsaved", "Non enregistré", "未保存", "未保存"],
|
||||
|
||||
@@ -1,26 +1,32 @@
|
||||
<script lang="ts">
|
||||
// The document area: plugin editor panes over the document panes, with the save conflict banner of each document.
|
||||
// The document area: the panes of documents and plugin editor views, with the save conflict banner of each document.
|
||||
// A plugin view stays in the plugin layer above the document panes, so that a move between panes does not mount it again.
|
||||
// Over the whole area, the layer shows the focused plugin view and its plugin split. In a pane, the view covers its pane.
|
||||
import type { ComponentProps } from "svelte";
|
||||
import { t } from "$lib/i18n";
|
||||
import PluginEditor from "$lib/plugins/PluginEditor.svelte";
|
||||
import { pluginEditors, pluginView, selectPluginEditor } from "$lib/plugins/external";
|
||||
import { pluginEditors, pluginView, selectPluginEditor, type OpenPluginView } from "$lib/plugins/external";
|
||||
import type { Documents } from "$lib/workspace/documents.svelte";
|
||||
import type { OpenBuffer } from "$lib/workspace/types";
|
||||
import EditorPane from "./EditorPane.svelte";
|
||||
import Icon from "$lib/ui/Icon.svelte";
|
||||
import IconButton from "$lib/ui/IconButton.svelte";
|
||||
import { paneBounds, stackLayout, type SplitLayout } from "$lib/workspace/splitLayout";
|
||||
import type { ViewMode } from "./ViewModes.svelte";
|
||||
import TabBar from "./TabBar.svelte";
|
||||
|
||||
type EditorProps = ComponentProps<typeof EditorPane>;
|
||||
type DocumentView = { id: string; reading: boolean; split: boolean; buffer: OpenBuffer | null; bounds: { width: number } };
|
||||
type DocumentView = { id: string; reading: boolean; split: boolean; buffer: OpenBuffer | null; pluginKey: string | null };
|
||||
type Bounds = { left: number; top: number; width: number; height: number };
|
||||
type Props = {
|
||||
editorPluginVisible: boolean;
|
||||
/** The focused plugin view covers the whole area. */
|
||||
pluginCover: boolean;
|
||||
split: boolean;
|
||||
pluginBounds: Record<string, Bounds | undefined>;
|
||||
tabBar: ComponentProps<typeof TabBar>;
|
||||
pluginPaneComponents: Record<string, PluginEditor | undefined>;
|
||||
documentViews: DocumentView[];
|
||||
layout: SplitLayout;
|
||||
activePane: string;
|
||||
paneComponents: Record<string, EditorPane | undefined>;
|
||||
documents: Documents | null;
|
||||
@@ -46,37 +52,36 @@
|
||||
onCursor: (pane: DocumentView, line: number, column: number) => void;
|
||||
onOpenLink: (pane: DocumentView, kind: "md" | "wiki", target: string) => void;
|
||||
};
|
||||
let { editorPluginVisible, split, pluginBounds, tabBar, pluginPaneComponents = $bindable(), documentViews, activePane, paneComponents = $bindable(),
|
||||
let { pluginCover, split, pluginBounds, tabBar, pluginPaneComponents = $bindable(), documentViews, layout, activePane, paneComponents = $bindable(),
|
||||
documents, tabs, hasWorkspace, browserOpening, focusLine, onFocusPane, onCloseSplit, onSaveCopy, onOpenWorkspace, onNewFile, onNewNote,
|
||||
onQuickOpen, onOpenBrowser, onChange, onInsertImage, onRenameTitle, tableActions, linkActions, onView, onSplitView, onCursor, onOpenLink }: Props = $props();
|
||||
|
||||
// A narrow area shows the panes one above the other, in the same order.
|
||||
let width = $state(0);
|
||||
const stacked = $derived(width > 0 && width <= 640);
|
||||
const bounds = $derived(paneBounds(stacked ? stackLayout(layout) : layout));
|
||||
const hosts = $derived(new Map(documentViews.flatMap(pane => pane.pluginKey ? [[pane.pluginKey, pane.id] as const] : [])));
|
||||
const several = $derived(documentViews.length > 1);
|
||||
function focusPlugin(view: OpenPluginView, host: string | undefined) {
|
||||
if (host) onFocusPane(host);
|
||||
else if ($pluginView?.key !== view.key) selectPluginEditor(view);
|
||||
}
|
||||
</script>
|
||||
|
||||
<main class="center">
|
||||
<div class="plugin-panes" class:inactive={!editorPluginVisible}>
|
||||
{#each $pluginEditors as pane (pane.key)}
|
||||
{@const bounds = pluginBounds[pane.key]}
|
||||
<!-- svelte-ignore a11y_no_static_element_interactions (Focus selects the pane that owns the input.) -->
|
||||
<div class="plugin-pane" class:inactive={!bounds} class:split class:focused={$pluginView?.key === pane.key}
|
||||
style:left={`${bounds?.left ?? 0}%`} style:top={`${bounds?.top ?? 0}%`}
|
||||
style:width={`${bounds?.width ?? 100}%`} style:height={`${bounds?.height ?? 100}%`}
|
||||
onfocusin={() => { if ($pluginView?.key !== pane.key) selectPluginEditor(pane); }}
|
||||
onpointerdown={() => { if ($pluginView?.key !== pane.key) selectPluginEditor(pane); }}>
|
||||
{#if split && bounds}
|
||||
<TabBar {...tabBar} tabs={tabBar.tabs.filter(tab => tab.id === pane.key)} />
|
||||
{/if}
|
||||
<PluginEditor bind:this={pluginPaneComponents[pane.key]} view={pane} />
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
<div class="editor-host" class:plugin-covered={editorPluginVisible}>
|
||||
<div class="document-panes" class:split={documentViews.length > 1}>
|
||||
<main class="center" bind:clientWidth={width}>
|
||||
<div class="editor-host" class:plugin-covered={pluginCover}>
|
||||
<div class="document-panes" class:split={several}>
|
||||
{#each documentViews as pane (pane.id)}
|
||||
{@const box = bounds[pane.id]}
|
||||
<!-- svelte-ignore a11y_no_static_element_interactions (Focus identifies the document for shared commands.) -->
|
||||
<div class="document-pane" class:focused={activePane === pane.id}
|
||||
<div class="document-pane" class:focused={activePane === pane.id} class:edge-left={(box?.left ?? 0) > 0} class:edge-top={(box?.top ?? 0) > 0}
|
||||
data-document-pane={pane.id} data-document-path={pane.buffer?.relPath ?? ""}
|
||||
style:--pane-weight={pane.bounds.width}
|
||||
style:left={`${box?.left ?? 0}%`} style:top={`${box?.top ?? 0}%`}
|
||||
style:width={`${box?.width ?? 100}%`} style:height={`${box?.height ?? 100}%`}
|
||||
onpointerdowncapture={() => onFocusPane(pane.id)}
|
||||
onfocusin={() => onFocusPane(pane.id)}>
|
||||
<!-- A pane with a plugin view stays empty. The plugin layer shows the view over it. -->
|
||||
{#if !pane.pluginKey}
|
||||
{#if pane.buffer?.diskError}
|
||||
<div class="conflict" role="alert"><Icon icon="alert" /><span>{pane.buffer.diskError}</span><button class="primary" onclick={() => void documents?.refresh()}>{$t("Retry")}</button><button onclick={() => onSaveCopy(pane.buffer)}>{$t("Save a copy")}</button></div>
|
||||
{:else if pane.buffer?.diskContents != null}
|
||||
@@ -94,7 +99,7 @@
|
||||
bind:this={paneComponents[pane.id]}
|
||||
{tabs}
|
||||
active={activePane === pane.id}
|
||||
onCloseSplit={documentViews.length > 1 ? () => onCloseSplit(pane.id) : undefined}
|
||||
onCloseSplit={several ? () => onCloseSplit(pane.id) : undefined}
|
||||
{hasWorkspace}
|
||||
{onOpenWorkspace}
|
||||
{onNewFile}
|
||||
@@ -117,10 +122,36 @@
|
||||
onCursor={(line, column) => onCursor(pane, line, column)}
|
||||
onOpenLink={(kind, target) => onOpenLink(pane, kind, target)}
|
||||
/>
|
||||
{/if}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
<div class="plugin-panes">
|
||||
{#each $pluginEditors as view (view.key)}
|
||||
{@const host = pluginCover ? undefined : hosts.get(view.key)}
|
||||
{@const box = pluginCover ? pluginBounds[view.key] : host ? bounds[host] : undefined}
|
||||
<!-- svelte-ignore a11y_no_static_element_interactions (Focus selects the pane that owns the input.) -->
|
||||
<div class="plugin-pane" class:inactive={!box} class:split={pluginCover && split} class:embedded={!!host}
|
||||
class:focused={host ? activePane === host : $pluginView?.key === view.key}
|
||||
class:edge-left={!!host && (box?.left ?? 0) > 0} class:edge-top={!!host && (box?.top ?? 0) > 0}
|
||||
data-plugin-pane={view.key}
|
||||
style:left={`${box?.left ?? 0}%`} style:top={`${box?.top ?? 0}%`}
|
||||
style:width={`${box?.width ?? 100}%`} style:height={`${box?.height ?? 100}%`}
|
||||
onfocusin={() => focusPlugin(view, host)}
|
||||
onpointerdowncapture={() => focusPlugin(view, host)}>
|
||||
{#if pluginCover && split && box}
|
||||
<TabBar {...tabBar} tabs={tabBar.tabs.filter(tab => tab.id === view.key)} />
|
||||
{:else if host && several}
|
||||
<div class="pane-bar">
|
||||
<span class="pane-title" title={view.title}>{view.title}</span>
|
||||
<IconButton icon="close" label={$t("Close split")} onclick={() => onCloseSplit(host)} />
|
||||
</div>
|
||||
{/if}
|
||||
<PluginEditor bind:this={pluginPaneComponents[view.key]} {view} />
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<style>
|
||||
@@ -155,19 +186,22 @@
|
||||
container-type: inline-size;
|
||||
}
|
||||
|
||||
.document-panes { flex: 1; min-height: 0; display: flex; overflow: hidden; }
|
||||
.document-pane { flex: var(--pane-weight) 1 0; min-width: 0; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
|
||||
.document-pane + .document-pane { border-left: 1px solid var(--rs-border); }
|
||||
/* Each pane has its place in the split layout. A pane beside or below another pane has a border on that edge. */
|
||||
.document-panes { position: relative; flex: 1; min-height: 0; overflow: hidden; }
|
||||
.document-pane { position: absolute; display: flex; flex-direction: column; min-width: 0; min-height: 0; overflow: hidden; box-sizing: border-box; }
|
||||
.edge-left { border-left: 1px solid var(--rs-border); }
|
||||
.edge-top { border-top: 1px solid var(--rs-border); }
|
||||
.document-panes.split .document-pane.focused :global(.chrome-bar) { background: var(--rs-bg-active); }
|
||||
@container (max-width: 640px) {
|
||||
.document-panes.split { flex-direction: column; overflow-y: auto; }
|
||||
.document-panes.split .document-pane { flex: 1 0 320px; }
|
||||
.document-pane + .document-pane { border-left: 0; border-top: 1px solid var(--rs-border); }
|
||||
}
|
||||
.editor-host.plugin-covered { display: none; }
|
||||
.plugin-panes { position: relative; flex: 1; min-height: 0; min-width: 0; overflow: hidden; }
|
||||
.plugin-pane { position: absolute; display: flex; flex-direction: column; min-width: 0; min-height: 0; box-sizing: border-box; }
|
||||
/* The plugin layer covers the area, but only its visible panes take the pointer. */
|
||||
.plugin-panes { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
|
||||
.plugin-pane { position: absolute; display: flex; flex-direction: column; min-width: 0; min-height: 0; box-sizing: border-box; pointer-events: auto; }
|
||||
.plugin-pane.embedded { background: var(--rs-bg); }
|
||||
.plugin-pane.split { border: 1px solid var(--rs-border); }
|
||||
.plugin-pane.split.focused { border-color: var(--rs-accent); }
|
||||
.plugin-panes.inactive, .plugin-pane.inactive { display: none; }
|
||||
.plugin-pane.inactive { display: none; }
|
||||
/* The bar of a plugin view in a pane matches the chrome bar of a document pane. */
|
||||
.pane-bar { display: flex; align-items: center; gap: 8px; min-height: 34px; flex: 0 0 auto; padding: 0 10px; background: var(--rs-bg); border-bottom: 1px solid var(--rs-border); color: var(--rs-text-dim); }
|
||||
.plugin-pane.focused .pane-bar { background: var(--rs-bg-active); }
|
||||
.pane-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
</style>
|
||||
|
||||
@@ -147,7 +147,9 @@
|
||||
const splitView = $derived(split && canPreview && !reading);
|
||||
const view: ViewMode = $derived(readOnly ? "read" : chrome === "Source" ? "source" : "write");
|
||||
// The tab already names the file, so the breadcrumb shows only its folder.
|
||||
const folder = $derived(buffer?.relPath.includes("/") ? buffer.relPath.slice(0, buffer.relPath.lastIndexOf("/")).split("/").join(" / ") : "");
|
||||
// Beside other panes, the breadcrumb also names the file, because the tab bar names only the file of the active pane.
|
||||
const folder = $derived(!buffer ? "" : onCloseSplit ? buffer.relPath.split("/").join(" / ")
|
||||
: buffer.relPath.includes("/") ? buffer.relPath.slice(0, buffer.relPath.lastIndexOf("/")).split("/").join(" / ") : "");
|
||||
</script>
|
||||
|
||||
{#if !buffer || buffer.kind === "empty"}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
<script lang="ts" module>
|
||||
/** The view of a document pane. Split is a separate toggle beside Write or Source. */
|
||||
/** The view of a document pane. Read beside is a separate toggle beside Write or Source. */
|
||||
export type ViewMode = "write" | "source" | "read";
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
// The view control of a document pane: Write, Source, and Read in one radio group, then the Split toggle.
|
||||
// The view control of a document pane: Write, Source, and Read in one radio group, then the Read beside toggle.
|
||||
// All choices stay visible. Narrow panes show only the icons.
|
||||
import { t } from "$lib/i18n";
|
||||
import Icon from "$lib/ui/Icon.svelte";
|
||||
@@ -16,7 +16,7 @@
|
||||
type Props = {
|
||||
view: ViewMode;
|
||||
split: boolean;
|
||||
/** Markdown files have Read and Split. Other text files have only Write and Source. */
|
||||
/** Markdown files have Read and Read beside. Other text files have only Write and Source. */
|
||||
readable: boolean;
|
||||
onView: (view: ViewMode) => void;
|
||||
onSplit: (split: boolean) => void;
|
||||
@@ -60,7 +60,7 @@
|
||||
{/each}
|
||||
</div>
|
||||
{#if readable}
|
||||
<button type="button" class="split" aria-pressed={split} aria-label={$t("Split")} use:tooltip={$t("Split: Read beside the editor")} onclick={() => onSplit(!split)}>
|
||||
<button type="button" class="split" aria-pressed={split} aria-label={$t("Read beside")} use:tooltip={$t("Read beside: show the Read view beside the editor")} onclick={() => onSplit(!split)}>
|
||||
<Icon icon="split" />
|
||||
</button>
|
||||
{/if}
|
||||
|
||||
@@ -6,13 +6,14 @@
|
||||
import type PluginEditor from "$lib/plugins/PluginEditor.svelte";
|
||||
import { featureOfOwner } from "$lib/features/catalog";
|
||||
import { pluginSplits, pinPluginEditor, pluginTabActions, pluginTabState, restorePluginTab, pluginTabDragData, dropPluginTab, waitForPluginStartup, pluginTools, installedPlugins, runPluginCommand } from "$lib/plugins/external";
|
||||
import { containsPane, paneBounds } from "$lib/workspace/splitLayout";
|
||||
import { containsPane, paneBounds, splitSibling } 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";
|
||||
import type { MenuLink } from "$lib/editor/linkMenu";
|
||||
import { WorkspaceSession } from "$lib/workspace/session.svelte";
|
||||
import { retainEditorStates } from "$lib/editor/editorStates";
|
||||
import { initialDocumentPanes, syncDocumentPanes, splitDocumentPane, closeDocumentPane, selectPane, readDocumentPane, splitDocumentView } from "$lib/workspace/documentPanes";
|
||||
import { initialDocumentPanes, syncDocumentPanes, splitDocumentPane, closeDocumentPane, selectPane, readDocumentPane, splitDocumentView,
|
||||
paneOf, showInActivePane, splitWithContent, releaseSolePlugin, type PaneContent } from "$lib/workspace/documentPanes";
|
||||
import type { ViewMode } from "./ViewModes.svelte";
|
||||
import { selectedEditorText } from "$lib/editor/selection";
|
||||
import { pluginHost } from "$lib/plugins/registry";
|
||||
@@ -229,7 +230,9 @@
|
||||
}, () => {
|
||||
if (documents !== next) return;
|
||||
const selected = next.active;
|
||||
documentPanes = syncDocumentPanes(documentPanes, next.buffers.map(tab => tab.id), selected?.id ?? null);
|
||||
// While a plugin view has the focus, a document change does not replace the content of a pane.
|
||||
const pluginFocused = $pluginEditorActive && $pluginView?.placement === "editor";
|
||||
documentPanes = syncDocumentPanes(documentPanes, next.buffers.map(tab => tab.id), pluginFocused ? null : selected?.id ?? null);
|
||||
if (selected) view = selected.chromeOverride ?? selected.chrome;
|
||||
});
|
||||
return next;
|
||||
@@ -238,8 +241,8 @@
|
||||
onDestroy(() => retainEditorStates([]));
|
||||
const documentViews = $derived.by(() => {
|
||||
const bounds = paneBounds(documentPanes.layout);
|
||||
return documentPanes.panes.map(pane => ({ ...pane, buffer: tabs.find(tab => tab.id === pane.documentId) ?? null, bounds: bounds[pane.id] }))
|
||||
.sort((a, b) => a.bounds.left - b.bounds.left);
|
||||
return documentPanes.panes.map(pane => ({ ...pane, buffer: tabs.find(tab => tab.id === pane.documentId) ?? null }))
|
||||
.sort((a, b) => bounds[a.id].left - bounds[b.id].left || bounds[a.id].top - bounds[b.id].top);
|
||||
});
|
||||
function setReading(next: boolean, paneId = documentPanes.active) {
|
||||
documentPanes = readDocumentPane(documentPanes, paneId, next);
|
||||
@@ -249,6 +252,13 @@
|
||||
if (!pane) return;
|
||||
const tab = tabs.find(tab => tab.id === pane.documentId);
|
||||
documentPanes = selectPane(documentPanes, paneId);
|
||||
if (pane.pluginKey) {
|
||||
const key = pane.pluginKey;
|
||||
const view = $pluginEditors.find(view => view.key === key);
|
||||
if (view && !($pluginEditorActive && $pluginView?.key === key)) selectPluginEditor(view);
|
||||
if (focusEditor) void tick().then(() => { if (documentPanes.active === paneId) pluginPaneComponents[key]?.focus(); });
|
||||
return;
|
||||
}
|
||||
pluginEditorActive.set(false);
|
||||
if (tab && documents?.active?.id !== tab.id) { focusLine = null; documents?.select(tab.relPath); }
|
||||
if (focusEditor) void tick().then(() => {
|
||||
@@ -258,26 +268,90 @@
|
||||
});
|
||||
}
|
||||
function closeSplit(paneId: string) {
|
||||
documentPanes = closeDocumentPane(documentPanes, paneId);
|
||||
focusDocumentPane(documentPanes.active, true);
|
||||
const released = releaseSolePlugin(closeDocumentPane(documentPanes, paneId));
|
||||
documentPanes = released.state;
|
||||
// The last pane does not keep a plugin view. The view covers the area again.
|
||||
const view = $pluginEditors.find(view => view.key === released.pluginKey);
|
||||
if (view) {
|
||||
selectPluginEditor(view);
|
||||
void tick().then(() => pluginPaneComponents[view.key]?.focus());
|
||||
} else focusDocumentPane(documentPanes.active, true);
|
||||
}
|
||||
/** The plugin view that covers the document area moves into the active pane, so that a new pane can open beside it. */
|
||||
function keepCoverInPane() {
|
||||
if (coverKey) documentPanes = showInActivePane(documentPanes, { pluginKey: coverKey });
|
||||
}
|
||||
async function openToRight(file: TreeEntry, sourcePane: string) {
|
||||
const current = documents;
|
||||
const covered = sourcePane === documentPanes.active ? coverKey : null;
|
||||
const source = documentPanes.panes.find(pane => pane.id === sourcePane);
|
||||
if (!current || switching || !source) return;
|
||||
await current.open(file, "background");
|
||||
if (documents !== current || switching || documentPanes.active !== sourcePane
|
||||
|| documentPanes.panes.find(pane => pane.id === sourcePane)?.documentId !== source.documentId) return;
|
||||
const now = documentPanes.panes.find(pane => pane.id === sourcePane);
|
||||
if (documents !== current || switching || documentPanes.active !== sourcePane || coverKey !== covered
|
||||
|| now?.documentId !== source.documentId || now?.pluginKey !== source.pluginKey) return;
|
||||
const target = current.buffers.find(tab => tab.relPath === file.rel_path);
|
||||
if (!target) return;
|
||||
keepCoverInPane();
|
||||
documentPanes = splitDocumentPane(documentPanes, sourcePane, target.id, crypto.randomUUID());
|
||||
focusDocumentPane(documentPanes.active, true);
|
||||
}
|
||||
function tabContent(tab: EditorTab): PaneContent {
|
||||
return "plugin" in tab ? { pluginKey: tab.plugin.key } : { documentId: tab.buffer.id };
|
||||
}
|
||||
/** A visible tab is the plugin view over the area, or the content of a pane. */
|
||||
function tabVisible(tab: EditorTab) {
|
||||
if (coverKey) return "plugin" in tab && tab.plugin.key === coverKey;
|
||||
return !!paneOf(documentPanes, tabContent(tab));
|
||||
}
|
||||
/** Show a tab in a new pane to the right of the active pane. The active pane keeps its content. */
|
||||
function openTabToRight(tab: EditorTab) {
|
||||
if (switching || tabVisible(tab)) return;
|
||||
keepCoverInPane();
|
||||
documentPanes = splitWithContent(documentPanes, documentPanes.active, tabContent(tab), crypto.randomUUID());
|
||||
focusDocumentPane(documentPanes.active, true);
|
||||
}
|
||||
// Each focused plugin view that is not in a pane gets a pane when the area has several panes.
|
||||
// A new view that a plugin opens beside its own view, for example a browser tab, opens beside the pane of that view.
|
||||
const placedPlugins = new Set<string>();
|
||||
$effect(() => {
|
||||
const view = $pluginEditorActive && $pluginView?.placement === "editor" ? $pluginView : null;
|
||||
const splits = $pluginSplits;
|
||||
if (!view) return;
|
||||
untrack(() => {
|
||||
const fresh = !placedPlugins.has(view.key);
|
||||
placedPlugins.add(view.key);
|
||||
const shown = paneOf(documentPanes, { pluginKey: view.key });
|
||||
if (shown) { documentPanes = selectPane(documentPanes, shown.id); return; }
|
||||
if (documentPanes.panes.length < 2) return;
|
||||
const group = fresh ? splits.find(tree => containsPane(tree, view.key)) : undefined;
|
||||
const beside = group ? splitSibling(group, view.key) : null;
|
||||
const source = beside ? paneOf(documentPanes, { pluginKey: beside.sibling }) : undefined;
|
||||
documentPanes = source && beside
|
||||
? splitWithContent(documentPanes, source.id, { pluginKey: view.key }, crypto.randomUUID(), beside.direction)
|
||||
: showInActivePane(documentPanes, { pluginKey: view.key });
|
||||
});
|
||||
});
|
||||
// The selected document is visible while no plugin view has the focus.
|
||||
$effect(() => {
|
||||
const selected = $pluginEditorActive && $pluginView?.placement === "editor" ? null : buffer?.id ?? null;
|
||||
if (selected) untrack(() => { documentPanes = showInActivePane(documentPanes, { documentId: selected }); });
|
||||
});
|
||||
// A closed plugin view leaves its pane. The last pane does not keep a plugin view.
|
||||
$effect(() => {
|
||||
const keys = $pluginEditors.map(view => view.key);
|
||||
untrack(() => {
|
||||
for (const key of placedPlugins) if (!keys.includes(key)) placedPlugins.delete(key);
|
||||
documentPanes = releaseSolePlugin(syncDocumentPanes(documentPanes, tabs.map(tab => tab.id), null, keys)).state;
|
||||
});
|
||||
});
|
||||
let focusLine = $state<number | null>(null);
|
||||
let dragging = $state<"left" | "right" | "bottom" | null>(null);
|
||||
let centerStack: HTMLElement | undefined = $state();
|
||||
|
||||
// A focused plugin view is the active tab. It covers the area only when no pane shows it.
|
||||
const editorPluginVisible = $derived($pluginView?.placement === "editor" && $pluginEditorActive);
|
||||
const coverKey = $derived(editorPluginVisible && $pluginView && !paneOf(documentPanes, { pluginKey: $pluginView.key }) ? $pluginView.key : null);
|
||||
type EditorTab = WorkbenchTab & ({ buffer: OpenBuffer } | { plugin: OpenPluginView });
|
||||
let tabOrder = $state<string[]>([]);
|
||||
const availableTabs = $derived<EditorTab[]>([
|
||||
@@ -412,7 +486,7 @@
|
||||
documents?.setChrome(next);
|
||||
}
|
||||
|
||||
/** Show Write, Source, or Read in a pane. Write and Source keep a Split view. */
|
||||
/** Show Write, Source, or Read in a pane. Write and Source keep Read beside. */
|
||||
function setDocumentView(paneId: string, next: ViewMode) {
|
||||
focusDocumentPane(paneId);
|
||||
if (next === "read") { setReading(true, paneId); return; }
|
||||
@@ -557,6 +631,8 @@
|
||||
pluginEditorActive.set(false);
|
||||
focusLine = null;
|
||||
documents?.select(relPath);
|
||||
const selected = documents?.active?.id;
|
||||
if (selected) documentPanes = showInActivePane(documentPanes, { documentId: selected });
|
||||
focusDocumentPane(documentPanes.active, true);
|
||||
status = documents?.active?.kind === "empty" ? $t("New tab") : "";
|
||||
}
|
||||
@@ -585,9 +661,11 @@
|
||||
}
|
||||
function workingTabActions(item: WorkbenchTab): ActionMenuItem[] {
|
||||
const tab = workingTabs.find(tab => tab.id === item.id);
|
||||
const actions = tab && "plugin" in tab ? pluginTabActions(tab.plugin) : [];
|
||||
if (actions.some(action => "label" in action && /(?:move|open).*new window/i.test(action.label))) return actions;
|
||||
return tab ? [{ label: $t("Open in new window"), icon: "external", disabled: !desktop || !workspace, run: () => { void detachTab(tab); } }, ...actions] : [];
|
||||
if (!tab) return [];
|
||||
const actions = "plugin" in tab ? pluginTabActions(tab.plugin) : [];
|
||||
const right: ActionMenuItem = { label: $t("Open to the right"), icon: "split-right", disabled: tabVisible(tab), run: () => openTabToRight(tab) };
|
||||
if (actions.some(action => "label" in action && /(?:move|open).*new window/i.test(action.label))) return [right, ...actions];
|
||||
return [right, { label: $t("Open in new window"), icon: "external", disabled: !desktop || !workspace, run: () => { void detachTab(tab); } }, ...actions];
|
||||
}
|
||||
async function detachTab(item: WorkbenchTab, keepSource = false) {
|
||||
const tab = workingTabs.find(tab => tab.id === item.id);
|
||||
@@ -722,7 +800,7 @@
|
||||
{ label: $t("Source"), icon: "code", radio: true, checked: viewMode === "source", run: () => setDocumentView(documentPanes.active, "source") },
|
||||
{ label: $t("Read"), icon: "book", radio: true, checked: viewMode === "read", run: () => setDocumentView(documentPanes.active, "read") },
|
||||
{ separator: true },
|
||||
{ label: $t("Split"), icon: "split-right", checked: activeDocumentPane?.split === true && !reading, run: () => setSplitView(documentPanes.active, !(activeDocumentPane?.split && !reading)) },
|
||||
{ label: $t("Read beside"), icon: "split-right", checked: activeDocumentPane?.split === true && !reading, run: () => setSplitView(documentPanes.active, !(activeDocumentPane?.split && !reading)) },
|
||||
] },
|
||||
);
|
||||
}
|
||||
@@ -1379,11 +1457,12 @@
|
||||
<DocumentArea
|
||||
bind:pluginPaneComponents
|
||||
bind:paneComponents
|
||||
{editorPluginVisible}
|
||||
pluginCover={coverKey !== null}
|
||||
split={!!activeSplit}
|
||||
{pluginBounds}
|
||||
tabBar={tabBarProps}
|
||||
{documentViews}
|
||||
layout={documentPanes.layout}
|
||||
activePane={documentPanes.active}
|
||||
{documents}
|
||||
{tabs}
|
||||
|
||||
@@ -1,30 +1,40 @@
|
||||
import { paneBounds, removePane, splitPane, type SplitLayout } from "./splitLayout.ts";
|
||||
|
||||
/** `reading` shows only the Read view. `split` shows the Read view beside the editor. A pane never has both. */
|
||||
export type DocumentPane = { id: string; documentId: string | null; reading: boolean; split: boolean };
|
||||
/**
|
||||
* A pane shows one document or one plugin editor view, for example a browser tab. A pane never shows both.
|
||||
* `reading` shows only the Read view. `split` shows the Read view beside the editor. A pane never has both.
|
||||
*/
|
||||
export type DocumentPane = { id: string; documentId: string | null; pluginKey: string | null; reading: boolean; split: boolean };
|
||||
export type DocumentPanes = { layout: SplitLayout; panes: DocumentPane[]; active: string };
|
||||
/** The content of a pane: a document ID or the key of a plugin editor view. */
|
||||
export type PaneContent = { documentId: string } | { pluginKey: string };
|
||||
|
||||
function emptyPane(id: string): DocumentPane {
|
||||
return { id, documentId: null, pluginKey: null, reading: false, split: false };
|
||||
}
|
||||
|
||||
function filled(id: string, content: PaneContent): DocumentPane {
|
||||
return "documentId" in content ? { ...emptyPane(id), documentId: content.documentId } : { ...emptyPane(id), pluginKey: content.pluginKey };
|
||||
}
|
||||
|
||||
export function initialDocumentPanes(): DocumentPanes {
|
||||
return { layout: "document-main", panes: [{ id: "document-main", documentId: null, reading: false, split: false }], active: "document-main" };
|
||||
return { layout: "document-main", panes: [emptyPane("document-main")], active: "document-main" };
|
||||
}
|
||||
|
||||
export function selectPane(state: DocumentPanes, id: string): DocumentPanes {
|
||||
return state.active === id || !state.panes.some(pane => pane.id === id) ? state : { ...state, active: id };
|
||||
}
|
||||
|
||||
/** Tabs own documents. Panes keep only a reference and local viewing state. */
|
||||
export function syncDocumentPanes(state: DocumentPanes, live: readonly string[], selected: string | null): DocumentPanes {
|
||||
const ids = new Set(live);
|
||||
let panes = state.panes.map(pane => pane.documentId && !ids.has(pane.documentId) ? { ...pane, documentId: null, reading: false, split: false } : pane);
|
||||
let active = state.active;
|
||||
if (selected && ids.has(selected)) {
|
||||
const existing = panes.find(pane => pane.documentId === selected);
|
||||
if (existing) active = existing.id;
|
||||
else panes = panes.map(pane => pane.id === active ? { ...pane, documentId: selected } : pane);
|
||||
}
|
||||
/** The pane that shows the content, if a pane shows it. */
|
||||
export function paneOf(state: DocumentPanes, content: PaneContent): DocumentPane | undefined {
|
||||
return state.panes.find(pane => "documentId" in content ? pane.documentId === content.documentId : pane.pluginKey === content.pluginKey);
|
||||
}
|
||||
|
||||
/** Remove the panes that became empty. The last pane always stays. */
|
||||
function collapse(state: DocumentPanes, panes: DocumentPane[], active: string): DocumentPanes {
|
||||
let layout = state.layout;
|
||||
for (const pane of panes) {
|
||||
if (pane.documentId === null && panes.length > 1) {
|
||||
if (pane.documentId === null && pane.pluginKey === null && panes.length > 1) {
|
||||
const next = removePane(layout, pane.id);
|
||||
if (next !== null) { layout = next; panes = panes.filter(item => item.id !== pane.id); }
|
||||
}
|
||||
@@ -34,14 +44,45 @@ export function syncDocumentPanes(state: DocumentPanes, live: readonly string[],
|
||||
return { layout, panes, active };
|
||||
}
|
||||
|
||||
export function splitDocumentPane(state: DocumentPanes, source: string, documentId: string, paneId: string): DocumentPanes {
|
||||
const existing = state.panes.find(pane => pane.documentId === documentId);
|
||||
/**
|
||||
* Tabs own documents. Panes keep only a reference and local viewing state.
|
||||
* A selected document that no pane shows replaces the content of the active pane.
|
||||
* Give `livePlugins` to also clear the panes of closed plugin views.
|
||||
*/
|
||||
export function syncDocumentPanes(state: DocumentPanes, live: readonly string[], selected: string | null, livePlugins?: readonly string[]): DocumentPanes {
|
||||
const ids = new Set(live);
|
||||
const plugins = livePlugins ? new Set(livePlugins) : null;
|
||||
let panes = state.panes.map(pane => {
|
||||
if (pane.documentId && !ids.has(pane.documentId)) return { ...pane, documentId: null, reading: false, split: false };
|
||||
if (plugins && pane.pluginKey && !plugins.has(pane.pluginKey)) return { ...pane, pluginKey: null };
|
||||
return pane;
|
||||
});
|
||||
let active = state.active;
|
||||
if (selected && ids.has(selected)) {
|
||||
const existing = panes.find(pane => pane.documentId === selected);
|
||||
if (existing) active = existing.id;
|
||||
else panes = panes.map(pane => pane.id === active ? { ...pane, documentId: selected, pluginKey: null } : pane);
|
||||
}
|
||||
return collapse(state, panes, active);
|
||||
}
|
||||
|
||||
/** Show the content in the active pane. When a pane already shows it, that pane becomes active. */
|
||||
export function showInActivePane(state: DocumentPanes, content: PaneContent): DocumentPanes {
|
||||
const existing = paneOf(state, content);
|
||||
if (existing) return selectPane(state, existing.id);
|
||||
return { ...state, panes: state.panes.map(pane => pane.id === state.active ? filled(pane.id, content) : pane) };
|
||||
}
|
||||
|
||||
/** Show the content in a new pane beside `source`. When a pane already shows it, that pane becomes active. */
|
||||
export function splitWithContent(state: DocumentPanes, source: string, content: PaneContent, paneId: string, direction: "right" | "down" = "right"): DocumentPanes {
|
||||
const existing = paneOf(state, content);
|
||||
if (existing) return selectPane(state, existing.id);
|
||||
if (!state.panes.some(pane => pane.id === source) || state.panes.some(pane => pane.id === paneId)) return state;
|
||||
return {
|
||||
layout: splitPane(state.layout, source, paneId, "right"),
|
||||
panes: [...state.panes, { id: paneId, documentId, reading: false, split: false }], active: paneId,
|
||||
};
|
||||
return { layout: splitPane(state.layout, source, paneId, direction), panes: [...state.panes, filled(paneId, content)], active: paneId };
|
||||
}
|
||||
|
||||
export function splitDocumentPane(state: DocumentPanes, source: string, documentId: string, paneId: string): DocumentPanes {
|
||||
return splitWithContent(state, source, { documentId }, paneId);
|
||||
}
|
||||
|
||||
export function closeDocumentPane(state: DocumentPanes, id: string): DocumentPanes {
|
||||
@@ -50,15 +91,26 @@ export function closeDocumentPane(state: DocumentPanes, id: string): DocumentPan
|
||||
if (layout === null) return state;
|
||||
const bounds = paneBounds(state.layout);
|
||||
const panes = state.panes.filter(pane => pane.id !== id);
|
||||
const neighbor = [...panes].sort((a, b) => Math.abs(bounds[a.id].left - bounds[id].left) - Math.abs(bounds[b.id].left - bounds[id].left))[0];
|
||||
const distance = (pane: DocumentPane) => Math.abs(bounds[pane.id].left - bounds[id].left) + Math.abs(bounds[pane.id].top - bounds[id].top);
|
||||
const neighbor = [...panes].sort((a, b) => distance(a) - distance(b))[0];
|
||||
return { layout, panes, active: state.active === id ? neighbor.id : state.active };
|
||||
}
|
||||
|
||||
/**
|
||||
* One pane shows a plugin view only beside other panes. When the last pane shows a plugin view,
|
||||
* the pane becomes empty and the result names the view, so that the caller can show it over the whole area.
|
||||
*/
|
||||
export function releaseSolePlugin(state: DocumentPanes): { state: DocumentPanes; pluginKey: string | null } {
|
||||
const [pane] = state.panes;
|
||||
if (state.panes.length !== 1 || pane.pluginKey === null) return { state, pluginKey: null };
|
||||
return { state: { ...state, panes: [{ ...pane, pluginKey: null }] }, pluginKey: pane.pluginKey };
|
||||
}
|
||||
|
||||
export function readDocumentPane(state: DocumentPanes, id: string, reading: boolean): DocumentPanes {
|
||||
return { ...state, panes: state.panes.map(pane => pane.id === id ? { ...pane, reading, split: reading ? false : pane.split } : pane) };
|
||||
}
|
||||
|
||||
/** Show or hide the Read view beside the editor. Split ends the Read-only view. */
|
||||
/** Show or hide the Read view beside the editor. Read beside ends the Read-only view. */
|
||||
export function splitDocumentView(state: DocumentPanes, id: string, split: boolean): DocumentPanes {
|
||||
return { ...state, panes: state.panes.map(pane => pane.id === id ? { ...pane, split, reading: split ? false : pane.reading } : pane) };
|
||||
}
|
||||
|
||||
@@ -23,3 +23,17 @@ export function paneBounds(tree: SplitLayout, bounds: PaneBounds = { left: 0, to
|
||||
else { a.height /= 2; b.height /= 2; b.top += a.height; }
|
||||
return { ...paneBounds(tree.first, a), ...paneBounds(tree.second, b) };
|
||||
}
|
||||
|
||||
/** The same panes in the same order, one above the other. A narrow area uses it. */
|
||||
export function stackLayout(tree: SplitLayout): SplitLayout {
|
||||
return typeof tree === "string" ? tree : { direction: "down", first: stackLayout(tree.first), second: stackLayout(tree.second) };
|
||||
}
|
||||
|
||||
/** The direction of the split that put `key` beside a different pane, and the first pane on the other side. */
|
||||
export function splitSibling(tree: SplitLayout, key: string): { direction: "right" | "down"; sibling: string } | null {
|
||||
if (typeof tree === "string") return null;
|
||||
const first = (node: SplitLayout): string => typeof node === "string" ? node : first(node.first);
|
||||
if (tree.second === key) return { direction: tree.direction, sibling: first(tree.first) };
|
||||
if (tree.first === key) return { direction: tree.direction, sibling: first(tree.second) };
|
||||
return splitSibling(tree.first, key) ?? splitSibling(tree.second, key);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user