提交 23e6b3ab 编写于 作者: J Jason Park 提交者: Jason

Enable dragging sections

上级 e7a3bbe9
...@@ -37,10 +37,10 @@ class Droppable extends React.Component { ...@@ -37,10 +37,10 @@ class Droppable extends React.Component {
const { onDropTab, onDropSection } = this.props; const { onDropTab, onDropSection } = this.props;
switch (data.type) { switch (data.type) {
case 'tab': case 'tab':
onDropTab(data.tab); onDropTab && onDropTab(data.tab);
break; break;
case 'section': case 'section':
onDropSection(data.section); onDropSection && onDropSection(data.section);
break; break;
} }
} }
......
...@@ -23,18 +23,26 @@ class WSSectionContainer extends React.Component { ...@@ -23,18 +23,26 @@ class WSSectionContainer extends React.Component {
handleDropTabToContainer(tab, index) { handleDropTabToContainer(tab, index) {
const tabContainer = new TabContainer(); const tabContainer = new TabContainer();
this.core.addChild(tabContainer, index); this.handleDropSectionToContainer(tabContainer, index);
tabContainer.addChild(tab); tabContainer.addChild(tab);
} }
handleDropSectionToContainer(section, index) {
this.core.addChild(section, index);
}
handleDropTabToSection(tab, index, before = false) { handleDropTabToSection(tab, index, before = false) {
const child = this.core.children[index];
const tabContainer = new TabContainer(); const tabContainer = new TabContainer();
this.handleDropSectionToSection(tabContainer, index, before);
tabContainer.addChild(tab);
}
handleDropSectionToSection(section, index, before = false) {
const child = this.core.children[index];
const sectionContainer = new SectionContainer({ horizontal: !this.core.horizontal }); const sectionContainer = new SectionContainer({ horizontal: !this.core.horizontal });
this.core.addChild(sectionContainer, index); this.core.addChild(sectionContainer, index);
sectionContainer.addChild(child); sectionContainer.addChild(child);
sectionContainer.addChild(tabContainer, before ? 0 : 1); sectionContainer.addChild(section, before ? 0 : 1);
tabContainer.addChild(tab);
} }
render() { render() {
...@@ -54,41 +62,41 @@ class WSSectionContainer extends React.Component { ...@@ -54,41 +62,41 @@ class WSSectionContainer extends React.Component {
elements.push( elements.push(
<Divider key={`divider-${i}`} horizontal={horizontal} <Divider key={`divider-${i}`} horizontal={horizontal}
onResize={(target, x, y) => this.handleResize(visibleIndex - 1, target, x, y)} onResize={(target, x, y) => this.handleResize(visibleIndex - 1, target, x, y)}
onDropTab={tab => this.handleDropTabToContainer(tab, i)} /> onDropTab={tab => this.handleDropTabToContainer(tab, i)}
); onDropSection={section => this.handleDropSectionToContainer(section, i)} />
}
if (child instanceof SectionContainer || visibleChildren.length === 1) {
elements.push(
<div key={child.key} className={styles.wrapper} style={style}>
{child.element}
</div>
);
} else {
elements.push(
<div key={child.key} className={classes(styles.wrapper, !horizontal && styles.horizontal)}
style={style}>
<Divider horizontal={!horizontal} onDrop={data => this.handleDropToSection(data, i, true)} />
{child.element}
<Divider horizontal={!horizontal} onDrop={data => this.handleDropToSection(data, i)} />
</div>
); );
} }
elements.push(
<div key={child.key} className={classes(styles.wrapper, !horizontal && styles.horizontal)}
style={style}>
<Divider horizontal={!horizontal}
onDropTab={tab => this.handleDropTabToSection(tab, i, true)}
onDropSection={section => this.handleDropSectionToSection(section, i, true)} />
{child.element}
<Divider horizontal={!horizontal}
onDropTab={tab => this.handleDropTabToSection(tab, i)}
onDropSection={section => this.handleDropSectionToSection(section, i)} />
</div>
);
}); });
if (elements.length) { if (elements.length) {
const firstIndex = children.indexOf(visibleChildren[0]); const firstIndex = children.indexOf(visibleChildren[0]);
const lastIndex = children.indexOf(visibleChildren[visibleChildren.length - 1]); const lastIndex = children.indexOf(visibleChildren[visibleChildren.length - 1]);
elements.unshift( elements.unshift(
<Divider key="divider-first" horizontal={horizontal} <Divider key="divider-first" horizontal={horizontal}
onDropTab={tab => this.handleDropTabToContainer(tab, firstIndex)} /> onDropTab={tab => this.handleDropTabToContainer(tab, firstIndex)}
onDropSection={section => this.handleDropSectionToContainer(section, firstIndex)} />
); );
elements.push( elements.push(
<Divider key="divider-last" horizontal={horizontal} <Divider key="divider-last" horizontal={horizontal}
onDropTab={tab => this.handleDropTabToContainer(tab, lastIndex + 1)} /> onDropTab={tab => this.handleDropTabToContainer(tab, lastIndex + 1)}
onDropSection={section => this.handleDropSectionToContainer(section, lastIndex + 1)} />
); );
} else { } else {
elements.push( elements.push(
<Droppable key="empty" className={styles.wrapper} droppingClassName={styles.dropping} <Droppable key="empty" className={styles.wrapper} droppingClassName={styles.dropping}
onDropTab={tab => this.handleDropTabToContainer(tab)} /> onDropTab={tab => this.handleDropTabToContainer(tab)}
onDropSection={section => this.handleDropSectionToContainer(section)} />
); );
} }
......
...@@ -26,7 +26,7 @@ class WSTabContainer extends React.Component { ...@@ -26,7 +26,7 @@ class WSTabContainer extends React.Component {
e.stopPropagation(); e.stopPropagation();
draggingData.set(e, { draggingData.set(e, {
type: 'section', type: 'section',
section: this.section, section: this.core,
}) })
} }
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
&.fake { &.fake {
&:first-child { &:first-child {
flex-shrink: 0;
width: $line-height / 2; width: $line-height / 2;
} }
......
...@@ -22,13 +22,13 @@ class TabContainer extends parentMixin(Section) { ...@@ -22,13 +22,13 @@ class TabContainer extends parentMixin(Section) {
} }
addChild(child, index = this.children.length) { addChild(child, index = this.children.length) {
this.tabIndex = index;
super.addChild(child, index); super.addChild(child, index);
this.setTabIndex(Math.min(index, this.children.length - 1));
} }
removeChild(index) { removeChild(index) {
this.tabIndex = Math.min(this.tabIndex, this.children.length - 2);
super.removeChild(index); super.removeChild(index);
this.setTabIndex(Math.min(this.tabIndex, this.children.length - 1));
} }
setTabIndex(tabIndex) { setTabIndex(tabIndex) {
......
...@@ -14,8 +14,15 @@ const parentMixin = (Base = Child) => class Parent extends Base { ...@@ -14,8 +14,15 @@ const parentMixin = (Base = Child) => class Parent extends Base {
} }
addChild(child, index = this.children.length) { addChild(child, index = this.children.length) {
this.children.splice(index, 0, child); if (child.parent === this) {
child.setParent(this); const oldIndex = this.children.indexOf(child);
this.children[oldIndex] = null;
this.children.splice(index, 0, child);
this.children = this.children.filter(child => child);
} else {
this.children.splice(index, 0, child);
child.setParent(this);
}
this.render(); this.render();
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册