Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
KaneFreeman committed Oct 23, 2023
2 parents ee29bff + c1ccc15 commit 434cbb8
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 58 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "3.3.9"
"version": "3.3.10"
}
4 changes: 2 additions & 2 deletions packages/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@staticcms/app",
"version": "3.3.9",
"version": "3.3.10",
"license": "MIT",
"description": "Static CMS application.",
"repository": "https://github.com/StaticJsCMS/static-cms",
Expand Down Expand Up @@ -39,7 +39,7 @@
"dependencies": {
"@babel/eslint-parser": "7.22.15",
"@babel/runtime": "7.23.1",
"@staticcms/core": "^3.3.9",
"@staticcms/core": "^3.3.10",
"buffer": "6.0.3",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@staticcms/core",
"version": "3.3.9",
"version": "3.3.10",
"license": "MIT",
"description": "Static CMS core application.",
"repository": "https://github.com/StaticJsCMS/static-cms",
Expand Down Expand Up @@ -110,6 +110,7 @@
"gray-matter": "4.0.3",
"history": "5.3.0",
"immer": "10.0.1",
"immutable": "4.3.4",
"ini": "4.1.0",
"is-hotkey": "0.2.0",
"js-base64": "3.7.5",
Expand Down
64 changes: 35 additions & 29 deletions packages/core/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import attempt from 'lodash/attempt';
import flatten from 'lodash/flatten';
import get from 'lodash/get';
import isError from 'lodash/isError';
import set from 'lodash/set';
import uniq from 'lodash/uniq';
import { dirname, extname } from 'path';

Expand Down Expand Up @@ -53,6 +52,7 @@ import { DRAFT_MEDIA_FILES, selectMediaFilePublicPath } from './lib/util/media.u
import { selectCustomPath, slugFromCustomPath } from './lib/util/nested.util';
import { isNotNullish, isNullish } from './lib/util/null.util';
import { fileSearch, sortByScore } from './lib/util/search.util';
import set from './lib/util/set.util';
import { dateParsers, expandPath, extractTemplateVars } from './lib/widgets/stringTemplate';
import { getUseWorkflow } from './reducers/selectors/config';
import createEntry from './valueObjects/createEntry';
Expand Down Expand Up @@ -193,19 +193,22 @@ export function expandSearchEntries(
field: string;
})[] {
// expand the entries for the purpose of the search
const expandedEntries = entries.reduce((acc, e) => {
const expandedFields = searchFields.reduce((acc, f) => {
const fields = expandPath({ data: e.data, path: f });
acc.push(...fields);
return acc;
}, [] as string[]);
const expandedEntries = entries.reduce(
(acc, e) => {
const expandedFields = searchFields.reduce((acc, f) => {
const fields = expandPath({ data: e.data, path: f });
acc.push(...fields);
return acc;
}, [] as string[]);

for (let i = 0; i < expandedFields.length; i++) {
acc.push({ ...e, field: expandedFields[i] });
}
for (let i = 0; i < expandedFields.length; i++) {
acc.push({ ...e, field: expandedFields[i] });
}

return acc;
}, [] as (Entry & { field: string })[]);
return acc;
},
[] as (Entry & { field: string })[],
);

return expandedEntries;
}
Expand All @@ -215,27 +218,30 @@ export function mergeExpandedEntries(entries: (Entry & { field: string })[]): En
const fields = entries.map(f => f.field);
const arrayPaths: Record<string, Set<string>> = {};

