Skip to content

Commit

Permalink
Fix entry duplication issues
Browse files Browse the repository at this point in the history
As per #137
  • Loading branch information
kyoshino committed May 10, 2024
1 parent 1fa5deb commit 1bd5900
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import { Group } from '@sveltia/ui';
import { Alert, Group, Toast } from '@sveltia/ui';
import { onMount, tick } from 'svelte';
import { _ } from 'svelte-i18n';
import PaneBody from '$lib/components/contents/details/pane-body.svelte';
Expand All @@ -12,6 +12,7 @@
entryDraft,
entryEditorSettings,
showContentOverlay,
showDuplicateToast,
} from '$lib/services/contents/editor';
import { defaultI18nConfig, getLocaleLabel } from '$lib/services/contents/i18n';
Expand Down Expand Up @@ -236,6 +237,12 @@
</Group>
</div>
<Toast bind:show={$showDuplicateToast}>
<Alert status="success">
{$_('entry_duplicated')}
</Alert>
</Toast>
<style lang="scss">
.wrapper {
display: contents;
Expand Down
22 changes: 2 additions & 20 deletions src/lib/components/contents/details/toolbar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import { deleteEntries } from '$lib/services/contents/data';
import {
copyFromLocaleToast,
duplicateDraft,
entryDraft,
entryEditorSettings,
revertChanges,
Expand All @@ -37,7 +38,6 @@
import { defaultI18nConfig, getLocaleLabel } from '$lib/services/contents/i18n';
import { formatSummary } from '$lib/services/contents/view';
let showDuplicateToast = false;
let showValidationToast = false;
let showDeleteDialog = false;
let showErrorDialog = false;
Expand Down Expand Up @@ -80,19 +80,6 @@
? getEntryPreviewURL(originalEntry, defaultLocale, collection, collectionFile)
: undefined;
/**
* Duplicate the current entry.
*/
const duplicateDraft = async () => {
showDuplicateToast = true;
goto(`/collections/${collection?.name}/new`, { replaceState: true, notifyChange: false });
$entryDraft = {
.../** @type {EntryDraft} */ ($entryDraft ?? {}),
isNew: true,
originalEntry: undefined,
};
};
/**
* Save the entry draft.
* @param {object} [options] - Options.
Expand Down Expand Up @@ -165,6 +152,7 @@
aria-label={$_('duplicate_entry')}
disabled={collection?.create === false}
on:click={() => {
goto(`/collections/${collection?.name}/new`, { replaceState: true, notifyChange: false });
duplicateDraft();
}}
/>
Expand Down Expand Up @@ -261,12 +249,6 @@
{/if}
</Toolbar>
<Toast bind:show={showDuplicateToast}>
<Alert status="success">
{$_('entry_duplicated')}
</Alert>
</Toast>
<Toast bind:show={showValidationToast}>
<Alert status="error">
{$_(errorCount === 1 ? 'entry_validation_error' : 'entry_validation_errors', {
Expand Down
30 changes: 30 additions & 0 deletions src/lib/services/contents/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ const storageKey = 'entry-view';
*/
export const showContentOverlay = writable(false);

/**
* @type {import('svelte/store').Writable<boolean>}
*/
export const showDuplicateToast = writable(false);

/**
* @type {import('svelte/store').Writable<?EntryEditorPane>}
*/
Expand Down Expand Up @@ -413,6 +418,31 @@ export const createDraft = (entry, dynamicValues) => {
});
};

/**
* Duplicate the current entry draft.
*/
export const duplicateDraft = () => {
const draft = /** @type {EntryDraft} */ (get(entryDraft));
const { collection, collectionFile } = draft;

const {
canonicalSlug: { key: canonicalSlugKey },
} = (collectionFile ?? collection)._i18n;

// Remove the canonical slug
Object.keys(draft.currentValues).forEach((locale) => {
delete draft.currentValues[locale][canonicalSlugKey];
});

entryDraft.set({
...draft,
isNew: true,
originalEntry: undefined,
});

showDuplicateToast.set(true);
};

/**
* Unproxify & unflatten the given object.
* @param {Proxy | object} obj - Original proxy or object.
Expand Down

0 comments on commit 1bd5900

Please sign in to comment.