-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b289e8
commit 5bff683
Showing
6 changed files
with
99 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { isString } from "doc-editor-utils"; | ||
import type { DiagramEditor } from "embed-drawio/dist/es/core/editor"; | ||
import type { DiagramViewer } from "embed-drawio/dist/es/core/viewer"; | ||
import { makeSVGDownloader, svgToBase64 } from "embed-drawio/dist/es/utils/svg"; | ||
import { stringToXml } from "embed-drawio/dist/es/utils/xml"; | ||
|
||
let editor: typeof DiagramEditor | null = null; | ||
export const loadEditor = async (): Promise<typeof DiagramEditor> => { | ||
if (editor) return Promise.resolve(editor); | ||
const res = await Promise.all([ | ||
import(/* webpackChunkName: "embed-drawio-editor" */ "embed-drawio/dist/es/core/editor"), | ||
// @ts-expect-error css declaration | ||
import(/* webpackChunkName: "embed-drawio-css" */ "embed-drawio/dist/es/index.css"), | ||
]); | ||
editor = res[0].DiagramEditor; | ||
return editor; | ||
}; | ||
|
||
let viewer: typeof DiagramViewer | null = null; | ||
export const loadViewer = async (): Promise<typeof DiagramViewer> => { | ||
if (viewer) return Promise.resolve(viewer); | ||
const res = await Promise.all([ | ||
import(/* webpackChunkName: "embed-drawio-viewer" */ "embed-drawio/dist/es/core/viewer"), | ||
]); | ||
viewer = res[0].DiagramViewer; | ||
return viewer; | ||
}; | ||
|
||
export const getSvg = async (xml: XMLDocument | string): Promise<SVGElement | null> => { | ||
const Viewer = await loadViewer(); | ||
const xmlDoc = isString(xml) ? stringToXml(xml) : xml; | ||
const viewer = new Viewer(xmlDoc); | ||
const svg = viewer.renderSVG(null, 1, 1); | ||
viewer.destroy(); | ||
return svg; | ||
}; | ||
|
||
export const makeEditor = async ( | ||
lang: "en" | "zh", | ||
init: string | null, | ||
onSave: (xml: Element) => void | ||
): Promise<{ start: () => void }> => { | ||
const Editor = await loadEditor(); | ||
const onExit = () => editor.exit(); | ||
const editor = new Editor(document.body, onExit); | ||
const intl = await Editor.getLang(lang); | ||
return { | ||
start: () => { | ||
editor.start(intl, init ? stringToXml(init) : null, onSave); | ||
}, | ||
}; | ||
}; | ||
|
||
export const diagramPreview = async (xml: string) => { | ||
const Viewer = await loadViewer(); | ||
const svg = Viewer.xmlToSvg(stringToXml(xml), "#fff"); | ||
return svg && svgToBase64(svg); | ||
}; | ||
|
||
export const diagramDownload = async (xml: string) => { | ||
const Viewer = await loadViewer(); | ||
const svg = Viewer.xmlToSvg(stringToXml(xml), null); | ||
if (!svg) return void 0; | ||
const func = await makeSVGDownloader(svg); | ||
func && func(); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.