Skip to content

Commit

Permalink
Added ability to duplicate one's own projects.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Jul 27, 2024
1 parent e73993c commit b46347c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Dates are in `YYYY-MM-DD` format and versions are in [semantic versioning](http:

## 0.10.7 2024-07-27

### Added

- Ability to copy one's own projects.

### Fixed

- Fixed text sorting.
Expand Down
10 changes: 10 additions & 0 deletions src/components/app/ProjectPreviewSet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
action: () => void;
}
| false;
export let copy:
| {
description: string;
label: string;
action: (project: Project) => void;
}
| false;
function sortProjects(projects: Project[]): Project[] {
return projects.sort((a, b) =>
Expand All @@ -40,6 +47,9 @@
tip={edit.description}
action={() => (edit ? edit.action(project) : undefined)}
>{edit.label}</Button
>{/if}{#if copy}<Button
tip={copy.description}
action={() => copy.action(project)}>{copy.label}</Button
>{/if}{#if removeMeta}<ConfirmButton
prompt={removeMeta.prompt}
tip={removeMeta.description}
Expand Down
21 changes: 20 additions & 1 deletion src/db/ProjectsDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Dexie, { liveQuery, type Observable, type Table } from 'dexie';
import { PersistenceType, ProjectHistory } from './ProjectHistory';
import { writable, type Writable } from 'svelte/store';
import { get, writable, type Writable } from 'svelte/store';
import Project from '../models/Project';
import type LocaleText from '../locale/LocaleText';
import { Locales, SaveStatus, type Database } from './Database';
Expand Down Expand Up @@ -32,6 +32,7 @@ import {
} from '../models/ProjectSchemas';
import { PossiblePII } from '@conflicts/PossiblePII';
import { EditFailure } from './EditFailure';
import { COPY_SYMBOL } from '@parser/Symbols';

/** The name of the projects collection in Firebase */
export const ProjectsCollection = 'projects';
Expand Down Expand Up @@ -253,6 +254,24 @@ export default class ProjectsDatabase {
this.saveSoon();
}

/**
* Duplicate a project and give it to the current user, returning it's ID.
*/
duplicate(project: Project): Project {
const nameExists = get(this.allEditableProjects).some(
(p) => p.getName() === project.getName(),
);
const copy = project
.copy(this.database.getUserID())
.withName(
nameExists
? `${project.getName()} ${COPY_SYMBOL}`
: project.getName(),
);
this.track(copy, true, PersistenceType.Online, false);
return copy;
}

/**
* Given a project, track it in memory. If we already track it, update it if it's more recently edited than this project.
*
Expand Down
12 changes: 11 additions & 1 deletion src/routes/gallery/[galleryid]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import type Project from '../../../models/Project';
import ProjectPreviewSet from '@components/app/ProjectPreviewSet.svelte';
import AddProject from '@components/app/AddProject.svelte';
import { EDIT_SYMBOL } from '../../../parser/Symbols';
import { COPY_SYMBOL, EDIT_SYMBOL } from '../../../parser/Symbols';
import Spinning from '@components/app/Spinning.svelte';
const user = getUser();
Expand Down Expand Up @@ -170,6 +170,16 @@
label: EDIT_SYMBOL,
}
: false}
copy={{
description: $locales.get(
(l) => l.ui.project.button.duplicate,
),
action: (project) =>
goto(
Projects.duplicate(project).getLink(false),
),
label: COPY_SYMBOL,
}}
remove={(project) => {
return editable
? {
Expand Down
9 changes: 8 additions & 1 deletion src/routes/projects/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import Feedback from '@components/app/Feedback.svelte';
import { get } from 'svelte/store';
import Subheader from '@components/app/Subheader.svelte';
import { EDIT_SYMBOL } from '../../parser/Symbols';
import { COPY_SYMBOL, EDIT_SYMBOL } from '../../parser/Symbols';
import AddProject from '@components/app/AddProject.svelte';
const user = getUser();
Expand Down Expand Up @@ -71,6 +71,12 @@
action: (project) => goto(project.getLink(false)),
label: EDIT_SYMBOL,
}}
copy={{
description: $locales.get((l) => l.ui.project.button.duplicate),
action: (project) =>
goto(Projects.duplicate(project).getLink(false)),
label: COPY_SYMBOL,
}}
remove={(project) => {
return {
prompt: $locales.get(
Expand Down Expand Up @@ -114,6 +120,7 @@
Projects.archiveProject(project.getID(), false),
label: '↑🗑️',
}}
copy={false}
remove={(project) =>
$user && project.getOwner() === $user.uid
? {
Expand Down

0 comments on commit b46347c

Please sign in to comment.