Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to set default month for calendar #2483

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/docs/queries/query-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,14 @@ CALENDAR due
WHERE typeof(due) = "date"
```
~~~

### CALENDAR DEFAULT MONTH

If you want to focus the calendar on a particular month (instead of the current month), you can use `CALENDAR DEFAULT MONTH`.

~~~
```dataview
CALENDAR file.ctime
DEFAULT MONTH 2024-11
```
~~~
5 changes: 4 additions & 1 deletion src/query/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,8 @@ export async function executeCalendar(
link: Fields.indexVariable("file.link"),
};

let displayedMonth = (query.header as CalendarQuery).displayedMonth;

return executeCoreExtract(fileset.value, rootContext, query.operations, fields).map(core => {
let data = core.data.map(p =>
iden({
Expand All @@ -497,11 +499,12 @@ export async function executeCalendar(
})
);

return { core, data };
return { core, data, displayedMonth };
});
}

export interface CalendarExecution {
core: CoreExecution;
data: { date: DateTime; link: Link; value?: Literal[] }[];
displayedMonth?: DateTime;
}
11 changes: 10 additions & 1 deletion src/query/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,20 @@ export const QUERY_LANGUAGE = P.createLanguage<QueryLanguageTypes>({
return P.succeed({ type });
case "calendar":
return P.whitespace.then(
P.seqMap(q.namedField, field => {
P.seqMap(
q.namedField,
P.whitespace
.then(P.regexp(/DEFAULT\s+MONTH/i))
.then(P.whitespace)
.then(EXPRESSION.date)
.skip(P.optWhitespace)
.atMost(1),
(field, month) => {
return {
type,
showId: true,
field,
displayedMonth: month.length == 1 ? month[0] : undefined,
} as QueryHeader;
})
antistic marked this conversation as resolved.
Show resolved Hide resolved
);
Expand Down
2 changes: 2 additions & 0 deletions src/query/query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** Provides an AST for complex queries. */
import { Source } from "data-index/source";
import { Field } from "expression/field";
import { DateTime } from "luxon";

/** The supported query types (corresponding to view types). */
export type QueryType = "list" | "table" | "task" | "calendar";
Expand Down Expand Up @@ -67,6 +68,7 @@ export interface CalendarQuery {
type: "calendar";
/** The date field that we'll be grouping notes by for the calendar view */
field: NamedField;
displayedMonth?: DateTime;
}

export type QueryHeader = ListQuery | TableQuery | TaskQuery | CalendarQuery;
Expand Down
3 changes: 2 additions & 1 deletion src/ui/views/calendar-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DataviewSettings } from "settings";
import { renderErrorPre } from "ui/render";
import { DataviewRefreshableRenderer } from "ui/refreshable-view";
import { asyncTryOrPropagate } from "util/normalize";
import type { Moment } from "moment";
import { default as moment, type Moment } from "moment";

// CalendarFile is a representation of a particular file, displayed in the calendar view.
// It'll be represented in the calendar as a dot.
Expand Down Expand Up @@ -99,6 +99,7 @@ export class DataviewCalendarRenderer extends DataviewRefreshableRenderer {
},
showWeekNums: false,
sources,
displayedMonth: moment(maybeResult.value.displayedMonth?.toISO()),
},
});
}
Expand Down
2 changes: 1 addition & 1 deletion test-vault/example calendars.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

```dataview
CALENDAR thoughtOfDate
DEFAULT MONTH 2021-12
FROM
"recipes"
```
Expand Down