Skip to content

Commit 5a4b8c1

Browse files
authored
Merge pull request #3317 from obsidian-tasks-group/add-frontmatter-command
feat: add new command 'Add all Query File Defaults properties'
2 parents faf6652 + 5003782 commit 5a4b8c1

16 files changed

+237
-117
lines changed

docs/Queries/Query File Defaults.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ To try this out:
3737
- Press `<Return>` or `<Enter>` to add the property.
3838
3. Now you can modify these `TQ_*` properties to change the behaviour of Tasks searches in this file.
3939

40-
> [!info]
41-
> We plan to streamline the addition of these properties to notes.
40+
> [!tip]
41+
> Use the command **Tasks: Add all Query File Defaults properties** to add all the available `TQ_*` properties to the active note.
4242
4343
#### Widening property names in Obsidian
4444

@@ -85,33 +85,32 @@ These are all the properties currently supported by Tasks, as Query File Default
8585
```yaml
8686
---
8787
TQ_explain:
88+
TQ_extra_instructions:
8889
TQ_short_mode:
89-
TQ_show_tree:
90-
TQ_show_tags:
91-
TQ_show_id:
92-
TQ_show_depends_on:
93-
TQ_show_priority:
94-
TQ_show_recurrence_rule:
95-
TQ_show_on_completion:
96-
TQ_show_created_date:
97-
TQ_show_start_date:
98-
TQ_show_scheduled_date:
99-
TQ_show_due_date:
90+
TQ_show_backlink:
10091
TQ_show_cancelled_date:
92+
TQ_show_created_date:
93+
TQ_show_depends_on:
10194
TQ_show_done_date:
102-
TQ_show_urgency:
103-
TQ_show_backlink:
95+
TQ_show_due_date:
10496
TQ_show_edit_button:
97+
TQ_show_id:
98+
TQ_show_on_completion:
10599
TQ_show_postpone_button:
100+
TQ_show_priority:
101+
TQ_show_recurrence_rule:
102+
TQ_show_scheduled_date:
103+
TQ_show_start_date:
104+
TQ_show_tags:
106105
TQ_show_task_count:
107-
TQ_extra_instructions:
106+
TQ_show_tree:
107+
TQ_show_urgency:
108108
---
109109
```
110110
<!-- endSnippet -->
111111

