fix(period): [ and ] snap odd ranges to calendar months, fresh visit opens the current period
This commit is contained in:
1 parent
741e5a49ba
commit
92db7d4093
2 files changed
+47
-8
No files matched your search
@@ -5,7 +5,7 @@ import {
|
||||
isDynamic,
|
||||
periodOf,
|
||||
type Range,
|
||||
shiftPeriod,
|
||||
stepPeriod,
|
||||
today,
|
||||
type ViewRange,
|
||||
} from "#lib/dates.js";
|
||||
@@ -23,7 +23,7 @@ export interface Preset {
|
||||
|
||||
function stored(): Range | null {
|
||||
try {
|
||||
const value = JSON.parse(localStorage.getItem(STORAGE_KEY) ?? "null");
|
||||
const value = JSON.parse(sessionStorage.getItem(STORAGE_KEY) ?? "null");
|
||||
return ISO_DATE.test(value?.start) && ISO_DATE.test(value?.end)
|
||||
? value
|
||||
: null;
|
||||
@@ -71,7 +71,7 @@ class Period {
|
||||
const range = rangeFromUrl(url) ?? stored() ?? periodOf(today(), view);
|
||||
this.start = range.start;
|
||||
this.end = range.end;
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(range));
|
||||
sessionStorage.setItem(STORAGE_KEY, JSON.stringify(range));
|
||||
this.ready = true;
|
||||
}
|
||||
|
||||
@@ -97,11 +97,11 @@ class Period {
|
||||
}
|
||||
this.start = range.start;
|
||||
this.end = range.end;
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(range));
|
||||
sessionStorage.setItem(STORAGE_KEY, JSON.stringify(range));
|
||||
}
|
||||
|
||||
shift(dir: 1 | -1) {
|
||||
this.set(shiftPeriod(this.range, this.view, dir));
|
||||
this.set(stepPeriod(this.range, this.view, dir));
|
||||
}
|
||||
|
||||
reset() {
|
||||
@@ -113,8 +113,8 @@ class Period {
|
||||
const { view } = this;
|
||||
const presets: Preset[] = [];
|
||||
if (!isDynamic(view)) {
|
||||
const prev = shiftPeriod(this.range, view, -1);
|
||||
const next = shiftPeriod(this.range, view, 1);
|
||||
const prev = stepPeriod(this.range, view, -1);
|
||||
const next = stepPeriod(this.range, view, 1);
|
||||
presets.push({ hint: "[", label: periodLabel(prev, view), range: prev });
|
||||
presets.push({ hint: "]", label: periodLabel(next, view), range: next });
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user