Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RNMobile] Add autosave to mobile apps #17329

Merged
merged 18 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
8546c4a
[RNMobile] Fix crash when adding separator
mchowning Aug 21, 2019
1ca5e2b
Build: remove global install of latest npm since we want to use the p…
gwwar Aug 22, 2019
ef3f290
[RNMobile] Try dark mode (iOS) (#17067)
etoledom Aug 23, 2019
32e80ea
RNMobile: Fix crash when viewing HTML on iOS
mchowning Aug 20, 2019
316396c
[RNMobile] Remove toolbar from html view
mchowning Aug 22, 2019
a7bb5b3
[RNMobile] Fix MaxListenersExceededWarning caused by dark-mode event …
etoledom Aug 26, 2019
fe4c02f
Fix focus title on new posts regression (#17180)
etoledom Aug 26, 2019
bf84833
BottomSheet: Setting DashIcon color directly when theme is default (l…
etoledom Aug 26, 2019
f210ff5
Add a preliminary version of the AutosaveMonitor for mobile that call…
daniloercoli Sep 4, 2019
c6dd15c
Add autosave mock function for tests
daniloercoli Sep 4, 2019
5e0bc0c
Merge branch 'rnmobile/master' of https://github.com/WordPress/gutenb…
daniloercoli Sep 4, 2019
d68a430
Fix merge conflicts
daniloercoli Sep 4, 2019
e2e8a0c
Fix lint
daniloercoli Sep 4, 2019
9c82ea0
Re-add autosave on mobile that was removed erroneously during import-…
daniloercoli Sep 4, 2019
27f6f80
Remove native variant of AutosaveMonitor and introduces changes at e…
daniloercoli Sep 6, 2019
09f1907
Default to false for `isEditedPostAutosaveable` on mobile. There was …
daniloercoli Sep 6, 2019
62e106e
Make sure to consider edits to the Title when checking if auto-save i…
daniloercoli Sep 6, 2019
5e763e9
Fix lint
daniloercoli Sep 9, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/edit-post/src/components/layout/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Component } from '@wordpress/element';
import { withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { HTMLTextInput, KeyboardAvoidingView, ReadableContentView, withTheme } from '@wordpress/components';
import { AutosaveMonitor } from '@wordpress/editor';

/**
* Internal dependencies
Expand Down Expand Up @@ -116,6 +117,7 @@ class Layout extends Component {

return (
<SafeAreaView style={ useStyle( styles.container, styles.containerDark ) } onLayout={ this.onRootViewLayout }>
<AutosaveMonitor />
<View style={ useStyle( styles.background, styles.backgroundDark ) }>
{ isHtmlView ? this.renderHTML() : this.renderVisual() }
</View>
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/components/index.native.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

// Post Related Components
export { default as AutosaveMonitor } from './autosave-monitor';
export { default as PostTitle } from './post-title';
export { default as EditorHistoryRedo } from './editor-history/redo';
export { default as EditorHistoryUndo } from './editor-history/undo';
Expand Down
13 changes: 13 additions & 0 deletions packages/editor/src/store/actions.native.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* External dependencies
*/
import RNReactNativeGutenbergBridge from 'react-native-gutenberg-bridge';

export * from './actions.js';

Expand All @@ -14,3 +18,12 @@ export function togglePostTitleSelection( isSelected = true ) {
isSelected,
};
}

/**
* Action generator used in signalling that the post should autosave.
*
* @param {Object?} options Extra flags to identify the autosave.
*/
export function* autosave( ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super minor, but there's an extra space here

RNReactNativeGutenbergBridge.editorDidAutosave();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's better indeed 👍

}
4 changes: 4 additions & 0 deletions packages/editor/src/store/reducer.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import {
editorSettings,
} from './reducer.js';

import { EDITOR_SETTINGS_DEFAULTS } from './defaults.js';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably make our own defaults.native.js instead


EDITOR_SETTINGS_DEFAULTS.autosaveInterval = 2; // This is a way to override default on mobile

export * from './reducer.js';

/**
Expand Down
42 changes: 42 additions & 0 deletions packages/editor/src/store/selectors.native.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/**
* WordPress dependencies
*/
import { createRegistrySelector } from '@wordpress/data';

/**
* Internal dependencies
*/
import {
isEditedPostDirty,
isEditedPostSaveable,
hasChangedContent,
} from './selectors.js';

export * from './selectors.js';

Expand All @@ -11,3 +24,32 @@ export * from './selectors.js';
export function isPostTitleSelected( state ) {
return state.postTitle.isSelected;
}

/**
* Returns true if the post can be autosaved, or false otherwise.
*
* @param {Object} state Global application state.
* @param {Object} autosave A raw autosave object from the REST API.
*
* @return {boolean} Whether the post can be autosaved.
*/
export const isEditedPostAutosaveable = createRegistrySelector( ( ) => function( state ) {
// A post must contain a title, an excerpt, or non-empty content to be valid for autosaving.
if ( ! isEditedPostSaveable( state ) ) {
return false;
}

// To avoid an expensive content serialization, use the content dirtiness
// flag in place of content field comparison against the known autosave.
// This is not strictly accurate, and relies on a tolerance toward autosave
// request failures for unnecessary saves.
if ( hasChangedContent( state ) ) {
return true;
}

if ( isEditedPostDirty( state ) ) {
return true;
}

return false;
} );
1 change: 1 addition & 0 deletions test/native/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jest.mock( 'react-native-gutenberg-bridge', () => {
subscribeUpdateHtml: jest.fn(),
subscribeMediaAppend: jest.fn(),
editorDidMount: jest.fn(),
editorDidAutosave: jest.fn(),
subscribeMediaUpload: jest.fn(),
requestMediaPickFromMediaLibrary: jest.fn(),
requestMediaPickFromDeviceLibrary: jest.fn(),
Expand Down