Skip to content

Commit 44e43b9

Browse files
committed
Rename some files and start to use context to inject classes
1 parent 2f085fd commit 44e43b9

13 files changed

+54
-37
lines changed

app/src/lib/components/Board.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script lang="ts" async="true">
22
import FullviewLoading from './FullviewLoading.svelte';
3-
import NewBranchDropZone from './NewBranchDropZone.svelte';
43
import dzenSvg from '$lib/assets/dzen-pc.svg?raw';
54
import { Project } from '$lib/backend/projects';
5+
import BranchDropzone from '$lib/components/BranchDropzone.svelte';
66
import BranchLane from '$lib/components/BranchLane.svelte';
77
import Icon from '$lib/components/Icon.svelte';
88
import { cloneWithRotation } from '$lib/dragging/draggable';
@@ -193,7 +193,7 @@
193193
</div>
194194
</div>
195195
{:else}
196-
<NewBranchDropZone />
196+
<BranchDropzone />
197197
{/if}
198198
</div>
199199
{/if}

app/src/lib/components/BranchCard/Dropzones.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import { BranchController } from '$lib/vbranches/branchController';
55
import { filesToOwnership } from '$lib/vbranches/ownership';
66
import { Branch } from '$lib/vbranches/types';
7-
import Dropzone from '$lib/components/NewNewDropzone/Dropzone.svelte';
7+
import Dropzone from '$lib/components/Dropzone/Dropzone.svelte';
88
import type { Snippet } from 'svelte';
9-
import CardOverlay from '$lib/components/NewNewDropzone/CardOverlay.svelte';
9+
import CardOverlay from '$lib/components/Dropzone/CardOverlay.svelte';
1010
1111
const branchController = getContext(BranchController);
1212
const branch = getContextStore(Branch);

app/src/lib/components/NewBranchDropZone.svelte renamed to app/src/lib/components/BranchDropzone.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import topSheetSvg from '$lib/assets/new-branch/top-sheet.svg?raw';
77
// import components
88
import Button from '$lib/components/Button.svelte';
9-
import Dropzone from '$lib/components/NewNewDropzone/Dropzone.svelte';
9+
import Dropzone from '$lib/components/Dropzone/Dropzone.svelte';
1010
import { DraggableFile, DraggableHunk } from '$lib/dragging/draggables';
1111
import { getContext } from '$lib/utils/context';
1212
import { BranchController } from '$lib/vbranches/branchController';

