@@ -5,13 +5,20 @@ import { SVGPathData } from 'svg-pathdata';
5
5
import BaseLayer from '../Base/BaseLayer' ;
6
6
import ShapePath from './ShapePath' ;
7
7
import ShapeGroup from './ShapeGroup' ;
8
- import { FrameType } from '../Base/Frame' ;
9
8
10
9
import { getUseReplacement , inlineStyles } from '../../utils/svg' ;
11
10
import { defaultExportOptions } from '../utils' ;
12
11
import { getGroupLayout } from '../../utils/layout' ;
13
12
import Style from '../Style/Style' ;
14
- import { ShapeGroupType , SvgShape , BaseLayerParams , SvgEllipse } from '../type' ;
13
+ import {
14
+ ShapeGroupType ,
15
+ BaseLayerParams ,
16
+ FrameType ,
17
+ // SvgEllipse,
18
+ // BaseSvgShape,
19
+ // AnySvgShape,
20
+ // SvgPath,
21
+ } from '../type' ;
15
22
16
23
interface SvgInitParams extends Partial < BaseLayerParams > {
17
24
svgString : string ;
@@ -39,7 +46,7 @@ class Svg extends BaseLayer {
39
46
// ------ 将 Svg 的子节点转换成内部格式 ------ //
40
47
41
48
// @ts -ignore
42
- let shapes : SvgShape [ ] = children
49
+ let shapes : { path : string ; style ?: string } [ ] = children
43
50
44
51
// eslint-disable-next-line array-callback-return
45
52
. map ( ( node ) => {
@@ -114,7 +121,7 @@ class Svg extends BaseLayer {
114
121
*/
115
122
layers : ShapeGroup [ ] = [ ] ;
116
123
117
- shapes : SvgShape [ ] = [ ] ;
124
+ shapes : { path : string ; style ?: string } [ ] = [ ] ;
118
125
119
126
/**
120
127
* 原生 Svg 字符串
@@ -312,89 +319,89 @@ class Svg extends BaseLayer {
312
319
* 将 svgon 解析出来的 node 转变为 svgShape
313
320
* @param node
314
321
*/
315
- static parseSvgsonToSvgShapes = (
316
- node ?: svgson . INode ,
317
- ) : SvgShape | SvgShape [ ] | undefined => {
318
- if ( ! node ) return ;
319
- const { children } = node ;
320
- if ( children . length > 0 ) {
321
- const svgShapes = children . map ( Svg . parseSvgsonToSvgShape ) ;
322
- return svgShapes . filter ( ( s ) => s ) as SvgShape [ ] ;
323
- }
324
- return Svg . parseSvgsonToSvgShape ( node ) ;
325
- } ;
326
-
327
- static parseSvgsonToSvgShape = (
328
- node : svgson . INode ,
329
- ) : SvgShape | SvgShape [ ] | undefined => {
330
- const { children, attributes, name, type, value } = node ;
331
-
332
- let layers : SvgShape [ ] = [ ] ;
333
-
334
- switch ( name ) {
335
- // 入口
336
- case 'svg' :
337
- if ( children . length > 0 ) {
338
- const svgShapes = children . map ( Svg . parseSvgsonToSvgShape ) ;
339
- layers = svgShapes . filter ( ( s ) => s ) as SvgShape [ ] ;
340
- }
341
- return layers ;
342
- // 编组
343
- case 'g' :
344
- if ( children . length > 0 ) {
345
- const svgShapes = children . map ( Svg . parseSvgsonToSvgShape ) ;
346
- layers = svgShapes . filter ( ( s ) => s ) as SvgShape [ ] ;
347
- }
348
- return {
349
- type : 'group' ,
350
- layers,
351
- path : '' ,
352
- style : attributes . style ,
353
- } ;
354
- case 'defs' :
355
- return { } ;
356
-
357
- // path
358
- case 'path' :
359
- return {
360
- path : attributes . d ,
361
- style : attributes . style ,
362
- layers,
363
- } ;
364
- // 圆形
365
- case 'ellipse' :
366
- return {
367
- style : attributes . style ,
368
- } ;
369
-
370
- // 两者是无关紧要的信息
371
- case 'title' :
372
- case 'desc' :
373
- default :
374
- }
375
- } ;
322
+ // static parseSvgsonToSvgShapes = (
323
+ // node?: svgson.INode,
324
+ // ): BaseSvgShape | BaseSvgShape[] | undefined => {
325
+ // if (!node) return;
326
+ // const { children } = node;
327
+ // if (children.length > 0) {
328
+ // const svgShapes = children.map(Svg.parseSvgsonToSvgShape);
329
+ // return svgShapes.filter((s) => s) as BaseSvgShape[];
330
+ // }
331
+ // return Svg.parseSvgsonToSvgShape(node);
332
+ // };
333
+
334
+ // static parseSvgsonToSvgShape = (
335
+ // node: svgson.INode,
336
+ // ): AnySvgShape | AnySvgShape[] | undefined => {
337
+ // const { children, attributes, name } = node;
338
+ //
339
+ // let layers: BaseSvgShape[] = [];
340
+ //
341
+ // switch (name) {
342
+ // // 入口
343
+ // case 'svg':
344
+ // if (children.length > 0) {
345
+ // const svgShapes = children.map(Svg.parseSvgsonToSvgShape);
346
+ // layers = svgShapes.filter((s) => s) as BaseSvgShape[];
347
+ // }
348
+ // return layers;
349
+ // // 编组
350
+ // case 'g':
351
+ // if (children.length > 0) {
352
+ // const svgShapes = children.map(Svg.parseSvgsonToSvgShape);
353
+ // layers = svgShapes.filter((s) => s) as BaseSvgShape[];
354
+ // }
355
+ // return {
356
+ // type: 'group',
357
+ // layers,
358
+ // style: attributes.style,
359
+ // };
360
+ // case 'defs':
361
+ // return {};
362
+ //
363
+ // // path
364
+ // case 'path':
365
+ // return {
366
+ // path: attributes.d,
367
+ // style: attributes.style,
368
+ // layers,
369
+ // };
370
+ // // 圆形
371
+ // case 'ellipse':
372
+ // return {
373
+ // style: attributes.style,
374
+ // };
375
+ //
376
+ // // 两者是无关紧要的信息
377
+ // case 'title':
378
+ // case 'desc':
379
+ // default:
380
+ // }
381
+ // };
376
382
377
383
/**
378
384
* 将 svg node 转换为 svg Group 对象
379
385
*/
380
- static parseGNodeToGroup = ( node : svgson . INode ) : SvgShape | undefined => {
381
- if ( node . name !== 'g' ) return ;
382
-
383
- const { attributes } = node ;
384
- const { fillRule } = attributes ;
385
-
386
- const layers = node . children . map ( Svg . parseSvgsonToSvgShapes ) as SvgShape [ ] ;
387
-
388
- const style = Svg . parseNodeAttrToStyle ( attributes ) ; // 解析样式
389
-
390
- return {
391
- type : 'group' ,
392
- layers : layers || [ ] ,
393
- windingRule : Svg . normalizeWindingRule ( fillRule ) ,
394
- style,
395
- path : '' ,
396
- } ;
397
- } ;
386
+ // static parseGNodeToGroup = (node: svgson.INode): BaseSvgShape | undefined => {
387
+ // if (node.name !== 'g') return;
388
+ //
389
+ // const { attributes } = node;
390
+ // const { fillRule } = attributes;
391
+ //
392
+ // const layers = node.children.map(
393
+ // Svg.parseSvgsonToSvgShapes,
394
+ // ) as BaseSvgShape[];
395
+ //
396
+ // const style = Svg.parseNodeAttrToStyle(attributes); // 解析样式
397
+ //
398
+ // return {
399
+ // type: 'group',
400
+ // layers: layers || [],
401
+ // windingRule: Svg.normalizeWindingRule(fillRule),
402
+ // style,
403
+ // };
404
+ // };
398
405
399
406
/**
400
407
* 一致化缠绕规则参数
@@ -434,17 +441,40 @@ class Svg extends BaseLayer {
434
441
* 将 ellipse 的节点解析为椭圆
435
442
* @param node
436
443
*/
437
- static parseNodeToEllipse = ( node : svgson . INode ) : SvgEllipse | undefined => {
438
- if ( ! node || ( node && node . name !== 'ellipse' ) ) return ;
439
-
440
- const { cx, cy, rx, ry } = node . attributes ;
441
- return {
442
- type : 'ellipse' ,
443
- frame : { width : rx * 2 , height : ry * 2 } ,
444
- style : Svg . parseNodeAttrToStyle ( node . attributes ) ,
445
- layers : [ ] ,
446
- } ;
447
- } ;
444
+ // static parseNodeToEllipse = (node: svgson.INode): SvgEllipse | undefined => {
445
+ // if (!node || (node && node.name !== 'ellipse')) return;
446
+ //
447
+ // const { rx, ry } = node.attributes;
448
+ // return {
449
+ // type: 'ellipse',
450
+ // frame: { width: rx * 2, height: ry * 2 },
451
+ // style: Svg.parseNodeAttrToStyle(node.attributes),
452
+ // layers: [],
453
+ // };
454
+ // };
455
+
456
+ // static parsePathToShape = (shapes: SvgPath[]) => {
457
+ // const fullPathString = shapes.reduce((prev, current) => {
458
+ // return { path: prev.path + current.path, layers: [] };
459
+ // }).path;
460
+ //
461
+ // // 计算定界框的缩放尺寸
462
+ // const shapeGroupFrame = Svg.getSvgPathGroupFrame(fullPathString);
463
+ // const scaleShapeGroupToFrame = Svg.calcFrameScale(
464
+ // shapeGroupFrame,
465
+ // this.frame.toJSON(),
466
+ // );
467
+ // // ------ 进行统一的坐标和尺寸变换 -------- //
468
+ // shapes = shapes.map((s) => ({
469
+ // ...s,
470
+ // path: new SVGPathData(s.path)
471
+ // // 将 shapeGroup 的坐标设为 0,0
472
+ // .translate(-shapeGroupFrame.x, -shapeGroupFrame.y)
473
+ // // 将 shapeGroup 与给定定界框 match 变成符合外部画板的尺寸
474
+ // .scale(scaleShapeGroupToFrame, scaleShapeGroupToFrame)
475
+ // .encode(),
476
+ // }));
477
+ // };
448
478
}
449
479
450
480
export default Svg ;
0 commit comments