mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-12 06:52:57 +02:00
parent
dcce96c08d
commit
4098032aa8
@ -1,4 +1,4 @@
|
|||||||
import {isDarkTheme} from '../utils.ts';
|
import {isDarkTheme, parseDom} from '../utils.ts';
|
||||||
import {makeCodeCopyButton} from './codecopy.ts';
|
import {makeCodeCopyButton} from './codecopy.ts';
|
||||||
import {displayError} from './common.ts';
|
import {displayError} from './common.ts';
|
||||||
import {queryElems} from '../utils/dom.ts';
|
import {queryElems} from '../utils/dom.ts';
|
||||||
@ -43,12 +43,19 @@ export async function initMarkupCodeMermaid(elMarkup: HTMLElement): Promise<void
|
|||||||
try {
|
try {
|
||||||
// can't use bindFunctions here because we can't cross the iframe boundary. This
|
// can't use bindFunctions here because we can't cross the iframe boundary. This
|
||||||
// means js-based interactions won't work but they aren't intended to work either
|
// means js-based interactions won't work but they aren't intended to work either
|
||||||
const {svg} = await mermaid.render('mermaid', source);
|
const {svg} = await mermaid.render('mermaid', source, pre);
|
||||||
|
const svgDoc = parseDom(svg, 'image/svg+xml');
|
||||||
|
const svgNode = (svgDoc.documentElement as unknown) as SVGSVGElement;
|
||||||
|
|
||||||
const iframe = document.createElement('iframe');
|
const iframe = document.createElement('iframe');
|
||||||
iframe.classList.add('markup-content-iframe', 'tw-invisible');
|
iframe.classList.add('markup-content-iframe', 'tw-invisible');
|
||||||
iframe.srcdoc = html`<html><head><style>${htmlRaw(iframeCss)}</style></head><body>${htmlRaw(svg)}</body></html>`;
|
iframe.srcdoc = html`<html><head><style>${htmlRaw(iframeCss)}</style></head><body>${htmlRaw(svg)}</body></html>`;
|
||||||
|
|
||||||
|
// although the "viewBox" is optional, mermaid's output should always have a correct viewBox with width and height
|
||||||
|
const iframeHeightFromViewBox = Math.ceil(svgNode.viewBox?.baseVal?.height ?? 0);
|
||||||
|
if (iframeHeightFromViewBox) iframe.style.height = `${iframeHeightFromViewBox}px`;
|
||||||
|
|
||||||
|
// FIXME: the logic is not right, the full fix is on main branch
|
||||||
const mermaidBlock = document.createElement('div');
|
const mermaidBlock = document.createElement('div');
|
||||||
mermaidBlock.classList.add('mermaid-block', 'is-loading', 'tw-hidden');
|
mermaidBlock.classList.add('mermaid-block', 'is-loading', 'tw-hidden');
|
||||||
mermaidBlock.append(iframe);
|
mermaidBlock.append(iframe);
|
||||||
@ -59,11 +66,12 @@ export async function initMarkupCodeMermaid(elMarkup: HTMLElement): Promise<void
|
|||||||
|
|
||||||
const updateIframeHeight = () => {
|
const updateIframeHeight = () => {
|
||||||
const body = iframe.contentWindow?.document?.body;
|
const body = iframe.contentWindow?.document?.body;
|
||||||
if (body) {
|
if (body?.clientHeight) {
|
||||||
iframe.style.height = `${body.clientHeight}px`;
|
iframe.style.height = `${body.clientHeight}px`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// FIXME: the logic is not right, the full fix is on main branch
|
||||||
iframe.addEventListener('load', () => {
|
iframe.addEventListener('load', () => {
|
||||||
pre.replaceWith(mermaidBlock);
|
pre.replaceWith(mermaidBlock);
|
||||||
mermaidBlock.classList.remove('tw-hidden');
|
mermaidBlock.classList.remove('tw-hidden');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user