Skip to content

Commit 4728b8e

Browse files
committed
Refactor dropzone manager
1 parent 4da0b36 commit 4728b8e

File tree

1 file changed

+63
-52
lines changed

1 file changed

+63
-52
lines changed

app/src/lib/dragging/reorderDropzoneManager.ts

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,18 @@ import { DraggableCommit } from '$lib/dragging/draggables';
22
import type { BranchController } from '$lib/vbranches/branchController';
33
import type { Branch, Commit } from '$lib/vbranches/types';
44

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-
215
// Exported for type access only
226
export class ReorderDropzone {
237
constructor(
248
private branchController: BranchController,
259
private branch: Branch,
26-
private index: number,
27-
private indexer: ReorderDropzoneManager
10+
private entry: Entry
2811
) {}
2912

3013
accepts(data: any) {
3114
if (!(data instanceof DraggableCommit)) return false;
3215
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;
3417

3518
return true;
3619
}
@@ -39,20 +22,49 @@ export class ReorderDropzone {
3922
if (!(data instanceof DraggableCommit)) return;
4023
if (data.branchId !== this.branch.id) return;
4124

42-
const offset = this.indexer.dropzoneCommitOffset(this.index, data.commit.id);
25+
const offset = this.entry.distanceToOtherCommit(data.commit.id);
4326
this.branchController.reorderCommit(this.branch.id, data.commit.id, offset);
4427
}
4528
}
4629

4730
export class ReorderDropzoneManager {
48-
private dropzoneIndexes = new Map<string, number>();
49-
private commitIndexes = new Map<string, number>();
31+
private indexer: Indexer;
5032

5133
constructor(
5234
private branchController: BranchController,
5335
private branch: Branch,
5436
commits: Commit[]
5537
) {
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[]) {
5668
this.dropzoneIndexes.set('top', 0);
5769

5870
commits.forEach((commit, index) => {
@@ -61,56 +73,55 @@ export class ReorderDropzoneManager {
6173
});
6274
}
6375

64-
get topDropzone() {
65-
const index = this.dropzoneIndexes.get('top') ?? 0;
76+
get(key: string) {
77+
const index = this.getIndex(key);
6678

67-
return new ReorderDropzone(this.branchController, this.branch, index, this);
79+
return new Entry(this.commitIndexes, index);
6880
}
6981

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);
7987

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+
}
8291

83-
if (index === undefined) {
84-
throw new Error(`Commit ${commitId} not found in commitIndexes`);
92+
return index;
8593
}
86-
87-
return index;
8894
}
95+
}
96+
97+
class Entry {
98+
constructor(
99+
private commitIndexes: Map<string, number>,
100+
private index: number
101+
) {}
89102

90103
/**
91104
* A negative offset means the commit has been dragged up, and a positive offset means the commit has been dragged down.
92105
*/
93-
dropzoneCommitOffset(dropzoneIndex: number, commitId: string) {
94-
const commitIndex = this.commitIndexes.get(commitId);
106+
distanceToOtherCommit(commitId: string) {
107+
const commitIndex = this.commitIndex(commitId);
95108

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;
101110

102111
if (offset > 0) {
103112
return offset - 1;
104113
} else {
105114
return offset;
106115
}
107116
}
108-
}
109117

110-
export class ReorderDropzoneManagerFactory {
111-
constructor(private branchController: BranchController) {}
118+
private commitIndex(commitId: string) {
119+
const index = this.commitIndexes.get(commitId);
112120

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;
115126
}
116127
}

0 commit comments

Comments
 (0)