@@ -3,6 +3,7 @@ import { toRaw } from '@sveltia/utils/object';
33import { IndexedDB } from '@sveltia/utils/storage' ;
44import equal from 'fast-deep-equal' ;
55import { get , writable } from 'svelte/store' ;
6+ import { createProxy } from '$lib/services/contents/draft/create' ;
67import { entryDraft , i18nAutoDupEnabled } from '$lib/services/contents/draft' ;
78import { siteConfigVersion } from '$lib/services/config' ;
89import { backend } from '$lib/services/backends' ;
@@ -113,10 +114,12 @@ export const saveBackup = async (draft) => {
113114
114115/**
115116 * Check if a draft backup is available, and restore it if requested by the user.
116- * @param {string } collectionName - Collection name.
117- * @param {string } [slug] - Entry slug. Existing entry only.
117+ * @param {object } args - Arguments.
118+ * @param {string } args.collectionName - Collection name.
119+ * @param {string } [args.fileName] - Collection file name.
120+ * @param {string } [args.slug] - Entry slug. Existing entry only.
118121 */
119- export const restoreBackupIfNeeded = async ( collectionName , slug = '' ) => {
122+ export const restoreBackupIfNeeded = async ( { collectionName, fileName , slug = '' } ) => {
120123 const backup = await getBackup ( collectionName , slug ) ;
121124
122125 if ( ! backup ) {
@@ -141,10 +144,9 @@ export const restoreBackupIfNeeded = async (collectionName, slug = '') => {
141144 entryDraft . update ( ( draft ) => {
142145 if ( draft ) {
143146 draft . currentLocales = currentLocales ;
144- Object . entries ( draft . currentValues ) . forEach ( ( [ locale , _currentValues ] ) => {
145- Object . assign ( _currentValues , currentValues [ locale ] ) ;
146147
147- Object . entries ( _currentValues ) . forEach ( ( [ keyPath , value ] ) => {
148+ Object . entries ( currentValues ) . forEach ( ( [ locale , valueMap ] ) => {
149+ Object . entries ( valueMap ) . forEach ( ( [ keyPath , value ] ) => {
148150 if ( typeof value === 'string' ) {
149151 [ ...value . matchAll ( getBlobRegex ( 'g' ) ) ] . forEach ( ( [ blobURL ] ) => {
150152 const file = files [ blobURL ] ;
@@ -153,12 +155,26 @@ export const restoreBackupIfNeeded = async (collectionName, slug = '') => {
153155 // Regenerate a blob URL
154156 const newURL = URL . createObjectURL ( file ) ;
155157
156- _currentValues [ keyPath ] = value . replaceAll ( blobURL , newURL ) ;
158+ valueMap [ keyPath ] = value . replaceAll ( blobURL , newURL ) ;
157159 draft . files [ newURL ] = file ;
158160 }
159161 } ) ;
160162 }
161163 } ) ;
164+
165+ if ( draft . currentValues [ locale ] ) {
166+ Object . assign ( draft . currentValues [ locale ] , valueMap ) ;
167+ } else {
168+ draft . currentValues [ locale ] = createProxy ( {
169+ draft : { collectionName, fileName } ,
170+ locale,
171+ target : structuredClone ( valueMap ) ,
172+ } ) ;
173+ }
174+
175+ if ( ! draft . originalValues [ locale ] ) {
176+ draft . originalValues [ locale ] = { } ;
177+ }
162178 } ) ;
163179 }
164180
0 commit comments