Skip to content

Commit

Permalink
Fix object i18n duplication issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoshino committed Jul 3, 2024
1 parent e1e426f commit 64f32e1
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import { Checkbox, Group } from '@sveltia/ui';
import { generateUUID } from '@sveltia/utils/crypto';
import { waitForVisibility } from '@sveltia/utils/element';
import { toRaw } from '@sveltia/utils/object';
import { onMount, tick } from 'svelte';
import { _ } from 'svelte-i18n';
import FieldEditor from '$lib/components/contents/details/editor/field-editor.svelte';
import AddItemButton from '$lib/components/contents/details/widgets/object/add-item-button.svelte';
import ObjectHeader from '$lib/components/contents/details/widgets/object/object-header.svelte';
import { entryDraft } from '$lib/services/contents/draft';
import { createProxy, getDefaultValues } from '$lib/services/contents/draft/create';
import { entryDraft, i18nAutoDupEnabled } from '$lib/services/contents/draft';
import { getDefaultValues } from '$lib/services/contents/draft/create';
import { syncExpanderStates } from '$lib/services/contents/draft/editor';
import { copyDefaultLocaleValues } from '$lib/services/contents/draft/update';
import { getFieldDisplayValue } from '$lib/services/contents/entry';
Expand Down Expand Up @@ -110,6 +111,9 @@
* will be `undefined`.
*/
const addFields = async (typeName) => {
// Avoid triggering the Proxy’s i18n duplication strategy for descendant fields
$i18nAutoDupEnabled = false;
if (typeName) {
Object.keys($entryDraft?.currentValues ?? {}).forEach((_locale) => {
if (_locale === locale || i18n === 'duplicate') {
Expand All @@ -128,20 +132,20 @@
),
);
Object.keys($entryDraft?.currentValues ?? {}).forEach((_locale) => {
Object.entries($entryDraft?.currentValues ?? {}).forEach(([_locale, _valueMap]) => {
if (_locale === locale || i18n === 'duplicate') {
// Since we don’t want to trigger the Proxy’s i18n duplication strategy for descendant
// fields, manually update the locale’s content and proxify the object again
/** @type {EntryDraft} */ ($entryDraft).currentValues[_locale] = createProxy({
draft: $entryDraft,
locale: _locale,
target: { ...newValueMap, ...$entryDraft?.currentValues[_locale] },
});
// Apply the new values while keeping the Proxy
/** @type {EntryDraft} */ ($entryDraft).currentValues[_locale] = Object.assign(
_valueMap,
toRaw({ ...newValueMap, ..._valueMap }),
);
// Disable validation
delete (/** @type {EntryDraft} */ ($entryDraft).currentValues[_locale][keyPath]);
}
});
$i18nAutoDupEnabled = true;
};
/**
Expand Down

0 comments on commit 64f32e1

Please sign in to comment.