From 13ba53c8074ba7d59b90734abb7cc3785e5706a2 Mon Sep 17 00:00:00 2001 From: pissang Date: Wed, 22 Dec 2021 16:48:25 +0800 Subject: [PATCH 1/3] feat(ssr): add useViewBox opt --- src/svg/Painter.ts | 14 ++++++++++---- src/svg/core.ts | 10 ++++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/svg/Painter.ts b/src/svg/Painter.ts index c159f722f..1a1f7f418 100644 --- a/src/svg/Painter.ts +++ b/src/svg/Painter.ts @@ -119,7 +119,8 @@ class SVGPainter implements PainterBase { renderToVNode(opts?: { animation?: boolean willUpdate?: boolean - compress?: boolean + compress?: boolean, + useViewBox?: boolean }) { opts = opts || {}; @@ -176,14 +177,18 @@ class SVGPainter implements PainterBase { } } - return createSVGVNode(width, height, children); + return createSVGVNode(width, height, children, opts.useViewBox); } renderToString(opts?: { /** * If add css animation. */ - cssAnimation: boolean + cssAnimation?: boolean + /** + * If use viewBox + */ + useViewBox?: boolean }) { const defaultOpts = { cssAnimation: true @@ -193,7 +198,8 @@ class SVGPainter implements PainterBase { return vNodeToString(this.renderToVNode({ animation: defaultOpts.cssAnimation, willUpdate: false, - compress: true + compress: true, + useViewBox: opts.useViewBox }), { newline: true }); } diff --git a/src/svg/core.ts b/src/svg/core.ts index 511b65a13..412038a44 100644 --- a/src/svg/core.ts +++ b/src/svg/core.ts @@ -174,7 +174,12 @@ export function createBrushScope(zrId: string): BrushScope { }; } -export function createSVGVNode(width?: number | string, height?: number | string, children?: SVGVNode[]) { +export function createSVGVNode( + width: number | string, + height: number | string, + children?: SVGVNode[], + useViewBox?: boolean +) { return createVNode( 'svg', 'root', @@ -184,7 +189,8 @@ export function createSVGVNode(width?: number | string, height?: number | string 'xmlns': SVGNS, 'xmlns:xlink': XLINKNS, 'version': '1.1', - 'baseProfile': 'full' + 'baseProfile': 'full', + 'viewBox': useViewBox ? `0 0 ${width} ${height}` : false }, children ); From d239b31fb60850182eaa3ab830823839756b9159 Mon Sep 17 00:00:00 2001 From: pissang Date: Wed, 22 Dec 2021 19:01:03 +0800 Subject: [PATCH 2/3] feat(ssr): change useViewBox to be true by default --- src/svg/Painter.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/svg/Painter.ts b/src/svg/Painter.ts index 1a1f7f418..be1697c7d 100644 --- a/src/svg/Painter.ts +++ b/src/svg/Painter.ts @@ -20,7 +20,7 @@ import { createSVGVNode } from './core'; import { normalizeColor, encodeBase64 } from './helper'; -import { defaults, extend, keys, logError, map } from '../core/util'; +import { defaults, extend, keys, logError, map, retrieve2 } from '../core/util'; import Path from '../graphic/Path'; import patch, { updateAttrs } from './patch'; import { getSize } from '../canvas/helper'; @@ -183,23 +183,21 @@ class SVGPainter implements PainterBase { renderToString(opts?: { /** * If add css animation. + * @default true */ cssAnimation?: boolean /** * If use viewBox + * @default true */ useViewBox?: boolean }) { - const defaultOpts = { - cssAnimation: true - }; - opts = defaults(opts || {}, defaultOpts); - + opts = opts || {}; return vNodeToString(this.renderToVNode({ - animation: defaultOpts.cssAnimation, + animation: retrieve2(opts.cssAnimation, true), willUpdate: false, compress: true, - useViewBox: opts.useViewBox + useViewBox: retrieve2(opts.useViewBox, true) }), { newline: true }); } From b0022565c81bf4216b6e21fa72f93773a3f2543a Mon Sep 17 00:00:00 2001 From: pissang Date: Wed, 22 Dec 2021 19:09:00 +0800 Subject: [PATCH 3/3] style: remove unused code --- src/svg/Painter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/svg/Painter.ts b/src/svg/Painter.ts index be1697c7d..51a4d8ca7 100644 --- a/src/svg/Painter.ts +++ b/src/svg/Painter.ts @@ -20,7 +20,7 @@ import { createSVGVNode } from './core'; import { normalizeColor, encodeBase64 } from './helper'; -import { defaults, extend, keys, logError, map, retrieve2 } from '../core/util'; +import { extend, keys, logError, map, retrieve2 } from '../core/util'; import Path from '../graphic/Path'; import patch, { updateAttrs } from './patch'; import { getSize } from '../canvas/helper';