Skip to content

Commit

Permalink
Added the feature requested in #13
Browse files Browse the repository at this point in the history
  • Loading branch information
Newbrict committed Nov 17, 2023
1 parent 8521a05 commit 03110e8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
34 changes: 29 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ interface ObligatorSettings {
delete_empty_headings: boolean;
keep_template_headings: boolean;
run_on_startup: boolean;
keep_until_parent_complete: boolean;
}

const DEFAULT_SETTINGS: ObligatorSettings = {
Expand All @@ -53,7 +54,8 @@ const DEFAULT_SETTINGS: ObligatorSettings = {
archive_path: "",
delete_empty_headings: true,
keep_template_headings: true,
run_on_startup: false
run_on_startup: false,
keep_until_parent_complete: false
}

export default class Obligator extends Plugin {
Expand Down Expand Up @@ -287,7 +289,9 @@ export default class Obligator extends Plugin {
// Delete from last_note_structure only if this setting is true
if (last_note_structure) {
if (this.settings.keep_template_headings) {
filter_structure(last_note_structure, this.settings.delete_empty_headings);
filter_structure(last_note_structure,
this.settings.delete_empty_headings,
this.settings.keep_until_parent_complete);
}
merge_structure(
template_structure,
Expand All @@ -297,7 +301,9 @@ export default class Obligator extends Plugin {

// Delete from the merged structure is false
if (!this.settings.keep_template_headings) {
filter_structure(template_structure, this.settings.delete_empty_headings);
filter_structure(template_structure,
this.settings.delete_empty_headings,
this.settings.keep_until_parent_complete);
}

let new_note_lines = destructure(template_structure).concat(OUTPUT_TERMINAL_LINES);
Expand Down Expand Up @@ -570,8 +576,8 @@ class ObligatorSettingTab extends PluginSettingTab {
// --------------------------------------------------------------------
setting_keep_template_headings = new Setting(containerEl)
.setName("Don't delete headings from template")
.setDesc(`This prevents the setting above from deleting any headings
which are present in the template`)
.setDesc(`This prevents the setting above from deleting any
headings which are present in the template`)
.addToggle(toggle => {toggle
.setValue(this.plugin.settings.keep_template_headings)
.onChange(async value => {
Expand All @@ -580,5 +586,23 @@ class ObligatorSettingTab extends PluginSettingTab {
})
toggle_keep_template_headings = toggle;
}).setDisabled(!this.plugin.settings.delete_empty_headings);

// --------------------------------------------------------------------
// Toggle for the setting to keep children if the parent isn't complete
// --------------------------------------------------------------------
setting_keep_template_headings = new Setting(containerEl)
.setName("Only delete to-dos when parent is complete")
.setDesc(`To-dos which are children of other to-dos will not be
deleted unless the parent is checked, and all of its
children are too. This setting would be used if you want
to retain checked to-dos until the whole structure is
checked.`)
.addToggle(toggle => {toggle
.setValue(this.plugin.settings.keep_until_parent_complete)
.onChange(async value => {
this.plugin.settings.keep_until_parent_complete = value;
await this.plugin.saveSettings();
})
});
}
}
25 changes: 20 additions & 5 deletions src/note_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,26 @@ export function merge_structure (first:Parent, second:Parent) {
});
}

export function filter_structure(structure:Parent, delete_headings:boolean) {
export function filter_structure(structure:Parent,
delete_headings:boolean,
keep_until_parent_complete:boolean) {
for (let i = 0; i < structure.children.length; i++) {
const child = structure.children[i];
if (typeof child === "object") {
filter_structure(child, delete_headings)

// child.text is checked here because it can be null when invoked.
if (child.text) {
if (CHECKBOX_REGEX.test(child.text)
&& !CHECKEDBOX_REGEX.test(child.text)
&& keep_until_parent_complete) {
// Here we have a non-checked checkbox and the setting is not
// to delete until everything is checked, so we should just
// continue at this point.
continue;
}
filter_structure(child,
delete_headings,
keep_until_parent_complete)
if (HEADING_REGEX.test(child.text)) {
if (delete_headings) {
const non_empty = child.children.filter((element) => {
Expand All @@ -173,9 +186,11 @@ export function filter_structure(structure:Parent, delete_headings:boolean) {
}
}
// Only delete checkedboxes if they have no unchecked children
if (CHECKEDBOX_REGEX.test(child.text) && child.children.length === 0) {
delete structure.children[i];
structure.total = structure.total - 1;
if (CHECKEDBOX_REGEX.test(child.text)) {
if (child.children.length === 0) {
delete structure.children[i];
structure.total = structure.total - 1;
}
}
}
}
Expand Down

0 comments on commit 03110e8

Please sign in to comment.