@@ -2,35 +2,18 @@ import { DraggableCommit } from '$lib/dragging/draggables';
2
2
import type { BranchController } from '$lib/vbranches/branchController' ;
3
3
import type { Branch , Commit } from '$lib/vbranches/types' ;
4
4
5
- /**
6
- * This class is used to determine how far a commit has been drag and dropped.
7
- *
8
- * We expect the dropzones to be in the following order:
9
- *
10
- * ```
11
- * const indexer = new ReorderDropzoneIndexer(commits);
12
- *
13
- * <ReorderDropzone index={indexer.topDropzoneIndex} />
14
- * <Commit id={commits[0].id} />
15
- * <ReorderDropzone index={indexer.dropzoneIndexBelowCommit(commits[0].id)} />
16
- * <Commit id={commits[1].id} />
17
- * <ReorderDropzone index={indexer.dropzoneIndexBelowCommit(commits[1].id)} />
18
- * ```
19
- */
20
-
21
5
// Exported for type access only
22
6
export class ReorderDropzone {
23
7
constructor (
24
8
private branchController : BranchController ,
25
9
private branch : Branch ,
26
- private index : number ,
27
- private indexer : ReorderDropzoneManager
10
+ private entry : Entry
28
11
) { }
29
12
30
13
accepts ( data : any ) {
31
14
if ( ! ( data instanceof DraggableCommit ) ) return false ;
32
15
if ( data . branchId !== this . branch . id ) return false ;
33
- if ( this . indexer . dropzoneCommitOffset ( this . index , data . commit . id ) === 0 ) return false ;
16
+ if ( this . entry . distanceToOtherCommit ( data . commit . id ) === 0 ) return false ;
34
17
35
18
return true ;
36
19
}
@@ -39,20 +22,49 @@ export class ReorderDropzone {
39
22
if ( ! ( data instanceof DraggableCommit ) ) return ;
40
23
if ( data . branchId !== this . branch . id ) return ;
41
24
42
- const offset = this . indexer . dropzoneCommitOffset ( this . index , data . commit . id ) ;
25
+ const offset = this . entry . distanceToOtherCommit ( data . commit . id ) ;
43
26
this . branchController . reorderCommit ( this . branch . id , data . commit . id , offset ) ;
44
27
}
45
28
}
46
29
47
30
export class ReorderDropzoneManager {
48
- private dropzoneIndexes = new Map < string , number > ( ) ;
49
- private commitIndexes = new Map < string , number > ( ) ;
31
+ private indexer : Indexer ;
50
32
51
33
constructor (
52
34
private branchController : BranchController ,
53
35
private branch : Branch ,
54
36
commits : Commit [ ]
55
37
) {
38
+ this . indexer = new Indexer ( commits ) ;
39
+ }
40
+
41
+ get topDropzone ( ) {
42
+ const entry = this . indexer . get ( 'top' ) ;
43
+
44
+ return new ReorderDropzone ( this . branchController , this . branch , entry ) ;
45
+ }
46
+
47
+ dropzoneBelowCommit ( commitId : string ) {
48
+ const entry = this . indexer . get ( commitId ) ;
49
+
50
+ return new ReorderDropzone ( this . branchController , this . branch , entry ) ;
51
+ }
52
+ }
53
+
54
+ export class ReorderDropzoneManagerFactory {
55
+ constructor ( private branchController : BranchController ) { }
56
+
57
+ build ( branch : Branch , commits : Commit [ ] ) {
58
+ return new ReorderDropzoneManager ( this . branchController , branch , commits ) ;
59
+ }
60
+ }
61
+
62
+ // Private classes used to calculate distances between commtis
63
+ class Indexer {
64
+ private dropzoneIndexes = new Map < string , number > ( ) ;
65
+ private commitIndexes = new Map < string , number > ( ) ;
66
+
67
+ constructor ( commits : Commit [ ] ) {
56
68
this . dropzoneIndexes . set ( 'top' , 0 ) ;
57
69
58
70
commits . forEach ( ( commit , index ) => {
@@ -61,56 +73,55 @@ export class ReorderDropzoneManager {
61
73
} ) ;
62
74
}
63
75
64
- get topDropzone ( ) {
65
- const index = this . dropzoneIndexes . get ( 'top' ) ?? 0 ;
76
+ get ( key : string ) {
77
+ const index = this . getIndex ( key ) ;
66
78
67
- return new ReorderDropzone ( this . branchController , this . branch , index , this ) ;
79
+ return new Entry ( this . commitIndexes , index ) ;
68
80
}
69
81
70
- dropzoneBelowCommit ( commitId : string ) {
71
- const index = this . dropzoneIndexes . get ( commitId ) ;
72
-
73
- if ( index === undefined ) {
74
- throw new Error ( `Commit ${ commitId } not found in dropzoneIndexes` ) ;
75
- }
76
-
77
- return new ReorderDropzone ( this . branchController , this . branch , index , this ) ;
78
- }
82
+ private getIndex ( key : string ) {
83
+ if ( key === 'top' ) {
84
+ return this . dropzoneIndexes . get ( key ) ?? 0 ;
85
+ } else {
86
+ const index = this . dropzoneIndexes . get ( key ) ;
79
87
80
- commitIndex ( commitId : string ) {
81
- const index = this . commitIndexes . get ( commitId ) ;
88
+ if ( index === undefined ) {
89
+ throw new Error ( `Commit ${ key } not found in dropzoneIndexes` ) ;
90
+ }
82
91
83
- if ( index === undefined ) {
84
- throw new Error ( `Commit ${ commitId } not found in commitIndexes` ) ;
92
+ return index ;
85
93
}
86
-
87
- return index ;
88
94
}
95
+ }
96
+
97
+ class Entry {
98
+ constructor (
99
+ private commitIndexes : Map < string , number > ,
100
+ private index : number
101
+ ) { }
89
102
90
103
/**
91
104
* A negative offset means the commit has been dragged up, and a positive offset means the commit has been dragged down.
92
105
*/
93
- dropzoneCommitOffset ( dropzoneIndex : number , commitId : string ) {
94
- const commitIndex = this . commitIndexes . get ( commitId ) ;
106
+ distanceToOtherCommit ( commitId : string ) {
107
+ const commitIndex = this . commitIndex ( commitId ) ;
95
108
96
- if ( commitIndex === undefined ) {
97
- throw new Error ( `Commit ${ commitId } not found in commitIndexes` ) ;
98
- }
99
-
100
- const offset = dropzoneIndex - commitIndex ;
109
+ const offset = this . index - commitIndex ;
101
110
102
111
if ( offset > 0 ) {
103
112
return offset - 1 ;
104
113
} else {
105
114
return offset ;
106
115
}
107
116
}
108
- }
109
117
110
- export class ReorderDropzoneManagerFactory {
111
- constructor ( private branchController : BranchController ) { }
118
+ private commitIndex ( commitId : string ) {
119
+ const index = this . commitIndexes . get ( commitId ) ;
112
120
113
- build ( branch : Branch , commits : Commit [ ] ) {
114
- return new ReorderDropzoneManager ( this . branchController , branch , commits ) ;
121
+ if ( index === undefined ) {
122
+ throw new Error ( `Commit ${ commitId } not found in commitIndexes` ) ;
123
+ }
124
+
125
+ return index ;
115
126
}
116
127
}
0 commit comments