tree: allow dragging widgets from a container to another

This commit is contained in:
bordun 2021-04-26 18:44:43 +02:00
parent 7ea95702e8
commit 8568c72a8b
3 changed files with 43 additions and 21 deletions

View file

@ -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]

View file

@ -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`<ol style="--depth:${++depth};"></ol>`)
var sublist = node.appendChild(html`<ol data-hash="${widget.hash}" data-children-type="${widget.childrenType}" style="--depth:${++depth};"></ol>`)
for (let child of widget.children) {
if (child) sublist.appendChild(this.parseWidgets(child, selectedWidgets, depth))
}