mirror of
https://github.com/ritz078/transform.git
synced 2026-11-22 17:11:32 +03:00
Merge branch 'fix/optional-optimize'
This commit is contained in:
commit
b354a34db6
4 files changed
+55
-18
No files matched your search
+3
-1
@@ -5,4 +5,6 @@ node_modules
|
||||
.idea
|
||||
out
|
||||
yarn-error.log
|
||||
transform
|
||||
transform
|
||||
|
||||
.vscode
|
||||
+6
-17
@@ -1,6 +1,5 @@
|
||||
import React, { PureComponent } from 'react'
|
||||
import HTMLtoJSX from '@tsuyoshiwada/htmltojsx'
|
||||
import convertSvgString from 'transform-svg-to-native'
|
||||
import Layout from '../components/Layout'
|
||||
import ConversionPanel from '../components/ConversionPanel'
|
||||
import isSvg from 'is-svg'
|
||||
@@ -16,11 +15,11 @@ const defaultText = `<svg xmlns="http://www.w3.org/2000/svg" width="200" height=
|
||||
|
||||
export default class Css2Js extends PureComponent {
|
||||
state = {
|
||||
native: false
|
||||
shouldOptimize: true
|
||||
}
|
||||
|
||||
convertHtmlToJsx = html => {
|
||||
if (isSvg(html)) {
|
||||
if (isSvg(html) && this.state.shouldOptimize) {
|
||||
return new Promise(resolve => {
|
||||
svgo.optimize(html, result => {
|
||||
resolve(converter.convert(result.data))
|
||||
@@ -31,17 +30,7 @@ export default class Css2Js extends PureComponent {
|
||||
}
|
||||
|
||||
getTransformedValue = async (newValue: string) => {
|
||||
if (this.state.native && !isSvg(newValue)) {
|
||||
throw new Error(
|
||||
'You can only convert an SVG in this mode. Try turning off the react-native-svg checkbox.'
|
||||
)
|
||||
}
|
||||
|
||||
return this.state.native
|
||||
? new Promise(resolve =>
|
||||
convertSvgString(newValue, code => resolve(code))
|
||||
)
|
||||
: this.convertHtmlToJsx(newValue)
|
||||
return this.convertHtmlToJsx(newValue)
|
||||
}
|
||||
|
||||
render () {
|
||||
@@ -56,11 +45,11 @@ export default class Css2Js extends PureComponent {
|
||||
getTransformedValue={this.getTransformedValue}
|
||||
name={'html_to_jsx'}
|
||||
defaultText={defaultText}
|
||||
checkboxText='react-native-svg'
|
||||
checkboxText='Optimize SVG'
|
||||
extensions={['.svg', '.html']}
|
||||
initialCheckboxValue={this.state.native}
|
||||
initialCheckboxValue={this.state.shouldOptimize}
|
||||
onCheckboxChange={(checked: boolean, cb: Function) =>
|
||||
this.setState({ native: checked }, cb)}
|
||||
this.setState({ shouldOptimize: checked }, cb)}
|
||||
/>
|
||||
</Layout>
|
||||
)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import React, { PureComponent } from 'react'
|
||||
import convertSvgString from 'transform-svg-to-native'
|
||||
import Layout from '../components/Layout'
|
||||
import ConversionPanel from '../components/ConversionPanel'
|
||||
import isSvg from 'is-svg'
|
||||
import svgo from 'transform-svg-to-native/dist/svgo'
|
||||
|
||||
const defaultText = `<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" version="1.1">
|
||||
<rect width="200" height="100" stroke="black" stroke-width="6" fill="green"/>
|
||||
</svg>`
|
||||
|
||||
export default class Svg2ReactNative extends PureComponent {
|
||||
getTransformedValue = async (newValue: string) => {
|
||||
if (!isSvg(newValue)) {
|
||||
throw new Error('Please provide a valid SVG markup.')
|
||||
}
|
||||
|
||||
return new Promise(resolve =>
|
||||
convertSvgString(newValue, code => resolve(code))
|
||||
)
|
||||
}
|
||||
|
||||
render () {
|
||||
return (
|
||||
<Layout pathname={this.props.url.pathname}>
|
||||
<ConversionPanel
|
||||
url={this.props.url}
|
||||
leftTitle='SVG'
|
||||
rightTitle='JSX'
|
||||
leftMode='html'
|
||||
rightMode='jsx'
|
||||
getTransformedValue={this.getTransformedValue}
|
||||
name={'svg_to_react_native'}
|
||||
defaultText={defaultText}
|
||||
extensions={['.svg', '.html']}
|
||||
/>
|
||||
</Layout>
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,12 @@ export const categorizedRoutes = [
|
||||
desc:
|
||||
'An online REPL for converting HTML to JSX with proper support for SVG.'
|
||||
},
|
||||
{
|
||||
label: 'SVG to React Native',
|
||||
path: '/svg-to-react-native',
|
||||
desc:
|
||||
'An online REPL for converting SVG to React Native compatible equivalent.'
|
||||
},
|
||||
{
|
||||
label: 'CSS to JS Objects',
|
||||
path: '/css-to-js',
|
||||
|
||||
Reference in new issue
Block a user