Skip to content

Commit

Permalink
Fix issues with color traits. Fixes GrapesJS#1104
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed May 13, 2018
1 parent 31c0def commit b02744c
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 33 deletions.
2 changes: 1 addition & 1 deletion dist/css/grapes.min.css

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/domain_abstract/ui/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ module.exports = Backbone.View.extend({
*/
handleChange(e) {
e.stopPropagation();
this.model.set('value', this.getInputEl().value);
const value = this.getInputEl().value;
this.model.set({ value }, { fromInput: 1 });
this.elementUpdated();
},

Expand All @@ -65,7 +66,7 @@ module.exports = Backbone.View.extend({
*/
getInputEl() {
if (!this.inputEl) {
const plh = this.model.get('defaults');
const plh = this.model.get('defaults') || '';
this.inputEl = $(`<input type="text" placeholder="${plh}">`);
}

Expand Down
5 changes: 4 additions & 1 deletion src/domain_abstract/ui/InputColor.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isUndefined } from 'underscore';

require('utils/ColorPicker');
const Input = require('./Input');
const $ = Backbone.$;
Expand Down Expand Up @@ -31,7 +33,8 @@ module.exports = Input.extend({
*/
setValue(val, opts = {}) {
const model = this.model;
const value = val || model.get('defaults');
const def = model.get('defaults');
const value = !isUndefined(val) ? val : !isUndefined(def) ? def : '';
const inputEl = this.getInputEl();
const colorEl = this.getColorEl();
const valueClr = value != 'none' ? value : '';
Expand Down
2 changes: 2 additions & 0 deletions src/styles/scss/_gjs_traits.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
.#{$app-prefix}label {
width: 30%;
text-align: left;
text-overflow: ellipsis;
overflow: hidden;
}

.#{$app-prefix}field {
Expand Down
17 changes: 12 additions & 5 deletions src/trait_manager/model/Trait.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,28 @@ module.exports = require('backbone').Model.extend({
getTargetValue() {
const name = this.get('name');
const target = this.target;
const prop = this.get('changeProp');
if (target) return prop ? target.get(name) : target.getAttributes()[name];
let value;

if (this.get('changeProp')) {
value = target.get(name);
} else {
value = target.getAttributes()[name];
}

return !isUndefined(value) ? value : '';
},

setTargetValue(value) {
setTargetValue(value, opts = {}) {
const target = this.target;
const name = this.get('name');
if (isUndefined(value)) return;

if (this.get('changeProp')) {
target.set(name, value);
target.set(name, value, opts);
} else {
const attrs = { ...target.get('attributes') };
attrs[name] = value;
target.set('attributes', attrs);
target.set('attributes', attrs, opts);
}
},

Expand Down
17 changes: 9 additions & 8 deletions src/trait_manager/view/TraitColorView.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ module.exports = TraitView.extend({
*/
getInputEl() {
if (!this.$input) {
var value = this.getModelValue();
var inputColor = new InputColor({
const model = this.model;
const value = this.getModelValue();
const inputColor = new InputColor({
model,
target: this.config.em,
contClass: this.ppfx + 'field-color',
model: this.model,
ppfx: this.ppfx
});
this.input = inputColor.render();
this.$input = this.input.colorEl;
value = value || '';
this.model.set('value', value).trigger('change:value');
this.input.setValue(value);
const input = inputColor.render();
this.$input = input.colorEl;
input.setValue(value, { fromTarget: 1 });
this.input = input;
}

return this.$input.get(0);
},

Expand Down
27 changes: 12 additions & 15 deletions src/trait_manager/view/TraitView.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = Backbone.View.extend({
this.inputhClass = this.ppfx + 'input-holder';
model.off('change:value', this.onValueChange);
this.listenTo(model, 'change:value', this.onValueChange);
model.view = this;
this.tmpl =
'<div class="' +
this.fieldClass +
Expand Down Expand Up @@ -64,7 +65,7 @@ module.exports = Backbone.View.extend({
this.setInputValue(mod.get('value'));
} else {
const value = this.getValueForTarget();
mod.setTargetValue(value);
mod.setTargetValue(value, opts);
}
},

Expand All @@ -73,8 +74,9 @@ module.exports = Backbone.View.extend({
* @private
*/
renderLabel() {
const label = this.getLabel();
this.$el.html(
'<div class="' + this.labelClass + '">' + this.getLabel() + '</div>'
`<div class="${this.labelClass}" title="${label}">${label}</div>`
);
},

Expand All @@ -96,17 +98,12 @@ module.exports = Backbone.View.extend({
*/
getInputEl() {
if (!this.$input) {
var md = this.model;
var trg = this.target;
var name = md.get('name');
const md = this.model;
const plh = md.get('placeholder') || md.get('default') || '';
const type = md.get('type') || 'text';
const attrs = trg.get('attributes');
const min = md.get('min');
const max = md.get('max');
const value = md.get('changeProp')
? trg.get(name)
: md.get('value') || attrs[name];
const value = this.getModelValue();
const input = $(`<input type="${type}" placeholder="${plh}">`);

if (value) {
Expand All @@ -127,19 +124,19 @@ module.exports = Backbone.View.extend({
},

getModelValue() {
var value;
var model = this.model;
var target = this.target;
var name = model.get('name');
let value;
const model = this.model;
const target = this.target;
const name = model.get('name');

if (model.get('changeProp')) {
value = target.get(name);
} else {
var attrs = target.get('attributes');
const attrs = target.get('attributes');
value = model.get('value') || attrs[name];
}

return value;
return !isUndefined(value) ? value : '';
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/trait_manager/view/TraitsView.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = DomainViews.extend({
this.pfx = config.stylePrefix || '';
this.ppfx = config.pStylePrefix || '';
this.className = this.pfx + 'traits';
const toListen = 'component:selected component:update:traits';
const toListen = 'component:selected';
this.listenTo(this.em, toListen, this.updatedCollection);
this.updatedCollection();
},
Expand Down

0 comments on commit b02744c

Please sign in to comment.