mirror of
https://github.com/ritz078/transform.git
synced 2026-11-22 17:11:32 +03:00
added css-to-js
This commit is contained in:
1 parent
8e10a8946a
commit
aaf77c94d4
8 files changed
+702
-421
No files matched your search
@@ -1,8 +1,10 @@
|
||||
import React, { PureComponent } from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import Router from 'next/router'
|
||||
import { transform } from 'babel-standalone'
|
||||
import isBrowser from 'is-in-browser'
|
||||
import store from 'store2'
|
||||
import {js_beautify} from 'js-beautify'
|
||||
import {beauty} from 'css-beauty'
|
||||
import copy from 'copy-text-to-clipboard'
|
||||
import Layout from '../components/Layout'
|
||||
|
||||
@@ -11,12 +13,14 @@ const theme = 'tomorrow'
|
||||
let brace
|
||||
let AceEditor
|
||||
if (isBrowser) {
|
||||
window.babelTransform = transform
|
||||
brace = require('brace').default
|
||||
AceEditor = require('react-ace').default
|
||||
require('brace/mode/javascript')
|
||||
require(`brace/theme/${theme}`)
|
||||
require('brace/mode/json')
|
||||
require('brace/mode/typescript')
|
||||
require('brace/mode/css')
|
||||
}
|
||||
|
||||
export default class ConversionPanel extends PureComponent {
|
||||
@@ -45,23 +49,14 @@ export default class ConversionPanel extends PureComponent {
|
||||
|
||||
componentDidMount() {
|
||||
const {name} = this.props
|
||||
const code = this.props.url.query.code || this.props.defaultText
|
||||
this.setState({
|
||||
value: store(`${name}_code`) || this.props.defaultText,
|
||||
resultValue: store(`${name}_result`) || this.props.getTransformedValue(this.props.defaultText)
|
||||
})
|
||||
}
|
||||
|
||||
componentWillReceiveProps(nextProps) {
|
||||
const {name} = nextProps
|
||||
this.setState({
|
||||
value: store(`${name}_code`) || nextProps.defaultText,
|
||||
resultValue: store(`${name}_result`) || nextProps.getTransformedValue(nextProps.defaultText)
|
||||
value: code,
|
||||
resultValue: this.props.getTransformedValue(code)
|
||||
})
|
||||
}
|
||||
|
||||
onChange = (newValue) => {
|
||||
const {name} = this.props;
|
||||
|
||||
try {
|
||||
const code = this.props.getTransformedValue(newValue)
|
||||
|
||||
@@ -70,8 +65,7 @@ export default class ConversionPanel extends PureComponent {
|
||||
info: ''
|
||||
})
|
||||
|
||||
store(`${name}_code`, newValue)
|
||||
store(`${name}_result`, code)
|
||||
this.setCodeInUrl(newValue)
|
||||
} catch (e) {
|
||||
this.setState({
|
||||
info: e.message
|
||||
@@ -83,12 +77,26 @@ export default class ConversionPanel extends PureComponent {
|
||||
})
|
||||
}
|
||||
|
||||
setCodeInUrl = (code) => {
|
||||
|
||||
Router.replace({
|
||||
pathname: this.props.url.pathname,
|
||||
query: {
|
||||
code
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
prettifyCode = () => {
|
||||
const value = js_beautify(this.state.value)
|
||||
const value = this.props.leftMode === 'css' ? beauty(this.state.value) :js_beautify(this.state.value)
|
||||
this.setState({
|
||||
value
|
||||
}, () => {
|
||||
store(`${this.props.name}_code`, value)
|
||||
this.props.url.push({
|
||||
query: {
|
||||
code: value
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -101,7 +109,7 @@ export default class ConversionPanel extends PureComponent {
|
||||
|
||||
render () {
|
||||
return (
|
||||
<Layout pathname={this.props.pathname}>
|
||||
<Layout pathname={this.props.url.pathname}>
|
||||
<div className="wrapper">
|
||||
<style jsx>{`
|
||||
@import url('https://fonts.googleapis.com/css?family=Lato');
|
||||
|
||||
@@ -7,18 +7,13 @@ import Router from 'next/router'
|
||||
import GithubCorner from 'react-github-corner'
|
||||
|
||||
Router.onRouteChangeStart = (url) => {
|
||||
console.log(`Loading: ${url}`)
|
||||
NProgress.start()
|
||||
if(url.indexOf('code=') === -1) {
|
||||
NProgress.start()
|
||||
}
|
||||
}
|
||||
Router.onRouteChangeComplete = () => NProgress.done()
|
||||
Router.onRouteChangeError = () => NProgress.done()
|
||||
|
||||
function isActiveRoute (routeName) {
|
||||
if (isBrowser) {
|
||||
return window.location.pathname === routeName
|
||||
}
|
||||
}
|
||||
|
||||
function Logo () {
|
||||
return (
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 125" fill="#ffffff" width="190">
|
||||
@@ -158,6 +153,7 @@ export default function ({children, pathname}) {
|
||||
<ul>
|
||||
<li className={getClass("/")}><Link prefetch href="/"><a>JS Object to React PropTypes</a></Link></li>
|
||||
<li className={getClass("/json-to-ts-interface")}><Link prefetch href="/json-to-ts-interface"><a>JSON to Typescript Interface <span className="badge">new</span></a></Link></li>
|
||||
<li className={getClass("/css-to-js")}><Link prefetch href="/css-to-js"><a>CSS to JS Objects <span className="badge">new</span></a></Link></li>
|
||||
</ul>
|
||||
|
||||
<div className="footer">
|
||||
|
||||
+3
-2
@@ -17,18 +17,19 @@
|
||||
"babel-standalone": "^6.25.0",
|
||||
"babili-webpack-plugin": "^0.1.2",
|
||||
"copy-text-to-clipboard": "^1.0.2",
|
||||
"css-beauty": "^0.2.0",
|
||||
"is-in-browser": "^1.1.3",
|
||||
"js-beautify": "^1.6.14",
|
||||
"json2dts": "^0.0.2",
|
||||
"lodash": "^4.17.4",
|
||||
"next": "^2.4.9",
|
||||
"next": "3.0.1-beta.20",
|
||||
"nprogress": "^0.2.0",
|
||||
"prettier": "^1.5.3",
|
||||
"prop-types": "^15.5.10",
|
||||
"react": "^15.6.1",
|
||||
"react-ace": "^5.1.2",
|
||||
"react-dom": "^15.6.1",
|
||||
"react-github-corner": "^0.3.0",
|
||||
"store2": "^2.5.1",
|
||||
"webpack-bundle-analyzer": "^2.9.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import React, {PureComponent} from 'react'
|
||||
import dynamic from 'next/dynamic'
|
||||
import plugin from '../utils/css-to-js'
|
||||
const ConversionPanel = dynamic(import('../components/ConversionPanel'))
|
||||
|
||||
const defaultText = `
|
||||
.abc {
|
||||
background-color: green;
|
||||
}
|
||||
`
|
||||
|
||||
export default class Main extends PureComponent {
|
||||
getTransformedValue = (newValue) => {
|
||||
return plugin(newValue)
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<ConversionPanel
|
||||
url={this.props.url}
|
||||
leftMode="css"
|
||||
getTransformedValue={this.getTransformedValue}
|
||||
name={'css_to_js'}
|
||||
defaultText={defaultText}
|
||||
/>
|
||||
)
|
||||
}
|
||||
}
|
||||
+5
-3
@@ -1,7 +1,8 @@
|
||||
import React, {PureComponent} from 'react'
|
||||
import { transform } from 'babel-standalone'
|
||||
import plugin from '../utils/babel-plugin-js-to-prop-types'
|
||||
import ConversionPanel from '../components/ConversionPanel'
|
||||
|
||||
import dynamic from 'next/dynamic'
|
||||
const ConversionPanel = dynamic(import('../components/ConversionPanel'))
|
||||
|
||||
const defaultText = `// Enter the object you want to convert in the
|
||||
// following form
|
||||
@@ -18,7 +19,7 @@ const x = {
|
||||
|
||||
export default class Main extends PureComponent {
|
||||
getTransformedValue = (newValue) => {
|
||||
const { code } = transform(newValue, {
|
||||
const { code } = babelTransform(newValue, {
|
||||
presets: ['es2015'],
|
||||
plugins: [plugin]
|
||||
})
|
||||
@@ -28,6 +29,7 @@ export default class Main extends PureComponent {
|
||||
render() {
|
||||
return (
|
||||
<ConversionPanel
|
||||
url={this.props.url}
|
||||
getTransformedValue={this.getTransformedValue}
|
||||
name={'prop_types'}
|
||||
defaultText={defaultText}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import React, { PureComponent } from 'react'
|
||||
import dynamic from 'next/dynamic'
|
||||
import { Json2dts, toValidJSON } from 'json2dts'
|
||||
import ConversionPanel from '../components/ConversionPanel'
|
||||
const ConversionPanel = dynamic(import('../components/ConversionPanel'))
|
||||
|
||||
const defaultText = `
|
||||
{
|
||||
@@ -33,7 +34,7 @@ export default class Json2Ts extends PureComponent {
|
||||
defaultText={defaultText}
|
||||
leftMode="json"
|
||||
rightMode="typescript"
|
||||
pathname={this.props.url.pathname}
|
||||
url={this.props.url}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,210 @@
|
||||
const SPACE = ' ';
|
||||
function toProperty(name){
|
||||
if(name.charAt(0) === "-") name = name.slice(0);
|
||||
|
||||
return name.replace(/[^a-z0-9]([a-z0-9])?/gi, function(v, l){
|
||||
if(l) return l.toUpperCase();
|
||||
return "";
|
||||
}) ;
|
||||
}
|
||||
|
||||
function toSelectors(name){
|
||||
const names = name.split(',')
|
||||
|
||||
return names.map(function(name){
|
||||
name = name.trim();
|
||||
let newName = ''
|
||||
|
||||
if(name.charAt(0) === "."){
|
||||
newName += "Class";
|
||||
name = name.slice(1);
|
||||
}else if(name.charAt(0) === "#"){
|
||||
newName += "Id";
|
||||
name = name.slice(1);
|
||||
}else{
|
||||
newName += "Element";
|
||||
}
|
||||
|
||||
return name.replace(/([^a-z0-9])([a-z0-9])?/gi, function(v, c, l){
|
||||
if(l) return (c === "," || c === " ") ? l.toLowerCase() : l.toUpperCase();
|
||||
return "";
|
||||
}) + newName;
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function tokenizer(code){
|
||||
|
||||
const tokens = [];
|
||||
let token = '';
|
||||
const whitespc = ['\r\n', '\n\r', '\n', '\r'];
|
||||
let lastChar = '\0';
|
||||
let nextChar = '\0';
|
||||
let char = '\0';
|
||||
const specialChars = ["{","}",":",";"];
|
||||
const specialCharsPB = ["{","}",";"];
|
||||
let sc = null;
|
||||
let inBrackets = false;
|
||||
|
||||
for(let i = 0; i < code.length; i++){
|
||||
|
||||
if(i) lastChar = code.charAt(i-1);
|
||||
char = code.charAt(i);
|
||||
if(i+1 < code.length) nextChar = code.charAt(i+1);
|
||||
|
||||
if(~whitespc.indexOf(char) && ~whitespc.indexOf(lastChar)){
|
||||
continue;
|
||||
}
|
||||
|
||||
sc = inBrackets ? specialChars : specialCharsPB;
|
||||
|
||||
|
||||
|
||||
if(~sc.indexOf(char)){
|
||||
if(char === "{") inBrackets = true;
|
||||
if(char === "}") inBrackets = false;
|
||||
tokens.push(token);
|
||||
tokens.push(char);
|
||||
token = '';
|
||||
continue;
|
||||
}
|
||||
|
||||
token += char;
|
||||
|
||||
}
|
||||
|
||||
if(token) tokens.push(token);
|
||||
|
||||
|
||||
return tokens
|
||||
.map(function(token){ return token.trim(); })
|
||||
.filter(function(token){ return token; });
|
||||
|
||||
}
|
||||
|
||||
function convertoToJS(tokens){
|
||||
const items = []
|
||||
let actualItem = null
|
||||
let actualProp = null
|
||||
function readSelector(token){
|
||||
const selectors = toSelectors(token)
|
||||
|
||||
actualItem = {
|
||||
originalValue: token,
|
||||
selectors: selectors,
|
||||
values: {}
|
||||
};
|
||||
|
||||
actualProp = null;
|
||||
items.push(actualItem);
|
||||
|
||||
return readBracketO;
|
||||
}
|
||||
|
||||
function readBracketO(token){
|
||||
|
||||
if(token !== "{") throw new Error("expected '{' ");
|
||||
|
||||
return readProperty;
|
||||
}
|
||||
|
||||
function readBracketC(token){
|
||||
if(token !== "}") throw new Error("expected '}' ");
|
||||
return readSelector;
|
||||
}
|
||||
|
||||
function readDefinition(token){
|
||||
if(token !== ":") throw new Error("expected ':' ");
|
||||
|
||||
return readValue;
|
||||
}
|
||||
|
||||
function readProperty(token){
|
||||
|
||||
if(token === "}") return readBracketC(token);
|
||||
|
||||
const property = toProperty(token)
|
||||
actualProp = property;
|
||||
|
||||
if(!actualItem.values[property]){
|
||||
actualItem.values[property] = [];
|
||||
}
|
||||
|
||||
return readDefinition;
|
||||
}
|
||||
|
||||
function readValue(token){
|
||||
|
||||
actualItem.values[actualProp].push(token);
|
||||
|
||||
return readFinal;
|
||||
}
|
||||
|
||||
function readFinal(token){
|
||||
if(token === "}") return readBracketC(token);
|
||||
if(token !== ";") throw new Error("expected ';' ");
|
||||
return readProperty;
|
||||
}
|
||||
|
||||
let nextAction = readSelector
|
||||
let i = 0
|
||||
tokens.forEach(function(token){
|
||||
i++;
|
||||
nextAction = nextAction(token);
|
||||
|
||||
});
|
||||
|
||||
return renderJS(items);
|
||||
|
||||
}
|
||||
|
||||
function renderJS(items){
|
||||
|
||||
let objects = ['var styles = {']
|
||||
objects = objects.concat(items.map(renderItem).join(","));
|
||||
objects.push("}");
|
||||
return objects.join("\n");
|
||||
}
|
||||
|
||||
function renderItem(item){
|
||||
|
||||
const code = ['\n//' + item.originalValue]
|
||||
|
||||
let properties = []
|
||||
|
||||
for(const prop in item.values){
|
||||
if(item.values.hasOwnProperty(prop)) {
|
||||
const propitem = {
|
||||
name: prop,
|
||||
value: item.values[prop][item.values[prop].length - 1]
|
||||
}
|
||||
let markup = '"'
|
||||
if (~propitem.value.indexOf('"')) {
|
||||
markup = "'";
|
||||
propitem.value = propitem.value.replace(/'/gi, "\\'");
|
||||
}
|
||||
properties.push(SPACE + SPACE + propitem.name + ": " + markup + propitem.value + markup);
|
||||
}
|
||||
}
|
||||
|
||||
properties = properties.map(function(x){ return SPACE + x; });
|
||||
|
||||
item.selectors.forEach(function(i){
|
||||
|
||||
code.push(SPACE + i + ": {");
|
||||
code.push(properties.join(",\n"));
|
||||
code.push(SPACE + "}");
|
||||
|
||||
});
|
||||
|
||||
return code.join("\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
export default function transformCode (code) {
|
||||
return convertoToJS(tokenizer(code))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user