@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
91 lines (75 loc) • 2.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = PostSchedule;
var _element = require("@wordpress/element");
var _date = require("@wordpress/date");
var _data = require("@wordpress/data");
var _components = require("@wordpress/components");
var _coreData = require("@wordpress/core-data");
var _store = require("../../store");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
function getDayOfTheMonth(date = new Date(), firstDay = true) {
const d = new Date(date);
return new Date(d.getFullYear(), d.getMonth() + (firstDay ? 0 : 1), firstDay ? 1 : 0).toISOString();
}
function PostSchedule() {
const {
postDate,
postType
} = (0, _data.useSelect)(select => ({
postDate: select(_store.store).getEditedPostAttribute('date'),
postType: select(_store.store).getCurrentPostType()
}), []);
const {
editPost
} = (0, _data.useDispatch)(_store.store);
const onUpdateDate = date => editPost({
date
});
const [previewedMonth, setPreviewedMonth] = (0, _element.useState)(getDayOfTheMonth(postDate)); // Pick up published and schduled site posts.
const eventsByPostType = (0, _data.useSelect)(select => select(_coreData.store).getEntityRecords('postType', postType, {
status: 'publish,future',
after: getDayOfTheMonth(previewedMonth),
before: getDayOfTheMonth(previewedMonth, false),
exclude: [select(_store.store).getCurrentPostId()]
}), [previewedMonth, postType]);
const events = (0, _element.useMemo)(() => (eventsByPostType || []).map(({
title,
type,
date: eventDate
}) => ({
title: title === null || title === void 0 ? void 0 : title.rendered,
type,
date: new Date(eventDate)
})), [eventsByPostType]);
const ref = (0, _element.useRef)();
const settings = (0, _date.__experimentalGetSettings)(); // To know if the current timezone is a 12 hour time with look for "a" in the time format
// We also make sure this a is not escaped by a "/"
const is12HourTime = /a(?!\\)/i.test(settings.formats.time.toLowerCase() // Test only the lower case a
.replace(/\\\\/g, '') // Replace "//" with empty strings
.split('').reverse().join('') // Reverse the string and test for "a" not followed by a slash
);
function onChange(newDate) {
onUpdateDate(newDate);
const {
ownerDocument
} = ref.current;
ownerDocument.activeElement.blur();
}
return (0, _element.createElement)(_components.DateTimePicker, {
ref: ref,
currentDate: postDate,
onChange: onChange,
is12Hour: is12HourTime,
events: events,
onMonthPreviewed: setPreviewedMonth
});
}
//# sourceMappingURL=index.js.map