Browse Source

Refactor Css Composer

pull/36/head
Artur Arseniev 10 years ago
parent
commit
e3875fd2c0
  1. 2
      src/code_manager/model/CssGenerator.js
  2. 2
      src/css_composer/config/config.js
  3. 120
      src/css_composer/main.js
  4. 1
      src/css_composer/view/CssRuleView.js
  5. 20
      src/dom_components/main.js
  6. 5
      src/dom_components/model/Components.js
  7. 89
      src/editor/config/config.js
  8. 2
      src/editor/main.js
  9. 6
      src/editor/model/Editor.js
  10. 4
      src/style_manager/view/LayersView.js
  11. 23
      src/style_manager/view/SectorsView.js
  12. 440
      styles/css/main.css
  13. 2
      styles/scss/main.scss
  14. 38
      test/specs/code_manager/model/CodeModels.js
  15. 10
      test/specs/css_composer/e2e/CssComposer.js
  16. 81
      test/specs/css_composer/main.js
  17. 2
      test/specs/grapesjs/main.js

2
src/code_manager/model/CssGenerator.js

@ -50,7 +50,7 @@ define(['backbone'],
var compCls = this.compCls;
if(cssc){
var rules = cssc.getRules();
var rules = cssc.getAll();
var mediaRules = {};
rules.each(function(rule){
var width = rule.get('maxWidth');

2
src/css_composer/config/config.js

@ -8,7 +8,7 @@ define(function () {
'staticRules': '',
// Default CSS style
'defaults': [],
rules: [],
};
});

120
src/css_composer/main.js

@ -1,9 +1,27 @@
/**
* * [add](#add)
* * [get](#get)
* * [getAll](#getall)
* * [load](#load)
* * [store](#store)
*
* This module contains and manage CSS rules for the template inside the canvas
* Before using methods you should get first the module from the editor instance, in this way:
*
* ```js
* var cssComposer = editor.CssComposer;
* ```
*
* @module CssComposer
* @param {Object} config Configurations
* @param {string|Array<Object>} [config.rules=[]] CSS string or an array of rule objects
* @example
* ...
* CssComposer: {
* rules: '.myClass{ color: red}',
* }
*/
define(function(require) {
/**
* @class CssComposer
* @param {Object} config Configurations
*
* */
return function() {
var c = {},
defaults = require('./config/config'),
@ -43,6 +61,7 @@ define(function(require) {
/**
* Initialize module. Automatically called with a new instance of the editor
* @param {Object} config Configurations
* @private
*/
init: function(config) {
c = config || {};
@ -56,11 +75,11 @@ define(function(require) {
c.stylePrefix = ppfx + c.stylePrefix;
var elStyle = (c.em && c.em.config.style) || '';
c.defaults = elStyle || c.defaults;
c.rules = elStyle || c.rules;
c.sm = c.em; // TODO Refactor
rules = new CssRules([], c);
rules.add(c.defaults);
rules.add(c.rules);
// Load if requested
if(c.stm && c.stm.getConfig().autoload)
@ -72,7 +91,7 @@ define(function(require) {
});
if(c.stm && c.stm.isAutosave())
c.em.listenRules(this.getRules());
c.em.listenRules(this.getAll());
return this;
},
@ -103,8 +122,6 @@ define(function(require) {
* Store data to the selected storage
* @param {Boolean} noStore If true, won't store
* @return {Object} Data to store
* @example
* var rules = cssComposer.store();
*/
store: function(noStore){
if(!c.stm)
@ -120,6 +137,56 @@ define(function(require) {
return obj;
},
/**
* Add new rule to the collection, if not yet exists with the same selectors
* @param {Array<Selector>} selectors Array of selectors
* @param {String} state Css rule state
* @param {String} width For which device this style is oriented
* @return {Model}
* */
add: function(selectors, state, width) {
var s = state || '';
var w = width || '';
var rule = this.get(selectors, s, w);
if(rule)
return rule;
else{
rule = new CssRule({
state: s,
maxWidth: w,
});
rule.get('selectors').add(selectors);
rules.add(rule);
return rule;
}
},
/**
* Get rule
* @param {Array<Selector>} selectors Array of selectors
* @param {String} state Css rule state
* @param {String} width For which device this style is oriented
* @return {Model|null}
* */
get: function(selectors, state, width) {
var rule = null;
rules.each(function(m){
if(rule)
return;
if(m.compare(selectors, state, width))
rule = m;
});
return rule;
},
/**
* Get the collection of rules
* @return {Collection}
* */
getAll: function() {
return rules;
},
/**
* Create new rule and return it. Don't add it to the collection
* @param {Array} selectors Array of selectors
@ -127,7 +194,7 @@ define(function(require) {
* @param {String} width For which device this style is oriented
*
* @return {Object}
* */
* *
newRule: function(selectors, state, width) {
var s = state || '';
var w = width || '';
@ -137,21 +204,21 @@ define(function(require) {
});
rule.get('selectors').add(selectors);
return rule;
},
},*/
/**
* Add new rule to the collection if not yet exists
* @param {Object} rule
*
* @return {Object}
* */
* *
addRule: function(rule){
var models = rule.get('selectors').models;
var r = this.getRule(models, rule.get('state'), rule.get('maxWidth'));
if(!r)
r = rules.add(rule);
return r;
},
},*/
/**
* Get class by its name
@ -160,8 +227,8 @@ define(function(require) {
* @param {String} width For which device this style is oriented
*
* @return {Object|null}
* */
getRule : function(selectors, state, width) {
* *
getRule: function(selectors, state, width) {
fRule = null;
rules.each(function(rule){
if(fRule)
@ -170,23 +237,22 @@ define(function(require) {
fRule = rule;
}, this);
return fRule;
},
},*/
/**
* Get collection of css rules
*
* @return {Object}
* */
getRules : function() {
return rules;
},
* Get the collection of css rules
* @return {Collection}
* *
getRules: function() {
return rules;
},*/
/**
* Render block of CSS rules
*
* @return {Object}
* @return {HTMLElement}
* @private
*/
render: function(){
render: function() {
return rulesView.render().el;
}

1
src/css_composer/view/CssRuleView.js

@ -11,6 +11,7 @@ define(['backbone'],
this.config = o.config || {};
this.listenTo(this.model, 'change:style', this.render);
this.listenTo(this.model, 'change:state', this.render);
this.listenTo(this.model, 'destroy remove', this.remove);
this.listenTo(this.model, 'change:maxWidth', this.render);
this.listenTo(this.model.get('selectors'), 'change', this.selChanged);
},

20
src/dom_components/main.js

@ -1,10 +1,12 @@
/**
*
* - [getWrapper](#getwrapper)
* - [getComponents](#getcomponents)
* - [addComponent](#addcomponent)
* - [clear](#clear)
* - [render](#render)
* * [getWrapper](#getwrapper)
* * [getComponents](#getcomponents)
* * [addComponent](#addcomponent)
* * [clear](#clear)
* * [load](#load)
* * [store](#store)
* * [render](#render)
*
* With this module is possible to manage components inside the canvas.
* Before using methods you should get first the module from the editor instance, in this way:
@ -119,8 +121,8 @@ define(function(require) {
},
/**
* Load data from the passed object, if the object is empty will try to fetch them
* autonomously from the storage manager.
* Load components from the passed object, if the object is empty will try to fetch them
* autonomously from the selected storage
* The fetched data will be added to the collection
* @param {Object} data Object of data to load
* @return {Object} Loaded data
@ -142,11 +144,9 @@ define(function(require) {
},
/**
* Store data to the selected storage
* Store components on the selected storage
* @param {Boolean} noStore If true, won't store
* @return {Object} Data to store
* @example
* var components = domComponents.store();
*/
store: function(noStore){
if(!c.stm)

5
src/dom_components/model/Components.js

@ -55,7 +55,7 @@ define([ 'backbone', 'require'],
var cssc = this.editor.get('CssComposer');
if(parsed.css && cssc)
cssc.getRules().add(parsed.css);
cssc.getAll().add(parsed.css);
}
return Backbone.Collection.prototype.add.apply(this, [models, opt]);
@ -69,8 +69,7 @@ define([ 'backbone', 'require'],
var newClass = this.editor.get('SelectorManager').add(model.cid);
model.set({style:{}});
model.get('classes').add(newClass);
var rule = cssC.newRule(newClass);
cssC.addRule(rule);
var rule = cssC.add(newClass);
rule.set('style', style);
}
},

89
src/editor/config/config.js

@ -1,26 +1,11 @@
define(function () {
var config = {
//TEMP
components: '',
return {
// Style prefix
stylePrefix: 'wte-',
// Prefix to use inside local storage name (!)
storagePrefix: 'wte-',
// Editor ID. Useful in case of multiple editors on the same page (!)
id: '',
// Where render the editor
container: '',
idCanvas : 'canvas', //(!)
idCanvasOverlay : 'canvas-overlay', //(!)
stylePrefix: 'gjs-',
idWrapper : 'wrapper', //(!)
//TEMP
components: '',
// Enable/Disable possibility to copy(ctrl + c) & paste(ctrl + v) components
copyPaste: true,
@ -34,47 +19,53 @@ define(function () {
// Width for the editor container
width: '100%',
//Indicates which storage to use. Available: local | remote | none (!)
storageType: 'local',
// The css that could only be seen (for instance, inside the code viewer)
protectedCss: '*{box-sizing: border-box;}body{margin:0;height:100%}#wrapper{min-height:100%; overflow:auto}',
// Default command
defaultCommand: 'select-comp',
// If true render a select of available devices
showDevices: 1,
// Dom element
el: '',
//Configurations for Asset Manager
assetManager : {},
assetManager: {},
//Configurations for Canvas
canvas : {},
canvas: {},
//Configurations for Style Manager
styleManager : {},
styleManager: {},
//Configurations for Layers
layers : {},
layers: {},
//Configurations for Storage Manager
storageManager : {},
storageManager: {},
//Configurations for Rich Text Editor
rte : {},
rte: {},
//Configurations for DomComponents
domComponents : {},
domComponents: {},
//Configurations for Modal Dialog
modal : {},
modal: {},
//Configurations for Code Manager
codeManager : {},
codeManager: {},
//Configurations for Panels
panels : {},
panels: {},
//Configurations for Commands
commands : {},
commands: {},
//Configurations for Css Composer
cssComposer : {},
cssComposer: {},
//Configurations for Selector Manager
selectorManager: {},
@ -102,22 +93,22 @@ define(function () {
id: 'b1',
label: '1 Block',
content: '<div class="blk-row"><div class="blk1"></div></div>',
attributes: {class:'wte-fonts wte-f-b1'}
attributes: {class:'gjs-fonts gjs-f-b1'}
},{
id: 'b2',
label: '2 Blocks',
content: '<div class="blk-row"><div class="blk2"></div><div class="blk2"></div></div>',
attributes: {class:'wte-fonts wte-f-b2'}
attributes: {class:'gjs-fonts gjs-f-b2'}
},{
id: 'b3',
label: '3 Blocks',
content: '<div class="blk-row"><div class="blk3"></div><div class="blk3"></div><div class="blk3"></div></div>',
attributes: {class:'wte-fonts wte-f-b3'}
attributes: {class:'gjs-fonts gjs-f-b3'}
},{
id: 'b4',
label: '3/7 Block',
content: '<div class="blk-row"><div class="blk37l"></div><div class="blk37r"></div></div></div>',
attributes: {class:'wte-fonts wte-f-b37'}
attributes: {class:'gjs-fonts gjs-f-b37'}
},{
id: 'hero',
label: 'Hero section',
@ -128,12 +119,12 @@ define(function () {
'</nav><div class="clearfix"></div>'+
'<div class="lead-title">Build your templates without coding</div>'+
'<div class="lead-btn">Try it now</div></div></header>',
attributes: {class:'wte-fonts wte-f-hero'}
attributes: {class:'gjs-fonts gjs-f-hero'}
},{
id: 'h1p',
label: 'Text section',
content: '<h1 class="heading">Insert title here</h1><p class="paragraph">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</p>',
attributes: {class:'wte-fonts wte-f-h1p'}
attributes: {class:'gjs-fonts gjs-f-h1p'}
},{
id: '3ba',
label: 'Badges',
@ -163,11 +154,11 @@ define(function () {
'<div class="badge-foot"><span class="badge-link">f</span><span class="badge-link">t</span><span class="badge-link">ln</span>'+
'</div>'+
'</div></div>',
attributes: {class:'wte-fonts wte-f-3ba'}
attributes: {class:'gjs-fonts gjs-f-3ba'}
},{
id: 'text',
label: 'Text',
attributes: {class:'wte-fonts wte-f-text'},
attributes: {class:'gjs-fonts gjs-f-text'},
content: {
type:'text',
content:'Insert your text here',
@ -177,25 +168,15 @@ define(function () {
},{
id: 'image',
label: 'Image',
attributes: {class:'wte-fonts wte-f-image'},
attributes: {class:'gjs-fonts gjs-f-image'},
content: { type:'image', activeOnRender: 1},
},{
id: 'quo',
label: 'Quote',
content: '<blockquote class="quote">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore ipsum dolor sit</blockquote>',
attributes: {class:'wte-fonts wte-f-quo'}
attributes: {class:'gjs-fonts gjs-f-quo'}
}],
},
// Default command
defaultCommand: 'select-comp',
// If true render a select of available devices
showDevices: 1,
// Dom element
el: '',
};
return config;
});

2
src/editor/main.js

@ -228,7 +228,7 @@ define(function (require){
* @return {Object}
*/
getStyle: function(){
return em.get('CssComposer').getRules();
return em.get('CssComposer').getAll();
},
/**

6
src/editor/model/Editor.js

@ -133,7 +133,7 @@ define(['backbone', 'backboneUndo', 'keymaster', 'Utils', 'StorageManager', 'Dev
if(cmp && this.config.undoManager){
var that = this;
this.um = new Backbone.UndoManager({
register: [cmp.getComponents(), this.get('CssComposer').getRules()],
register: [cmp.getComponents(), this.get('CssComposer').getAll()],
track: true
});
this.UndoManager = this.um;
@ -313,7 +313,7 @@ define(['backbone', 'backboneUndo', 'keymaster', 'Utils', 'StorageManager', 'Dev
* @private
*/
setStyle: function(style){
var rules = this.get('CssComposer').getRules();
var rules = this.get('CssComposer').getAll();
for(var i = 0, len = rules.length; i < len; i++)
rules.pop();
rules.add(style);
@ -326,7 +326,7 @@ define(['backbone', 'backboneUndo', 'keymaster', 'Utils', 'StorageManager', 'Dev
* @private
*/
getStyle: function(){
return this.get('CssComposer').getRules();
return this.get('CssComposer').getAll();
},
/**

4
src/style_manager/view/LayersView.js

@ -15,14 +15,14 @@ define(['backbone','./LayerView'],
this.listenTo( this.collection, 'deselectAll', this.deselectAll );
this.listenTo( this.collection, 'reset', this.render);
var em = this.config.target || '';
var em = this.config.em || '';
var utils = em ? em.get('Utils') : '';
this.sorter = utils ? new utils.Sorter({
container: this.el,
containerSel: '.' + this.pfx + 'layers',
itemSel: '.' + this.pfx + 'layer',
pfx: 'wte-',
pfx: this.config.pStylePrefix,
}) : '';
this.$el.data('collection', this.collection);

23
src/style_manager/view/SectorsView.js

@ -50,13 +50,13 @@ define(['backbone', './SectorView'],
var valid = _.filter(classes.models, function(item){
return item.get('active');
});
var iContainer = cssC.getRule(valid, state, deviceW);
var iContainer = cssC.get(valid, state, deviceW);
if(!iContainer){
iContainer = cssC.newRule(valid, state, deviceW);
// Hydrate styles from component element
iContainer = cssC.add(valid, state, deviceW);
// Get styles from the component
iContainer.set('style', el.get('style'));
cssC.addRule(iContainer);
//cssC.addRule(iContainer);
el.set('style', {});
}else{
// Ensure to clean element
@ -64,19 +64,18 @@ define(['backbone', './SectorView'],
el.set('style', {});
}
// If the state is not empty, there is should be a helper rule in play
// If the state is not empty, there should be a helper rule in play
// The helper rule will get the same style of the iContainer
if(state){
var clm = this.target.get('SelectorManager');
var helperClass = clm.add('hc-state');
var helperRule = cssC.getRule([helperClass],'','');
if(!helperRule){
helperRule = cssC.newRule([helperClass],'','');
cssC.addRule(helperRule);
}else{
var helperRule = cssC.get([helperClass]);
if(!helperRule)
helperRule = cssC.add([helperClass]);
else{
// I will make it last again, otherwise it could be overridden
helperRule = cssC.getRules().remove(helperRule);
cssC.getRules().add(helperRule);
cssC.getAll().remove(helperRule);
cssC.getAll().add(helperRule);
}
helperRule.set('style', iContainer.get('style'));
pt.helper = helperRule;

440
styles/css/main.css

File diff suppressed because it is too large

2
styles/scss/main.scss

@ -1,7 +1,7 @@
@import "spectrum";
@import "font-awesome";
$app-prefix: 'wte-';
$app-prefix: 'gjs-';
$nv-prefix: $app-prefix + 'nv-';
$rte-prefix: $app-prefix + 'rte-';
$comp-prefix: $app-prefix + 'comp-';

38
test/specs/code_manager/model/CodeModels.js

@ -96,23 +96,21 @@ define([path + 'HtmlGenerator',
var cls1 = m1.get('classes').add({name: 'class1'});
var cssc = newCssComp();
var rule = cssc.newRule(cls1);
var rule = cssc.add(cls1);
rule.set('style',{'prop1':'value1', 'prop2':'value2'});
cssc.addRule(rule);
this.obj.build(comp, cssc).should.equal('.class1{prop1:value1;prop2:value2;}');
});
it('Build correctly component with class styled', function() {
it('Build correctly component styled with class and state', function() {
var comp = new Component();
var m1 = comp.get('components').add({tagName: 'article'});
var cls1 = m1.get('classes').add({name: 'class1'});
var cssc = newCssComp();
var rule = cssc.newRule(cls1);
var rule = cssc.add(cls1);
rule.set('style',{'prop1':'value1', 'prop2':'value2'});
rule.set('state', 'hover');
cssc.addRule(rule);
this.obj.build(comp, cssc).should.equal('.class1:hover{prop1:value1;prop2:value2;}');
});
@ -125,9 +123,8 @@ define([path + 'HtmlGenerator',
var cls2 = m1.get('classes').add({name: 'class2'});
var cssc = newCssComp();
var rule = cssc.newRule([cls1, cls2]);
var rule = cssc.add([cls1, cls2]);
rule.set('style',{'prop1':'value1', 'prop2':'value2'});
cssc.addRule(rule);
this.obj.build(comp, cssc).should.equal('.class1.class2{prop1:value1;prop2:value2;}');
this.obj.build(comp, cssc).should.equal('.class1.class2{prop1:value1;prop2:value2;}');
@ -140,12 +137,10 @@ define([path + 'HtmlGenerator',
var cls2 = m1.get('classes').add({name: 'class2'});
var cssc = newCssComp();
var rule = cssc.newRule([cls1, cls2]);
var rule = cssc.add([cls1, cls2]);
rule.set('style',{'prop1':'value1'});
cssc.addRule(rule);
var rule2 = cssc.newRule(cls2);
var rule2 = cssc.add(cls2);
rule2.set('style',{'prop2':'value2'});
cssc.addRule(rule2);
this.obj.build(comp, cssc).should.equal('.class1.class2{prop1:value1;}.class2{prop2:value2;}');
});
@ -157,10 +152,9 @@ define([path + 'HtmlGenerator',
var cls2 = m1.get('classes').add({name: 'class2'});
var cssc = newCssComp();
var rule = cssc.newRule([cls1, cls2]);
var rule = cssc.add([cls1, cls2]);
rule.set('style',{'prop1':'value1'});
rule.set('maxWidth', '999px');
cssc.addRule(rule);
this.obj.build(comp, cssc).should.equal('@media (max-width: 999px){.class1.class2{prop1:value1;}}');
});
@ -173,23 +167,18 @@ define([path + 'HtmlGenerator',
var cssc = newCssComp();
var rule = cssc.newRule([cls1, cls2]);
var rule = cssc.add([cls1, cls2]);
rule.set('style',{'prop1':'value1'});
cssc.addRule(rule);
var rule2 = cssc.newRule(cls2);
var rule2 = cssc.add(cls2);
rule2.set('style',{'prop2':'value2'});
cssc.addRule(rule2);
var rule3 = cssc.newRule(cls1, '', '999px');
var rule3 = cssc.add(cls1, '', '999px');
rule3.set('style',{'prop3':'value3'});
cssc.addRule(rule3);
var rule4 = cssc.newRule(cls2, '', '999px');
var rule4 = cssc.add(cls2, '', '999px');
rule4.set('style',{'prop4':'value4'});
cssc.addRule(rule4);
var rule5 = cssc.newRule(cls1, '', '100px');
var rule5 = cssc.add(cls1, '', '100px');
rule5.set('style',{'prop5':'value5'});
cssc.addRule(rule5);
this.obj.build(comp, cssc).should.equal('.class1.class2{prop1:value1;}.class2{prop2:value2;}'+
'@media (max-width: 999px){.class1{prop3:value3;}.class2{prop4:value4;}}'+
@ -202,9 +191,8 @@ define([path + 'HtmlGenerator',
var cls1 = m1.get('classes').add({name: 'class1'});
var cssc = newCssComp();
var rule = cssc.newRule(cls1);
var rule = cssc.add(cls1);
rule.set('style',{'prop1':'value1', 'prop2':'value2'});
cssc.addRule(rule);
comp.get('components').remove(m1);
this.obj.build(comp, cssc).should.equal('');

10
test/specs/css_composer/e2e/CssComposer.js

@ -46,24 +46,24 @@ define(['GrapesJS'],function(Grapes) {
stylePrefix: '',
storageManager: { autoload: 0, type:'none' },
assetManager: { storageType: 'none', },
cssComposer: { defaults: this.rulesSet},
cssComposer: { rules: this.rulesSet},
container: 'csscomposer-fixture',
});
var cssc = gj.editor.get('CssComposer');
cssc.getRules().length.should.equal(this.rulesSet.length);
cssc.getAll().length.should.equal(this.rulesSet.length);
var cls = gj.editor.get('SelectorManager').getAll();
cls.length.should.equal(3);
});
it('New rule adds correctly the class inside selector manager', function() {
var rules = this.cssc.getRules();
var rules = this.cssc.getAll();
rules.add({ selectors: [{name: 'test1'}] });
this.clsm.getAll().at(0).get('name').should.equal('test1');
});
it('New rules are correctly imported inside selector manager', function() {
var rules = this.cssc.getRules();
var rules = this.cssc.getAll();
this.rulesSet.forEach(function(item){
rules.add(item);
});
@ -76,7 +76,7 @@ define(['GrapesJS'],function(Grapes) {
it('Add rules from the new component added as a string with style tag', function() {
var comps = this.domc.getComponents();
var rules = this.cssc.getRules();
var rules = this.cssc.getAll();
comps.add("<div>Test</div><style>.test{color: red} .test2{color: blue}</style>");
comps.length.should.equal(1);
rules.length.should.equal(2);

81
test/specs/css_composer/main.js

@ -19,12 +19,14 @@ define([
describe('Main', function() {
var obj;
beforeEach(function () {
this.obj = new CssComposer().init();
obj = new CssComposer().init();
});
afterEach(function () {
delete this.obj;
delete obj;
});
it('Object exists', function() {
@ -32,104 +34,91 @@ define([
});
it("Rules are empty", function() {
this.obj.getRules().length.should.equal(0);
obj.getAll().length.should.equal(0);
});
it('Create new rule with correct selectors', function() {
var sel = new this.obj.Selectors();
var sel = new obj.Selectors();
var s1 = sel.add({name: 'test1'});
var rule = this.obj.newRule(sel.models);
var rule = obj.add(sel.models);
rule.get('selectors').at(0).should.deep.equal(s1);
});
it('Create new rule correctly', function() {
var sel = new this.obj.Selectors();
var sel = new obj.Selectors();
var s1 = sel.add({name: 'test1'});
var rule = this.obj.newRule(sel.models, 'state1', 'width1');
var rule = obj.add(sel.models, 'state1', 'width1');
rule.get('state').should.equal('state1');
rule.get('maxWidth').should.equal('width1');
});
it("Add rule to collection", function() {
var sel = new this.obj.Selectors([{name: 'test1'}]);
var rule = this.obj.newRule(sel.models);
this.obj.addRule(rule);
this.obj.getRules().length.should.equal(1);
this.obj.getRules().at(0).get('selectors').at(0).get('name').should.equal('test1');
var sel = new obj.Selectors([{name: 'test1'}]);
var rule = obj.add(sel.models);
obj.getAll().length.should.equal(1);
obj.getAll().at(0).get('selectors').at(0).get('name').should.equal('test1');
});
it("Returns correct rule with the same selector", function() {
var sel = new this.obj.Selectors([{name: 'test1'}]);
var rule1 = this.obj.newRule(sel.models);
this.obj.addRule(rule1);
var rule2 = this.obj.getRule(sel.models);
var sel = new obj.Selectors([{name: 'test1'}]);
var rule1 = obj.add(sel.models);
var rule2 = obj.get(sel.models);
rule1.should.deep.equal(rule2);
});
it("Returns correct rule with the same selectors", function() {
var sel1 = new this.obj.Selectors([{name: 'test1'}]);
var rule1 = this.obj.newRule(sel1.models);
this.obj.addRule(rule1);
var sel1 = new obj.Selectors([{name: 'test1'}]);
var rule1 = obj.add(sel1.models);
var sel2 = new this.obj.Selectors([{name: 'test21'}, {name: 'test22'}]);
var rule2 = this.obj.newRule(sel2.models);
this.obj.addRule(rule2);
var sel2 = new obj.Selectors([{name: 'test21'}, {name: 'test22'}]);
var rule2 = obj.add(sel2.models);
var rule3 = this.obj.getRule(sel2.models);
var rule3 = obj.get(sel2.models);
rule3.should.deep.equal(rule2);
});
it("Don't duplicate rules", function() {
var sel = new this.obj.Selectors([]);
var sel = new obj.Selectors([]);
var s1 = sel.add({name: 'test1'});
var s2 = sel.add({name: 'test2'});
var s3 = sel.add({name: 'test3'});
var rule1 = this.obj.newRule([s1, s3]);
var aRule1 = this.obj.addRule(rule1);
var rule2 = this.obj.newRule([s3, s1]);
var aRule2 = this.obj.addRule(rule2);
var rule1 = obj.add([s1, s3]);
var rule2 = obj.add([s3, s1]);
aRule2.should.deep.equal(aRule1);
rule2.should.deep.equal(rule1);
});
it("Returns correct rule with the same mixed selectors", function() {
var sel = new this.obj.Selectors([]);
var sel = new obj.Selectors([]);
var s1 = sel.add({name: 'test1'});
var s2 = sel.add({name: 'test2'});
var s3 = sel.add({name: 'test3'});
var rule1 = this.obj.newRule([s1, s3]);
this.obj.addRule(rule1);
var rule2 = this.obj.getRule([s3, s1]);
var rule1 = obj.add([s1, s3]);
var rule2 = obj.get([s3, s1]);
rule2.should.deep.equal(rule1);
});
it("Returns correct rule with the same selectors and state", function() {
var sel = new this.obj.Selectors([]);
var sel = new obj.Selectors([]);
var s1 = sel.add({name: 'test1'});
var s2 = sel.add({name: 'test2'});
var s3 = sel.add({name: 'test3'});
var rule1 = this.obj.newRule([s1, s3], 'hover');
this.obj.addRule(rule1);
var rule2 = this.obj.getRule([s3, s1], 'hover');
var rule1 = obj.add([s1, s3], 'hover');
var rule2 = obj.get([s3, s1], 'hover');
rule2.should.deep.equal(rule1);
});
it("Returns correct rule with the same selectors, state and width", function() {
var sel = new this.obj.Selectors([]);
var sel = new obj.Selectors([]);
var s1 = sel.add({name: 'test1'});
var rule1 = this.obj.newRule([s1], 'hover','1');
this.obj.addRule(rule1);
var rule2 = this.obj.getRule([s1], 'hover', '1');
var rule1 = obj.add([s1], 'hover','1');
var rule2 = obj.get([s1], 'hover', '1');
rule2.should.deep.equal(rule1);
});
it("Renders correctly", function() {
this.obj.render().should.be.ok;
obj.render().should.be.ok;
});
});

2
test/specs/grapesjs/main.js

@ -81,7 +81,7 @@ define(['GrapesJS', 'PluginManager', 'chai'],
it('Init editor with css', function() {
config.style = cssString;
var editor = obj.init(config);
var rules = editor.CssComposer.getRules();
var rules = editor.CssComposer.getAll();
rules.length.should.equal(2);
rules.at(0).get('selectors').at(0).get('name').should.equal('test2');
});

Loading…
Cancel
Save