1+ import { BranchDropData } from '$lib/branches/dropHandler' ;
12import { changesToDiffSpec } from '$lib/commits/utils' ;
23import { ChangeDropData } from '$lib/dragging/draggables' ;
34import StackMacros from '$lib/stacks/macros' ;
5+ import { handleMoveBranchResult } from '$lib/stacks/stack' ;
46import { ensureValue } from '$lib/utils/validation' ;
57import { chipToasts } from '@gitbutler/ui' ;
68import type { DropzoneHandler } from '$lib/dragging/handler' ;
@@ -23,13 +25,25 @@ export class OutsideLaneDzHandler implements DropzoneHandler {
2325 this . macros = new StackMacros ( this . projectId , this . stackService , this . uiState ) ;
2426 }
2527
26- accepts ( data : unknown ) {
28+ private acceptsChangeDropData ( data : unknown ) : data is ChangeDropData {
2729 if ( ! ( data instanceof ChangeDropData ) ) return false ;
2830 if ( data . selectionId . type === 'commit' && data . stackId === undefined ) return false ;
2931 return true ;
3032 }
3133
32- async ondrop ( data : ChangeDropData ) {
34+ private acceptsBranchDropData ( data : unknown ) : data is BranchDropData {
35+ if ( ! ( data instanceof BranchDropData ) ) return false ;
36+ if ( data . hasConflicts ) return false ;
37+ if ( data . numberOfBranchesInStack <= 1 ) return false ; // Can't tear off the last branch of a stack
38+ if ( data . numberOfCommits === 0 ) return false ; // TODO: Allow to rip empty branches
39+ return true ;
40+ }
41+
42+ accepts ( data : unknown ) {
43+ return this . acceptsChangeDropData ( data ) || this . acceptsBranchDropData ( data ) ;
44+ }
45+
46+ async ondropChangeData ( data : ChangeDropData ) {
3347 switch ( data . selectionId . type ) {
3448 case 'branch' : {
3549 const newBranchName = await this . stackService . fetchNewBranchName ( this . projectId ) ;
@@ -112,4 +126,28 @@ export class OutsideLaneDzHandler implements DropzoneHandler {
112126 }
113127 }
114128 }
129+
130+ async ondropBranchData ( data : BranchDropData ) {
131+ await this . stackService
132+ . tearOffBranch ( {
133+ projectId : this . projectId ,
134+ sourceStackId : data . stackId ,
135+ subjectBranchName : data . branchName
136+ } )
137+ . then ( ( result ) => {
138+ handleMoveBranchResult ( result ) ;
139+ } ) ;
140+ }
141+
142+ async ondrop ( data : unknown ) : Promise < void > {
143+ if ( this . acceptsChangeDropData ( data ) ) {
144+ await this . ondropChangeData ( data ) ;
145+ return ;
146+ }
147+
148+ if ( this . acceptsBranchDropData ( data ) ) {
149+ await this . ondropBranchData ( data ) ;
150+ return ;
151+ }
152+ }
115153}
0 commit comments