|
| 1 | +import { renderToSvg } from '@zenuml/core'; |
1 | 2 | import { getConfig, log } from './mermaidUtils.js'; |
2 | | -import ZenUml from '@zenuml/core'; |
3 | 3 |
|
4 | 4 | const regexp = /^\s*zenuml/; |
5 | 5 |
|
6 | | -// Create a Zen UML container outside the svg first for rendering, otherwise the Zen UML diagram cannot be rendered properly |
7 | | -function createTemporaryZenumlContainer(id: string) { |
8 | | - const container = document.createElement('div'); |
9 | | - container.id = `container-${id}`; |
10 | | - container.style.display = 'flex'; |
11 | | - container.innerHTML = `<div id="zenUMLApp-${id}"></div>`; |
12 | | - const app = container.querySelector(`#zenUMLApp-${id}`)!; |
13 | | - return { container, app }; |
14 | | -} |
15 | | - |
16 | | -// Create a foreignObject to wrap the Zen UML container in the svg |
17 | | -function createForeignObject(id: string) { |
18 | | - const foreignObject = document.createElementNS('http://www.w3.org/2000/svg', 'foreignObject'); |
19 | | - foreignObject.setAttribute('x', '0'); |
20 | | - foreignObject.setAttribute('y', '0'); |
21 | | - foreignObject.setAttribute('width', '100%'); |
22 | | - foreignObject.setAttribute('height', '100%'); |
23 | | - const { container, app } = createTemporaryZenumlContainer(id); |
24 | | - foreignObject.appendChild(container); |
25 | | - return { foreignObject, container, app }; |
26 | | -} |
| 6 | +export const calculateSvgSizeAttrs = ( |
| 7 | + width: number, |
| 8 | + height: number, |
| 9 | + useMaxWidth: boolean |
| 10 | +): Map<string, string> => { |
| 11 | + const attrs = new Map<string, string>(); |
| 12 | + |
| 13 | + if (useMaxWidth) { |
| 14 | + attrs.set('width', '100%'); |
| 15 | + attrs.set('style', `max-width: ${width}px;`); |
| 16 | + } else { |
| 17 | + attrs.set('width', String(width)); |
| 18 | + attrs.set('height', String(height)); |
| 19 | + } |
| 20 | + |
| 21 | + return attrs; |
| 22 | +}; |
27 | 23 |
|
28 | 24 | /** |
29 | | - * Draws a Zen UML in the tag with id: id based on the graph definition in text. |
30 | | - * |
31 | | - * @param text - The text of the diagram |
32 | | - * @param id - The id of the diagram which will be used as a DOM element id¨ |
| 25 | + * Resolves the root document and SVG element, handling sandbox mode. |
| 26 | + * Follows the same pattern as mermaid's selectSvgElement utility. |
33 | 27 | */ |
34 | | -export const draw = async function (text: string, id: string) { |
35 | | - log.info('draw with Zen UML renderer', ZenUml); |
36 | | - |
37 | | - text = text.replace(regexp, ''); |
| 28 | +const selectSvgElement = (id: string): SVGSVGElement | null => { |
38 | 29 | const { securityLevel } = getConfig(); |
39 | | - // Handle root and Document for when rendering in sandbox mode |
40 | | - let sandboxElement: HTMLIFrameElement | null = null; |
| 30 | + let root: Document = document; |
| 31 | + |
41 | 32 | if (securityLevel === 'sandbox') { |
42 | | - sandboxElement = document.getElementById('i' + id) as HTMLIFrameElement; |
| 33 | + const sandboxElement = document.querySelector<HTMLIFrameElement>(`#i${id}`); |
| 34 | + root = sandboxElement?.contentDocument ?? document; |
43 | 35 | } |
44 | 36 |
|
45 | | - const root = securityLevel === 'sandbox' ? sandboxElement?.contentWindow?.document : document; |
| 37 | + return root.querySelector<SVGSVGElement>(`#${id}`); |
| 38 | +}; |
46 | 39 |
|
47 | | - const svgContainer = root?.querySelector(`svg#${id}`); |
| 40 | +const configureSvgSize = ( |
| 41 | + svgEl: SVGSVGElement, |
| 42 | + width: number, |
| 43 | + height: number, |
| 44 | + useMaxWidth: boolean |
| 45 | +) => { |
| 46 | + const attrs = calculateSvgSizeAttrs(width, height, useMaxWidth); |
48 | 47 |
|
49 | | - if (!root || !svgContainer) { |
50 | | - log.error('Cannot find root or svgContainer'); |
51 | | - return; |
| 48 | + svgEl.removeAttribute('height'); |
| 49 | + svgEl.style.removeProperty('max-width'); |
| 50 | + |
| 51 | + for (const [attr, value] of attrs) { |
| 52 | + svgEl.setAttribute(attr, value); |
52 | 53 | } |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * Draws a ZenUML diagram in the SVG element with id: id based on the |
| 58 | + * graph definition in text, using native SVG rendering. |
| 59 | + * |
| 60 | + * @param text - The text of the diagram |
| 61 | + * @param id - The id of the diagram which will be used as a DOM element id |
| 62 | + */ |
| 63 | +export const draw = function (text: string, id: string): Promise<void> { |
| 64 | + log.info('draw with ZenUML native SVG renderer'); |
| 65 | + |
| 66 | + const code = text.replace(regexp, ''); |
| 67 | + const config = getConfig(); |
| 68 | + const useMaxWidth = config.sequence?.useMaxWidth ?? true; |
| 69 | + |
| 70 | + const svgEl = selectSvgElement(id); |
| 71 | + |
| 72 | + if (!svgEl) { |
| 73 | + log.error('Cannot find svg element'); |
| 74 | + return Promise.resolve(); |
| 75 | + } |
| 76 | + |
| 77 | + const result = renderToSvg(code); |
53 | 78 |
|
54 | | - const { foreignObject, container, app } = createForeignObject(id); |
55 | | - svgContainer.appendChild(foreignObject); |
56 | | - const zenuml = new ZenUml(app); |
57 | | - // default is a theme name. More themes to be added and will be configurable in the future |
58 | | - await zenuml.render(text, { theme: 'default', mode: 'static' }); |
| 79 | + configureSvgSize(svgEl, result.width, result.height, useMaxWidth); |
| 80 | + svgEl.setAttribute('viewBox', result.viewBox); |
| 81 | + svgEl.innerHTML = result.innerSvg; |
59 | 82 |
|
60 | | - const { width, height } = window.getComputedStyle(container); |
61 | | - log.debug('zenuml diagram size', width, height); |
62 | | - svgContainer.setAttribute('style', `width: ${width}; height: ${height};`); |
| 83 | + return Promise.resolve(); |
63 | 84 | }; |
64 | 85 |
|
65 | 86 | export default { |
|
0 commit comments