Skip to content

Commit

Permalink
refactor: 💡 create /vcssom folder and move some fn there
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 22, 2019
1 parent 857b2d2 commit aea14e5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .storybook/vcssom/createUseCssDiffWithNano.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {cssToTree} from '../../addon/vcssom';
import {cssToTree} from '../../addon/vcssom/cssToTree';
import {HookUseMemo} from './types';

const createUseCssDiffWithNano = (nano: any) => (useMemo: HookUseMemo) => {
Expand Down
56 changes: 1 addition & 55 deletions addon/vcssom.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,6 @@
'use strict';

var cssToTree = function (tree, css, selector, prelude) {
var declarations = {};
var hasDeclarations = false;
var key, value;

for (key in css) {
value = css[key];
if (typeof value !== 'object') {
hasDeclarations = true;
declarations[key] = value;
}
}

if (hasDeclarations) {
if (!tree[prelude]) tree[prelude] = {};
tree[prelude][selector] = declarations;
}

for (key in css) {
value = css[key];
if (typeof value === 'object') {
if (key[0] === '@') {
cssToTree(tree, value, selector, key);
} else {
var hasCurrentSymbol = key.indexOf('&') > -1;
var selectorParts = selector.split(',');
if (hasCurrentSymbol) {
for (var i = 0; i < selectorParts.length; i++) {
selectorParts[i] = key.replace(/&/g, selectorParts[i]);
}
} else {
for (var i = 0; i < selectorParts.length; i++) {
selectorParts[i] = selectorParts[i] + ' ' + key;
}
}
cssToTree(tree, value, selectorParts.join(','), prelude);
}
}
}
};

exports.cssToTree = cssToTree;

function removeRule (sh, rule) {
var maxIndex = rule.index;
var rules = sh.cssRules || sh.rules;
maxIndex = Math.max(maxIndex, rules.length - 1);
while (maxIndex >= 0) {
if (rules[maxIndex] === rule) {
sh.deleteRule(maxIndex);
break;
}
maxIndex--;
}
}
var removeRule = require('./vcssom/removeRule').removeRule;

exports.addon = function (renderer) {
if (process.env.NODE_ENV !== 'production') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable */
'use strict';

var cssToTree = require('../vcssom').cssToTree;
var cssToTree = require('../cssToTree').cssToTree;

describe('cssToTree', function () {
test('exist', function () {
Expand Down
42 changes: 42 additions & 0 deletions addon/vcssom/cssToTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function cssToTree (tree, css, selector, prelude) {
var declarations = {};
var hasDeclarations = false;
var key, value;

for (key in css) {
value = css[key];
if (typeof value !== 'object') {
hasDeclarations = true;
declarations[key] = value;
}
}

if (hasDeclarations) {
if (!tree[prelude]) tree[prelude] = {};
tree[prelude][selector] = declarations;
}

for (key in css) {
value = css[key];
if (typeof value === 'object') {
if (key[0] === '@') {
cssToTree(tree, value, selector, key);
} else {
var hasCurrentSymbol = key.indexOf('&') > -1;
var selectorParts = selector.split(',');
if (hasCurrentSymbol) {
for (var i = 0; i < selectorParts.length; i++) {
selectorParts[i] = key.replace(/&/g, selectorParts[i]);
}
} else {
for (var i = 0; i < selectorParts.length; i++) {
selectorParts[i] = selectorParts[i] + ' ' + key;
}
}
cssToTree(tree, value, selectorParts.join(','), prelude);
}
}
}
};

exports.cssToTree = cssToTree;
14 changes: 14 additions & 0 deletions addon/vcssom/removeRule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function removeRule (sh, rule) {
var maxIndex = rule.index;
var rules = sh.cssRules || sh.rules;
maxIndex = Math.max(maxIndex, rules.length - 1);
while (maxIndex >= 0) {
if (rules[maxIndex] === rule) {
sh.deleteRule(maxIndex);
break;
}
maxIndex--;
}
}

exports.removeRule = removeRule;

0 comments on commit aea14e5

Please sign in to comment.