Browse Source

Clone properly the component with its related styles. FIxes #3093

pull/3147/head
Artur Arseniev 5 years ago
parent
commit
5b4963e4a5
  1. 5
      src/commands/view/CopyComponent.js
  2. 18
      src/css_composer/index.js
  3. 8
      src/css_composer/model/CssRule.js
  4. 16
      src/dom_components/model/Component.js

5
src/commands/view/CopyComponent.js

@ -2,9 +2,6 @@ export default {
run(ed) {
const em = ed.getModel();
const models = [...ed.getSelectedAll()];
if (models.length) {
em.set('clipboard', models);
}
models.length && em.set('clipboard', models);
}
};

18
src/css_composer/index.js

@ -373,6 +373,24 @@ export default () => {
);
},
/**
* Find rules, in different states (eg. like `:hover`) and media queries, matching the selector.
* @param {string} selector Selector, eg. '.myclass'
* @returns {Array<CssRule>}
* @example
* // Common scenario, take all the component specific rules
* const id = someComponent.getId();
* const rules = cc.getRules(`#${id}`);
* console.log(rules.map(rule => rule.toCSS()))
*/
getRules(selector) {
const rules = this.getAll();
const result = rules.filter(
r => r.getSelectors().getFullString() === selector
);
return result;
},
/**
* Add/update the CSS rule with id selector
* @param {string} name Id selector name, eg. 'my-id'

8
src/css_composer/model/CssRule.js

@ -41,10 +41,18 @@ export default Backbone.Model.extend(Styleable).extend({
initialize(c, opt = {}) {
this.config = c || {};
this.opt = opt;
this.em = opt.em;
this.ensureSelectors();
},
clone() {
const opts = { ...this.opt };
const attr = { ...this.attributes };
attr.selectors = this.get('selectors').map(s => s.clone());
return new this.constructor(attr, opts);
},
ensureSelectors() {
const { em } = this;
const result = [];

16
src/dom_components/model/Component.js

@ -861,9 +861,10 @@ const Component = Backbone.Model.extend(Styleable).extend(
*/
clone() {
const em = this.em;
const style = this.getStyle();
const attr = { ...this.attributes };
const opts = { ...this.opt };
const id = this.getId();
const cssc = em && em.get('CssComposer');
attr.attributes = { ...attr.attributes };
delete attr.attributes.id;
attr.components = [];
@ -884,15 +885,20 @@ const Component = Backbone.Model.extend(Styleable).extend(
attr.view = '';
opts.collection = null;
if (em && em.getConfig('avoidInlineStyle') && !isEmpty(style)) {
attr.style = style;
}
const cloned = new this.constructor(attr, opts);
const event = 'component:clone';
em && em.trigger(event, cloned);
this.trigger(event, cloned);
// Clone component specific rules
const newId = `#${cloned.getId()}`;
const rulesToClone = cssc ? cssc.getRules(`#${id}`) : [];
rulesToClone.forEach(rule => {
const newRule = rule.clone();
newRule.set('selectors', [newId]);
cssc.getAll().add(newRule);
});
return cloned;
},

Loading…
Cancel
Save