mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-10-06 19:26:55 +08:00
fix(snooze/send-later): Change chrono config to always prefer dates in future
This commit is contained in:
parent
ecf3909f7d
commit
030d86e3bd
3 changed files with 53 additions and 8 deletions
|
@ -60,7 +60,7 @@ class SendLaterPopover extends Component {
|
||||||
};
|
};
|
||||||
|
|
||||||
onSelectCustomOption = (value)=> {
|
onSelectCustomOption = (value)=> {
|
||||||
const date = DateUtils.fromString(value);
|
const date = DateUtils.futureDateFromString(value);
|
||||||
if (date) {
|
if (date) {
|
||||||
this.onSelectDate(date);
|
this.onSelectDate(date);
|
||||||
} else {
|
} else {
|
||||||
|
@ -83,7 +83,7 @@ class SendLaterPopover extends Component {
|
||||||
|
|
||||||
renderCustomTimeSection() {
|
renderCustomTimeSection() {
|
||||||
const onChange = (event)=> {
|
const onChange = (event)=> {
|
||||||
this.setState({inputDate: DateUtils.fromString(event.target.value)});
|
this.setState({inputDate: DateUtils.futureDateFromString(event.target.value)});
|
||||||
}
|
}
|
||||||
|
|
||||||
const onKeyDown = (event)=> {
|
const onKeyDown = (event)=> {
|
||||||
|
@ -130,7 +130,7 @@ class SendLaterPopover extends Component {
|
||||||
|
|
||||||
if (scheduledDate === 'saving') {
|
if (scheduledDate === 'saving') {
|
||||||
return (
|
return (
|
||||||
<button className={className} title="Send later...">
|
<button className={className} title="Saving send date...">
|
||||||
<RetinaImg
|
<RetinaImg
|
||||||
name="inline-loading-spinner.gif"
|
name="inline-loading-spinner.gif"
|
||||||
mode={RetinaImg.Mode.ContentDark}
|
mode={RetinaImg.Mode.ContentDark}
|
||||||
|
@ -142,13 +142,13 @@ class SendLaterPopover extends Component {
|
||||||
let dateInterpretation = false;
|
let dateInterpretation = false;
|
||||||
if (scheduledDate) {
|
if (scheduledDate) {
|
||||||
className += ' btn-enabled';
|
className += ' btn-enabled';
|
||||||
const momentDate = DateUtils.fromString(scheduledDate);
|
const momentDate = DateUtils.futureDateFromString(scheduledDate);
|
||||||
if (momentDate) {
|
if (momentDate) {
|
||||||
dateInterpretation = <span className="at">Sending in {momentDate.fromNow(true)}</span>;
|
dateInterpretation = <span className="at">Sending in {momentDate.fromNow(true)}</span>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<button className={className}>
|
<button className={className} title="Send later...">
|
||||||
<RetinaImg name="icon-composer-sendlater.png" mode={RetinaImg.Mode.ContentIsMask}/>
|
<RetinaImg name="icon-composer-sendlater.png" mode={RetinaImg.Mode.ContentIsMask}/>
|
||||||
{dateInterpretation}
|
{dateInterpretation}
|
||||||
<span> </span>
|
<span> </span>
|
||||||
|
|
|
@ -99,7 +99,7 @@ class SnoozePopoverBody extends Component {
|
||||||
};
|
};
|
||||||
|
|
||||||
updateInputDateValue = _.debounce((dateValue)=> {
|
updateInputDateValue = _.debounce((dateValue)=> {
|
||||||
const inputDate = DateUtils.fromString(dateValue)
|
const inputDate = DateUtils.futureDateFromString(dateValue)
|
||||||
this.setState({inputDate})
|
this.setState({inputDate})
|
||||||
}, 250);
|
}, 250);
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,51 @@
|
||||||
/** @babel */
|
/** @babel */
|
||||||
import moment from 'moment'
|
import moment from 'moment'
|
||||||
import chrono from 'chrono-node'
|
import chrono from 'chrono-node'
|
||||||
|
import _ from 'underscore'
|
||||||
|
|
||||||
|
function isPastDate({year, month, day}, ref) {
|
||||||
|
const refDay = ref.getDate();
|
||||||
|
const refMonth = ref.getMonth() + 1;
|
||||||
|
const refYear = ref.getFullYear();
|
||||||
|
if (refYear > year) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (refMonth > month) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (refDay > day) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const EnforceFutureDate = new chrono.Refiner();
|
||||||
|
EnforceFutureDate.refine = (text, results)=> {
|
||||||
|
results.forEach((result)=> {
|
||||||
|
const current = _.extend({}, result.start.knownValues, result.start.impliedValues);
|
||||||
|
|
||||||
|
if (result.start.isCertain('weekday') && !result.start.isCertain('day')) {
|
||||||
|
if (isPastDate(current, result.ref)) {
|
||||||
|
result.start.imply('day', result.start.impliedValues.day + 7);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.start.isCertain('day') && !result.start.isCertain('month')) {
|
||||||
|
if (isPastDate(current, result.ref)) {
|
||||||
|
result.start.imply('month', result.start.impliedValues.month + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (result.start.isCertain('month') && !result.start.isCertain('year')) {
|
||||||
|
if (isPastDate(current, result.ref)) {
|
||||||
|
result.start.imply('year', result.start.impliedValues.year + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return results;
|
||||||
|
};
|
||||||
|
|
||||||
|
const chronoFuture = new chrono.Chrono(chrono.options.casualOption());
|
||||||
|
chronoFuture.refiners.push(EnforceFutureDate);
|
||||||
|
|
||||||
const Hours = {
|
const Hours = {
|
||||||
Morning: 9,
|
Morning: 9,
|
||||||
|
@ -86,8 +131,8 @@ const DateUtils = {
|
||||||
* @param {string} dateLikeString - a string representing a date.
|
* @param {string} dateLikeString - a string representing a date.
|
||||||
* @return {moment} - moment object representing date
|
* @return {moment} - moment object representing date
|
||||||
*/
|
*/
|
||||||
fromString(dateLikeString) {
|
futureDateFromString(dateLikeString) {
|
||||||
const date = chrono.parseDate(dateLikeString)
|
const date = chronoFuture.parseDate(dateLikeString)
|
||||||
if (!date) {
|
if (!date) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue