Fix mermaid display problem in the editing state. (#2334)

This commit is contained in:
Jachin 2023-01-16 21:54:46 +08:00 committed by GitHub
parent d664c5a1c6
commit 9353c9ef40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View File

@ -188,7 +188,7 @@ class GraphPreviewer {
if (p_svgNode.getAttribute('width').indexOf('%') != -1) { if (p_svgNode.getAttribute('width').indexOf('%') != -1) {
// Try maxWidth. // Try maxWidth.
if (p_svgNode.style.maxWidth && p_svgNode.style.maxWidth.endsWith('px')) { if (p_svgNode.style.maxWidth && p_svgNode.style.maxWidth.endsWith('px') && p_svgNode.style.maxWidth != "0px") {
p_svgNode.setAttribute('width', p_svgNode.style.maxWidth); p_svgNode.setAttribute('width', p_svgNode.style.maxWidth);
} else { } else {
// Set as window width. // Set as window width.
@ -220,10 +220,12 @@ class GraphPreviewer {
return; return;
} }
if (p_svgNode.getAttribute('width').indexOf('%') == -1) { let width = p_svgNode.getAttribute('width')
if (width && width.indexOf('%') == -1) {
p_svgNode.width.baseVal.valueInSpecifiedUnits *= scaleFactor; p_svgNode.width.baseVal.valueInSpecifiedUnits *= scaleFactor;
} }
if (p_svgNode.getAttribute('height').indexOf('%') == -1) { let height = p_svgNode.getAttribute('height')
if (height && height.indexOf('%') == -1) {
p_svgNode.height.baseVal.valueInSpecifiedUnits *= scaleFactor; p_svgNode.height.baseVal.valueInSpecifiedUnits *= scaleFactor;
} }
} }

View File

@ -60,7 +60,7 @@ class SvgToImage {
let url = domUrl.createObjectURL(blob); let url = domUrl.createObjectURL(blob);
this.loadImage(url, p_opt, function(err, img) { this.loadImage(url, p_opt, function(err, img) {
domUrl.revokeObjectURL(url); domUrl.revokeObjectURL(url);
if (err) { if (err || SvgToImage.checkCanvasToDataURL(img) === null) {
// try again for Safari 8.0, using simple encodeURIComponent // try again for Safari 8.0, using simple encodeURIComponent
// this will fail with DOM content but at least it works with SVG. // this will fail with DOM content but at least it works with SVG.
let url2 = 'data:image/svg+xml,' + encodeURIComponent(p_svg.join('')); let url2 = 'data:image/svg+xml,' + encodeURIComponent(p_svg.join(''));
@ -74,4 +74,19 @@ class SvgToImage {
static getUrl() { static getUrl() {
return window.URL || window.webkitURL || window.mozURL || window.msURL; return window.URL || window.webkitURL || window.mozURL || window.msURL;
} }
static checkCanvasToDataURL(p_image) {
let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d');
canvas.height = p_image.height;
canvas.width = p_image.width;
ctx.drawImage(p_image, 0, 0);
let dataUrl = null;
try {
dataUrl = canvas.toDataURL();
} catch (err) {
}
return dataUrl
}
} }