use transform-css-to-js

This commit is contained in:
Ritesh Kumar committed 2017-09-03 02:29:04 +05:30
1 parent 668cbc0299
commit e2bd9b00ed
4 files changed
+12 -223

No files matched your search

+1 -1
View File
@@ -25,7 +25,6 @@
"codemirror": "^5.29.0",
"codemirror-graphql": "^0.6.11",
"copy-text-to-clipboard": "^1.0.3",
"css-to-react-native": "^2.0.4",
"generate-schema": "^2.5.1",
"graphql": "^0.10.5",
"is-in-browser": "^1.1.3",
@@ -42,6 +41,7 @@
"react-fuzzy-picker": "^0.5.1",
"react-github-corner": "^1.0.0",
"stringify-object": "^3.2.0",
"transform-css-to-js": "^0.1.1",
"unfetch": "^3.0.0",
"xml-js": "^1.4.1"
},
+3 -23
View File
@@ -1,9 +1,7 @@
import React, { PureComponent } from "react";
import plugin from "../utils/css-to-js";
import Layout from "../components/Layout";
import ConversionPanel from "../components/ConversionPanel";
import transform from "css-to-react-native";
import kebabCase from "lodash/kebabCase";
import cssToJS from "transform-css-to-js";
const defaultText = `.main-wrapper {
flex-direction: row;
@@ -32,26 +30,8 @@ export default class Css2Js extends PureComponent {
isRn: false
};
getRnCode = (newValue: string) => {
const styles = {};
const code = eval("(" + plugin(newValue).split("=")[1] + ")");
Object.keys(code).forEach(key => {
styles[key] = {};
const arr = [];
Object.keys(code[key]).forEach(key2 => {
arr.push([kebabCase(key2), code[key][key2]]);
});
styles[key] = transform(arr);
});
return styles;
};
getTransformedValue = (newValue: string) => {
return this.state.isRn
? `const styles = StyleSheet.create(${JSON.stringify(
this.getRnCode(newValue)
)})`
: plugin(newValue);
getTransformedValue = (newValue) => {
return cssToJS(newValue, this.state.isRn)
};
render() {
-199
View File
@@ -1,199 +0,0 @@
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 = ["const 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));
}
+8
View File
@@ -4946,6 +4946,14 @@ tough-cookie@~2.3.0:
dependencies:
punycode "^1.4.1"
transform-css-to-js@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/transform-css-to-js/-/transform-css-to-js-0.1.1.tgz#20fd40b137849555c063aa55f168680ad00724bb"
dependencies:
css-to-react-native "^2.0.4"
lodash "^4.17.4"
stringify-object "^3.2.0"
trim-right@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003"