Refactor block highlight to be managed fully by highlight behavior

[MAILPOET-1819]
This commit is contained in:
Rostislav Wolny
2019-02-27 17:26:27 +01:00
committed by M. Shull
parent c526ba97e0
commit d69e0dea9e
5 changed files with 50 additions and 47 deletions

View File

@ -24,10 +24,6 @@ $block-text-line-height: $text-line-height;
z-index: 1; z-index: 1;
} }
&:hover > .mailpoet_block_highlight {
border: 2px solid $editor-content-color;
}
&.mailpoet_highlight > .mailpoet_block_highlight { &.mailpoet_highlight > .mailpoet_block_highlight {
border: 2px solid $editor-content-color !important; border: 2px solid $editor-content-color !important;
} }
@ -39,10 +35,6 @@ $block-text-line-height: $text-line-height;
left: -2px; left: -2px;
} }
&:hover > .mailpoet_container_horizontal ~ .mailpoet_block_highlight {
border: 2px solid $editor-column-color;
}
&.mailpoet_highlight > .mailpoet_container_horizontal ~ .mailpoet_block_highlight { &.mailpoet_highlight > .mailpoet_container_horizontal ~ .mailpoet_block_highlight {
border: 2px solid $editor-column-color !important; border: 2px solid $editor-column-color !important;
} }

View File

@ -1,30 +1,49 @@
/** /**
* Highlight Editing Behavior * Highlight Editing Behavior
* *
* Highlights a block,column that is being edited * Highlights a block,column that is being hovered by mouse or edited
*/ */
import Marionette from 'backbone.marionette'; import Marionette from 'backbone.marionette';
import BL from 'newsletter_editor/behaviors/BehaviorsLookup'; import BL from 'newsletter_editor/behaviors/BehaviorsLookup';
BL.HighlightEditingBehavior = Marionette.Behavior.extend({ BL.HighlightEditingBehavior = Marionette.Behavior.extend({
modelEvents: { modelEvents: {
startEditing: 'enableHighlight', startEditing: 'onStartEditing',
stopEditing: 'disableHighlight', stopEditing: 'onStopEditing',
}, },
enableHighlight: function enableHighlight() { events: {
this.view._isBeingEdited = true; mouseenter: 'onMouseEnter',
this.view.showTools(); mouseleave: 'onMouseLeave',
this.$el.addClass('mailpoet_highlight');
}, },
disableHighlight: function disableHighlight() { onMouseEnter: function onMouseEnter(mouseEvent) {
this.view._isBeingEdited = false; this.isFocusedByPointer = true;
this.view.hideTools(); // Ignore mouse events when dragging
this.$el.removeClass('mailpoet_highlight'); if (mouseEvent && mouseEvent.buttons > 0) {
return;
}
this.view.addHighlight();
},
onMouseLeave: function onMouseLeave() {
this.isFocusedByPointer = false;
// Ignore mouse events when item is being edited
if (this.isBeingEdited) {
return;
}
this.view.removeHighlight();
},
onStartEditing: function onStartEditing() {
this.isBeingEdited = true;
this.view.addHighlight();
},
onStopEditing: function onStopEditing() {
this.isBeingEdited = false;
if (!this.isFocusedByPointer) {
this.view.removeHighlight();
}
}, },
onDomRefresh: function onDomRefresh() { onDomRefresh: function onDomRefresh() {
if (this.view._isBeingEdited) { if (this.isBeingEdited) {
this.view.showTools(); this.view.addHighlight();
this.$el.addClass('mailpoet_highlight');
} }
}, },
}); });

View File