app/src/lib/components/CommitDragItem.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import { BranchController } from '$lib/vbranches/branchController';
66
import { filesToOwnership, filesToSimpleOwnership } from '$lib/vbranches/ownership';
77
import { RemoteCommit, Branch, Commit, LocalFile, RemoteFile } from '$lib/vbranches/types';
8-
import Dropzone from '$lib/components/NewNewDropzone/Dropzone.svelte';
8+
import Dropzone from '$lib/components/Dropzone/Dropzone.svelte';
99
import type { Snippet } from 'svelte';
10-
import CardOverlay from '$lib/components/NewNewDropzone/CardOverlay.svelte';
10+
import CardOverlay from '$lib/components/Dropzone/CardOverlay.svelte';
1111
1212
interface Props {
1313
commit: Commit | RemoteCommit;

app/src/lib/components/CommitList.svelte

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import { Project } from '$lib/backend/projects';
55
import InsertEmptyCommitAction from '$lib/components/InsertEmptyCommitAction.svelte';
66
import {
7-
ReorderDropzoneIndexer,
7+
getReorderDropzoneManager,
88
type ReorderDropzone
9-
} from '$lib/dragging/reorderDropzoneIndexer';
9+
} from '$lib/dragging/reorderDropzoneManager';
1010
import { getAvatarTooltip } from '$lib/utils/avatar';
1111
import { getContext } from '$lib/utils/context';
1212
import { getContextStore } from '$lib/utils/context';
@@ -19,8 +19,8 @@
1919
} from '$lib/vbranches/contexts';
2020
import { BaseBranch, Branch, Commit, type CommitStatus } from '$lib/vbranches/types';
2121
import { goto } from '$app/navigation';
22-
import Dropzone from '$lib/components/NewNewDropzone/Dropzone.svelte';
23-
import LineOverlay from '$lib/components/NewNewDropzone/LineOverlay.svelte';
22+
import Dropzone from '$lib/components/Dropzone/Dropzone.svelte';
23+
import LineOverlay from '$lib/components/Dropzone/LineOverlay.svelte';
2424
2525
export let isUnapplied: boolean;
2626
@@ -33,6 +33,8 @@
3333
const project = getContext(Project);
3434
const branchController = getContext(BranchController);
3535
36+
const ReorderDropzoneManager = getReorderDropzoneManager();
37+
3638
// Force the "base" commit lines to update when $branch updates.
3739
let tsKey: number | undefined;
3840
$: {
@@ -48,7 +50,7 @@
4850
$: hasIntegratedCommits = $integratedCommits.length > 0;
4951
$: hasRemoteCommits = $remoteCommits.length > 0;
5052
$: hasShadowedCommits = $localCommits.some((c) => c.relatedTo);
51-
$: reorderDropzoneIndexer = new ReorderDropzoneIndexer(
53+
$: reorderDropzoneManager = new ReorderDropzoneManager(
5254
[...$localCommits, ...$remoteCommits],
5355
$branch,
5456
branchController
@@ -170,7 +172,7 @@
170172
<!-- LOCAL COMMITS -->
171173
{#if $localCommits.length > 0}
172174
{@render reorderDropzone(
173-
reorderDropzoneIndexer.topDropzone,
175+
reorderDropzoneManager.topDropzone,
174176
getReorderDropzoneOffset({ isFirst: true })
175177
)}
176178
{#each $localCommits as commit, idx (commit.id)}
@@ -208,7 +210,7 @@
208210
</CommitCard>
209211

210212
{@render reorderDropzone(
211-
reorderDropzoneIndexer.dropzoneBelowCommit(commit.id),
213+
reorderDropzoneManager.dropzoneBelowCommit(commit.id),
212214
getReorderDropzoneOffset({
213215
isLast: $remoteCommits.length === 0 && idx + 1 === $localCommits.length,
214216
isMiddle: $remoteCommits.length > 0 && idx + 1 === $localCommits.length
@@ -255,7 +257,7 @@
255257
</svelte:fragment>
256258
</CommitCard>
257259
{@render reorderDropzone(
258-
reorderDropzoneIndexer.dropzoneBelowCommit(commit.id),
260+
reorderDropzoneManager.dropzoneBelowCommit(commit.id),
259261
getReorderDropzoneOffset({
260262
isLast: idx + 1 === $remoteCommits.length
261263
})

app/src/lib/dragging/dropzone.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ export interface DropzoneConfiguration {
88
onHoverEnd: () => void;
99
target: string;
1010
}
11-
12-
const defaultDropzoneOptions: DropzoneConfiguration = {
13-
disabled: false,
14-
accepts: (data) => data === 'default',
15-
onDrop: () => {},
16-
onActivationStart: () => {},
17-
onActivationEnd: () => {},
18-
onHoverStart: () => {},
19-
onHoverEnd: () => {},
20-
target: '.dropzone-target'
21-
};
22-
2311
export class Dropzone {
2412
private active: boolean = false;
2513

@@ -70,9 +58,10 @@ export class Dropzone {
7058
}
7159

7260
unregister() {
73-
// Mark as no longer active
61+
// Mark as no longer active and ensure its not stuck in the hover state
7462
this.active = false;
7563
this.configuration.onActivationEnd();
64+
this.configuration.onHoverEnd();
7665

7766
// Unregister listeners
7867
if (this.registeredOnDrop) {
@@ -109,27 +98,26 @@ export class Dropzone {
10998

11099
export const dropzoneRegistry = new Map<HTMLElement, Dropzone>();
111100

112-
export function dropzone(node: HTMLElement, opts: Partial<DropzoneConfiguration> | undefined) {
113-
function setup(opts: Partial<DropzoneConfiguration> | undefined) {
114-
const configuration = { ...defaultDropzoneOptions, ...opts };
115-
116-
if (configuration.disabled) return;
101+
export function dropzone(node: HTMLElement, opts: DropzoneConfiguration) {
102+
function setup(opts: DropzoneConfiguration) {
103+
if (opts.disabled) return;
117104

118105
if (dropzoneRegistry.has(node)) {
119106
clean();
120107
}
121108

122-
dropzoneRegistry.set(node, new Dropzone(configuration, node));
109+
dropzoneRegistry.set(node, new Dropzone(opts, node));
123110
}
124111

125112
function clean() {
113+
dropzoneRegistry.get(node)?.unregister();
126114
dropzoneRegistry.delete(node);
127115
}
128116

129117
setup(opts);
130118

131119
return {
132-
update(opts: Partial<DropzoneConfiguration> | undefined) {
120+
update(opts: DropzoneConfiguration) {
133121
clean();
134122
setup(opts);
135123
},

app/src/lib/dragging/reorderDropzoneIndexer.ts renamed to app/src/lib/dragging/reorderDropzoneManager.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DraggableCommit } from '$lib/dragging/draggables';
2+
import { buildConstructorStore } from '$lib/utils/context';
23
import type { BranchController } from '$lib/vbranches/branchController';
34
import type { Branch, Commit } from '$lib/vbranches/types';
45

@@ -23,7 +24,7 @@ export class ReorderDropzone {
2324
constructor(
2425
private branch: Branch,
2526
private index: number,
26-
private indexer: ReorderDropzoneIndexer,
27+
private indexer: ReorderDropzoneManager,
2728
private branchController: BranchController
2829
) {}
2930

@@ -45,7 +46,7 @@ export class ReorderDropzone {
4546
}
4647
}
4748

48-
export class ReorderDropzoneIndexer {
49+
export class ReorderDropzoneManager {
4950
private dropzoneIndexes = new Map<string, number>();
5051
private commitIndexes = new Map<string, number>();
5152

@@ -107,3 +108,6 @@ export class ReorderDropzoneIndexer {
107108
}
108109
}
109110
}
111+
112+
export const [getReorderDropzoneManager, setReorderDropzoneManager] =
113+
buildConstructorStore<typeof ReorderDropzoneManager>('ReorderDropzoneManager');

app/src/lib/utils/context.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,20 @@ export function getContextStoreBySymbol<T, S extends Readable<T> = Readable<T>>(
100100
if (!instance) throw new Error(`no instance of \`Readable<${key.toString()}[]>\` in context`);
101101
return instance;
102102
}
103+
104+
export function buildConstructorStore<T extends Class>(
105+
name: string
106+
): [() => T, (constructor: T) => void] {
107+
const identifier = Symbol(name);
108+
109+
return [
110+
() => {
111+
const constructor = svelteGetContext<T | undefined>(identifier);
112+
if (!constructor) throw new Error(`no constructor in context \`${name}\``);
113+
return constructor;
114+
},
115+
(constructor: T) => {
116+
setContext(identifier, constructor);
117+
}
118+
];
119+
}

app/src/routes/+layout.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import GlobalSettingsMenuAction from '$lib/components/GlobalSettingsMenuAction.svelte';
1414
import PromptModal from '$lib/components/PromptModal.svelte';
1515
import ShareIssueModal from '$lib/components/ShareIssueModal.svelte';
16+
import {
17+
ReorderDropzoneManager,
18+
setReorderDropzoneManager
19+
} from '$lib/dragging/reorderDropzoneManager';
1620
import { GitHubService } from '$lib/github/service';
1721
import ToastController from '$lib/notifications/ToastController.svelte';
1822
import { RemotesService } from '$lib/remotes/service';
@@ -47,6 +51,7 @@
4751
setContext(User, data.userService.user);
4852
setContext(RemotesService, data.remotesService);
4953
setContext(AIPromptService, data.aiPromptService);
54+
setReorderDropzoneManager(ReorderDropzoneManager);
5055
5156
let shareIssueModal: ShareIssueModal;
5257

app/src/routes/+layout.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { mockTauri } from '$lib/testing/index';
1414
import lscache from 'lscache';
1515
import { BehaviorSubject, config } from 'rxjs';
1616
import { env } from '$env/dynamic/public';
17+
import { ReorderDropzoneManager } from '$lib/dragging/reorderDropzoneManager';
1718

1819
// call on startup so we don't accumulate old items
1920
lscache.flushExpired();

0 commit comments

Comments
 (0)