112112
> [!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.
113+
> Use the command **Tasks: Add all Query File Defaults properties** to add all these properties to the active note.
115114
116115
### Types of Query File Defaults property values
117116

291 Bytes
Loading

resources/sample_vaults/Tasks-Demo/Manual Testing/Frontmatter/Placeholder examples to capture in tests and docs.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
---
22
TQ_explain: false
3+
TQ_extra_instructions: |-
4+
# press shift-return to add new lines
5+
# not done
6+
# sort by done date
7+
# ignore global query
38
TQ_short_mode: false
4-
TQ_show_tree: true
5-
TQ_show_tags: true
6-
TQ_show_id: true
7-
TQ_show_depends_on: true
8-
TQ_show_priority: true
9-
TQ_show_recurrence_rule: true
10-
TQ_show_on_completion: true
11-
TQ_show_created_date: true
12-
TQ_show_start_date: true
13-
TQ_show_scheduled_date: true
14-
TQ_show_due_date: true
9+
TQ_show_backlink: true
1510
TQ_show_cancelled_date: true
11+
TQ_show_created_date: true
12+
TQ_show_depends_on: true
1613
TQ_show_done_date: true
17-
TQ_show_urgency: true
18-
TQ_show_backlink: true
14+
TQ_show_due_date: true
1915
TQ_show_edit_button: true
16+
TQ_show_id: true
17+
TQ_show_on_completion: true
2018
TQ_show_postpone_button: true
19+
TQ_show_priority: true
20+
TQ_show_recurrence_rule: true
21+
TQ_show_scheduled_date: true
22+
TQ_show_start_date: true
23+
TQ_show_tags: true
2124
TQ_show_task_count: true
25+
TQ_show_tree: true
26+
TQ_show_urgency: true
2227
TQ_sort_by:
2328
- description
2429
TQ_group_by:
2530
- status.type
2631
- happens reverse
2732
- function task.tags.sort().join(' ')
28-
TQ_extra_instructions: |-
29-
# press shift-return to add new lines
30-
# not done
31-
# sort by done date
32-
# ignore global query
3333
---
3434

3535
# Placeholder examples to capture in tests and docs

resources/sample_vaults/Tasks-Demo/Test Data/query_file_defaults_all_options_false.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
---
22
TQ_explain: false
3+
TQ_extra_instructions: |-
4+
# press shift-return to add new lines
5+
not done
36
TQ_short_mode: false
4-
TQ_show_tree: false
5-
TQ_show_tags: false
6-
TQ_show_id: false
7-
TQ_show_depends_on: false
8-
TQ_show_priority: false
9-
TQ_show_recurrence_rule: false
10-
TQ_show_on_completion: false
11-
TQ_show_created_date: false
12-
TQ_show_start_date: false
13-
TQ_show_scheduled_date: false
14-
TQ_show_due_date: false
7+
TQ_show_backlink: false
158
TQ_show_cancelled_date: false
9+
TQ_show_created_date: false
10+
TQ_show_depends_on: false
1611
TQ_show_done_date: false
17-
TQ_show_urgency: false
18-
TQ_show_backlink: false
12+
TQ_show_due_date: false
1913
TQ_show_edit_button: false
14+
TQ_show_id: false
15+
TQ_show_on_completion: false
2016
TQ_show_postpone_button: false
17+
TQ_show_priority: false
18+
TQ_show_recurrence_rule: false
19+
TQ_show_scheduled_date: false
20+
TQ_show_start_date: false
21+
TQ_show_tags: false
2122
TQ_show_task_count: false
22-
TQ_extra_instructions: |-
23-
# press shift-return to add new lines
24-
not done
23+
TQ_show_tree: false
24+
TQ_show_urgency: false
2525
---
2626

2727
# query_file_defaults_all_options_false

resources/sample_vaults/Tasks-Demo/Test Data/query_file_defaults_all_options_null.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
---
22
TQ_explain:
3+
TQ_extra_instructions:
34
TQ_short_mode:
4-
TQ_show_tree:
5-
TQ_show_tags:
6-
TQ_show_id:
7-
TQ_show_depends_on:
8-
TQ_show_priority:
9-
TQ_show_recurrence_rule:
10-
TQ_show_on_completion:
11-
TQ_show_created_date:
12-
TQ_show_start_date:
13-
TQ_show_scheduled_date:
14-
TQ_show_due_date:
5+
TQ_show_backlink:
156
TQ_show_cancelled_date:
7+
TQ_show_created_date:
8+
TQ_show_depends_on:
169
TQ_show_done_date:
17-
TQ_show_urgency:
18-
TQ_show_backlink:
10+
TQ_show_due_date:
1911
TQ_show_edit_button:
12+
TQ_show_id:
13+
TQ_show_on_completion:
2014
TQ_show_postpone_button:
15+
TQ_show_priority:
16+
TQ_show_recurrence_rule:
17+
TQ_show_scheduled_date:
18+
TQ_show_start_date:
19+
TQ_show_tags:
2120
TQ_show_task_count:
22-
TQ_extra_instructions:
21+
TQ_show_tree:
22+
TQ_show_urgency:
2323
---
2424

2525
# query_file_defaults_all_options_null

resources/sample_vaults/Tasks-Demo/Test Data/query_file_defaults_all_options_true.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
---
22
TQ_explain: true
3+
TQ_extra_instructions: |-
4+
# press shift-return to add new lines
5+
not done
36
TQ_short_mode: true
4-
TQ_show_tree: true
5-
TQ_show_tags: true
6-
TQ_show_id: true
7-
TQ_show_depends_on: true
8-
TQ_show_priority: true
9-
TQ_show_recurrence_rule: true
10-
TQ_show_on_completion: true
11-
TQ_show_created_date: true
12-
TQ_show_start_date: true
13-
TQ_show_scheduled_date: true
14-
TQ_show_due_date: true
7+
TQ_show_backlink: true
158
TQ_show_cancelled_date: true
9+
TQ_show_created_date: true
10+
TQ_show_depends_on: true
1611
TQ_show_done_date: true
17-
TQ_show_urgency: true
18-
TQ_show_backlink: true
12+
TQ_show_due_date: true
1913
TQ_show_edit_button: true
14+
TQ_show_id: true
15+
TQ_show_on_completion: true
2016
TQ_show_postpone_button: true
17+
TQ_show_priority: true
18+
TQ_show_recurrence_rule: true
19+
TQ_show_scheduled_date: true
20+
TQ_show_start_date: true
21+
TQ_show_tags: true
2122
TQ_show_task_count: true
22-
TQ_extra_instructions: |-
23-
# press shift-return to add new lines
24-
not done
23+
TQ_show_tree: true
24+
TQ_show_urgency: true
2525
---
2626

2727
# query_file_defaults_all_options_true
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { type App, Notice, type TFile } from 'obsidian';
2+
import { QueryFileDefaults } from '../Query/QueryFileDefaults';
3+
4+
export async function ensureQueryFileDefaultsInFrontmatter(app: App, file: TFile) {
5+
await app.fileManager.processFrontMatter(file, (frontmatter) => {
6+
const requiredKeys = new QueryFileDefaults().allPropertyNamesSorted();
7+
let updated = false;
8+
requiredKeys.forEach((key) => {
9+
if (!(key in frontmatter)) {
10+
frontmatter[key] = null;
11+
updated = true;
12+
}
13+
});
14+
15+
if (!updated) {
16+
new Notice('All supported properties are already present.');
17+
} else {
18+
new Notice('Properties updated successfully.');
19+
}
20+
});
21+
}

src/Commands/index.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import type { App, Editor, MarkdownFileInfo, MarkdownView, View } from 'obsidian';
1+
import type { App, Editor, MarkdownFileInfo, MarkdownView, TFile, View } from 'obsidian';
22
import type TasksPlugin from '../main';
33
import { createOrEdit } from './CreateOrEdit';
44

55
import { toggleDone } from './ToggleDone';
6+
import { ensureQueryFileDefaultsInFrontmatter } from './AddQueryFileDefaultsProperties';
67

78
export class Commands {
89
private readonly plugin: TasksPlugin;
@@ -30,5 +31,30 @@ export class Commands {
3031
icon: 'check-in-circle',
3132
editorCheckCallback: toggleDone,
3233
});
34+
35+
plugin.addCommand({
36+
id: 'add-query-file-defaults-properties',
37+
name: 'Add all Query File Defaults properties',
38+
icon: 'settings',
39+
checkCallback: (checking: boolean) => {
40+
const activeFile = this.app.workspace.getActiveFile();
41+
if (!activeFile) {
42+
return false;
43+
}
44+
if (activeFile.extension !== 'md') {
45+
return false;
46+
}
47+
48+
if (!checking) {
49+
this.ensureQueryFileDefaultsFrontmatter(activeFile).catch(console.error);
50+
}
51+
return true;
52+
},
53+
});
54+
}
55+
56+
async ensureQueryFileDefaultsFrontmatter(file: TFile): Promise<void> {
57+
const { app } = this;
58+
await ensureQueryFileDefaultsInFrontmatter(app, file);
3359
}
3460
}

