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

Remove moment from the public API of the date module #11418

Merged
merged 2 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions docs/reference/deprecated.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Gutenberg's deprecation policy is intended to support backwards-compatibility fo
- `wp.data` `registry.registerSelectors` has been deprecated. Use `registry.registerStore` instead.
- `wp.data` `registry.registerActions` has been deprecated. Use `registry.registerStore` instead.
- `wp.data` `registry.registerResolvers` has been deprecated. Use `registry.registerStore` instead.
- `moment` has been removed from the public API for the date module.

## 4.3.0

Expand Down
8 changes: 7 additions & 1 deletion packages/date/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
## 2.2.0 (Unreleased)

### Deprecations

- Remove `moment` from the public API for the date module.
tofumatt marked this conversation as resolved.
Show resolved Hide resolved

## 2.1.0 (2018-10-29)

### Breaking Change

- Marked getSettings as experimental
- Marked getSettings as experimental

## 2.0.3 (2018-09-26)

Expand Down
20 changes: 20 additions & 0 deletions packages/date/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ function setupWPTimezone() {
// the attached timezone, instead of setting a default timezone on
// the global moment object.
export const moment = ( ...args ) => {
deprecated( 'wp.date.moment', {
version: '4.4',
alternative: 'the moment script as a dependency',
tofumatt marked this conversation as resolved.
Show resolved Hide resolved
plugin: 'Gutenberg',
} );

return momentLib.tz( ...args, 'WP' );
};

Expand Down Expand Up @@ -386,4 +392,18 @@ export function dateI18n( dateFormat, dateValue = new Date(), gmt = false ) {
return format( dateFormat, dateMoment );
}

/**
* Check whether a date is considered in the future according to the WordPress settings.
*
* @param {(Date|string)} dateValue Date object or string.
*
* @return {boolean} Is in the future.
*/
export function isInTheFuture( dateValue ) {
const now = momentLib.tz( 'WP' );
const momentObject = momentLib.tz( dateValue, 'WP' );

return momentObject.isAfter( now );
}

setupWPTimezone();
13 changes: 7 additions & 6 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
getFreeformContentHandlerName,
isUnmodifiedDefaultBlock,
} from '@wordpress/blocks';
import { moment } from '@wordpress/date';
import { isInTheFuture } from '@wordpress/date';
import { removep } from '@wordpress/autop';
import { select } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
Expand All @@ -54,6 +54,7 @@ export const INSERTER_UTILITY_NONE = 0;
const MILLISECONDS_PER_HOUR = 3600 * 1000;
const MILLISECONDS_PER_DAY = 24 * 3600 * 1000;
const MILLISECONDS_PER_WEEK = 7 * 24 * 3600 * 1000;
const ONE_MINUTE_IN_MS = 60 * 1000;

/**
* Shared reference to an empty array for cases where it is important to avoid
Expand Down Expand Up @@ -323,7 +324,7 @@ export function isCurrentPostPublished( state ) {
const post = getCurrentPost( state );

return [ 'publish', 'private' ].indexOf( post.status ) !== -1 ||
( post.status === 'future' && moment( post.date ).isBefore( moment() ) );
( post.status === 'future' && ! isInTheFuture( new Date( Number( new Date( post.date ) ) + ONE_MINUTE_IN_MS ) ) );
}

/**
Expand Down Expand Up @@ -481,11 +482,11 @@ export function hasAutosave( state ) {
* @return {boolean} Whether the post has been published.
*/
export function isEditedPostBeingScheduled( state ) {
const date = moment( getEditedPostAttribute( state, 'date' ) );
// Adding 1 minute as an error threshold between the server and the client dates.
const now = moment().add( 1, 'minute' );
const date = getEditedPostAttribute( state, 'date' );
// Offset the date by one minute (network latency)
const checkedDate = new Date( Number( new Date( date ) ) + ONE_MINUTE_IN_MS );

return date.isAfter( now );
return isInTheFuture( checkedDate );
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
setDefaultBlockName,
setFreeformContentHandlerName,
} from '@wordpress/blocks';
import { moment } from '@wordpress/date';
import { RawHTML } from '@wordpress/element';

/**
Expand Down Expand Up @@ -1513,10 +1512,12 @@ describe( 'selectors', () => {

describe( 'isEditedPostBeingScheduled', () => {
it( 'should return true for posts with a future date', () => {
const time = Date.now() + ( 1000 * 3600 * 24 * 7 ); // 7 days in the future
const date = new Date( time );
const state = {
editor: {
present: {
edits: { date: moment().add( 7, 'days' ).format( '' ) },
edits: { date: date.toUTCString() },
},
},
};
Expand Down