Browse Source

Update `getModelToStyle` method and make it the single source of truth for styling

pull/798/merge
Artur Arseniev 9 years ago
parent
commit
f00e04fbe2
  1. 23
      src/style_manager/index.js
  2. 90
      src/style_manager/view/SectorsView.js

23
src/style_manager/index.js

@ -251,20 +251,35 @@ module.exports = () => {
if (em) { if (em) {
const config = em.getConfig(); const config = em.getConfig();
const um = em.get('UndoManager');
const cssC = em.get('CssComposer'); const cssC = em.get('CssComposer');
const state = !config.devicePreviewMode ? model.get('state') : ''; const state = !config.devicePreviewMode ? model.get('state') : '';
const valid = classes.getStyleable(); const valid = classes.getStyleable();
const hasClasses = valid.length; const hasClasses = valid.length;
const opts = { state }; const opts = { state };
let rule;
if (hasClasses) { if (hasClasses) {
const deviceW = em.getCurrentMedia(); const deviceW = em.getCurrentMedia();
const CssRule = cssC.get(valid, state, deviceW); rule = cssC.get(valid, state, deviceW);
if (CssRule) return CssRule;
if (!rule) {
// I stop undo manager here as after adding the CSSRule (generally after
// selecting the component) and calling undo() it will remove the rule from
// the collection, therefore updating it in style manager will not affect it
// #268
um.stop();
rule = cssC.add(valid, state, deviceW);
rule.setStyle(model.getStyle());
model.setStyle({});
um.start();
}
} else if (config.avoidInlineStyle) { } else if (config.avoidInlineStyle) {
const rule = cssC.getIdRule(id, opts); rule = cssC.getIdRule(id, opts);
return rule ? rule : cssC.setIdRule(id, {}, opts); !rule && (rule = cssC.setIdRule(id, {}, opts));
} }
model = rule;
} }
return model; return model;

90
src/style_manager/view/SectorsView.js

@ -40,45 +40,33 @@ module.exports = Backbone.View.extend({
* @private * @private
*/ */
targetUpdated() { targetUpdated() {
console.log('Target update'); const em = this.target;
var em = this.target; const pt = this.propTarget;
let model = em.getSelected(); let model = em.getSelected();
const um = em.get('UndoManager'); if (!model) return;
const cc = em.get('CssComposer');
const avoidInline = em.getConfig('avoidInlineStyle');
if (!model) {
return;
}
const id = model.getId();
const config = em.get('Config'); const config = em.get('Config');
var classes = model.get('classes');
var pt = this.propTarget;
const state = !config.devicePreviewMode ? model.get('state') : ''; const state = !config.devicePreviewMode ? model.get('state') : '';
const opts = { state }; const el = model.getEl();
var stateStr = state ? `:${state}` : null;
var view = model.view;
const media = em.getCurrentMedia();
pt.helper = null; pt.helper = null;
if (view) { // Create computed style container
pt.computed = window.getComputedStyle( if (el) {
view.el, const stateStr = state ? `:${state}` : null;
state ? `:${state}` : null pt.computed = window.getComputedStyle(el, stateStr);
);
} }
// Create a new rule for the state as a helper
const appendStateRule = (style = {}) => { const appendStateRule = (style = {}) => {
const sm = em.get('SelectorManager'); const cc = em.get('CssComposer');
const helperClass = sm.add('hc-state'); const helperCls = 'hc-state';
let helperRule = cc.get([helperClass]); const rules = cc.getAll();
let helperRule = cc.getClassRule(helperCls);
if (!helperRule) { if (!helperRule) {
helperRule = cc.add([helperClass]); helperRule = cc.setClassRule(helperCls);
} else { } else {
// I will make it last again, otherwise it could be overridden // I will make it last again, otherwise it could be overridden
const rules = cc.getAll();
rules.remove(helperRule); rules.remove(helperRule);
rules.add(helperRule); rules.add(helperRule);
} }
@ -88,54 +76,8 @@ module.exports = Backbone.View.extend({
pt.helper = helperRule; pt.helper = helperRule;
}; };
// If true the model will be always a rule model = em.get('StyleManager').getModelToStyle(model);
if (avoidInline) { state && appendStateRule(model.getStyle());
const ruleId = cc.getIdRule(id, opts);
if (!ruleId) {
model = cc.setIdRule(id, {}, opts);
} else {
model = ruleId;
}
}
if (classes.length) {
var valid = classes.getStyleable();
var iContainer = cc.get(valid, state, media);
if (!iContainer && valid.length) {
// I stop undo manager here as after adding the CSSRule (generally after
// selecting the component) and calling undo() it will remove the rule from
// the collection, therefore updating it in style manager will not affect it
// #268
um.stop();
iContainer = cc.add(valid, state, media);
iContainer.setStyle(model.getStyle());
model.setStyle({});
um.start();
}
if (!iContainer) {
// In this case it's just a Component without any valid selector
pt.model = model;
pt.trigger('update');
return;
}
// 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
state && appendStateRule(iContainer.getStyle());
pt.model = iContainer;
pt.trigger('update');
return;
}
if (state) {
const ruleState = cc.getIdRule(id, opts);
state && appendStateRule(ruleState && ruleState.getStyle());
}
pt.model = model; pt.model = model;
pt.trigger('update'); pt.trigger('update');
}, },

Loading…
Cancel
Save