From 0d408824daca9a82e52251ce038da800bcf5c548 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Thu, 1 Mar 2018 09:37:56 +0100 Subject: [PATCH] editor: make context-menu work with multi selection (issue #156) --- src/browser/js/app/editor/context-menu.js | 176 +++++++++++++--------- src/browser/js/app/editor/edit-field.js | 4 +- src/browser/js/app/editor/index.js | 6 +- 3 files changed, 116 insertions(+), 70 deletions(-) diff --git a/src/browser/js/app/editor/context-menu.js b/src/browser/js/app/editor/context-menu.js index 2b74748d..845c269a 100644 --- a/src/browser/js/app/editor/context-menu.js +++ b/src/browser/js/app/editor/context-menu.js @@ -122,12 +122,6 @@ var handleClick = function(event) { if (!widget) return - var container = widget.container, - index = DOM.index(container), - data = widget.props, - type = widget.props.type == 'tab' ? 'tab' : 'widget' - - // if the widget is not already selected if (!widget.container.classList.contains('editing')) { // add a flag to the original event to prevent draginit @@ -138,9 +132,16 @@ var handleClick = function(event) { editor.select(widget, {multi: event.detail.ctrlKey}) - if (event.type!='fast-right-click') return + // right-click menu + if (event.type!='fast-right-click' || !editor.selectedWidgets.length) return - if (container.classList.contains('root-container')) { + var index = editor.selectedWidgets.map((w) => DOM.index(w.container)).sort().reverse(), + data = editor.selectedWidgets.map((w) => w.props), + type = editor.selectedWidgets[0].props.type == 'tab' ? 'tab' : 'widget', + parent = editor.selectedWidgets[0].parent + + // case root: only "add tab" option + if (widget.props.type === 'root') { contextMenu.open(eventData,{ ' Add tab': function(){ data.tabs.push({}) @@ -151,89 +152,123 @@ var handleClick = function(event) { return } + // case !root + var actions = {}, clickX = Math.round((eventData.offsetX + eventData.target.scrollLeft) / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH, clickY = Math.round((eventData.offsetY + eventData.target.scrollTop) / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH - if (widget.parent != widgetManager) { - actions[' Edit parent'] = function(){editor.select(widget.parent)} + actions[' Edit parent'] = ()=>{ + editor.select(parent) } - if (type=='widget') actions[' Copy'] = function(){CLIPBOARD=JSON.stringify(data)} + if (type === 'widget') { + + actions[' Copy'] = ()=>{ + CLIPBOARD = JSON.stringify(data) + } + + actions[' Cut'] = ()=>{ + CLIPBOARD = JSON.stringify(data) + for (var i of index) { + parent.props.widgets.splice(i,1) + } + updateWidget(parent) + } - if (type=='widget') actions[' Cut'] = function(){ - CLIPBOARD=JSON.stringify(data) - widget.parent.props.widgets.splice(index,1) - updateWidget(widget.parent) } - if (((type=='widget' && widgets[data.type].defaults().widgets) || (type=='tab')) && (!data.tabs||!data.tabs.length)) { - if (CLIPBOARD!=null) { + if (data.length == 1 && (!data[0].tabs || !data[0].tabs.length) && (data[0].widgets)) { + + if (CLIPBOARD !== null) { + + function paste(increment) { + + var pastedData = JSON.parse(CLIPBOARD), + minTop = Infinity, + minLeft = Infinity + + for (var i in pastedData) { + pastedData[i] = incrementWidget(pastedData[i]) + if (!isNaN(pastedData[i]).top && pastedData[i].top < minTop) { + minTop = pastedData[i].top + } + if (!isNaN(pastedData[i]).left && pastedData[i].left < minLeft) { + minLeft = pastedData[i].left + } + } + + var keepPosition = eventData.target.classList.contains('tablink') + for (var i in pastedData) { + + if (!keepPosition) { + if (!isNaN(pastedData[i].top)) pastedData[i].top = pastedData[i].top - minTop + clickY + if (!isNaN(pastedData[i].left)) pastedData[i].left = pastedData[i].left - minLeft + clickX + } + + } + + data[0].widgets = data[0].widgets || [] + data[0].widgets = data[0].widgets.concat(pastedData) + + updateWidget(editor.selectedWidgets[0]) + + } + actions[' Paste'] = { - ' ID + 1':function(){ - data.widgets = data.widgets || [] - var newData = incrementWidget(JSON.parse(CLIPBOARD)) - - - if (!eventData.target.classList.contains('tablink')) { - newData.top = clickY - newData.left= clickX - } else { - delete newData.top - delete newData.left - } - - data.widgets.push(newData) - updateWidget(widget) + ' ID + 1': ()=>{ + paste(true) }, - ' Clone':function(){ - data.widgets = data.widgets || [] - var newData = JSON.parse(CLIPBOARD) - if (!eventData.target.classList.contains('tablink')) { - newData.top = clickY - newData.left= clickX - } else { - delete newData.top - delete newData.left - } - data.widgets.push(newData) - updateWidget(widget) + ' Clone': ()=>{ + paste() } } + } actions[' Add widget'] = {} + for (let category in categories) { + actions[' Add widget'][category] = {} + for (let t in categories[category]) { - let wtype = categories[category][t] - actions[' Add widget'][category][wtype] = function(){ - data.widgets = data.widgets || [] - var newData = {type:wtype} - if (!eventData.target.classList.contains('tablink')) { - newData.top = clickY - newData.left= clickX - } - data.widgets.push(newData) - updateWidget(widget) + + let type = categories[category][t] + + actions[' Add widget'][category][type] = ()=>{ + + var newData = {type: type} + + if (!eventData.target.classList.contains('tablink')) { + newData.top = clickY + newData.left= clickX + } + + data[0].widgets = data[0].widgets || [] + data[0].widgets.push(newData) + updateWidget(editor.selectedWidgets[0]) } } - } - } - if (((type=='widget' && widgets[data.type].defaults().tabs) || (type=='tab')) && (!data.widgets||!data.widgets.length)) { - - actions[' Add tab'] = function(){ - data.tabs = data.tabs || [] - data.tabs.push({}) - updateWidget(widget) } } - actions[' Delete'] = function(){ + if (data.length == 1 && (!data[0].widgets || !data[0].widgets.length) && (data[0].tabs)) { + + actions[' Add tab'] = ()=>{ + data[0].tabs = data[0].tabs || [] + data[0].tabs.push({}) + updateWidget(editor.selectedWidgets[0]) + } + + } + + actions[' Delete'] = ()=>{ + var popup = new Popup({ title: 'Are you sure ?', content:` @@ -246,16 +281,21 @@ var handleClick = function(event) { enterKey: function(){DOM.get(this.html, '.confirm-delete')[0].click()} }) - DOM.get(popup.html, '.confirm-delete')[0].addEventListener('click', function(){ + DOM.get(popup.html, '.confirm-delete')[0].addEventListener('click', ()=>{ + popup.close() - if (widget.props.type != 'tab') { - widget.parent.props.widgets.splice(index,1) + if (type === 'widget') { + for (var i of index) { + parent.props.widgets.splice(i,1) + } } else { - widget.parent.props.tabs.splice(index,1) + for (var i of index) { + parent.props.tabs.splice(i,1) + } } - updateWidget(widget.parent) + updateWidget(parent) }) DOM.get(popup.html, '.cancel-delete')[0].addEventListener('click', function(){ diff --git a/src/browser/js/app/editor/edit-field.js b/src/browser/js/app/editor/edit-field.js index 5e661617..a34760c6 100644 --- a/src/browser/js/app/editor/edit-field.js +++ b/src/browser/js/app/editor/edit-field.js @@ -88,10 +88,12 @@ module.exports = function editField(editor, widget, propName, defaultValue){ v = input.value } + var newWidgets = [] for (var w of editor.selectedWidgets) { w.props[propName] = v !== '' ? v : JSON.parse(JSON.stringify(defaultValue)) - updateWidget(w) + newWidgets.push(updateWidget(w)) } + 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 84607a3f..447ca397 100644 --- a/src/browser/js/app/editor/index.js +++ b/src/browser/js/app/editor/index.js @@ -116,7 +116,11 @@ var Editor = class Editor { select(widget, options={}){ - if (options.multi) { + if (Array.isArray(widget)) { + + this.selectedWidgets = widget + + } else if (options.multi) { if (!this.selectedWidgets.includes(widget)) {