const merged = entries.reduce((acc, e) => {
if (!acc[e.slug]) {
const { field: _field, ...rest } = e;
acc[e.slug] = rest;
arrayPaths[e.slug] = new Set();
}
const merged = entries.reduce(
(acc, e) => {
if (!acc[e.slug]) {
const { field: _field, ...rest } = e;
acc[e.slug] = rest;
arrayPaths[e.slug] = new Set();
}

const nestedFields = e.field.split('.');
let value: ValueOrNestedValue = acc[e.slug].data;
for (let i = 0; i < nestedFields.length; i++) {
if (isNotNullish(value)) {
value = value[nestedFields[i]];
if (Array.isArray(value)) {
const path = nestedFields.slice(0, i + 1).join('.');
arrayPaths[e.slug] = arrayPaths[e.slug].add(path);
const nestedFields = e.field.split('.');
let value: ValueOrNestedValue = acc[e.slug].data;
for (let i = 0; i < nestedFields.length; i++) {
if (isNotNullish(value)) {
value = value[nestedFields[i]];
if (Array.isArray(value)) {
const path = nestedFields.slice(0, i + 1).join('.');
arrayPaths[e.slug] = arrayPaths[e.slug].add(path);
}
}
}
}

return acc;
}, {} as Record<string, Entry>);
return acc;
},
{} as Record<string, Entry>,
);

// this keeps the search score sorting order designated by the order in entries
// and filters non matching items
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import get from 'lodash/get';
import set from 'lodash/set';

import { COMMIT_AUTHOR, COMMIT_DATE } from '../constants/commitProps';
import { sanitizeSlug } from './urlHelper';
import { getFields, selectIdentifier, selectInferredField } from './util/collection.util';
import { getField, selectField } from './util/field.util';
import set from './util/set.util';
import { isEmpty } from './util/string.util';
import {
addFileTemplateFields,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import escapeRegExp from 'lodash/escapeRegExp';
import get from 'lodash/get';
import groupBy from 'lodash/groupBy';
import set from 'lodash/set';

import { fileForEntry, selectEntrySlug } from './util/collection.util';
import set from './util/set.util';

import type {
BaseField,
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/lib/util/set.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { setIn } from 'immutable';

export default function set<T>(object: T, path: string, value: unknown): T {
return setIn(object, path.split('.'), value);
}
2 changes: 1 addition & 1 deletion packages/core/src/reducers/entries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import once from 'lodash/once';
import set from 'lodash/set';
import sortBy from 'lodash/sortBy';

import {
Expand All @@ -24,6 +23,7 @@ import {
SORT_ENTRIES_SUCCESS,
} from '../constants';
import { VIEW_STYLES, VIEW_STYLE_TABLE } from '../constants/views';
import set from '../lib/util/set.util';

import type { EntriesAction } from '../actions/entries';
import type { SearchAction } from '../actions/search';
Expand Down
31 changes: 9 additions & 22 deletions packages/core/src/reducers/entryDraft.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import cloneDeep from 'lodash/cloneDeep';
import isEqual from 'lodash/isEqual';
import set from 'lodash/set';
import { v4 as uuid } from 'uuid';

import {} from '../actions/editorialWorkflow';
Expand Down Expand Up @@ -35,6 +34,7 @@ import {
import { duplicateI18nFields, getDataPath } from '../lib/i18n';
import { fileForEntry } from '../lib/util/collection.util';
import { applyDefaultsToDraftData } from '../lib/util/entry.util';
import set from '../lib/util/set.util';

import type { EditorialWorkflowAction } from '../actions/editorialWorkflow';
import type { EntriesAction } from '../actions/entries';
Expand Down Expand Up @@ -172,20 +172,13 @@ function entryDraftReducer(
}

case DRAFT_UPDATE: {
let newState = { ...state };
if (!newState.entry) {
if (!state.entry) {
return state;
}

const { data } = action.payload;

newState = {
...newState,
entry: {
...newState.entry,
data,
},
};
const newState = set(state, 'entry.data', data);

let hasChanged =
!isEqual(newState.entry?.meta, newState.original?.meta) ||
Expand All @@ -205,8 +198,7 @@ function entryDraftReducer(
}

case DRAFT_CHANGE_FIELD: {
let newState = { ...state };
if (!newState.entry) {
if (!state.entry) {
return state;
}

Expand All @@ -215,26 +207,21 @@ function entryDraftReducer(
? ['meta']
: (i18n && getDataPath(i18n.currentLocale, i18n.defaultLocale)) || ['data'];

const newEntry = cloneDeep(newState.entry);

newState = {
...newState,
entry: set(newEntry, `${dataPath.join('.')}.${path}`, value),
};
let newState = set(state, `entry.${dataPath.join('.')}.${path}`, value);

if (i18n) {
newState = duplicateI18nFields(newState, field, i18n.locales, i18n.defaultLocale, path);
}

let hasChanged =
!isEqual(newEntry?.meta, newState.original?.meta) ||
!isEqual(newEntry?.data, newState.original?.data);
!isEqual(newState.entry?.meta, newState.original?.meta) ||
!isEqual(newState.entry?.data, newState.original?.data);

const i18nData = newEntry?.i18n ?? {};
const i18nData = newState.entry?.i18n ?? {};
for (const locale in i18nData) {
hasChanged =
hasChanged ||
!isEqual(newEntry?.i18n?.[locale]?.data, newState.original?.i18n?.[locale]?.data);
!isEqual(newState.entry?.i18n?.[locale]?.data, newState.original?.i18n?.[locale]?.data);
}

return {
Expand Down
5 changes: 5 additions & 0 deletions packages/docs/content/releases.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"releases": [
{
"date": "2023-10-15T10:00:00.000Z",
"version": "v3.3.10",
"type": "patch"
},
{
"date": "2023-10-12T10:00:00.000Z",
"version": "v3.3.9",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10469,6 +10469,11 @@ immer@^9.0.21, immer@^9.0.6:
resolved "https://registry.yarnpkg.com/immer/-/immer-9.0.21.tgz#1e025ea31a40f24fb064f1fef23e931496330176"
integrity sha512-bc4NBHqOqSfRW7POMkHd51LvClaeMXpm8dx0e8oE2GORbq5aRK7Bxl4FyzVLdGtLmvLKL7BTDBG5ACQm4HWjTA==

[email protected]:
version "4.3.4"
resolved "https://registry.yarnpkg.com/immutable/-/immutable-4.3.4.tgz#2e07b33837b4bb7662f288c244d1ced1ef65a78f"
integrity sha512-fsXeu4J4i6WNWSikpI88v/PcVflZz+6kMhUfIwc5SY+poQRPnaf5V7qds6SUyUN3cVxEzuCab7QIoLOQ+DQ1wA==

import-fresh@^3.2.1, import-fresh@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.0.tgz#37162c25fcb9ebaa2e6e53d5b4d88ce17d9e0c2b"
Expand Down

0 comments on commit 434cbb8

Please sign in to comment.