Skip to content

Commit 4cfca48

Browse files
authored
Merge pull request #3300 from obsidian-tasks-group/query-file-defaults
feat: Add "Query File Defaults" facility
2 parents 9351dae + a6842e3 commit 4cfca48

File tree

40 files changed

+2693
-121
lines changed

40 files changed

+2693
-121
lines changed

docs/Getting Started/Obsidian Properties.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ group by function \
183183
return value ? window.moment(value).format('YYYY MMMM') : 'no date'
184184
```
185185

186-
## Using Query Properties in Placeholders
186+
## Using Query Properties in Searches
187187

188188
> [!released]
189189
> Use of Obsidian properties in placeholders was introduced in Tasks X.Y.Z.
@@ -192,31 +192,32 @@ group by function \
192192
- `query.file.hasProperty()` works.
193193
- `query.file.property()` works.
194194

195-
Imagine this text at the top of the note containing the query:
195+
Imagine this text at the top of **the note containing the query**:
196196

197197
```yaml
198198
---
199199
search-text: exercise
200+
workdate: 2024-04-01
201+
groupby: group by happens
200202
---
201203
```
202204

203-
It can be used in your query in two ways:
205+
It can be used in queries in several ways:
204206

205-
1. A search term from front-matter embedded via placeholder:
207+
1. A search term from frontmatter embedded via placeholder:
206208

207209
```javascript
208210
description includes {{query.file.property('search-text')}}
211+
due on or before {{query.file.property('workdate')}}
209212
```
210213

211-
1. An entire instruction controlled by front-matter value:
214+
2. An entire instruction controlled by front-matter value:
212215

213216
```javascript
214-
{{const prop = 'TQ-explain'; return query.file.hasProperty(prop) ? ( query.file.property(prop) ? 'explain' : '') : '';}}
215-
216-
{{const prop = 'TQ-show-tree'; return query.file.hasProperty(prop) && ( query.file.property(prop) ? 'show' : 'hide') + ' tree' || ''}}
217+
{{query.file.property('groupby')}}
217218
```
218219

219-
1. Scripting, which allows creation of a custom filter, which works when the search term is empty
220+
3. Scripting, which allows creation of a custom filter, which works when the search term is empty
220221

221222
```javascript
222223
filter by function \
@@ -226,6 +227,9 @@ It can be used in your query in two ways:
226227
return task.description.toLowerCase().includes(propertyLower);
227228
```
228229

230+
> [!tip]
231+
> See also [[Query File Defaults]] for built-in properties automatically supported by Tasks searches.
232+
229233
> [!warning] Using properties with no value
230234
> Currently when a property in a placeholder is not set:
231235
>
@@ -235,13 +239,6 @@ It can be used in your query in two ways:
235239
> [!Info]
236240
> In a future release, we will likely allow Tasks to silently ignore built filters created from properties that have no value.
237241

238-
> [!Info]
239-
> In a future release, we will likely introduce standard property names for instructions that will automatically be included inside Tasks queries.
240-
> Perhaps:
241-
>
242-
> - tasks-search-explain: true/false
243-
> - tasks-search-limit: number
244-
245242
## How does Tasks interpret Obsidian Properties?
246243

247244
Consider a file with the following example properties (or "Frontmatter"):
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
publish: true
3+
---
4+
5+
# Query File Defaults
6+
7+
## Summary
8+
9+
Scenarios:
10+
11+
- You have multiple Tasks queries in a Markdown file, and they have a lot of
12+
text in common. You would like to avoid repeating those common instructions in
13+
each one.
14+
- Usually you like to see your Tasks search results with certain [[Layout]]
15+
options, but sometimes it is nice to see more detail, perhaps turning
16+
`show tree` on and off, depending on your mood.
17+
18+
## Applying instructions to every Tasks search in a file
19+
20+
### Obsidian-native User Interface
21+
22+
You can use the Obsidian **File properties** view to customise Tasks searches:
23+
24+
![Obsidian's 'File properties' widget, with checkboxes and a text box to modify query file defaults for a file containing Tasks searches.](../images/query-file-defaults-file-properties-controls.png)
25+
<span class="caption">Obsidian's **File properties** widget, with checkboxes and a text box to modify query file defaults for a file containing Tasks searches. See [[#Widening property names in Obsidian]] for the CSS snippet used to widen property names.</span>
26+
27+
To try this out:
28+
29+
1. Show the `File properties` panel:
30+
- Enable the Obsidian core Properties view plugin: `Settings` > `Core plugins` > turn on `Properties view`
31+
- Switch to Reading or Live Preview modes.
32+
- Run the `Properties view: Show file properties` command.
33+
2. Add one or more `tasks_query_*` properties to the file
34+
- Click on `Add property`.
35+
- Start typing `tasks_query_`.
36+
- Or you can paste in property names from the [[#Supported Query File Defaults property values]] section below.
37+
- Press `<Return>` or `<Enter>` to add the property.
38+
3. Now you can modify these `tasks_query_*` properties to change the behaviour of Tasks searches in this file.
39+
40+
> [!info]
41+
> We plan to streamline the addition of these properties to notes.
42+
43+
#### Widening property names in Obsidian
44+
45+
By default, the property names are very narrow in Obsidian.
46+
47+
You can make them wider with the following CSS Snippet.
48+
49+
<!-- snippet: resources/sample_vaults/Tasks-Demo/.obsidian/snippets/widen-property-labels.css -->
50+
```css
51+
/* Make property labels wider, to fit the names of Tasks-specific properties */
52+
.metadata-content {
53+
--metadata-label-width: 18em;
54+
}
55+
```
56+
<!-- endSnippet -->
57+
58+
The Obsidian user guide shows how to [use CSS snippets in Obsidian](https://help.obsidian.md/How+to/Add+custom+styles#Use+Themes+and+or+CSS+snippets).
59+
60+
### Meta Bind User Interface
61+
62+
You can use the [Meta Bind](https://obsidian.md/plugins?search=Meta%20Bind) plugin to create a User Interface to easily change your Tasks searches:
63+
64+
![Meta Bind widgets to edit Query File Defaults](../images/query-file-defaults-meta-bind-controls.png)
65+
<span class="caption">Meta Bind widgets to edit Query File Defaults</span>
66+
67+
> [!info]- Set up Meta Bind widgets to edit Query File Defaults
68+
> Steps to do this, which assume you have already [turned off Obsidian's Restricted mode](https://help.obsidian.md/Extending+Obsidian/Plugin+security):
69+
>
70+
> 1. Install and enable [Meta Bind](https://obsidian.md/plugins?search=Meta%20Bind)
71+
> 2. Click the Copy button to copy the Markdown below.
72+
> 3. Paste the markdown in to a note in Obsidian that has one or more Tasks searches.
73+
> 4. Switch to Live Preview or Reading modes, to see the widgets.
74+
> 5. After experimenting, delete any labels and widgets that you do not need.
75+
>
76+
> ![[Meta Bind Query Widgets]]
77+
78+
## Technical details
79+
80+
### Supported Query File Defaults property values
81+
82+
These are all the properties currently supported by Tasks, as Query File Defaults.
83+
84+
<!-- snippet: DocsSamplesForDefaults.test.DocsSamplesForDefaults_supported-properties-empty.approved.yaml -->
85+
```yaml
86+
---
87+
tasks_query_explain:
88+
tasks_query_short_mode:
89+
tasks_query_show_tree:
90+
tasks_query_show_tags:
91+
tasks_query_show_id:
92+
tasks_query_show_depends_on:
93+
tasks_query_show_priority:
94+
tasks_query_show_recurrence_rule:
95+
tasks_query_show_on_completion:
96+
tasks_query_show_created_date:
97+
tasks_query_show_start_date:
98+
tasks_query_show_scheduled_date:
99+
tasks_query_show_due_date:
100+
tasks_query_show_cancelled_date:
101+
tasks_query_show_done_date:
102+
tasks_query_show_urgency:
103+
tasks_query_show_backlink:
104+
tasks_query_show_edit_button:
105+
tasks_query_show_postpone_button:
106+
tasks_query_show_task_count:
107+
tasks_query_extra_instructions:
108+
---
109+
```
110+
<!-- endSnippet -->
111+
112+
> [!tip]
113+
> We hope in the future to add an Obsidian Command to add all supported Query
114+
> File Defaults properties to the current file automatically.
115+
116+
### Types of Query File Defaults property values
117+
118+
These are all the types of properties currently supported by Tasks, as Query
119+
File Defaults.
120+
121+
> [!tip]
122+
> We plan to make Tasks add these types to the Obsidian vault automatically in
123+
> future.
124+
125+
```json
126+
{
127+
"types": {
128+
"tasks_query_explain": "checkbox",
129+
"tasks_query_short_mode": "checkbox",
130+
"tasks_query_show_tree": "checkbox",
131+
"tasks_query_show_tags": "checkbox",
132+
"tasks_query_show_id": "checkbox",
133+
"tasks_query_show_depends_on": "checkbox",
134+
"tasks_query_show_priority": "checkbox",
135+
"tasks_query_show_recurrence_rule": "checkbox",
136+
"tasks_query_show_on_completion": "checkbox",
137+
"tasks_query_show_created_date": "checkbox",
138+
"tasks_query_show_start_date": "checkbox",
139+
"tasks_query_show_scheduled_date": "checkbox",
140+
"tasks_query_show_due_date": "checkbox",
141+
"tasks_query_show_cancelled_date": "checkbox",
142+
"tasks_query_show_done_date": "checkbox",
143+
"tasks_query_show_urgency": "checkbox",
144+
"tasks_query_show_backlink": "checkbox",
145+
"tasks_query_show_edit_button": "checkbox",
146+
"tasks_query_show_postpone_button": "checkbox",
147+
"tasks_query_show_task_count": "checkbox",
148+
"tasks_query_extra_instructions": "text"
149+
}
150+
}
151+
```

docs/Scripting/Placeholders.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Explanation of this Tasks code block query:
7777
> [!released]
7878
> Use of Obsidian properties in placeholders was introduced in Tasks X.Y.Z.
7979
80-
It is now possible to use properties in the query file. See [[Obsidian Properties#Using Query Properties in Placeholders]]
80+
It is now possible to use properties in the query file. See [[Obsidian Properties#Using Query Properties in Searches]]
8181

8282
## Error checking: invalid variables
8383

docs/What is New/Changelog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ _In recent [Tasks releases](https://github.com/obsidian-tasks-group/obsidian-tas
1414

1515
- X.Y.Z:
1616
- Add `query.file.hasProperty()` and `query.file.property()` in custom filters
17-
- Add `{{query.file.hasProperty()}}` and `{{query.file.property()}}` in placeholders - see [[Obsidian Properties#Using Query Properties in Placeholders|Using Query Properties in Placeholders]].
17+
- Add `{{query.file.hasProperty()}}` and `{{query.file.property()}}` in placeholders - see [[Obsidian Properties#Using Query Properties in Searches|Using Query Properties in Placeholders]].
1818
- Placeholders can now call functions and contain expressions.
19+
- New [[Query File Defaults]] facility allows easy customising of all the
20+
Tasks searches in a file, by editing properties
1921
- Add Chinese translation of [[Settings]], [[Editing a Status]] and [[Check your Statuses]]
2022
- 7.14.0:
2123
- Add [[Editing Dates#Date-picker on task dates|date picker]] to Reading mode and Tasks query search results.
58.1 KB
Loading
44 KB
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!-- snippet: DocsSamplesForDefaults.test.DocsSamplesForDefaults_meta-bind-widgets-snippet.approved.md -->
2+
```md
3+
short mode: `INPUT[toggle:tasks_query_short_mode]`
4+
tree: `INPUT[toggle:tasks_query_show_tree]`
5+
tags: `INPUT[toggle:tasks_query_show_tags]`
6+
id: `INPUT[toggle:tasks_query_show_id]` depends on: `INPUT[toggle:tasks_query_show_depends_on]`
7+
priority: `INPUT[toggle:tasks_query_show_priority]`
8+
recurrence rule: `INPUT[toggle:tasks_query_show_recurrence_rule]` on completion: `INPUT[toggle:tasks_query_show_on_completion]`
9+
start date: `INPUT[toggle:tasks_query_show_start_date]` scheduled date: `INPUT[toggle:tasks_query_show_scheduled_date]` due date: `INPUT[toggle:tasks_query_show_due_date]`
10+
created date: `INPUT[toggle:tasks_query_show_created_date]` cancelled date: `INPUT[toggle:tasks_query_show_cancelled_date]` done date: `INPUT[toggle:tasks_query_show_done_date]`
11+
urgency: `INPUT[toggle:tasks_query_show_urgency]`
12+
backlink: `INPUT[toggle:tasks_query_show_backlink]`
13+
edit button: `INPUT[toggle:tasks_query_show_edit_button]` postpone button: `INPUT[toggle:tasks_query_show_postpone_button]`
14+
task count: `INPUT[toggle:tasks_query_show_task_count]`
15+
extra instructions: `INPUT[textArea:tasks_query_extra_instructions]`
16+
explain: `INPUT[toggle:tasks_query_explain]`
17+
```
18+
<!-- endSnippet -->

resources/sample_vaults/Tasks-Demo/.obsidian/appearance.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"js-engine-code-block-border",
66
"dataview-block",
77
"dataview-clare",
8-
"dataview-blocks"
8+
"dataview-blocks",
9+
"widen-property-labels"
910
],
1011
"accentColor": ""
1112
}

resources/sample_vaults/Tasks-Demo/.obsidian/community-plugins.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
"obsidian-kanban",
1717
"js-engine",
1818
"table-editor-obsidian",
19-
"reveal-active-file-button"
19+
"reveal-active-file-button",
20+
"obsidian-meta-bind-plugin"
2021
]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"devMode": false,
3+
"ignoreCodeBlockRestrictions": false,
4+
"preferredDateFormat": "YYYY-MM-DD",
5+
"firstWeekday": {
6+
"index": 1,
7+
"name": "Monday",
8+
"shortName": "Mo"
9+
},
10+
"syncInterval": 200,
11+
"minSyncInterval": 50,
12+
"maxSyncInterval": 1000,
13+
"enableJs": false,
14+
"viewFieldDisplayNullAsEmpty": false,
15+
"enableSyntaxHighlighting": true,
16+
"enableEditorRightClickMenu": true,
17+
"inputFieldTemplates": [],
18+
"buttonTemplates": [],
19+
"excludedFolders": [
20+
"templates"
21+
]
22+
}

0 commit comments

Comments
 (0)