Файли
openidea/plugins/official/excalidraw/web-src/src/main.tsx
T
1c5a92f952 feat(plugins): plugin system with extension points, consent, Wasm runtime and plugin API 1.0 (#971)
A plugin system built on WebAssembly components (wasmtime, WASI p2), with a
typed WIT API (`openidea:[email protected]`) and a Rust SDK, shipped as a preview.

- Extension points declared in `plugin.toml` (commands, languages, themes,
  icon themes, menus, key bindings, status items, settings, views, custom
  editors, file icons and templates, run detectors, editor providers),
  validated before anything takes effect and projected onto the workbench's
  own registries: plugin themes and file icon themes are chosen in
  Settings › Appearance and Quick Switch, plugin shortcuts are edited in
  Settings › Keymap, plugin problems are grouped in Settings › Inspections.
- Capabilities with risk levels and consent at install, pinned `process:`
  programs, secrets in the OS keychain.
- Runtime: one thread per active plugin, fuel/time/memory budgets, soft and
  hard deadlines on the editing path, cancellation, crash backoff, idle
  reclaim, a precompiled-component cache; remote projects run code plugins on
  `openidea-server`.
- Web views and custom editors with a sandboxed page, a document bridge
  joined to the text editor's undo history, and split or preview layouts.
- Distribution: signed `.oiplugin` packages, a static marketplace index with
  revocation, versioned installs with rollback, install from a `.zip` or a
  folder. The release workflow signs and publishes the official plugins and
  their index, which released builds use as the default marketplace.
- Official plugins: String Tools, Key Promoter, Git Toolbox, AI Completion,
  Checkstyle, MyBatis Navigator, CSV Editor, Solidity, Material Icons, Harbor
  Night, Excalidraw, Graphviz, Regex Tester, License Header.

Closes #732

Co-authored-by: Deng Liu <[email protected]>
Co-authored-by: florentvdry <[email protected]>
2026-11-04 02:54:31 +08:00

124 рядки
4.1 KiB
TypeScript

/**
* The Excalidraw plugin's page: the canvas of the `.excalidraw` file its
* custom editor shows, kept in step with the file's text (see sync.ts), in
* the IDE's light or dark theme.
*/
import "./assets";
import { Excalidraw, MainMenu, THEME } from "@excalidraw/excalidraw";
import "@excalidraw/excalidraw/index.css";
import type { ExcalidrawImperativeAPI, UIOptions } from "@excalidraw/excalidraw/types";
import { StrictMode, useEffect, useMemo, useRef, useState } from "react";
import { createRoot } from "react-dom/client";
import { routeDownloadsToTheIde } from "./downloads";
import { applyScene, codec, exportName, loadSceneFonts, type Scene } from "./scene";
import { DocumentSync } from "./sync";
// The canvas is the file's: the IDE opens, saves and themes it, so the page
// keeps only what works on the drawing itself (no Open / Save to, no links
// out of a page without network).
const UI_OPTIONS: UIOptions = {
canvasActions: {
changeViewBackgroundColor: true,
clearCanvas: true,
export: false,
loadScene: false,
saveAsImage: true,
saveToActiveFile: false,
toggleTheme: false,
},
};
function Menu() {
return (
<MainMenu>
<MainMenu.DefaultItems.SaveAsImage />
<MainMenu.DefaultItems.SearchMenu />
<MainMenu.DefaultItems.Help />
<MainMenu.DefaultItems.ClearCanvas />
<MainMenu.Separator />
<MainMenu.DefaultItems.ChangeCanvasBackground />
</MainMenu>
);
}
function useIdeTheme(bridge: OpenideaBridge): "dark" | "light" {
const [dark, setDark] = useState(bridge.theme?.dark ?? window.matchMedia?.("(prefers-color-scheme: dark)").matches);
useEffect(() => bridge.onThemeChange((t) => setDark(t.dark)), [bridge]);
return dark ? THEME.DARK : THEME.LIGHT;
}
type Start = { scene: Scene | null; error: string | null; readOnly: boolean; name: string };
function App({ bridge }: { bridge: OpenideaBridge }) {
const theme = useIdeTheme(bridge);
const [api, setApi] = useState<ExcalidrawImperativeAPI | null>(null);
const [start, setStart] = useState<Start | null>(null);
const [error, setError] = useState<string | null>(null);
// The document's `source`: the canvas doesn't keep it, the file should.
const source = useRef<string | undefined>(undefined);
// `apply` reads the API when the document changes, which is after the canvas exists.
const sync = useMemo(() => {
let current: ExcalidrawImperativeAPI | null = null;
const s = new DocumentSync(bridge.document, codec, {
apply: (scene) => {
source.current = scene.source;
if (current) applyScene(current, scene);
},
error: setError,
});
return Object.assign(s, { bind: (a: ExcalidrawImperativeAPI | null) => void (current = a) });
}, [bridge]);
useEffect(() => {
sync.start().then(
(s) => {
source.current = s.scene?.source;
setStart(s);
setError(s.error);
},
(e: unknown) =>
setStart({ scene: null, error: String(e instanceof Error ? e.message : e), readOnly: true, name: "" }),
);
}, [sync]);
useEffect(() => {
sync.bind(api);
if (api) void loadSceneFonts(api);
}, [api, sync]);
if (!start) return null;
if (!start.scene && error)
return (
<div className="openidea-error">
<p>This file can't be shown as a drawing until its text is valid again:</p>
<pre>{error}</pre>
</div>
);
return (
<Excalidraw
excalidrawAPI={setApi}
initialData={start.scene ? { ...start.scene, scrollToContent: true } : null}
theme={theme}
viewModeEnabled={start.readOnly}
name={exportName(start.name)}
UIOptions={UI_OPTIONS}
onChange={(elements, appState, files) => sync.changed({ elements, appState, files, source: source.current })}
>
<Menu />
</Excalidraw>
);
}
const bridge = window.openidea;
const root = createRoot(document.getElementById("root")!);
if (bridge) {
routeDownloadsToTheIde(bridge);
root.render(
<StrictMode>
<App bridge={bridge} />
</StrictMode>,
);
} else {
root.render(<p className="openidea-error">This page runs inside OpenIDEA.</p>);
}