Skip to content

Commit

Permalink
Preliminary support for data-ko-height (vertical resizer)
Browse files Browse the repository at this point in the history
  • Loading branch information
bago committed Apr 27, 2022
1 parent 5cfb024 commit b91bca4
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"jquery-ui/ui/widgets/sortable": "global:jQuery.ui.sortable",
"jquery-ui/ui/widgets/draggable": "global:jQuery.ui.draggable",
"jquery-ui/ui/widgets/droppable": "global:jQuery.ui.droppable",
"jquery-ui/ui/widgets/resizable": "global:jQuery.ui.resizable",
"jquery-ui/ui/widgets/spinner": "global:jQuery.ui.spinner",
"jquery-ui/ui/widgets/tabs": "global:jQuery.ui.tabs",
"knockout": "global:ko",
Expand Down
78 changes: 77 additions & 1 deletion src/css/style_mosaico_content.less
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,82 @@
visibility: visible;
}

#main-edit-area .isresizing .tools {
visibility: hidden !important;
}

// resizable
#main-edit-area .ui-resizable {
position: relative;
}
#main-edit-area .ui-resizable-container {
position: relative;
}
#main-edit-area .ui-resizable.ui-resizing-div {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;

&.ui-resizable-resizing:before {
display: block;
}

&:before {
content: attr(data-size);
display: none;
position: absolute;
text-align: center;
top: 50%;
margin-top: -6px;
bottom: 0;
left: 0;
right: 0;
font-size: 12px;
line-height: 12px;
color: black;
}
}

#main-edit-area .ui-resizable-handle.ui-resizable-s {
.button-style();
margin: 0;
padding: 0;
min-height: 0;
display: none;
position: absolute;
bottom: -8px;
height: 16px;
border-radius: 8px;
left: 50%;
width: 50px;
margin-left: -25px;
box-shadow: 0 0 5px @mosaico-button-shadow-color;

&:before {
content: "\f107";
position: relative;
font-size: 16px;
line-height: 16px;
display: block;
font-family: FontAwesome;
text-align: center;
cursor: row-resize;
// color: #eee;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
}

#main-edit-area .ui-resizable:hover .ui-resizable-handle.ui-resizable-s,
#main-edit-area .ui-resizable .ui-resizable-handle:hover {
display: block;
}


#main-edit-area .ui-sortable-helper > *,
#main-edit-area .ui-sortable-helper .tools > * {
opacity: .3;
Expand Down Expand Up @@ -307,7 +383,7 @@
}

// selectable elements
#main-wysiwyg-area:not(.isdragging):not(.isdraggingimg) {
#main-wysiwyg-area:not(.isdragging):not(.isdraggingimg):not(.isresizing) {
// blocks
.editable {
.makeSelectable(10px, ~'.selected', 0, ~' .mo-blockselectionhelper');
Expand Down
103 changes: 103 additions & 0 deletions src/js/bindings/extresizable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
;(function(factory) {
if (typeof define === "function" && define.amd) {
// AMD anonymous module
define(["knockout", "jquery", "jquery-ui/ui/widgets/resizable"], factory);
} else if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS module
var ko = require("knockout"),
jQuery = require("jquery");
require("jquery-ui/ui/widgets/resizable");
factory(ko, jQuery);
} else {
// No module loader (plain <script> tag) - put directly in global namespace
factory(window.ko, window.jQuery);
}
})(function(ko, $) {

var unwrap = ko.utils.unwrapObservable;

// Simple Resizable Implementation
// binding that updates (function or observable)
ko.bindingHandlers.extresizable = {
init: function(element, valueAccessor, allBindingsAccessor, data, bindingContext) {
var value = unwrap(valueAccessor()) || {},
options = value.options || {},
resizableOptions = ko.utils.extend({}, ko.bindingHandlers.extresizable.options),
isEnabled = value.isEnabled !== undefined ? value.isEnabled : ko.bindingHandlers.extresizable.isEnabled;

// TODO this is a Mosaico specific handling while this could be a very generic binding.
var isWysiwygMode = (typeof bindingContext.templateMode != 'undefined' && bindingContext.templateMode == 'wysiwyg');

if (isWysiwygMode) {

//get reference to drop method
value = "data" in value ? value.data : valueAccessor();


// TODO questi devono venire da parametri
options.minHeight = 2;
options.maxHeight = 1000;
options.autoHide = true;
// options.helper = $("<div/>");
// options.ghost = true;
options.start = function(event, ui) {
console.log("start");
if (typeof options.resizing == 'function') options.resizing(true);
ko.utils.toggleDomNodeCssClass(element, "resizable-resizing", true);
/*
rootEl.focus();
addMovingClass('handle');
originalOuterTop = cropModel.container.top;
originalMethod = getCurrentComputedSizes().method;
maxHeight = getScaledImageSize().height;
*/
};
options.stop = function(event, ui) {
console.log("stop");
if (typeof options.resizing == 'function') options.resizing(false);
ko.utils.toggleDomNodeCssClass(element, "resizable-resizing", false);
event.target.style.height = "auto";
// is this really needed?
event.preventDefault();
/*
removeMovingClass();
changed("resized");
*/
};
options.resize = function(event, ui) {
console.log("resize", ui.size.height, ui.originalSize.height, value());
value(ui.size.height);
ui.size.height = value();
};

//override global options with override options passed in
ko.utils.extend(resizableOptions, options);

//initialize resizable
$(element).resizable(resizableOptions);
console.log(element, "resizable init");

//handle enabling/disabling resizable
if (isEnabled !== undefined) {
ko.computed({
read: function() {
$(element).resizable(unwrap(isEnabled) ? "enable": "disable");
console.log(element, "resizable enabled read");
},
disposeWhenNodeIsRemoved: element
});
}

//handle disposal
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$(element).resizable("destroy");
console.log(element, "resizable destroy");
});

}
},
options: {
}
};

});
19 changes: 19 additions & 0 deletions src/js/converter/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,25 @@ var processBlock = function(element, defs, themeUpdater, blockPusher, templateUr
domutils.removeAttribute(element, 'data-ko-link');
});

