Add image resize feature

This commit is contained in:
Amine Ben hammou
2017-08-23 12:34:04 +00:00
parent 78d52d6298
commit a1d0acfac2
7 changed files with 65 additions and 12 deletions

View File

@@ -3,12 +3,12 @@
img img
vertical-align: bottom vertical-align: bottom
max-width: 100% max-width: 100%
width: auto
height: auto height: auto
&.mailpoet_full_image &.mailpoet_full_image
padding-left: 0 padding-left: 0
padding-right: 0 padding-right: 0
margin: auto
margin-bottom: 0 margin-bottom: 0
.mailpoet_content a:hover .mailpoet_content a:hover

View File

@@ -14,8 +14,9 @@ define([
defaults: { defaults: {
elementSelector: null, elementSelector: null,
resizeHandleSelector: true, // true will use edges of the element itself resizeHandleSelector: true, // true will use edges of the element itself
transformationFunction: function (y) { return y; }, transformationFunction: function(event) { return event.dy; },
minLength: 0, minLength: 0,
maxLength: Infinity,
modelField: 'styles.block.height' modelField: 'styles.block.height'
}, },
events: { events: {
@@ -45,9 +46,10 @@ define([
that.$el.addClass('mailpoet_resize_active'); that.$el.addClass('mailpoet_resize_active');
}).on('resizemove', function (event) { }).on('resizemove', function (event) {
var currentLength = parseFloat(that.view.model.get(that.options.modelField)), var currentLength = parseFloat(that.view.model.get(that.options.modelField)),
newLength = currentLength + that.options.transformationFunction(event.dy); newLength = currentLength + that.options.transformationFunction(event);
if (newLength < that.options.minLength) newLength = that.options.minLength; if (newLength < that.options.minLength) newLength = that.options.minLength;
if (newLength > that.options.maxLength) newLength = that.options.maxLength;
that.view.model.set(that.options.modelField, newLength + 'px'); that.view.model.set(that.options.modelField, newLength + 'px');
}) })

View File

@@ -44,11 +44,30 @@ define([
}, base.BlockView.prototype.templateContext.apply(this)); }, base.BlockView.prototype.templateContext.apply(this));
}, },
behaviors: _.extend({}, base.BlockView.prototype.behaviors, { behaviors: _.extend({}, base.BlockView.prototype.behaviors, {
ShowSettingsBehavior: {} ResizableBehavior: {
elementSelector: '.mailpoet_image',
resizeHandleSelector: '.mailpoet_resize_handle',
minLength: 36,
maxLength: 660,
modelField: 'width',
transformationFunction: function(event) { return event.dx; }
},
ShowSettingsBehavior: {
ignoreFrom: '.mailpoet_resize_handle'
}
}), }),
initialize: function() {
base.BlockView.prototype.initialize.apply(this, arguments);
this.listenTo(this.model, 'change:width', this.changeWidth);
},
changeWidth: function() {
this.$('.mailpoet_resize_handle_text').text(this.model.get('width'));
},
onRender: function() { onRender: function() {
this.toolsView = new Module.ImageBlockToolsView({ model: this.model }); this.toolsView = new Module.ImageBlockToolsView({ model: this.model });
this.showChildView('toolsRegion', this.toolsView); this.showChildView('toolsRegion', this.toolsView);
this.$('.mailpoet_resize_handle').css('padding-right', '20px');
this.$('.mailpoet_resize_handle').css('cursor', 'ew-resize');
if (this.model.get('fullWidth')) { if (this.model.get('fullWidth')) {
this.$el.addClass('mailpoet_full_image'); this.$el.addClass('mailpoet_full_image');
@@ -82,9 +101,16 @@ define([
'change .mailpoet_field_image_full_width': _.partial(this.changeBoolCheckboxField, 'fullWidth'), 'change .mailpoet_field_image_full_width': _.partial(this.changeBoolCheckboxField, 'fullWidth'),
'change .mailpoet_field_image_alignment': _.partial(this.changeField, 'styles.block.textAlign'), 'change .mailpoet_field_image_alignment': _.partial(this.changeField, 'styles.block.textAlign'),
'click .mailpoet_field_image_select_another_image': 'showMediaManager', 'click .mailpoet_field_image_select_another_image': 'showMediaManager',
'click .mailpoet_done_editing': 'close' 'click .mailpoet_done_editing': 'close',
'input .mailpoet_field_image_width': _.partial(this.updateValueAndCall, '.mailpoet_field_image_width_input', _.partial(this.changePixelField, 'width').bind(this)),
'change .mailpoet_field_image_width': _.partial(this.updateValueAndCall, '.mailpoet_field_image_width_input', _.partial(this.changePixelField, 'width').bind(this)),
'input .mailpoet_field_image_width_input': _.partial(this.updateValueAndCall, '.mailpoet_field_image_width', _.partial(this.changePixelField, 'width').bind(this))
}; };
}, },
updateValueAndCall: function(fieldToUpdate, callable, event) {
this.$(fieldToUpdate).val(jQuery(event.target).val());
callable(event);
},
initialize: function(options) { initialize: function(options) {
base.BlockSettingsView.prototype.initialize.apply(this, arguments); base.BlockSettingsView.prototype.initialize.apply(this, arguments);

View File

@@ -1,3 +1,15 @@
<div class="mailpoet_tools"></div> <div class="mailpoet_tools"></div>
<div class="mailpoet_content" style="text-align: {{ model.styles.block.textAlign }}"><a href="{{ model.link }}" onClick="return false;"><img src="{{#ifCond model.src '!=' ''}}{{ model.src }}{{ else }}{{ imageMissingSrc }}{{/ifCond}}" alt="{{ model.alt }}" onerror="if(this.src != '{{ imageMissingSrc }}') {this.src = '{{ imageMissingSrc }}'; this.style.width='auto';}" /></a></div> <div class="mailpoet_content" style="text-align: {{ model.styles.block.textAlign }}">
<div class="mailpoet_image">
<a href="{{ model.link }}" onClick="return false;">
<img src="{{#ifCond model.src '!=' ''}}{{ model.src }}{{ else }}{{ imageMissingSrc }}{{/ifCond}}" alt="{{ model.alt }}" onerror="if(this.src != '{{ imageMissingSrc }}') {this.src = '{{ imageMissingSrc }}'; this.style.width='auto';}" width="{{model.width}}" />
</a>
<div class="mailpoet_resize_handle_container">
<div class="mailpoet_resize_handle">
<span class="mailpoet_resize_handle_text">{{ model.width }}</span>
<span class="mailpoet_resize_handle_icon"><%= source('newsletter/templates/svg/block-icons/horizontal-spacer.svg') %></span>
</div>
</div>
</div>
</div>
<div class="mailpoet_block_highlight"></div> <div class="mailpoet_block_highlight"></div>

View File

@@ -23,6 +23,15 @@
</div> </div>
</label> </label>
</div> </div>
<div class="mailpoet_form_field">
<label>
<div class="mailpoet_form_field_title"><%= __('Width') %></div>
<div class="mailpoet_form_field_input_option">
<input type="number" name="border-width-input" class="mailpoet_input mailpoet_input_small mailpoet_field_image_width_input" value="{{getNumber model.width}}" min="32" max="660" step="2" /> px
<input type="range" min="32" max="660" step="2" name="border-width" class="mailpoet_range mailpoet_range_small mailpoet_field_image_width" value="{{getNumber model.width}}" />
</div>
</label>
</div>
<div class="mailpoet_form_field"> <div class="mailpoet_form_field">
<div class="mailpoet_form_field_checkbox_option"> <div class="mailpoet_form_field_checkbox_option">
<label> <label>

View File

@@ -0,0 +1,4 @@
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 300 150" enable-background="new 0 0 300 150" xml:space="preserve">
<path d="M20,75L120,0L120,150ZM180,150L180,0L280,75Z"/>
</svg>

After

Width:  |  Height:  |  Size: 271 B