From c85b59629f818f8609478987fbcd5e6645f38e5f Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Mon, 5 Mar 2018 12:05:26 +0100 Subject: [PATCH] editor: allow multi widget drag/resize (#190) --- CHANGELOG.md | 2 + resources/docs/editing-sessions.md | 4 +- src/browser/js/app/editor/data-workers.js | 2 +- src/browser/js/app/editor/edit-field.js | 2 +- src/browser/js/app/editor/index.js | 106 +++++++++++++--------- 5 files changed, 68 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e6a9d78..44a301e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ - mac: disabled unsupported pinch zoom - themes - alt-buttons: modal font-weight & color fix +- editor + - allow multi widget resizing/dragging ## 0.25.0 diff --git a/resources/docs/editing-sessions.md b/resources/docs/editing-sessions.md index 914fff20..0d7de3db 100644 --- a/resources/docs/editing-sessions.md +++ b/resources/docs/editing-sessions.md @@ -41,7 +41,7 @@ Properties are written in JSON, with some flexibility brought by the [JSON5](htt ## Widget resizing / dragging -Selected widget can be resized using its south, south-east and east handles. It can be dragged with its north-west handle. +Selected widget can be resized using its south, south-east and east handles. It can be dragged with its north-west handle. Using this feature will convert position and size values to plain number values, thus breaking previously set percent or inherited values. ## Bulk editing @@ -49,4 +49,4 @@ When holding the `ctrl` key, multiple widgets can be selected for edition: - they must share the same parent widget - context-menu actions and properties changes apply to all selected widgets -- resizing / dragging will not be available +- resizing / dragging will affect all selected widgets, relatively to the first selected widget diff --git a/src/browser/js/app/editor/data-workers.js b/src/browser/js/app/editor/data-workers.js index af264fc9..e5c04047 100644 --- a/src/browser/js/app/editor/data-workers.js +++ b/src/browser/js/app/editor/data-workers.js @@ -60,7 +60,7 @@ var updateWidget = function(widget, options = {}) { } } - if (!options.remote || editing) { + if (!options.preventSelect && editing) { editor.select(newWidget) } diff --git a/src/browser/js/app/editor/edit-field.js b/src/browser/js/app/editor/edit-field.js index 4dfd715a..16f29c05 100644 --- a/src/browser/js/app/editor/edit-field.js +++ b/src/browser/js/app/editor/edit-field.js @@ -91,7 +91,7 @@ module.exports = function editField(editor, widget, propName, defaultValue){ var newWidgets = [] for (var w of editor.selectedWidgets) { w.props[propName] = v !== '' ? v : JSON.parse(JSON.stringify(defaultValue)) - newWidgets.push(updateWidget(w)) + newWidgets.push(updateWidget(w, {preventSelect: editor.selectedWidgets.length > 1})) } if (newWidgets.length > 1) editor.select(newWidgets) diff --git a/src/browser/js/app/editor/index.js b/src/browser/js/app/editor/index.js index 74f6e36f..e90ff27a 100644 --- a/src/browser/js/app/editor/index.js +++ b/src/browser/js/app/editor/index.js @@ -233,56 +233,74 @@ var Editor = class Editor { }) } - if (this.selectedWidgets.length == 1) { - var widget = this.selectedWidgets[0] - if (widget.props.height !== undefined || widget.props.width !== undefined) { + var widget = this.selectedWidgets[0] - var handleTarget - var $container = $(widget.container) - $container.resizable({ - handles: 's, e, se', - helper: "ui-helper", - start: (event, ui)=>{ - handleTarget = $(event.originalEvent.target) - }, - resize: (event, ui)=>{ - ui.size.height = Math.round(ui.size.height / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH * PXSCALE - ui.size.width = Math.round(ui.size.width / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH * PXSCALE - }, - stop: (event, ui)=>{ - if (handleTarget.hasClass('ui-resizable-se') || handleTarget.hasClass('ui-resizable-s')) widget.props.height = Math.round((Math.max(ui.size.height,1)) / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH - if (handleTarget.hasClass('ui-resizable-se') || handleTarget.hasClass('ui-resizable-e')) widget.props.width = Math.round(ui.size.width / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH - updateWidget(widget) - }, - grid: [GRIDWIDTH * PXSCALE, GRIDWIDTH * PXSCALE] - }) + if (widget.props.height !== undefined || widget.props.width !== undefined) { - } + var handleTarget + var $container = $(widget.container) + $container.resizable({ + handles: 's, e, se', + helper: "ui-helper", + start: (event, ui)=>{ + handleTarget = $(event.originalEvent.target) + }, + resize: (event, ui)=>{ + ui.size.height = Math.round(ui.size.height / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH * PXSCALE + ui.size.width = Math.round(ui.size.width / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH * PXSCALE + }, + stop: (event, ui)=>{ + var deltaH = ui.size.height - ui.originalSize.height, + deltaW = ui.size.width - ui.originalSize.width - if (widget.props.top !== undefined) { - var $container = $(widget.container) - $container.draggable({ - cursor:'-webkit-grabbing', - drag: (event, ui)=>{ - ui.position.top = Math.round(ui.position.top / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH * PXSCALE - ui.position.left = Math.round(ui.position.left / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH * PXSCALE - }, - stop: (event, ui)=>{ - event.preventDefault() - widget.props.top = (ui.helper.position().top + $container.parent().scrollTop())/PXSCALE - widget.props.left = (ui.helper.position().left + $container.parent().scrollLeft())/PXSCALE - ui.helper.remove() - updateWidget(widget) - }, - handle:'.ui-draggable-handle, > .label', - grid: [GRIDWIDTH * PXSCALE, GRIDWIDTH * PXSCALE], - helper:()=>{ - return $('
').css({height:$container.outerHeight(),width:$container.outerWidth()}) + var newWidgets = [] + for (var w of this.selectedWidgets) { + var originalW = w === widget ? ui.originalSize.width : w.container.offsetWidth, + originalH = w === widget ? ui.originalSize.height : w.container.offsetHeight + + if (w.props.width !== undefined) w.props.width = Math.max((originalW + deltaW), GRIDWIDTH) / PXSCALE + if (w.props.height !== undefined) w.props.height = Math.max((originalH + deltaH), GRIDWIDTH) / PXSCALE + if (w.props.width !== undefined || w.props.height !== undefined) newWidgets.push(updateWidget(w)) } - }).append('
') + if (newWidgets.length > 1) editor.select(newWidgets, {preventSelect: this.selectedWidgets.length > 1}) + }, + grid: [GRIDWIDTH * PXSCALE, GRIDWIDTH * PXSCALE] + }) + + } + + if (widget.props.top !== undefined) { + var $container = $(widget.container) + $container.draggable({ + cursor:'-webkit-grabbing', + drag: (event, ui)=>{ + ui.position.top = Math.round(ui.position.top / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH * PXSCALE + ui.position.left = Math.round(ui.position.left / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH * PXSCALE + }, + stop: (event, ui)=>{ + event.preventDefault() + + var deltaX = (ui.helper.position().left + $container.parent().scrollLeft()) / PXSCALE - widget.container.offsetLeft / PXSCALE, + deltaY = (ui.helper.position().top + $container.parent().scrollTop()) / PXSCALE - widget.container.offsetTop / PXSCALE + + var newWidgets = [] + for (var w of this.selectedWidgets) { + w.props.top = w.container.offsetTop / PXSCALE + deltaY + w.props.left = w.container.offsetLeft / PXSCALE + deltaX + newWidgets.push(updateWidget(w, {preventSelect: this.selectedWidgets.length > 1})) + } + if (newWidgets.length > 1) editor.select(newWidgets) + + ui.helper.remove() + }, + handle:'.ui-draggable-handle, > .label', + grid: [GRIDWIDTH * PXSCALE, GRIDWIDTH * PXSCALE], + helper:()=>{ + return $('
').css({height:$container.outerHeight(),width:$container.outerWidth()}) + } + }).append('
') - } }