diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b9e98d3..488ea3b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - editor - tree: add icons before widget ids depending on the category - tree: activate tab when selecting it in the project tree + - tree: allow dragging widgets from a container to another - smarter shift+drag selection: allow selecting widgets in the area when the event started on the parent container - remote control diff --git a/src/client/editor/index.js b/src/client/editor/index.js index d37b3d08..b90a30dd 100644 --- a/src/client/editor/index.js +++ b/src/client/editor/index.js @@ -80,25 +80,40 @@ class Editor { this.widgetTree = new UiTree({selector: '#osc-tree', parent: leftUiSidePanel}) this.widgetTree.on('sorted', (event)=>{ - var {widget, oldIndex, newIndex} = event, - propName = widget.props.tabs && widget.props.tabs.length ? 'tabs' : 'widgets' + var {oldIndex, newIndex, from, to} = event, + propName = from.childrenType + 's' - if (widget.props[propName].length < 2) return + if (from === to) { - widget.props[propName].splice(newIndex, 0, widget.props[propName].splice(oldIndex, 1)[0]) + if (to.props[propName].length < 2) return - var indices = [newIndex, oldIndex] - if (Math.abs(oldIndex - newIndex) > 1) { - for (var i = Math.min(newIndex, oldIndex) + 1; i < Math.max(newIndex, oldIndex); i++) { - indices.push(i) + to.props[propName].splice(newIndex, 0, to.props[propName].splice(oldIndex, 1)[0]) + + var indices = [newIndex, oldIndex] + if (Math.abs(oldIndex - newIndex) > 1) { + for (var i = Math.min(newIndex, oldIndex) + 1; i < Math.max(newIndex, oldIndex); i++) { + indices.push(i) + } } + + var container = updateWidget(to, {removedIndexes: indices, addedIndexes: indices, preventSelect: true}) + + this.pushHistory({removedIndexes: indices, addedIndexes: indices}) + this.select(container.children[newIndex]) + + } else { + + to.props[propName].splice(newIndex, 0, from.props[propName][oldIndex]) + from.props[propName].splice(oldIndex, 1) + + updateWidget(from, {removedIndexes: [oldIndex], preventSelect: true}) + var container = updateWidget(to, {addedIndexes: [newIndex], preventSelect: true}) + + this.pushHistory() + this.select(container.children[newIndex]) + } - var container = updateWidget(widget, {removedIndexes: indices, addedIndexes: indices, preventSelect: true}) - - this.pushHistory({removedIndexes: indices, addedIndexes: indices}) - this.select(container.children[newIndex]) - }) this.oscContainer = DOM.get('#osc-container')[0] diff --git a/src/client/ui/ui-tree.js b/src/client/ui/ui-tree.js index 78818d5e..d4b7df4e 100644 --- a/src/client/ui/ui-tree.js +++ b/src/client/ui/ui-tree.js @@ -100,13 +100,19 @@ class UiTree extends UiWidget { this.sortables = [] this.sortCallback = (event)=>{ - var widget = widgetManager.widgets[event.to.parentNode.getAttribute('data-widget')] - if (!widget || event.oldIndex === event.newIndex) return + + var from = widgetManager.widgets[event.from.getAttribute('data-hash')], + to = widgetManager.widgets[event.to.getAttribute('data-hash')] + + if (!to || (from === to && event.oldIndex === event.newIndex)) return + this.trigger('sorted', { - widget: widget, oldIndex: event.oldIndex, - newIndex: event.newIndex + newIndex: event.newIndex, + from: from, + to: to }) + } this.filter.addEventListener('change', ()=>{ @@ -172,9 +178,9 @@ class UiTree extends UiWidget { this.sortables.push(new Sortable(el, { group: { - name: 'group-' + el.parentNode.getAttribute('data-widget'), - pull: false, - put: false + name: 'group', + pull: (to, from)=>from.el.dataset.childrenType === to.el.dataset.childrenType || to.el.dataset.childrenType === '', + put: (to, from)=>from.el.dataset.childrenType === to.el.dataset.childrenType || to.el.dataset.childrenType === '', }, onEnd: this.sortCallback, setData: (dataTransfer)=>{ @@ -244,7 +250,7 @@ class UiTree extends UiWidget { this.expanded[widget.hash] = true init = true } - var sublist = node.appendChild(html`
    `) + var sublist = node.appendChild(html`
      `) for (let child of widget.children) { if (child) sublist.appendChild(this.parseWidgets(child, selectedWidgets, depth)) }