refactor(canvas): getDrawioSrc takes the editor's source, and CanvasStage can be given one

This commit is contained in:
dayuan.jiang
2026-10-11 20:56:07 +09:00
parent 20ce02a602
commit 7e2edbf8a1
2 changed files with 53 additions and 27 deletions
+41 -22
View File
@@ -6,28 +6,49 @@ import { BLANK_MXFILE } from "@/packages/mcp-server/src/pages.ts"
const EXTERNAL_DRAWIO_URL = process.env.NEXT_PUBLIC_DRAWIO_BASE_URL || ""
/**
* URL of the draw.io editor iframe. By default this is the copy bundled in
* public/drawio, served from our own origin.
* Where the editor comes from and how it starts. The web app fills it from
* its build settings (webDrawioSource); the MCP's canvas shell from the
* config its server writes into the page.
*/
export function getDrawioSrc({
lang,
isElectron,
dark,
}: {
lang: Locale
isElectron: boolean
dark: boolean
}): string {
// index.html: Next.js does not serve directory indexes
const base =
EXTERNAL_DRAWIO_URL ||
`${window.location.origin}${getAssetUrl("/drawio/index.html")}`
const url = new URL(base)
export interface DrawioSource {
/** The editor page: the bundled copy's index.html on this origin, or an
* external draw.io (cross-origin, limited features) */
editorUrl: string
/** draw.io theme (its ui parameter); none: draw.io's default and the
* user's own pick */
ui?: string
/** Language in draw.io's naming ("zh-tw"); none: draw.io's own pick */
lang?: string
/** No calls to external services (the desktop app) */
offline?: boolean
}
/**
* The web app's editor: the copy in public/drawio, served from our own
* origin (index.html, as Next.js serves no directory indexes), or
* NEXT_PUBLIC_DRAWIO_BASE_URL; the simple theme in the page's language.
*/
export function webDrawioSource(lang: Locale): DrawioSource {
const isElectron = !!(window as { electronAPI?: unknown }).electronAPI
return {
editorUrl: EXTERNAL_DRAWIO_URL || getAssetUrl("/drawio/index.html"),
ui: "simple",
// draw.io names Traditional Chinese "zh-tw"
lang: lang === "zh-Hant" ? "zh-tw" : lang,
offline: isElectron && !EXTERNAL_DRAWIO_URL,
}
}
/** URL of the draw.io editor iframe */
export function getDrawioSrc(source: DrawioSource, dark: boolean): string {
const url = new URL(source.editorUrl, window.location.href)
const params: Record<string, string> = {
embed: "1",
proto: "json",
configure: "1",
ui: "simple",
}
if (source.ui) params.ui = source.ui
Object.assign(params, {
spin: "0",
libraries: "0",
noSaveBtn: "1",
@@ -36,11 +57,9 @@ export function getDrawioSrc({
// draw.io's own dark mode (switched in place later when the editor
// can be driven directly)
dark: dark ? "1" : "0",
// draw.io names Traditional Chinese "zh-tw"
lang: lang === "zh-Hant" ? "zh-tw" : lang,
}
// No calls to external services from the desktop app
if (isElectron && !EXTERNAL_DRAWIO_URL) params.offline = "1"
})
if (source.lang) params.lang = source.lang
if (source.offline) params.offline = "1"
for (const [key, value] of Object.entries(params)) {
url.searchParams.set(key, value)
}