// should be only used on resizable items, like TD.
$("[data-ko-height]", element).each(function(index, element) {
var heightVar = domutils.getAttribute(element, 'data-ko-height');

// var bindingValue = bindingProvider(heightVar, value, false, 'wysiwyg');
var bindingValue = bindingProvider(heightVar);
var resizingBinding = "extresizable: { data: " + bindingValue + ", options: { handles: 's', resizing: $root.resizing } }, attr: { 'data-size': ko.utils.unwrapObservable(" + bindingValue + ")+'px' } ";

var resizingDiv = $('<div class="ui-resizing-div" data-ko-wrap="false" style="width: 100%; height: 100%"></div>')[0];
domutils.setAttribute(resizingDiv, 'data-bind', resizingBinding);
$(element).append(resizingDiv);

var newBinding = "wysiwygCss: { 'ui-resizable-container': true }";
var currentBindings = domutils.getAttribute(element, 'data-bind');
var dataBind = (currentBindings !== null ? currentBindings + ", " : "") + newBinding;
domutils.setAttribute(element, 'data-bind', dataBind);
domutils.removeAttribute(element, 'data-ko-height');
});

$("[replacedstyle]", element).each(function(index, element) {
processStyle(element, templateUrlConverter, bindingProvider, false);
});
Expand Down
1 change: 1 addition & 0 deletions src/js/ko-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require("./bindings/wysiwygs.js");
require("./bindings/scrollfix.js");
require("./bindings/if-subs.js");
require("./bindings/extsortables.js");
require("./bindings/extresizable.js");
require("./bindings/eventable.js");
require("./bindings/tooltips.js");
require("./bindings/extender-pagination.js");
Expand Down
1 change: 1 addition & 0 deletions src/js/viewmodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function initializeEditor(content, blockDefs, thumbPathConverter, galleryUrl) {
selectedTool: ko.observable(0),
selectedImageTab: ko.observable(0),
dragging: ko.observable(false),
resizing: ko.observable(false),
draggingImage: ko.observable(false),
galleryLoaded: ko.observable(false),
showPreviewFrame: ko.observable(false),
Expand Down
2 changes: 1 addition & 1 deletion src/tmpl/main.tmpl.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div id="page" style="display: none;" data-bind="visible: true, css: { withToolbox: $root.showToolbox, withPreviewFrame: showPreviewFrame }">
<div id="main-edit-area" data-bind="click: function(obj, evt) { $root.selectBlock(null); return true; }, clickBubble: false">
<!-- ko withProperties: { templateMode: 'wysiwyg', templateModeFallback: 'show' } -->
<div id="main-wysiwyg-area" data-bind="wysiwygScrollfix: true, scrollable: true, fudroppable: { active: draggingImage }, css: { isdragging: dragging, isdraggingimg: draggingImage }, block: content"></div>
<div id="main-wysiwyg-area" data-bind="wysiwygScrollfix: true, scrollable: true, fudroppable: { active: draggingImage }, css: { isdragging: dragging, isdraggingimg: draggingImage, isresizing: resizing }, block: content"></div>
<!-- /ko -->
</div>

Expand Down

0 comments on commit b91bca4

Please sign in to comment.