Merge branch 'main' into next

This commit is contained in:
Daniel Lautzenheiser
2023-02-24 08:18:07 -05:00
8 changed files with 439 additions and 577 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@staticcms/core",
"version": "1.2.8",
"version": "1.2.9",
"license": "MIT",
"description": "Static CMS core application.",
"repository": "https://github.com/StaticJsCMS/static-cms",
@ -112,7 +112,7 @@
"jwt-decode": "3.1.2",
"localforage": "1.10.0",
"lodash": "4.17.21",
"minimatch": "7.1.0",
"minimatch": "7.1.1",
"moment": "2.29.4",
"node-polyglot": "2.5.0",
"ol": "7.2.2",
@ -141,10 +141,10 @@
"sanitize-filename": "1.6.3",
"scheduler": "0.23.0",
"semaphore": "1.1.0",
"slate": "0.91.3",
"slate": "0.91.4",
"slate-history": "0.86.0",
"slate-hyperscript": "0.77.0",
"slate-react": "0.91.3",
"slate-react": "0.91.4",
"stream-browserify": "3.0.0",
"styled-components": "5.3.6",
"symbol-observable": "4.0.0",

View File

@ -383,16 +383,16 @@ export function duplicateI18nFields(
field: Field,
locales: string[],
defaultLocale: string,
fieldPath: string[] = [field.name],
fieldPath: string,
) {
const value = get(entryDraft, ['entry', 'data', ...fieldPath]);
const value = get(entryDraft, ['entry', 'data', ...fieldPath.split('.')]);
if (field.i18n === I18N_FIELD.DUPLICATE) {
locales
.filter(l => l !== defaultLocale)
.forEach(l => {
entryDraft = set(
entryDraft,
['entry', ...getDataPath(l, defaultLocale), ...fieldPath].join('.'),
['entry', ...getDataPath(l, defaultLocale), fieldPath].join('.'),
value,
);
});
@ -400,10 +400,13 @@ export function duplicateI18nFields(
if ('fields' in field && !Array.isArray(value)) {
field.fields?.forEach(field => {
entryDraft = duplicateI18nFields(entryDraft, field, locales, defaultLocale, [
...fieldPath,
field.name,
]);
entryDraft = duplicateI18nFields(
entryDraft,
field,
locales,
defaultLocale,
`${fieldPath}.${field.name}`,
);
});
}

View File

@ -2,6 +2,7 @@ import { DRAFT_CHANGE_FIELD, DRAFT_CREATE_EMPTY } from '@staticcms/core/constant
import mockEntry from '@staticcms/core/lib/test-utils/mock-data/MockEntry';
import entryDraftReducer from '../entryDraft';
import type { I18nSettings, StringOrTextField } from '@staticcms/core/interface';
import type { EntryDraftState } from '../entryDraft';
describe('entryDraft', () => {
@ -72,6 +73,119 @@ describe('entryDraft', () => {
path1: ['newValue1', 'newValue2Updated', 'newValue3'],
});
});
describe('i18n', () => {
it('duplicate values to other locales', () => {
const state = entryDraftReducer(startState, {
type: DRAFT_CHANGE_FIELD,
payload: {
path: 'path1.path2',
field: {
widget: 'string',
name: 'stringInput',
i18n: 'duplicate',
},
value: 'newValue',
i18n: {
locales: ['en', 'fr', 'es'],
defaultLocale: 'en',
currentLocale: 'en',
},
},
});
expect(state.entry?.data).toEqual({
path1: {
path2: 'newValue',
},
});
expect(state.entry?.i18n).toEqual({
fr: {
data: {
path1: {
path2: 'newValue',
},
},
},
es: {
data: {
path1: {
path2: 'newValue',
},
},
},
});
});
it('should duplicate values to other locales for singleton list', () => {
const field: StringOrTextField = {
widget: 'string',
name: 'stringInput',
i18n: 'duplicate',
};
const i18n: I18nSettings = {
locales: ['en', 'fr', 'es'],
defaultLocale: 'en',
currentLocale: 'en',
};
let state = entryDraftReducer(startState, {
type: DRAFT_CHANGE_FIELD,
payload: {
path: 'path1',
field,
value: ['newValue1', 'newValue2', 'newValue3'],
i18n,
},
});
expect(state.entry?.data).toEqual({
path1: ['newValue1', 'newValue2', 'newValue3'],
});
expect(state.entry?.i18n).toEqual({
fr: {
data: {
path1: ['newValue1', 'newValue2', 'newValue3'],
},
},
es: {
data: {
path1: ['newValue1', 'newValue2', 'newValue3'],
},
},
});
state = entryDraftReducer(state, {
type: DRAFT_CHANGE_FIELD,
payload: {
path: 'path1.1',
field,
value: 'newValue2Updated',
i18n,
},
});
expect(state.entry?.data).toEqual({
path1: ['newValue1', 'newValue2Updated', 'newValue3'],
});
expect(state.entry?.i18n).toEqual({
fr: {
data: {
path1: ['newValue1', 'newValue2Updated', 'newValue3'],
},
},
es: {
data: {
path1: ['newValue1', 'newValue2Updated', 'newValue3'],
},
},
});
});
});
});
});
});

View File

@ -163,7 +163,7 @@ function entryDraftReducer(
};
if (i18n) {
newState = duplicateI18nFields(newState, field, i18n.locales, i18n.defaultLocale);
newState = duplicateI18nFields(newState, field, i18n.locales, i18n.defaultLocale, path);
}
const newData = get(newState.entry, dataPath) ?? {};