From 5e15e695c40e8a379ab89adef2b0cad10e05305b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 8 Feb 2026 18:41:59 +0800 Subject: [PATCH 01/36] fix --- web_src/js/markup/mermaid.ts | 76 +++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/web_src/js/markup/mermaid.ts b/web_src/js/markup/mermaid.ts index 5d37c81b8f9cc..ef302de6da99e 100644 --- a/web_src/js/markup/mermaid.ts +++ b/web_src/js/markup/mermaid.ts @@ -10,7 +10,8 @@ const {mermaidMaxSourceCharacters} = window.config; const iframeCss = `:root {color-scheme: normal} body {margin: 0; padding: 0; overflow: hidden} -#mermaid {display: block; margin: 0 auto}`; +#mermaid {display: block; margin: 0 auto} +.view-controller {position: absolute; z-index: 1; right: 0; bottom: 0}`; function isSourceTooLarge(source: string) { return mermaidMaxSourceCharacters >= 0 && source.length > mermaidMaxSourceCharacters; @@ -77,6 +78,73 @@ async function loadMermaid(needElkRender: boolean) { }; } +function initMermaidViewController(dragElement: SVGSVGElement) { + let inited = false, isDragging = false; + let currentScale = 1, initLeft = 0, lastLeft = 0, lastTop = 0, lastPageX = 0, lastPageY = 0; + const container = dragElement.parentElement!; + + const resetView = () => { + currentScale = 1; + lastLeft = initLeft; + lastTop = 0; + dragElement.style.left = `${lastLeft}px`; + dragElement.style.top = `${lastTop}px`; + dragElement.style.position = 'absolute'; + dragElement.style.margin = '0'; + }; + + const initAbsolutePosition = () => { + if (inited) return; + // if we need to drag or zoom, use absolute position and get the current "left" from the "margin: auto" layout. + inited = true; + initLeft = container.getBoundingClientRect().width / 2 - dragElement.getBoundingClientRect().width / 2; + resetView(); + }; + + for (const el of queryElems(container, '[data-control-action]')) { + el.addEventListener('click', () => { + initAbsolutePosition(); + switch (el.getAttribute('data-control-action')) { + case 'zoom-in': + currentScale *= 1.2; + break; + case 'zoom-out': + currentScale /= 1.2; + break; + case 'reset': + resetView(); + break; + } + dragElement.style.transform = `scale(${currentScale})`; + }); + } + + dragElement.addEventListener('mousedown', (e) => { + if (e.button !== 0 || e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) return; // only left mouse button can drag + initAbsolutePosition(); + isDragging = true; + lastPageX = e.pageX; + lastPageY = e.pageY; + dragElement.style.cursor = 'grabbing'; + }); + + dragElement.ownerDocument.addEventListener('mousemove', (e) => { + if (!isDragging) return; + lastLeft = e.pageX - lastPageX + lastLeft; + lastTop = e.pageY - lastPageY + lastTop; + dragElement.style.left = `${lastLeft}px`; + dragElement.style.top = `${lastTop}px`; + lastPageX = e.pageX; + lastPageY = e.pageY; + }); + + dragElement.ownerDocument.addEventListener('mouseup', () => { + if (!isDragging) return; + isDragging = false; + dragElement.style.removeProperty('cursor'); + }); +} + let elkLayoutsRegistered = false; export async function initMarkupCodeMermaid(elMarkup: HTMLElement): Promise { @@ -122,10 +190,12 @@ export async function initMarkupCodeMermaid(elMarkup: HTMLElement): Promise`; + // create an iframe to sandbox the svg with styles, and set correct height by reading svg's viewBox height const iframe = document.createElement('iframe'); iframe.classList.add('markup-content-iframe', 'is-loading'); - iframe.srcdoc = html``; + iframe.srcdoc = html`${htmlRaw(viewController)}`; // 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); @@ -143,6 +213,8 @@ export async function initMarkupCodeMermaid(elMarkup: HTMLElement): Promise