Файли
openidea/plugins/official/graphviz/web-src/src/render.ts
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

69 рядки
2.6 KiB
TypeScript

/**
* The preview's logic apart from the page: rendering options, Graphviz's
* messages as problems, export names. Kept free of the DOM so it is tested
* against the real Graphviz in Node.
*/
import type { RenderOptions, RenderResult } from "@viz-js/viz";
/** Layout engines offered, the default first. */
export const ENGINES = ["dot", "neato", "fdp", "sfdp", "circo", "twopi", "osage", "patchwork"] as const;
export type Engine = (typeof ENGINES)[number];
export interface Problem {
/** 1-based, when Graphviz names one. */
line: number | null;
message: string;
level: "error" | "warning";
}
/**
* Defaults a graph can still override: transparent background and the IDE's
* text colour, so an unstyled graph reads in a dark theme too. Exports use
* none of them (`null`): they look as Graphviz draws them.
*/
export function renderOptions(engine: Engine, foreground: string | null): RenderOptions {
const options: RenderOptions = { format: "svg", engine };
const color = foreground && graphvizColor(foreground);
if (color) {
options.graphAttributes = { bgcolor: "transparent", color, fontcolor: color };
options.nodeAttributes = { color, fontcolor: color };
options.edgeAttributes = { color, fontcolor: color };
}
return options;
}
/**
* A CSS colour as Graphviz reads it (`#rrggbb[aa]`): the theme's variables
* are `rgb()` / `rgba()`, which it doesn't know. `null` when not one.
*/
export function graphvizColor(css: string): string | null {
const hex = (n: number) =>
Math.round(Math.min(255, Math.max(0, n)))
.toString(16)
.padStart(2, "0");
const value = css.trim();
if (/^#([0-9a-f]{6}|[0-9a-f]{8})$/i.test(value)) return value.toLowerCase();
const m = /^rgba?\(\s*([\d.]+)[\s,]+([\d.]+)[\s,]+([\d.]+)(?:\s*[,/]\s*([\d.]+)(%?))?\s*\)$/i.exec(value);
if (!m) return null;
const alpha = m[4] === undefined ? 1 : Number(m[4]) / (m[5] ? 100 : 1);
return `#${hex(+m[1]!)}${hex(+m[2]!)}${hex(+m[3]!)}${alpha < 1 ? hex(alpha * 255) : ""}`;
}
/** What Graphviz reported, as problems (on the line it names, if any). */
export function problemsOf(result: RenderResult): Problem[] {
return result.errors.map((e) => {
const line = /\bline (\d+)\b/.exec(e.message);
return {
line: line ? Number(line[1]) : null,
message: e.message.replace(/^(Error|Warning):\s*/i, "").trim(),
level: e.level === "warning" ? "warning" : "error",
};
});
}
/** The name an export starts with: the file's, without `.dot` / `.gv`. */
export function exportName(file: string, extension: string): string {
const base = file.replace(/\.(dot|gv)$/i, "") || "graph";
return `${base}.${extension}`;
}