+ {shownError === INVALID_SVG ? tx("The SVG source is not valid.") : tx`Cannot show the image: ${shownError}`}
+
) : (
- {src && (
+ {shownSrc && (
= 4 && styles.pixelated)}
@@ -266,7 +291,7 @@ export function ImageViewer({ input }: { input: EditorInput }) {
: { visibility: "hidden" }
}
onLoad={(e) => onLoad(e.currentTarget)}
- onError={() => setError("the file is not a valid image")}
+ onError={() => (live ? setInvalidSrc(shownSrc) : setError("the file is not a valid image"))}
/>
)}
diff --git a/src/features/viewers/LayoutActions.tsx b/src/features/viewers/LayoutActions.tsx
new file mode 100644
index 000000000..1d14ddb18
--- /dev/null
+++ b/src/features/viewers/LayoutActions.tsx
@@ -0,0 +1,38 @@
+import { IconButton } from "@/ui";
+import type { LayoutMode } from "./layoutModes";
+import styles from "./markdown/Markdown.module.css";
+
+/** Editor Only / Editor and Preview / Preview Only buttons of a text editor with a preview. */
+export function LayoutActions({ tabId, mode, label }: { tabId: string; mode: LayoutMode; label: string }) {
+ return (
+
+
+
+
+
+ );
+}
diff --git a/src/features/viewers/index.ts b/src/features/viewers/index.ts
index 2f37e0b81..4d1fc721d 100644
--- a/src/features/viewers/index.ts
+++ b/src/features/viewers/index.ts
@@ -1,7 +1,7 @@
/**
* Non-text editors and the Markdown preview: images open in an image viewer,
* binary and very large files in a read-only hex view (both through file
- * editor providers consulted by `openFile`), and Markdown files get IDEA's
+ * editor providers consulted by `openFile`), and Markdown and SVG files get IDEA's
* Editor / Editor and Preview / Preview layouts around the text editor.
*/
import { registerActions, type Action } from "@/platform/actions";
@@ -29,12 +29,16 @@ import {
imageInput,
isImagePath,
isMarkdownPath,
+ isSvgPath,
wantsHexView,
} from "./fileKinds";
import { ImageViewer, toggleChessboard, useImageViewers } from "./ImageViewer";
import { HexViewer } from "./HexViewer";
import { registerMarkdownLiveSource } from "./markdown/live";
-import { setMarkdownMode, useMarkdownSourceHidden, type MarkdownMode } from "./markdown/modes";
+import { setMarkdownMode, useMarkdownModes, useMarkdownSourceHidden } from "./markdown/modes";
+import type { LayoutMode } from "./layoutModes";
+import { registerSvgLiveSource } from "./svg/live";
+import { SvgLayoutActions, svgModes } from "./svg/SvgEditor";
import { MarkdownLayoutActions } from "./markdown/MarkdownLayoutActions";
import { canExtractImages, extractImages, EXTRACT_IMAGES, EXTRACT_IMAGES_TITLE } from "./markdown/extractImages";
@@ -65,19 +69,35 @@ function imageActions(): Action[] {
];
}
+/**
+ * The tab whose layout an Editor Only / Editor and Preview / Preview Only action sets: the one its
+ * tab button passes, else the focused editor's when it's a Markdown or SVG file.
+ */
+function layoutTarget(arg: unknown): { tabId: string; set: (tabId: string, mode: LayoutMode) => void } | null {
+ if (typeof arg === "string") {
+ return { tabId: arg, set: svgModes.useModes.getState().byTab[arg] ? svgModes.set : setMarkdownMode };
+ }
+ const editor = activeEditor();
+ const path = editor?.ctx.path ?? "";
+ if (!editor?.tabId) return null;
+ if (isSvgPath(path)) return { tabId: editor.tabId, set: svgModes.set };
+ if (isMarkdownPath(path) || useMarkdownModes.getState().byTab[editor.tabId])
+ return { tabId: editor.tabId, set: setMarkdownMode };
+ return null;
+}
+
function markdownActions(): Action[] {
- const mode = (id: string, title: string, m: MarkdownMode, icon: string): Action => ({
+ const mode = (id: string, title: string, m: LayoutMode, icon: string): Action => ({
id,
title,
category: "Markdown",
icon,
- keywords: ["markdown", "preview", "layout"],
- when: "textEditorActive && editorLangId == markdown",
+ keywords: ["markdown", "svg", "preview", "layout"],
+ when: "textEditorActive",
+ enabled: ({ arg }) => !!layoutTarget(arg),
run: ({ arg }) => {
- // The tab's layout buttons pass their tab; the keyboard acts on the focused editor's.
- const editor = activeEditor();
- const tabId = typeof arg === "string" ? arg : isMarkdownPath(editor?.ctx.path ?? "") ? editor?.tabId : undefined;
- if (tabId) setMarkdownMode(tabId, m);
+ const target = layoutTarget(arg);
+ target?.set(target.tabId, m);
},
});
return [
@@ -143,6 +163,15 @@ export function activate(): () => void {
useSourceHidden: useMarkdownSourceHidden,
}),
registerMarkdownLiveSource(),
+ registerTextEditorWrapper({
+ id: "viewers.svg",
+ fallback: true,
+ matches: isSvgPath,
+ component: lazyComponent(() => import("./svg/SvgEditor").then((m) => m.SvgEditor)),
+ tabActions: SvgLayoutActions,
+ useSourceHidden: svgModes.useSourceHidden,
+ }),
+ registerSvgLiveSource(),
registerMenuGroup({
id: EXTRACT_IMAGES,
menus: ["editor"],
diff --git a/src/features/viewers/layoutModes.ts b/src/features/viewers/layoutModes.ts
new file mode 100644
index 000000000..2e9972dbc
--- /dev/null
+++ b/src/features/viewers/layoutModes.ts
@@ -0,0 +1,56 @@
+/**
+ * Layout of a text editor with a preview (IDEA's TextEditorWithPreview): Editor Only / Editor and
+ * Preview / Preview Only, per editor tab. Split panes showing the same file each keep their own;
+ * tabs opened later take the layout chosen last for that kind of file.
+ */
+import { create, type StoreApi, type UseBoundStore } from "zustand";
+
+export type LayoutMode = "editor" | "split" | "preview";
+
+const MODES: readonly LayoutMode[] = ["editor", "split", "preview"];
+
+export interface LayoutModesState {
+ byTab: Record;
+ /** Mode of tabs opened since: the last one chosen. */
+ fallback: LayoutMode;
+}
+
+export const modeOf = (s: LayoutModesState, tabId: string): LayoutMode => s.byTab[tabId] ?? s.fallback;
+
+export interface LayoutModes {
+ useModes: UseBoundStore>;
+ /** Preview Only hides the source editor of `tabId`. */
+ useSourceHidden: (tabId: string) => boolean;
+ /** Fixes a shown tab's mode, so choosing one in another tab doesn't change it. */
+ pin: (tabId: string) => void;
+ set: (tabId: string, mode: LayoutMode) => void;
+}
+
+/** The layouts of one kind of file, the last one chosen kept under `storageKey`. */
+export function createLayoutModes(storageKey: string): LayoutModes {
+ const stored = (): LayoutMode => {
+ try {
+ const v = localStorage.getItem(storageKey);
+ return MODES.includes(v as LayoutMode) ? (v as LayoutMode) : "split";
+ } catch {
+ return "split";
+ }
+ };
+ const useModes = create(() => ({ byTab: {}, fallback: stored() }));
+ return {
+ useModes,
+ useSourceHidden: (tabId) => useModes((s) => modeOf(s, tabId) === "preview"),
+ pin: (tabId) => {
+ if (useModes.getState().byTab[tabId]) return;
+ useModes.setState((s) => ({ byTab: { ...s.byTab, [tabId]: s.fallback } }));
+ },
+ set: (tabId, mode) => {
+ useModes.setState((s) => ({ byTab: { ...s.byTab, [tabId]: mode }, fallback: mode }));
+ try {
+ localStorage.setItem(storageKey, mode);
+ } catch {
+ // Per-viewer convenience only.
+ }
+ },
+ };
+}
diff --git a/src/features/viewers/markdown/MarkdownLayoutActions.tsx b/src/features/viewers/markdown/MarkdownLayoutActions.tsx
index aab56690c..31de348ef 100644
--- a/src/features/viewers/markdown/MarkdownLayoutActions.tsx
+++ b/src/features/viewers/markdown/MarkdownLayoutActions.tsx
@@ -1,39 +1,8 @@
-import { IconButton } from "@/ui";
import { tx } from "@/platform/i18n";
+import { LayoutActions } from "../LayoutActions";
import { modeOf, useMarkdownModes } from "./modes";
-import styles from "./Markdown.module.css";
export function MarkdownLayoutActions({ tabId }: { path: string; tabId: string }) {
const mode = useMarkdownModes((s) => modeOf(s, tabId));
- return (
-
-
-
-
-
- );
+ return ;
}
diff --git a/src/features/viewers/markdown/modes.ts b/src/features/viewers/markdown/modes.ts
index 620af5327..ab23b18f9 100644
--- a/src/features/viewers/markdown/modes.ts
+++ b/src/features/viewers/markdown/modes.ts
@@ -1,47 +1,12 @@
-/**
- * Markdown editor layout per editor tab: Editor only / Editor and Preview / Preview only. Split
- * panes showing the same file each keep their own.
- */
-import { create } from "zustand";
+/** Markdown editor layout per editor tab: Editor only / Editor and Preview / Preview only. */
+import { createLayoutModes, modeOf, type LayoutMode } from "../layoutModes";
-export type MarkdownMode = "editor" | "split" | "preview";
+export type MarkdownMode = LayoutMode;
+export { modeOf };
-const DEFAULT_KEY = "openidea.markdown.defaultMode";
-const MODES: readonly MarkdownMode[] = ["editor", "split", "preview"];
+const markdown = createLayoutModes("openidea.markdown.defaultMode");
-function storedDefault(): MarkdownMode {
- try {
- const v = localStorage.getItem(DEFAULT_KEY);
- return MODES.includes(v as MarkdownMode) ? (v as MarkdownMode) : "split";
- } catch {
- return "split";
- }
-}
-
-interface ModesState {
- byTab: Record;
- /** Mode of tabs opened since: the last one chosen. */
- fallback: MarkdownMode;
-}
-
-export const useMarkdownModes = create(() => ({ byTab: {}, fallback: storedDefault() }));
-
-export const modeOf = (s: ModesState, tabId: string): MarkdownMode => s.byTab[tabId] ?? s.fallback;
-
-/** Preview Only hides the source editor of `tabId`. */
-export const useMarkdownSourceHidden = (tabId: string) => useMarkdownModes((s) => modeOf(s, tabId) === "preview");
-
-/** Fixes a shown tab's mode, so choosing one in another tab doesn't change it. */
-export function pinMarkdownMode(tabId: string) {
- if (useMarkdownModes.getState().byTab[tabId]) return;
- useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, [tabId]: s.fallback } }));
-}
-
-export function setMarkdownMode(tabId: string, mode: MarkdownMode) {
- useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, [tabId]: mode }, fallback: mode }));
- try {
- localStorage.setItem(DEFAULT_KEY, mode);
- } catch {
- // Per-viewer convenience only.
- }
-}
+export const useMarkdownModes = markdown.useModes;
+export const useMarkdownSourceHidden = markdown.useSourceHidden;
+export const pinMarkdownMode = markdown.pin;
+export const setMarkdownMode = markdown.set;
diff --git a/src/features/viewers/svg/SvgEditor.tsx b/src/features/viewers/svg/SvgEditor.tsx
new file mode 100644
index 000000000..797792a18
--- /dev/null
+++ b/src/features/viewers/svg/SvgEditor.tsx
@@ -0,0 +1,95 @@
+/**
+ * SVG files as IDEA opens them: the source with the image viewer beside it (Editor Only / Editor and
+ * Preview / Preview Only), the preview redrawn from the source as it's typed.
+ */
+import { useEffect, useRef, type ReactNode } from "react";
+import clsx from "clsx";
+import { editorsFor } from "@/editor/api";
+import { tx } from "@/platform/i18n";
+import { FloatingToolbar, SplitGroup, SplitHandle, SplitPanel } from "@/ui";
+import { imageInput } from "../fileKinds";
+import { ImageViewer } from "../ImageViewer";
+import { LayoutActions } from "../LayoutActions";
+import { createLayoutModes, modeOf } from "../layoutModes";
+import { useLiveSvg } from "./live";
+import styles from "../markdown/Markdown.module.css";
+
+export const svgModes = createLayoutModes("openidea.svg.defaultMode");
+
+const SPLIT_STORAGE_KEY = "openidea.svg.split";
+const DEFAULT_SPLIT_LAYOUT = { editor: 50, preview: 50 };
+
+export function SvgLayoutActions({ tabId }: { path: string; tabId: string }) {
+ const mode = svgModes.useModes((s) => modeOf(s, tabId));
+ return ;
+}
+
+function SvgPreview({ path }: { path: string }) {
+ const source = useLiveSvg((s) => s[path]) ?? null;
+ return ;
+}
+
+export function SvgEditor({
+ path,
+ tabId,
+ children,
+ actionsInTabs = false,
+}: {
+ path: string;
+ tabId: string;
+ children: ReactNode;
+ actionsInTabs?: boolean;
+}) {
+ const mode = svgModes.useModes((s) => modeOf(s, tabId));
+ useEffect(() => svgModes.pin(tabId), [tabId]);
+ const ref = useRef(null);
+ const editorRef = useRef(null);
+
+ // CodeMirror skips resizes right after a view update; re-measure on drags and mode changes.
+ useEffect(() => {
+ const el = editorRef.current;
+ if (!el || typeof ResizeObserver === "undefined") return;
+ let width = el.clientWidth;
+ const ro = new ResizeObserver(() => {
+ const next = el.clientWidth;
+ if (next === width) return;
+ width = next;
+ if (next <= 0) return;
+ for (const ed of editorsFor(path)) {
+ if (!ed.tabId || ed.tabId === tabId) ed.view.requestMeasure();
+ }
+ });
+ ro.observe(el);
+ return () => ro.disconnect();
+ }, [path, tabId]);
+
+ const splitBounds = mode === "split" ? { minSize: "10%", maxSize: "90%" } : {};
+ return (
+
+
+ {/* Hidden but mounted in Preview Only, so the document stays open (see MarkdownEditor). */}
+
+