diff --git a/frontend/appflowy_flutter/lib/workspace/application/panes/panes_cubit/panes_cubit.dart b/frontend/appflowy_flutter/lib/workspace/application/panes/panes_cubit/panes_cubit.dart index f6dbc63d06b7..f5b0905daaaa 100644 --- a/frontend/appflowy_flutter/lib/workspace/application/panes/panes_cubit/panes_cubit.dart +++ b/frontend/appflowy_flutter/lib/workspace/application/panes/panes_cubit/panes_cubit.dart @@ -48,7 +48,7 @@ class PanesCubit extends Cubit { axis: axis, ); - final firstLeafNode = panesService.findFirstLeaf(node: state.root); + final firstLeafNode = panesService.findFirstLeaf(node: root); emit( state.copyWith( @@ -57,17 +57,19 @@ class PanesCubit extends Cubit { firstLeafNode: firstLeafNode, ), ); - setActivePane(state.root.children.last); + setActivePane(root.children.last); } - void closePane({required String paneId, bool closingToMove = false}) { + void closePane({ + required String paneId, + }) { final root = panesService.closePaneHandler( node: state.root, targetPaneId: paneId, - closingToMove: closingToMove, + closingToMove: false, ); - final firstLeafNode = panesService.findFirstLeaf(node: state.root); + final firstLeafNode = panesService.findFirstLeaf(node: root); emit( state.copyWith( @@ -77,8 +79,8 @@ class PanesCubit extends Cubit { ), ); - final children = state.root.children; - setActivePane(children.isEmpty ? state.root : children.last); + final children = root.children; + setActivePane(children.isEmpty ? root : children.last); } void openTab({required Plugin plugin}) { @@ -111,30 +113,36 @@ class PanesCubit extends Cubit { ) { final direction = [ FlowyDraggableHoverPosition.top, - FlowyDraggableHoverPosition.left + FlowyDraggableHoverPosition.left, ].contains(position) ? Direction.back : Direction.front; final axis = [ FlowyDraggableHoverPosition.left, - FlowyDraggableHoverPosition.right + FlowyDraggableHoverPosition.right, ].contains(position) ? Axis.vertical : Axis.horizontal; + final root = panesService.movePaneHandler( + toNode: to, + direction: direction, + axis: axis, + root: state.root, + fromNode: from, + ); + + final firstLeafNode = panesService.findFirstLeaf(node: root); + emit( state.copyWith( - root: panesService.splitHandler( - node: state.root, - targetPaneId: to.paneId, - direction: direction, - axis: axis, - fromNode: from, - ), - count: state.count + 1, + root: root, + firstLeafNode: firstLeafNode, ), ); - closePane(paneId: from.paneId, closingToMove: true); + + final children = state.root.children; + setActivePane(children.isEmpty ? state.root : children.last); } } diff --git a/frontend/appflowy_flutter/lib/workspace/application/panes/panes_service.dart b/frontend/appflowy_flutter/lib/workspace/application/panes/panes_service.dart index 151cd46624e6..3044d36db124 100644 --- a/frontend/appflowy_flutter/lib/workspace/application/panes/panes_service.dart +++ b/frontend/appflowy_flutter/lib/workspace/application/panes/panes_service.dart @@ -57,7 +57,9 @@ class PanesService { oldChildNode, ], ); - return ret; + return ret.copyWith( + children: ret.children.map((e) => e.copyWith(parent: ret)).toList(), + ); } /// if we haven't found our target node there is a possibility that our @@ -94,10 +96,13 @@ class PanesService { ///reconstructed tabscontroller to trigger build of consecutive ///widget else it won't reflect on ui. - return node.copyWith( + final parent = node.copyWith( paneId: nanoid(), children: list.map((e) => e.copyWith()).toList(), ); + return parent.copyWith( + children: list.map((e) => e.copyWith(parent: parent)).toList(), + ); } } } @@ -141,16 +146,19 @@ class PanesService { if (node.children.length == 1) { final ret = node.children.first.copyWith( - paneId: nanoid(), + paneId: closingToMove ? node.children.first.paneId : nanoid(), parent: node.parent, ); return ret; } - return node.copyWith( - paneId: nanoid(), + final parent = node.copyWith( + paneId: closingToMove ? node.paneId : nanoid(), children: list.map((e) => e.copyWith()).toList(), ); + return parent.copyWith( + children: list.map((e) => e.copyWith(parent: parent)).toList(), + ); } } @@ -162,7 +170,32 @@ class PanesService { ); }).toList(); - return node.copyWith(children: newChildren); + return node.copyWith( + paneId: closingToMove ? node.paneId : nanoid(), + children: newChildren, + ); + } + + PaneNode movePaneHandler({ + required PaneNode root, + required Direction direction, + required Axis axis, + required PaneNode fromNode, + required PaneNode toNode, + }) { + final response = splitHandler( + node: closePaneHandler( + node: root, + targetPaneId: fromNode.paneId, + closingToMove: true, + ), + targetPaneId: toNode.paneId, + direction: direction, + axis: axis, + fromNode: fromNode, + ); + + return response.copyWith(paneId: nanoid()); } PaneNode findFirstLeaf({required PaneNode node}) {