Skip to content

Commit

Permalink
Remove moment from the public API of the date module
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 2, 2018
1 parent 122e569 commit 8e9b7f3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/reference/deprecated.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Gutenberg's deprecation policy is intended to support backwards-compatibility fo
- The following editor store actions have been removed: `createNotice`, `removeNotice`, `createSuccessNotice`, `createInfoNotice`, `createErrorNotice`, `createWarningNotice`. Use the equivalent actions by the same name from the `@wordpress/notices` module.
- The id prop of wp.nux.DotTip has been removed. Please use the tipId prop instead.
- `wp.blocks.isValidBlock` has been removed. Please use `wp.blocks.isValidBlockContent` instead but keep in mind that the order of params has changed.
- `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.1.1 (Unreleased)

### Deprecations

- Remove `moment` from the public API for the date module.

## 2.1.0 (2018-10-29)

### Breaking Change

- Marked getSettings as experimental
- Marked getSettings as experimental

## 2.0.3 (2018-09-26)

Expand Down
13 changes: 13 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',
plugin: 'Gutenberg',
} );

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

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

export function isInTheFuture( dateString, minutesOffset = 0 ) {
const now = momentLib.tz( 'WP' ).add( minutesOffset, 'minute' );
const momentObject = momentLib.tz( dateString, 'WP' );

return momentObject.isAfter( now );
}

setupWPTimezone();
10 changes: 3 additions & 7 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 Down Expand Up @@ -300,7 +300,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( post.date, 1 ) );
}

/**
Expand Down Expand Up @@ -458,11 +458,7 @@ 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' );

return date.isAfter( now );
return isInTheFuture( getEditedPostAttribute( state, 'date' ), 1 );
}

/**
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 @@ -1497,10 +1496,12 @@ describe( 'selectors', () => {

describe( 'isEditedPostBeingScheduled', () => {
it( 'should return true for posts with a future date', () => {
const time = Date.now() + ( 1000 * 3600 * 24 * 7 ); // sdays 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

0 comments on commit 8e9b7f3

Please sign in to comment.