feat: better monthly select

This commit is contained in:
2026-07-21 10:23:56 +02:00
parent 02bf7bcc04
commit 69512a4f1d
4 changed files with 74 additions and 27 deletions
+8 -4
View File
@@ -22,10 +22,14 @@
"Sunday" "Sunday"
], ],
"month": { "month": {
"st": "st", "st": "The {0}st",
"nd": "nd", "nd": "The {0}nd",
"rd": "rd", "rd": "The {0}rd",
"th": "th" "th": "The {0}th",
"first_day": "First day of month",
"last_day": "Last day of month",
"last": "Last",
"first": "First"
}, },
"year": [ "year": [
"January", "January",
+8 -4
View File
@@ -22,10 +22,14 @@
"Dimanche" "Dimanche"
], ],
"month": { "month": {
"st": "", "st": "Le {0}",
"nd": "", "nd": "Le {0}",
"rd": "", "rd": "Le {0}",
"th": "" "th": "Le {0}",
"first_day": "Premier jour du mois",
"last_day": "Dernier jour du mois",
"last": "Dernier",
"first": "Premier"
}, },
"year": [ "year": [
"Janvier", "Janvier",
+34 -19
View File
@@ -11,23 +11,23 @@ const valid = ref<boolean>(true);
const props = defineProps<{ editMode: boolean }>(); const props = defineProps<{ editMode: boolean }>();
const cronType = ref<CronType>(CronType.MANUAL); const cronType = ref<CronType>(CronType.MANUAL);
const cronValue = ref<number>(0); const cronValue = ref<string>('0');
function onCronChanged() { function onCronChanged() {
const data = parseCron(cron.value); const data = parseCron(cron.value);
rawCron.value = cron.value ?? DEFAULT_CRON; rawCron.value = cron.value ?? DEFAULT_CRON;
cronType.value = data.type; cronType.value = data.type;
cronValue.value = data.value; cronValue.value = data.value.toFixed(0);
} }
function updateCron() { function updateCron() {
cron.value = getCron(cronType.value, cronValue.value, rawCron.value); cron.value = getCron(cronType.value, parseInt(cronValue.value), rawCron.value);
} }
function onChangeCronType() { function onChangeCronType() {
switch (cronType.value) { switch (cronType.value) {
case CronType.EVERY_DAY: case CronType.EVERY_DAY:
cronValue.value = 0; cronValue.value = '0';
break; break;
case CronType.EVERY_WEEK: case CronType.EVERY_WEEK:
case CronType.EVERY_MONTH: case CronType.EVERY_MONTH:
@@ -35,7 +35,7 @@ function onChangeCronType() {
case CronType.EVERY_N_DAYS: case CronType.EVERY_N_DAYS:
case CronType.EVERY_N_WEEKS: case CronType.EVERY_N_WEEKS:
case CronType.EVERY_N_MONTHS: case CronType.EVERY_N_MONTHS:
cronValue.value = 1; cronValue.value = '1';
break; break;
case CronType.CUSTOM: case CronType.CUSTOM:
cron.value ??= DEFAULT_CRON; cron.value ??= DEFAULT_CRON;
@@ -106,7 +106,7 @@ watch(cron, () => {
> >
<option <option
v-for="i in Array.from(Array(24).keys())" v-for="i in Array.from(Array(24).keys())"
:key="`h${i}`" :key="`h${i+1}`"
:value="i" :value="i"
> >
{{ i.toFixed(0).padStart(2, "0") }}:00 {{ i.toFixed(0).padStart(2, "0") }}:00
@@ -120,7 +120,7 @@ watch(cron, () => {
> >
<option <option
v-for="i in Array.from(Array(7).keys())" v-for="i in Array.from(Array(7).keys())"
:key="`w${i}`" :key="`w${i+1}`"
:value="i + 1" :value="i + 1"
> >
{{ $t(`cron.week.${i.toFixed(0)}`) }} {{ $t(`cron.week.${i.toFixed(0)}`) }}
@@ -132,20 +132,35 @@ watch(cron, () => {
class="select flex-1" class="select flex-1"
@change="onChangeCronValue" @change="onChangeCronValue"
> >
<option :value="1">{{ $t('cron.month.first_day') }}</option>
<option :value="32">{{ $t('cron.month.last_day') }}</option>
<option <option
v-for="i in Array.from(Array(31).keys())" v-for="i in Array.from(Array(7).keys())"
:key="`d${i}`" :key="`d${-(i+1)}`"
:value="i + 1" :value="-(i + 1)"
> >
{{ i + 1 {{ $t("cron.month.first") }} {{ $t(`cron.week.${i.toFixed(0)}`).toLowerCase() }}
}}{{ </option>
i % 10 == 0 <option
? $t("cron.month.st") v-for="i in Array.from(Array(7).keys())"
: i % 10 === 1 :key="`d${-(i+8)}`"
? $t("cron.month.nd") :value="-(i + 8)"
: i % 10 === 2 >
? $t("cron.month.rd") {{ $t("cron.month.last") }} {{ $t(`cron.week.${i.toFixed(0)}`).toLowerCase() }}
: $t("cron.month.th") </option>
<option
v-for="i in Array.from(Array(30).keys())"
:key="`d${i+2}`"
:value="i + 2"
>
{{
(i % 10 == 9 && i !== 9)
? $t("cron.month.st", [i+2])
: (i % 10 === 0 && i !== 10)
? $t("cron.month.nd", [i+2])
: (i % 10 === 1 && i !== 11)
? $t("cron.month.rd", [i+2])
: $t("cron.month.th", [i+2])
}} }}
</option> </option>
</select> </select>
+24
View File
@@ -36,6 +36,21 @@ export function parseCron(cron: string | null): {
type: CronType.EVERY_MONTH, type: CronType.EVERY_MONTH,
value: parseInt(value[1] ?? ""), value: parseInt(value[1] ?? ""),
}; };
} else if ((value = /^0 0 L \* \*$/i.exec(cron)) !== null) {
return {
type: CronType.EVERY_MONTH,
value: 32,
};
} else if ((value = /^0 0 \* \* (\d+)\#1$/i.exec(cron)) !== null) {
return {
type: CronType.EVERY_MONTH,
value: -parseInt(value[1] ?? ""),
};
} else if ((value = /^0 0 \* \* (\d+)L$/i.exec(cron)) !== null) {
return {
type: CronType.EVERY_MONTH,
value: -parseInt(value[1] ?? "")-7,
};
} else if ((value = /^0 0 1 (\d+) \*$/i.exec(cron)) !== null) { } else if ((value = /^0 0 1 (\d+) \*$/i.exec(cron)) !== null) {
return { return {
type: CronType.EVERY_YEAR, type: CronType.EVERY_YEAR,
@@ -99,6 +114,15 @@ export function getCron(
case CronType.EVERY_WEEK: case CronType.EVERY_WEEK:
return `0 0 * * ${value.toFixed(0)}`; return `0 0 * * ${value.toFixed(0)}`;
case CronType.EVERY_MONTH: case CronType.EVERY_MONTH:
if (value === 32) {
return `0 0 L * *`;
}
if (value < 0 && value >= -7) {
return `0 0 * * ${(-value).toFixed(0)}#1`;
}
if (value < -7 && value >= -14) {
return `0 0 * * ${(-value-7).toFixed(0)}L`;
}
return `0 0 ${value.toFixed(0)} * *`; return `0 0 ${value.toFixed(0)} * *`;
case CronType.EVERY_YEAR: case CronType.EVERY_YEAR:
return `0 0 1 ${value.toFixed(0)} *`; return `0 0 1 ${value.toFixed(0)} *`;