@ -59,10 +59,6 @@ Module.BlockView = AugmentedView.extend({
delete: 'deleteBlock', delete: 'deleteBlock',
duplicate: 'duplicateBlock', duplicate: 'duplicateBlock',
}, },
events: {
mouseenter: 'showTools',
mouseleave: 'hideTools',
},
behaviors: { behaviors: {
DraggableBehavior: { DraggableBehavior: {
cloneOriginal: true, cloneOriginal: true,
@ -105,20 +101,18 @@ Module.BlockView = AugmentedView.extend({
this.on('dom:refresh', this.showBlock, this); this.on('dom:refresh', this.showBlock, this);
this._isFirstRender = true; this._isFirstRender = true;
}, },
showTools: function showTools(mouseEvent) { addHighlight: function addHighlight() {
// Skip if user is dragging/resizing this.$el.addClass('mailpoet_highlight');
if (mouseEvent && mouseEvent.buttons > 0) {
return;
}
if (!this.showingToolsDisabled) { if (!this.showingToolsDisabled) {
this.$('> .mailpoet_tools').addClass('mailpoet_display_tools'); this.$('> .mailpoet_tools').addClass('mailpoet_display_tools');
this.toolsView.triggerMethod('showTools'); this.toolsView.triggerMethod('showTools');
} }
}, },
removeHighlight: function removeHighlight() {
this.$el.removeClass('mailpoet_highlight');
this.hideTools();
},
hideTools: function hideTools() { hideTools: function hideTools() {
if (this._isBeingEdited) {
return;
}
this.$('> .mailpoet_tools').removeClass('mailpoet_display_tools'); this.$('> .mailpoet_tools').removeClass('mailpoet_display_tools');
this.toolsView.triggerMethod('hideTools'); this.toolsView.triggerMethod('hideTools');
}, },

View File

@ -190,22 +190,16 @@ Module.ContainerBlockView = base.BlockView.extend({
this.$('> .mailpoet_container').attr('class', this.$('> .mailpoet_container').attr('class',
'mailpoet_container mailpoet_container_' + this.model.get('orientation') + ' ' + classIrregular); 'mailpoet_container mailpoet_container_' + this.model.get('orientation') + ' ' + classIrregular);
}, },
showTools: function (mouseEvent) { addHighlight: function addHighlight() {
// Skip if user is dragging/resizing if (this.renderOptions.depth !== 1 || this.$el.hasClass('mailpoet_container_layer_active')) {
if (mouseEvent && mouseEvent.buttons > 0) {
return; return;
} }
if (this.renderOptions.depth === 1 && !this.$el.hasClass('mailpoet_container_layer_active')) {
this.$(this.ui.tools).addClass('mailpoet_display_tools'); this.$(this.ui.tools).addClass('mailpoet_display_tools');
this.$el.addClass('mailpoet_highlight'); this.$el.addClass('mailpoet_highlight');
this.toolsView.triggerMethod('showTools'); this.toolsView.triggerMethod('showTools');
}
}, },
hideTools: function () { removeHighlight: function removeHighlight() {
if (this.renderOptions.depth !== 1 if (this.renderOptions.depth !== 1 || this.$el.hasClass('mailpoet_container_layer_active')) {
|| this.$el.hasClass('mailpoet_container_layer_active')
|| this._isBeingEdited
) {
return; return;
} }
this.$(this.ui.tools).removeClass('mailpoet_display_tools'); this.$(this.ui.tools).removeClass('mailpoet_display_tools');

View File

@ -278,6 +278,10 @@ describe('Container', function () {
var settingsView; var settingsView;
var blockView = new (ContainerBlock.ContainerBlockView)({ model: model }); var blockView = new (ContainerBlock.ContainerBlockView)({ model: model });
blockView.render(); blockView.render();
// Set proper depth since we want to highlight only top level containers
blockView.renderOptions = {
depth: 1,
};
expect(blockView.$el.hasClass('mailpoet_highlight')).to.equal(false); expect(blockView.$el.hasClass('mailpoet_highlight')).to.equal(false);
settingsView = new (ContainerBlock.ContainerBlockSettingsView)({ model: model }); settingsView = new (ContainerBlock.ContainerBlockSettingsView)({ model: model });
settingsView.render(); settingsView.render();