forked from vault-development/react-native-svg-uri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
212 lines (183 loc) · 6.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'use strict';
import React, {Component, PropTypes} from "react";
import {View} from 'react-native';
import xmldom from 'xmldom';
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
import Svg,{
Circle,
Ellipse,
G ,
LinearGradient,
RadialGradient,
Line,
Path,
Polygon,
Polyline,
Rect,
Symbol,
Use,
Defs,
Stop
} from 'react-native-svg';
import * as utils from './utils';
const ACEPTED_SVG_ELEMENTS = [
'svg',
'g',
'circle',
'path',
'rect',
'linearGradient',
'radialGradient',
'stop',
'ellipse',
'polygon'
];
// Attributes from SVG elements that are mapped directly.
const SVG_ATTS = ['viewBox'];
const G_ATTS = ['id'];
const CIRCLE_ATTS = ['cx', 'cy', 'r', 'fill', 'stroke'];
const PATH_ATTS = ['d', 'fill', 'stroke'];
const RECT_ATTS = ['width', 'height', 'fill', 'stroke'];
const LINEARG_ATTS = ['id', 'x1', 'y1', 'x2', 'y2'];
const RADIALG_ATTS = ['id', 'cx', 'cy', 'r'];
const STOP_ATTS = ['offset'];
const ELLIPSE_ATTS = ['fill', 'cx', 'cy', 'rx', 'ry'];
const POLYGON_ATTS = ['points'];
let ind = 0;
class SvgUri extends Component{
constructor(props){
super(props);
this.state = {svgXmlData: props.svgXmlData};
this.createSVGElement = this.createSVGElement.bind(this);
this.obtainComponentAtts = this.obtainComponentAtts.bind(this);
this.inspectNode = this.inspectNode.bind(this);
this.fecthSVGData = this.fecthSVGData.bind(this);
this.isComponentMounted = false;
// Gets the image data from an URL or a static file
if (props.source) {
const source = resolveAssetSource(props.source) || {};
this.fecthSVGData(source.uri);
}
}
componentWillMount() {
this.isComponentMounted = true;
}
componentWillUnmount() {
this.isComponentMounted = false
}
componentWillReceiveProps (nextProps){
if (nextProps.source) {
const source = resolveAssetSource(nextProps.source) || {};
const oldSource = resolveAssetSource(this.props.source) || {};
if(source.uri !== oldSource.uri){
this.fecthSVGData(source.uri);
}
}
}
async fecthSVGData(uri){
let responseXML = null;
try {
let response = await fetch(uri);
responseXML = await response.text();
} catch(e) {
console.error("ERROR SVG", e);
}finally {
if (this.isComponentMounted) {
this.setState({svgXmlData:responseXML});
}
}
return responseXML;
}
createSVGElement(node, childs){
let componentAtts = {};
let i = ind++;
switch (node.nodeName) {
case 'svg':
componentAtts = this.obtainComponentAtts(node, SVG_ATTS);
if (this.props.width)
componentAtts.width = this.props.width;
if (this.props.height)
componentAtts.height = this.props.height;
return <Svg key={i} {...componentAtts}>{childs}</Svg>;
case 'g':
componentAtts = this.obtainComponentAtts(node, G_ATTS);
return <G key={i} {...componentAtts}>{childs}</G>;
case 'path':
componentAtts = this.obtainComponentAtts(node, PATH_ATTS);
return <Path key={i} {...componentAtts}>{childs}</Path>;
case 'circle':
componentAtts = this.obtainComponentAtts(node, CIRCLE_ATTS);
return <Circle key={i} {...componentAtts}>{childs}</Circle>;
case 'rect':
componentAtts = this.obtainComponentAtts(node, RECT_ATTS);
return <Rect key={i} {...componentAtts}>{childs}</Rect>;
case 'linearGradient':
componentAtts = this.obtainComponentAtts(node, LINEARG_ATTS);
return <Defs key={i}><LinearGradient {...componentAtts}>{childs}</LinearGradient></Defs>;
case 'radialGradient':
componentAtts = this.obtainComponentAtts(node, RADIALG_ATTS);
return <Defs key={i}><RadialGradient {...componentAtts}>{childs}</RadialGradient></Defs>;
case 'stop':
componentAtts = this.obtainComponentAtts(node, STOP_ATTS);
return <Stop key={i} {...componentAtts}>{childs}</Stop>;
case 'ellipse':
componentAtts = this.obtainComponentAtts(node, ELLIPSE_ATTS);
return <Ellipse key={i} {...componentAtts}>{childs}</Ellipse>;
case 'polygon':
componentAtts = this.obtainComponentAtts(node, POLYGON_ATTS);
return <Polygon key={i} {...componentAtts}>{childs}</Polygon>;
default:
return null;
}
}
obtainComponentAtts({attributes}, enabledAttributes) {
return Array.from(attributes)
.map(utils.camelCaseNodeName)
.map(utils.removePixelsFromNodeValue)
.map(utils.transformStyle)
.filter(utils.getEnabledAttributes(enabledAttributes))
.reduce((acc, {nodeName, nodeValue}) => ({
...acc,
[nodeName]: this.props.fill && nodeName === 'fill' ? this.props.fill : nodeValue,
}), {});
}
inspectNode(node){
//Process the xml node
let arrayElements = [];
// Only process accepted elements
if (!ACEPTED_SVG_ELEMENTS.includes(node.nodeName))
return null;
// if have children process them.
// Recursive function.
if (node.childNodes && node.childNodes.length > 0){
for (let i = 0; i < node.childNodes.length; i++){
let nodo = this.inspectNode(node.childNodes[i]);
if (nodo != null)
arrayElements.push(nodo);
}
}
let element = this.createSVGElement(node, arrayElements);
return element;
}
render(){
try{
if (this.state.svgXmlData == null)
return null;
let inputSVG = this.state.svgXmlData.substring(this.state.svgXmlData.indexOf("<svg "), (this.state.svgXmlData.indexOf("</svg>") + 6));
let doc = new xmldom.DOMParser().parseFromString(inputSVG);
let rootSVG = this.inspectNode(doc.childNodes[0]);
return(
<View style={this.props.style}>
{rootSVG}
</View>
);
}catch(e){
console.error("ERROR SVG", e);
return null;
}
}
}
SvgUri.propTypes = {
fill: PropTypes.string,
}
module.exports = SvgUri;