Skip to content

Commit

Permalink
fix: ensure draft changes
Browse files Browse the repository at this point in the history
  • Loading branch information
barthc committed Feb 23, 2020
1 parent 14d5a65 commit c185bf7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
10 changes: 8 additions & 2 deletions packages/netlify-cms-core/src/actions/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,16 @@ export function discardDraft() {
return { type: DRAFT_DISCARD };
}

export function changeDraftField(field: string, value: string, metadata: Record<string, unknown>) {
export function changeDraftField(
field: string,
value: string,
metadata: Record<string, unknown>,
publishedEntry: EntryMap,
unpublishedEntry: EntryMap,
) {
return {
type: DRAFT_CHANGE_FIELD,
payload: { field, value, metadata },
payload: { field, value, metadata, publishedEntry, unpublishedEntry },
};
}

Expand Down
16 changes: 12 additions & 4 deletions packages/netlify-cms-core/src/components/Editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ export class Editor extends React.Component {
if (entry) this.props.createDraftFromEntry(entry, metadata);
};

handleChangeDraftField = (field, value, metadata) => {
const publishedEntry = this.props.publishedEntry;
const unPublishedEntry = this.props.unPublishedEntry;
this.props.changeDraftField(field, value, metadata, publishedEntry, unPublishedEntry);
};

handleChangeStatus = newStatusName => {
const {
entryDraft,
Expand Down Expand Up @@ -378,7 +384,6 @@ export class Editor extends React.Component {
entryDraft,
fields,
collection,
changeDraftField,
changeDraftFieldValidation,
user,
hasChanged,
Expand Down Expand Up @@ -421,7 +426,7 @@ export class Editor extends React.Component {
fields={fields}
fieldsMetaData={entryDraft.get('fieldsMetaData')}
fieldsErrors={entryDraft.get('fieldsErrors')}
onChange={changeDraftField}
onChange={this.handleChangeDraftField}
onValidate={changeDraftFieldValidation}
onPersist={this.handlePersistEntry}
onDelete={this.handleDeleteEntry}
Expand Down Expand Up @@ -463,8 +468,9 @@ function mapStateToProps(state, ownProps) {
const useOpenAuthoring = globalUI.get('useOpenAuthoring', false);
const isModification = entryDraft.getIn(['entry', 'isModification']);
const collectionEntriesLoaded = !!entries.getIn(['pages', collectionName]);
const unpublishedEntry = selectUnpublishedEntry(state, collectionName, slug);
const currentStatus = unpublishedEntry && unpublishedEntry.getIn(['metaData', 'status']);
const unPublishedEntry = selectUnpublishedEntry(state, collectionName, slug);
const publishedEntry = selectEntry(state, collectionName, slug);
const currentStatus = unPublishedEntry && unPublishedEntry.getIn(['metaData', 'status']);
const deployPreview = selectDeployPreview(state, collectionName, slug);
const localBackup = entryDraft.get('localBackup');
const draftKey = entryDraft.get('key');
Expand All @@ -488,6 +494,8 @@ function mapStateToProps(state, ownProps) {
deployPreview,
localBackup,
draftKey,
publishedEntry,
unPublishedEntry,
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/netlify-cms-core/src/formats/frontmatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class FrontmatterFormatter {
const result = matter(content, { engines: parsers, ...format });
return {
...result.data,
body: result.content,
...(result.content.trim() && { body: result.content }),
};
}

Expand Down
13 changes: 11 additions & 2 deletions packages/netlify-cms-core/src/reducers/entryDraft.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,22 @@ const entryDraftReducer = (state = Map(), action) => {
});
return state.set('localBackup', newState);
}
case DRAFT_CHANGE_FIELD:
return state.withMutations(state => {
case DRAFT_CHANGE_FIELD: {
const publishedEntry = action.payload.publishedEntry || Map();
const unpublishedEntry = action.payload.unpublishedEntry || Map();
let newState = state.withMutations(state => {
state.setIn(['entry', 'data', action.payload.field], action.payload.value);
state.mergeDeepIn(['fieldsMetaData'], fromJS(action.payload.metadata));
state.set('hasChanged', true);
});
const newStateData = newState.getIn(['entry', 'data']);

(newStateData.equals(unpublishedEntry.get('data')) ||
newStateData.equals(publishedEntry.get('data'))) &&
(newState = newState.set('hasChanged', false));

return newState;
}
case DRAFT_VALIDATION_ERRORS:
if (action.payload.errors.length === 0) {
return state.deleteIn(['fieldsErrors', action.payload.uniquefieldId]);
Expand Down

0 comments on commit c185bf7

Please sign in to comment.