From c91a7b7b810791921544a45182cfe4ba4ef9ad31 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Wed, 14 Mar 2018 16:17:55 +0100 Subject: [PATCH] editor: preverve pecentages when dragging/resizing a widget that has its position/size written in percents (#216) --- CHANGELOG.md | 1 + src/browser/js/app/editor/index.js | 36 ++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eae4ddc1..d6ec7802 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - wrap in: insert wrapper at selected widget's index - wrap in: set label to false by default when wrapping in a strip or a panel - css property `pointer-events:none;` is now ignored when the editor is enabled + - preverve pecentages when dragging/resizing a widget that has its position/size written in percents - widgets - **new** `OSC{/address, default_value}` syntax - ui diff --git a/src/browser/js/app/editor/index.js b/src/browser/js/app/editor/index.js index 2e81858d..62dd5169 100644 --- a/src/browser/js/app/editor/index.js +++ b/src/browser/js/app/editor/index.js @@ -263,8 +263,24 @@ var Editor = class Editor { 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) { + var newWidth = Math.max((originalW + deltaW), GRIDWIDTH) / PXSCALE + if (typeof w.props.width === 'string' && w.props.width.indexOf('%') > -1) { + w.props.width = (100 * PXSCALE * newWidth / w.container.parentNode.offsetWidth).toFixed(2) + '%' + } else { + w.props.width = newWidth + } + } + + if (w.props.height !== undefined) { + var newHeight = Math.max((originalH + deltaH), GRIDWIDTH) / PXSCALE + if (typeof w.props.height === 'string' && w.props.height.indexOf('%') > -1) { + w.props.height = (100 * PXSCALE * newHeight / w.container.parentNode.offsetHeight).toFixed(2) + '%' + } else { + w.props.height = newHeight + } + } + if (w.props.width !== undefined || w.props.height !== undefined) newWidgets.push(updateWidget(w)) } if (newWidgets.length > 1) editor.select(newWidgets, {preventSelect: this.selectedWidgets.length > 1}) @@ -290,8 +306,20 @@ var Editor = class Editor { var newWidgets = [] for (var w of this.selectedWidgets) { - w.props.top = w.container.offsetTop / PXSCALE + deltaY - w.props.left = w.container.offsetLeft / PXSCALE + deltaX + + var newTop = w.container.offsetTop / PXSCALE + deltaY + if (typeof w.props.top === 'string' && w.props.top.indexOf('%') > -1) { + w.props.top = (100 * PXSCALE * newTop / w.container.parentNode.offsetHeight).toFixed(2) + '%' + } else { + w.props.top = newTop + } + var newLeft = w.container.offsetLeft / PXSCALE + deltaX + if (typeof w.props.left === 'string' && w.props.left.indexOf('%') > -1) { + w.props.left = (100 * PXSCALE * newLeft / w.container.parentNode.offsetWidth).toFixed(2) + '%' + } else { + w.props.left = newLeft + } + newWidgets.push(updateWidget(w, {preventSelect: this.selectedWidgets.length > 1})) } if (newWidgets.length > 1) editor.select(newWidgets)