229 lines
7.9 KiB
Svelte
229 lines
7.9 KiB
Svelte
<script lang="ts">
|
|
import EditorHost from "$lib/editor/EditorHost.svelte";
|
|
import LiveWriter from "$lib/editor/LiveWriter.svelte";
|
|
import { onDestroy, tick } from "svelte";
|
|
import { retainEditorSessions } from "$lib/editor/sessions";
|
|
import ImageViewer from "./ImageViewer.svelte";
|
|
import MarkdownView from "./MarkdownView.svelte";
|
|
import ModeSwitcher from "$lib/ui/ModeSwitcher.svelte";
|
|
import Icon from "$lib/ui/Icon.svelte";
|
|
import { isMarkdownPath } from "$lib/workspace/media";
|
|
import type { Chrome, OpenBuffer } from "$lib/workspace/types";
|
|
import type { TableContextActions } from "$lib/editor/tableCellActions";
|
|
|
|
type Props = {
|
|
buffer: OpenBuffer | null;
|
|
tabs: OpenBuffer[];
|
|
chrome: Chrome;
|
|
reading: boolean;
|
|
focusLine: number | null;
|
|
onChange: (value: string, id: string) => void;
|
|
onRenameTitle?: (id: string, title: string) => Promise<void>;
|
|
onInsertImage?: (file: File) => Promise<string | null>;
|
|
onToggleReading?: () => void;
|
|
onToggleChrome?: () => void;
|
|
onOpenLink?: (kind: "md" | "wiki", target: string) => void;
|
|
onOpenWorkspace: () => void;
|
|
onNewFile: () => void;
|
|
onQuickOpen: () => void;
|
|
hasWorkspace: boolean;
|
|
tableActions?: TableContextActions;
|
|
};
|
|
|
|
let { buffer, tabs, chrome, reading, focusLine, onChange, onRenameTitle, onInsertImage, onToggleReading, onToggleChrome, onOpenLink, onOpenWorkspace, onNewFile, onQuickOpen, hasWorkspace, tableActions }: Props = $props();
|
|
let previewMode = $state<"split" | "preview">("split");
|
|
let modeControls = $state<HTMLDivElement>();
|
|
const shouldFocusOnMount = () => !modeControls?.contains(document.activeElement);
|
|
const previewScroll = new Map<string, { top: number; left: number }>();
|
|
$effect(() => {
|
|
const ids = tabs.map(tab => tab.id);
|
|
retainEditorSessions(ids);
|
|
for (const id of previewScroll.keys()) if (!ids.includes(id)) previewScroll.delete(id);
|
|
});
|
|
onDestroy(() => retainEditorSessions([]));
|
|
|
|
function rememberPreviewScroll(node: HTMLDivElement, id: string) {
|
|
const saved = previewScroll.get(id);
|
|
let cancelled = false;
|
|
let restoring = true;
|
|
let frame = 0;
|
|
const save = () => {
|
|
if (!restoring) previewScroll.set(id, { top: node.scrollTop, left: node.scrollLeft });
|
|
};
|
|
node.addEventListener("scroll", save);
|
|
// Markdown renders in an effect; wait for its DOM before restoring offsets.
|
|
void tick().then(() => {
|
|
if (cancelled) return;
|
|
frame = requestAnimationFrame(() => {
|
|
node.scrollTop = saved?.top ?? 0;
|
|
node.scrollLeft = saved?.left ?? 0;
|
|
restoring = false;
|
|
});
|
|
});
|
|
return { destroy() {
|
|
cancelled = true;
|
|
cancelAnimationFrame(frame);
|
|
node.removeEventListener("scroll", save);
|
|
} };
|
|
}
|
|
|
|
const isIde = $derived(chrome === "Ide");
|
|
const isImage = $derived(buffer?.kind === "image");
|
|
const canPreview = $derived(buffer !== null && buffer.kind === "text" && isMarkdownPath(buffer.relPath));
|
|
const documentMode = $derived(!reading ? "edit" : previewMode === "preview" ? "read" : "split");
|
|
|
|
function setDocumentMode(mode: string) {
|
|
if (mode !== "edit") previewMode = mode === "read" ? "preview" : "split";
|
|
if (reading !== (mode !== "edit")) onToggleReading?.();
|
|
}
|
|
</script>
|
|
|
|
{#if !buffer || buffer.kind === "empty"}
|
|
<div class="empty">
|
|
<h1>{buffer?.kind === "empty" ? "New tab" : "One project. Notes, code, terminal."}</h1>
|
|
<p>{hasWorkspace ? "Open a file to get started, or create a note or script." : "Open a folder to work with your own files."}</p>
|
|
<div class="welcome-actions">
|
|
<button onclick={onOpenWorkspace}><Icon icon="folder-open" /> Open folder <kbd>Ctrl O</kbd></button>
|
|
{#if hasWorkspace}
|
|
<button onclick={onNewFile}><Icon icon="file-plus" /> New file <kbd>Ctrl N</kbd></button>
|
|
<button onclick={onQuickOpen}><Icon icon="search" /> Find a file <kbd>Ctrl P</kbd></button>
|
|
{/if}
|
|
</div>
|
|
<p class="hint">Save · Ctrl S Terminal · Ctrl ` Find in file · Ctrl F</p>
|
|
</div>
|
|
{:else}
|
|
<div class="editor">
|
|
<div class="chrome-bar">
|
|
<span class="breadcrumb" title={buffer.relPath}>{buffer.relPath}</span>
|
|
<div class="actions" bind:this={modeControls}>
|
|
{#if canPreview}
|
|
<ModeSwitcher
|
|
label="Document view"
|
|
value={documentMode}
|
|
options={[
|
|
{ value: "edit", label: "Edit", icon: "pen" },
|
|
{ value: "read", label: "Read", icon: "book-open" },
|
|
{ value: "split", label: "Split", icon: "split" },
|
|
]}
|
|
onchange={setDocumentMode}
|
|
/>
|
|
{/if}
|
|
<ModeSwitcher
|
|
label="Editor layout"
|
|
value={chrome}
|
|
options={[
|
|
{ value: "Ide", label: "IDE", icon: "code" },
|
|
{ value: "Writing", label: "Writer", icon: "text" },
|
|
]}
|
|
shortcut=" (Ctrl E)"
|
|
onchange={(mode) => { if (mode !== chrome) onToggleChrome?.(); }}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div class="body" class:split={reading && canPreview && previewMode === "split"}>
|
|
{#if isImage && buffer.imageUrl}
|
|
<ImageViewer src={buffer.imageUrl} name={buffer.name} />
|
|
{:else}
|
|
<div class="editor-wrap" class:hidden={reading && canPreview && previewMode === "preview"}>
|
|
{#key buffer.id}
|
|
{#if !isIde && canPreview && !reading}
|
|
<LiveWriter {buffer} {onChange} {onRenameTitle} {onInsertImage} {onOpenLink} {focusLine} {tableActions} {shouldFocusOnMount} />
|
|
{:else}
|
|
<EditorHost {buffer} {chrome} {onChange} {onInsertImage} {focusLine} {tableActions} {shouldFocusOnMount} />
|
|
{/if}
|
|
{/key}
|
|
</div>
|
|
{#if reading && canPreview}
|
|
{#key buffer.id}
|
|
<div class="md-page" use:rememberPreviewScroll={buffer.id}>
|
|
<MarkdownView noteRel={buffer.relPath} contents={buffer.contents} {onOpenLink} />
|
|
</div>
|
|
{/key}
|
|
{/if}
|
|
{/if}
|
|
</div>
|
|
</div>
|
|
{/if}
|
|
|
|
<style>
|
|
.breadcrumb { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-right: auto; }
|
|
.welcome-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; }
|
|
.welcome-actions button { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--rs-border); border-radius: var(--rs-control-radius); background: var(--rs-bg); padding: 10px 14px; }
|
|
kbd { margin-left: 12px; color: var(--rs-text-dim); font-size: 11px; }
|
|
h1 { color: var(--rs-text-strong); font-size: 24px; font-weight: 500; }
|
|
.hint { margin-top: 32px; font-size: 12px; }
|
|
.hidden { display: none; }
|
|
.split .md-page { border-left: 1px solid var(--rs-border); }
|
|
.editor { container-type: inline-size; }
|
|
.empty,
|
|
.editor {
|
|
height: 100%;
|
|
display: flex;
|
|
flex-direction: column;
|
|
min-width: 0;
|
|
min-height: 0;
|
|
flex: 1;
|
|
}
|
|
|
|
.empty {
|
|
align-items: center;
|
|
justify-content: center;
|
|
color: var(--rs-text-dim);
|
|
padding: 24px;
|
|
text-align: center;
|
|
}
|
|
|
|
.chrome-bar {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: flex-end;
|
|
gap: 8px;
|
|
height: 42px;
|
|
padding: 0 10px;
|
|
background: var(--rs-bg-elevated);
|
|
border-bottom: 1px solid var(--rs-border);
|
|
color: var(--rs-text-dim);
|
|
flex: 0 0 auto;
|
|
}
|
|
|
|
.actions {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
flex: 0 0 auto;
|
|
}
|
|
|
|
.body {
|
|
flex: 1;
|
|
display: flex;
|
|
min-height: 0;
|
|
min-width: 0;
|
|
}
|
|
|
|
.body > :global(*) {
|
|
flex: 1;
|
|
min-width: 0;
|
|
}
|
|
|
|
.md-page {
|
|
flex: 1;
|
|
min-height: 0;
|
|
overflow: auto;
|
|
padding: clamp(24px, 4vw, 56px) clamp(22px, 4vw, 64px) 96px;
|
|
scrollbar-gutter: stable;
|
|
}
|
|
|
|
.editor-wrap {
|
|
flex: 1;
|
|
min-width: 0;
|
|
min-height: 0;
|
|
position: relative;
|
|
}
|
|
|
|
@container (max-width: 680px) {
|
|
.body.split { flex-direction: column; }
|
|
.md-page { padding: 28px 24px 64px; }
|
|
.split .md-page { border-left: 0; border-top: 1px solid var(--rs-border); }
|
|
}
|
|
</style>
|