Browse Source

Make the device change work with current frame

pull/2524/head
Artur Arseniev 7 years ago
parent
commit
bbaffc2839
  1. 14
      src/canvas/model/Canvas.js
  2. 4
      src/canvas/model/Frame.js
  3. 41
      src/canvas/view/FrameView.js

14
src/canvas/model/Canvas.js

@ -14,13 +14,27 @@ export default Backbone.Model.extend({
}, },
initialize(config = {}) { initialize(config = {}) {
const { em } = config;
const { styles = [], scripts = [] } = config; const { styles = [], scripts = [] } = config;
const frame = new Frame({}, config); const frame = new Frame({}, config);
styles.forEach(style => frame.addLink(style)); styles.forEach(style => frame.addLink(style));
scripts.forEach(script => frame.addScript(script)); scripts.forEach(script => frame.addScript(script));
this.em = em;
this.set('frame', frame); this.set('frame', frame);
this.set('frames', new Frames([frame], config)); this.set('frames', new Frames([frame], config));
this.listenTo(this, 'change:zoom', this.onZoomChange); this.listenTo(this, 'change:zoom', this.onZoomChange);
this.listenTo(em, 'change:device', this.updateDevice);
},
updateDevice() {
const { em } = this;
const device = em.getDeviceModel();
const { model } = em.get('currentFrame') || {};
if (model && device) {
const { width, height } = device.attributes;
model.set({ width, height });
}
}, },
onZoomChange() { onZoomChange() {

4
src/canvas/model/Frame.js

@ -6,8 +6,8 @@ import { isString } from 'underscore';
export default Backbone.Model.extend({ export default Backbone.Model.extend({
defaults: { defaults: {
wrapper: '', wrapper: '',
width: '', width: null,
height: '', height: null,
head: '', head: '',
x: 0, x: 0,
y: 0, y: 0,

41
src/canvas/view/FrameView.js

@ -1,5 +1,5 @@
import Backbone from 'backbone'; import Backbone from 'backbone';
import { bindAll, isNumber } from 'underscore'; import { bindAll, isNumber, isNull, debounce } from 'underscore';
import CssRulesView from 'css_composer/view/CssRulesView'; import CssRulesView from 'css_composer/view/CssRulesView';
import ComponentView from 'dom_components/view/ComponentView'; import ComponentView from 'dom_components/view/ComponentView';
import { import {
@ -33,7 +33,6 @@ export default Backbone.View.extend({
this.listenTo(model, 'change:head', this.updateHead); this.listenTo(model, 'change:head', this.updateHead);
this.listenTo(model, 'change:x change:y', this.updatePos); this.listenTo(model, 'change:x change:y', this.updatePos);
this.listenTo(model, 'change:width change:height', this.updateDim); this.listenTo(model, 'change:width change:height', this.updateDim);
this.listenTo(this.em, 'change:device', this.updateDim);
this.updatePos(); this.updatePos();
model.view = this; model.view = this;
}, },
@ -64,39 +63,33 @@ export default Backbone.View.extend({
const { em, el, $el, model } = this; const { em, el, $el, model } = this;
const { width, height } = model.attributes; const { width, height } = model.attributes;
const { style } = el; const { style } = el;
const device = em.getDeviceModel();
const currW = style.width || ''; const currW = style.width || '';
const currH = style.height || ''; const currH = style.height || '';
const newW = width || (device ? device.get('width') : ''); const newW = width;
const newH = height || (device ? device.get('height') : ''); const newH = height;
const noChanges = currW == newW && currH == newH; const noChanges = currW == newW && currH == newH;
const un = 'px'; const un = 'px';
style.width = isNumber(newW) ? `${newW}${un}` : newW; style.width = isNumber(newW) ? `${newW}${un}` : newW;
style.height = isNumber(newH) ? `${newH}${un}` : newH; style.height = isNumber(newH) ? `${newH}${un}` : newH;
if (!width || !height)
model.set( // Set width and height from DOM (should be done only once)
{ if (isNull(width) || isNull(height)) {
...(!width ? { width: el.offsetWidth } : {}), const newDims = {
...(!height ? { height: el.offsetHeight } : {}) ...(!width ? { width: el.offsetWidth } : {}),
}, ...(!height ? { height: el.offsetHeight } : {})
{ silent: 1 } };
); model.set(newDims, { silent: 1 });
}
// Prevent fixed highlighting box which appears when on // Prevent fixed highlighting box which appears when on
// component hover during the animation // component hover during the animation
em.stopDefault({ preserveSelected: 1 }); em.stopDefault({ preserveSelected: 1 });
// TODO in updateOffset make use of internal API instead of Canvas noChanges ? this.updateOffset() : $el.one(motionsEv, this.updateOffset);
// noChanges ? this.updateOffset() : $el.on(motionsEv, this.updateOffset);
}, },
updateOffset() { updateOffset: debounce(function() {
const { em } = this; this.em.runDefault({ preserveSelected: 1 });
const cv = em.get('Canvas'); }),
if (!cv) return;
const offset = cv.getOffset();
em.set('canvasOffset', offset);
em.runDefault({ preserveSelected: 1 });
this.$el.off(motionsEv, this.updateOffset);
},
getEl() { getEl() {
return this.el; return this.el;

Loading…
Cancel
Save