src/Query/QueryFileDefaults.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ export class QueryFileDefaults {
160160
return new Query(this.source(queryFile), queryFile);
161161
}
162162

163+
public allPropertyNames(): string[] {
164+
return queryProperties.map((prop) => prop.name);
165+
}
166+
167+
public allPropertyNamesSorted(): string[] {
168+
return this.allPropertyNames().sort((a, b) => a.localeCompare(b));
169+
}
170+
163171
/**
164172
* Return text that creates MetaBind widgets for users to edit query file defaults.
165173
*/
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
---
22
TQ_explain:
3+
TQ_extra_instructions:
34
TQ_short_mode:
4-
TQ_show_tree:
5-
TQ_show_tags:
6-
TQ_show_id:
7-
TQ_show_depends_on:
8-
TQ_show_priority:
9-
TQ_show_recurrence_rule:
10-
TQ_show_on_completion:
11-
TQ_show_created_date:
12-
TQ_show_start_date:
13-
TQ_show_scheduled_date:
14-
TQ_show_due_date:
5+
TQ_show_backlink:
156
TQ_show_cancelled_date:
7+
TQ_show_created_date:
8+
TQ_show_depends_on:
169
TQ_show_done_date:
17-
TQ_show_urgency:
18-
TQ_show_backlink:
10+
TQ_show_due_date:
1911
TQ_show_edit_button:
12+
TQ_show_id:
13+
TQ_show_on_completion:
2014
TQ_show_postpone_button:
15+
TQ_show_priority:
16+
TQ_show_recurrence_rule:
17+
TQ_show_scheduled_date:
18+
TQ_show_start_date:
19+
TQ_show_tags:
2120
TQ_show_task_count:
22-
TQ_extra_instructions:
21+
TQ_show_tree:
22+
TQ_show_urgency:
2323
---

0 commit comments

Comments
 (0)