Make drag & drop code a bit more dry

This commit is contained in:
Mattias Granlund 2023-06-13 17:35:24 +02:00
parent d4e4f9749a
commit 48e3143c79
4 changed files with 88 additions and 77 deletions

View File

@ -4,6 +4,7 @@
import Lane from './BranchLane.svelte';
import type { Branch, Commit, File, Hunk } from './types';
import type { DndEvent } from 'svelte-dnd-action/typings';
import { createBranch, createCommit, createFile } from './helpers';
export let branches: Branch[];
@ -19,56 +20,41 @@
const hunkItems = e.detail.items.filter((item) => item.kind == 'hunk') as Hunk[];
for (const hunk of hunkItems) {
branchItems.push({
id: `${Date.now()}-${hunk.id}-branch`,
name: 'new branch',
active: true,
kind: 'branch',
commits: [
{
id: `${Date.now()}-${hunk.id}-commit`,
description: 'New commit',
kind: 'commit',
files: [
{
id: `${Date.now()}-${hunk.id}-hunk`,
path: hunk.filePath,
kind: 'file',
hunks: [{ ...hunk, isDndShadowItem: !isFinal }]
}
]
}
]
});
branchItems.push(
createBranch({
commits: [
createCommit({
files: [
createFile({
hunks: [{ ...hunk, isDndShadowItem: !isFinal }],
isShadow: false,
filePath: hunk.filePath
})
],
isShadow: false
})
]
})
);
}
for (const file of fileItems) {
branchItems.push({
id: `${Date.now()}-${file.id}-branch`,
name: 'new branch',
active: true,
kind: 'branch',
commits: [
{
id: `${Date.now()}-${file.id}-commit`,
description: '',
kind: 'commit',
files: [{ ...file, isDndShadowItem: false }],
isDndShadowItem: !isFinal
}
]
});
branchItems.push(
createBranch({
commits: [
createCommit({ files: [{ ...file, isDndShadowItem: !isFinal }], isShadow: false })
]
})
);
}
for (const commit of commitItems) {
branchItems.push({
id: `${Date.now()}-${commit.id}-branch`,
name: 'new branch',
kind: 'branch',
active: true,
commits: [commit],
isDndShadowItem: !isFinal
});
branchItems.push(
createBranch({
commits: [commit]
})
);
}
branches = branchItems.filter((commit) => commit.active);
console.log(branches);
}
function handleEmpty() {

View File

@ -5,6 +5,7 @@
import type { Commit, File, Hunk } from './types';
import CommitGroup from './CommitGroup.svelte';
import { createEventDispatcher } from 'svelte';
import { createCommit, createFile } from './helpers';
export let name: string;
export let commits: Commit[];
@ -19,28 +20,23 @@
// Merge hunks into existing files, or create new where none exist
for (const hunk of hunkItems) {
commitItems.push({
id: `${Date.now()}-${hunk.id}-commit`,
description: 'New commit',
kind: 'commit',
files: [
{
id: `${Date.now()}-${hunk.id}-hunk`,
path: hunk.filePath,
kind: 'file',
hunks: [{ ...hunk, isDndShadowItem: !isFinal }]
}
]
});
commitItems.push(
createCommit({
files: [
createFile({
hunks: [{ ...hunk, isDndShadowItem: !isFinal }],
isShadow: false,
filePath: hunk.filePath
})
],
isShadow: false
})
);
}
for (const file of fileItems) {
commitItems.push({
id: `${Date.now()}-${file.id}`,
description: 'New commit',
kind: 'commit',
files: [{ ...file, isDndShadowItem: false }],
isDndShadowItem: !isFinal
});
commitItems.push(
createCommit({ files: [{ ...file, isDndShadowItem: true }], isShadow: false })
);
}
commits = commitItems.filter((commit) => commit.files && commit.files.length > 0);

View File

@ -5,6 +5,7 @@
import type { File, Hunk } from './types';
import FileCard from './FileCard.svelte';
import { createEventDispatcher } from 'svelte';
import { createFile } from './helpers';
export let description: string;
export let id: string;
@ -23,12 +24,13 @@
if (file) {
file.hunks.push(hunk);
} else {
fileItems.push({
id: `${Date.now()}-${hunk.id}`,
path: hunk.filePath,
kind: 'file',
hunks: [{ ...hunk, isDndShadowItem: !isFinal }]
});
fileItems.push(
createFile({
filePath: hunk.filePath,
hunks: [{ ...hunk, isDndShadowItem: !isFinal }],
isShadow: false
})
);
}
}
files = fileItems.filter((file) => file.hunks && file.hunks.length > 0);

View File

@ -1,11 +1,38 @@
import type { Commit, File } from './types';
import type { Branch, Commit, File, Hunk } from './types';
export function createCommit(files: File[], isShadow: boolean): Commit {
let fileCounter = 0;
let commitCounter = 0;
let branchCounter = 0;
export function createFile(args: { hunks: [Hunk]; filePath: string; isShadow: boolean }): File {
fileCounter++;
return {
id: `commit-${Date.now()}`,
description: '',
kind: 'commit',
files: files,
isDndShadowItem: isShadow
id: `file-${fileCounter}`,
path: args.filePath,
kind: 'file',
hunks: args.hunks,
isDndShadowItem: args.isShadow
};
}
export function createCommit(args: { files: File[]; isShadow: boolean }): Commit {
commitCounter++;
return {
id: `commit-${commitCounter}`,
description: `New commit # ${commitCounter}`,
kind: 'commit',
files: args.files,
isDndShadowItem: args.isShadow
};
}
export function createBranch(args: { commits: Commit[] }): Branch {
branchCounter++;
return {
id: `branch-${branchCounter}`,
name: `new branch ${branchCounter}`,
active: true,
kind: 'branch',
commits: args.commits
};
}