Skip to content

Commit

Permalink
Allow N offset days in date widget (#336).
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Aug 16, 2024
1 parent dfc21b4 commit a9aeedd
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion defaults/core/cms/fields/date.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,37 @@
export let schema, parentKeys, field;
let today;
let today, todayPlusN, todayMinusN, N;
if (schema && schema[parentKeys]?.options) {
today = schema[parentKeys].options.includes("today");
const regex = /^today\s?(\+|\-)\s?([0-9]+)$/g;
const match = schema[parentKeys].options.find(option => regex.test(option));
if (match) {
const [, operation, daysOffset] = new RegExp(regex).exec(match);
todayPlusN = operation === "+";
todayMinusN = operation === "-";
N = parseInt(daysOffset);
}
}
if (today) {
let date = new Date();
date = makeDate(date);
field = formatDate(date, field);
}
if (todayPlusN) {
let date = new Date();
date.setDate(date.getDate() + N);
date = makeDate(date);
field = formatDate(date, field);
}
if (todayMinusN) {
let date = new Date();
date.setDate(date.getDate() - N);
date = makeDate(date);
field = formatDate(date, field);
}
const bindDate = date => {
field = formatDate(date, field);
Expand Down

0 comments on commit a9aeedd

Please sign in to comment.