Skip to content

Commit

Permalink
- Added: Natural date dropdown suggestions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesb committed Sep 25, 2021
1 parent 15551d8 commit 1460dfb
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 63 deletions.
23 changes: 8 additions & 15 deletions data.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
{
"queueTagMap": {
"IW-Queue": ["iw-queue"],
"Tasks": ["tasks"]
},
"defaultPriorityMin": 17,
"defaultPriorityMax": 64,
"queueTagMap": {},
"defaultPriorityMin": 10,
"defaultPriorityMax": 50,
"queueFolderPath": "IW-Queues",
"queueFileName": "Tasks.md",
"defaultQueueType": "simple",
"skipAddNoteWindow": true,
"queueFileName": "IW-Queue.md",
"defaultQueueType": "afactor",
"skipAddNoteWindow": false,
"autoAddNewNotes": false,
"defaultFirstRepDate": "tomorrow",
"defaultFirstRepDate": "today",
"askForNextRepDate": false,
"vimMode": false,
"defaultPriority": 30,
"queueFilePath": "queue.md",
"defaultAFactor": 2,
"defaultInterval": 1
"dropdownNaturalDates": {"today": "today", "tomorrow": "tomorrow", "in two days": "in two days", "next week": "next week"}
}
7 changes: 7 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface IWSettings {
autoAddNewNotes: boolean;
defaultFirstRepDate: string;
askForNextRepDate: boolean;
dropdownNaturalDates: Record<string, string>;
}

export const DefaultSettings: IWSettings = {
Expand All @@ -22,4 +23,10 @@ export const DefaultSettings: IWSettings = {
autoAddNewNotes: false,
defaultFirstRepDate: "1970-01-01",
askForNextRepDate: false,
dropdownNaturalDates: {
"today": "today",
"tomorrow": "tomorrow",
"in two days": "in two days",
"next week": "next week",
}
};
15 changes: 11 additions & 4 deletions src/views/bulk-adding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { LogTo } from "../logger";
import "../helpers/number-utils.ts";
import "../helpers/date-utils.ts";
import "../helpers/str-utils.ts";
import { NaturalDateSuggest } from "./date-suggest";

export class BulkAdderModal extends ModalBase {
private queuePath: string;
Expand Down Expand Up @@ -142,11 +143,15 @@ export class BulkAdderModal extends ModalBase {
// Rep Dates

this.contentEl.appendText("Earliest Rep Date: ");
this.inputFirstRepMin = new TextComponent(contentEl).setValue("1970-01-01");
this.inputFirstRepMin = new TextComponent(contentEl)
.setPlaceholder("1970-01-01")
new NaturalDateSuggest(this.plugin, this.inputFirstRepMin.inputEl)
this.contentEl.createEl("br");

this.contentEl.appendText("Latest Rep Date: ");
this.inputFirstRepMax = new TextComponent(contentEl).setValue("1970-01-01");
this.inputFirstRepMax = new TextComponent(contentEl)
.setPlaceholder("1970-01-01")
new NaturalDateSuggest(this.plugin, this.inputFirstRepMax.inputEl)
this.contentEl.createEl("br");
//
// Events
Expand Down Expand Up @@ -177,8 +182,10 @@ export class BulkAdderModal extends ModalBase {

const priMin = Number(this.minPriorityComponent.getValue());
const priMax = Number(this.maxPriorityComponent.getValue());
const dateMin = this.parseDate(this.inputFirstRepMin.getValue());
const dateMax = this.parseDate(this.inputFirstRepMax.getValue());
const dateMinStr = this.inputFirstRepMin.getValue();
const dateMaxStr = this.inputFirstRepMax.getValue();
const dateMin = this.parseDate(dateMinStr === "" ? "1970-01-01" : dateMinStr);
const dateMax = this.parseDate(dateMaxStr === "" ? "1970-01-01" : dateMaxStr);

if (
!(
Expand Down
25 changes: 25 additions & 0 deletions src/views/date-suggest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import IW from 'src/main';
import { TextInputSuggest } from './suggest'

export class NaturalDateSuggest extends TextInputSuggest<string> {
private plugin: IW;
constructor(plugin: IW, inputEl: HTMLInputElement) {
super(plugin.app, inputEl);
this.plugin = plugin;
}

getSuggestions(inputStr: string): string[] {
return Object.keys(this.plugin.settings.dropdownNaturalDates)
.filter(date => date.contains(inputStr));
}

renderSuggestion(date: string, el: HTMLElement): void {
el.setText(date);
}

selectSuggestion(date: string): void {
this.inputEl.value = date;
this.inputEl.trigger("input");
this.close();
}
}
10 changes: 6 additions & 4 deletions src/views/edit-data.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { SliderComponent, TextComponent, ButtonComponent } from "obsidian";
import { SliderComponent, TextComponent, ButtonComponent, DropdownComponent } from "obsidian";
import IW from "../main";
import { ModalBase } from "./modal-base";
import { LogTo } from "../logger";
import { MarkdownTable, MarkdownTableRow } from "../markdown";
import "../helpers/date-utils";
import "../helpers/number-utils";
import { NaturalDateSuggest } from "./date-suggest";

export class EditDataModal extends ModalBase {
private inputSlider: SliderComponent;
Expand All @@ -31,8 +32,8 @@ export class EditDataModal extends ModalBase {

contentEl.appendText("Next Rep Date: ");
this.inputNextRep = new TextComponent(contentEl)
.setPlaceholder("Date")
.setValue(this.currentRep.nextRepDate.formatYYMMDD());
.setPlaceholder(this.currentRep.nextRepDate.formatYYMMDD());
new NaturalDateSuggest(this.plugin, this.inputNextRep.inputEl);
contentEl.createEl("br");

this.inputNextRep.inputEl.focus();
Expand Down Expand Up @@ -84,7 +85,8 @@ export class EditDataModal extends ModalBase {
}

async updateRepData() {
const date = this.parseDate(this.inputNextRep.getValue());
const dateStr = this.inputNextRep.getValue();
const date = this.parseDate(dateStr === "" ? "1970-01-01" : dateStr);
if (!date) {
LogTo.Console("Failed to parse next repetition date!", true);
return;
Expand Down
50 changes: 23 additions & 27 deletions src/views/modals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { PriorityUtils } from "../helpers/priority-utils";
import { MarkdownTableRow } from "../markdown";
import "../helpers/date-utils";
import "../helpers/number-utils";
import { NaturalDateSuggest } from "./date-suggest";

abstract class ReviewModal extends ModalBase {
protected title: string;
Expand Down Expand Up @@ -54,8 +55,8 @@ abstract class ReviewModal extends ModalBase {
const firstRepDate = this.plugin.settings.defaultFirstRepDate;
contentEl.appendText("First Rep Date: ");
this.inputFirstRep = new TextComponent(contentEl)
.setPlaceholder("Date")
.setValue(firstRepDate);
.setPlaceholder(firstRepDate);
new NaturalDateSuggest(this.plugin, this.inputFirstRep.inputEl);
contentEl.createEl("br");

this.inputFirstRep.inputEl.focus();
Expand Down Expand Up @@ -111,14 +112,6 @@ abstract class ReviewModal extends ModalBase {
});
}

getPriority(): number {
return this.inputSlider.getValue();
}

getNotes() {
return this.inputNoteField.getValue();
}

getQueuePath() {
let queue = this.inputQueueField.getValue();
if (!queue.endsWith(".md")) queue += ".md";
Expand All @@ -141,23 +134,24 @@ export class ReviewNoteModal extends ReviewModal {
}

async addToOutstanding() {
let date = this.parseDate(this.inputFirstRep.getValue());
const dateStr = this.inputFirstRep.getValue();
const date = this.parseDate(dateStr === "" ? "1970-01-01" : dateStr);
if (!date) {
LogTo.Console("Failed to parse initial repetition date!");
return;
}

let queue = new Queue(this.plugin, this.getQueuePath());
let file = this.plugin.files.getActiveNoteFile();
const queue = new Queue(this.plugin, this.getQueuePath());
const file = this.plugin.files.getActiveNoteFile();
if (!file) {
LogTo.Console("Failed to add to outstanding.", true);
return;
}
let link = this.plugin.files.toLinkText(file);
let row = new MarkdownTableRow(
const link = this.plugin.files.toLinkText(file);
const row = new MarkdownTableRow(
link,
this.getPriority(),
this.getNotes(),
this.inputSlider.getValue(),
this.inputNoteField.getValue(),
1,
date
);
Expand All @@ -178,23 +172,24 @@ export class ReviewFileModal extends ReviewModal {
}

async addToOutstanding() {
let date = this.parseDate(this.inputFirstRep.getValue());
const dateStr = this.inputFirstRep.getValue();
const date = this.parseDate(dateStr === "" ? "1970-01-01" : dateStr);
if (!date) {
LogTo.Console("Failed to parse initial repetition date!");
return;
}

let queue = new Queue(this.plugin, this.getQueuePath());
let file = this.plugin.files.getTFile(this.filePath);
const queue = new Queue(this.plugin, this.getQueuePath());
const file = this.plugin.files.getTFile(this.filePath);
if (!file) {
LogTo.Console("Failed to add to outstanding because file was null", true);
return;
}
let link = this.plugin.files.toLinkText(file);
let row = new MarkdownTableRow(
const link = this.plugin.files.toLinkText(file);
const row = new MarkdownTableRow(
link,
this.getPriority(),
this.getNotes(),
this.inputSlider.getValue(),
this.inputNoteField.getValue(),
1,
date
);
Expand Down Expand Up @@ -229,7 +224,8 @@ export class ReviewBlockModal extends ReviewModal {
}

async addToOutstanding() {
const date = this.parseDate(this.inputFirstRep.getValue());
const dateStr = this.inputFirstRep.getValue();
const date = this.parseDate(dateStr === "" ? "1970-01-01" : dateStr);
if (!date) {
LogTo.Console("Failed to parse initial repetition date!");
return;
Expand Down Expand Up @@ -262,8 +258,8 @@ export class ReviewBlockModal extends ReviewModal {
await queue.add(
new MarkdownTableRow(
blockLink,
this.getPriority(),
this.getNotes(),
this.inputSlider.getValue(),
this.inputNoteField.getValue(),
1,
date
)
Expand Down
14 changes: 8 additions & 6 deletions src/views/next-rep-schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SliderComponent, TextComponent, ButtonComponent } from "obsidian";
import { ModalBase } from "./modal-base";
import { MarkdownTableRow, MarkdownTable } from "../markdown";
import "../helpers/date-utils";
import { NaturalDateSuggest } from "./date-suggest";

export class NextRepScheduler extends ModalBase {
private intervalComponent: TextComponent;
Expand All @@ -28,9 +29,9 @@ export class NextRepScheduler extends ModalBase {
// Date

contentEl.appendText("Next repetition date: ");
this.repDateComponent = new TextComponent(contentEl).setValue(
this.curRep.nextRepDate.formatYYMMDD()
);
this.repDateComponent = new TextComponent(contentEl)
.setPlaceholder(this.curRep.nextRepDate.formatYYMMDD())
new NaturalDateSuggest(this.plugin, this.repDateComponent.inputEl)
contentEl.createEl("br");

this.repDateComponent.inputEl.focus();
Expand Down Expand Up @@ -83,8 +84,9 @@ export class NextRepScheduler extends ModalBase {
}

async schedule() {
const nextRepDate = this.parseDate(this.repDateComponent.getValue());
if (!nextRepDate) {
const dateStr = this.repDateComponent.getValue();
const date = this.parseDate(dateStr === "" ? "1970-01-01" : dateStr);
if (!date) {
LogTo.Console("Failed to parse next repetition date!", true);
return;
}
Expand All @@ -96,7 +98,7 @@ export class NextRepScheduler extends ModalBase {
}

const priority = this.priorityComponent.getValue();
this.curRep.nextRepDate = nextRepDate;
this.curRep.nextRepDate = date;
this.curRep.priority = priority;
this.curRep.interval = interval;
await this.plugin.queue.writeQueueTable(this.table);
Expand Down
59 changes: 52 additions & 7 deletions src/views/settings-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import IW from "../main";
import { FileSuggest, FolderSuggest } from "./file-suggest";
import { LogTo } from "src/logger";
import { NaturalDateSuggest } from "./date-suggest";

export class IWSettingsTab extends PluginSettingTab {
plugin: IW;
Expand Down Expand Up @@ -87,22 +88,66 @@ export class IWSettingsTab extends PluginSettingTab {
this.plugin.saveData(settings);
});
});


const nldates = (<any>this.plugin.app).plugins.getPlugin(
"nldates-obsidian"
);
const hasNlDates = nldates != null;

//
// Dropdown Dates
new Setting(containerEl)
.setName("Dropdown Date List")
.setDesc("Sets the default list of dropdown dates that show up in modals so you can quickly set repetition dates.")
.addTextArea((comp) => {
comp.setPlaceholder("Example:\ntoday\ntomorrow\nnext week")
const currentValue = Object.keys(this.plugin.settings.dropdownNaturalDates).join("\n");
comp.setValue(currentValue).onChange(
debounce(value => {
if (hasNlDates) {
const inputDates = String(value)
?.split(/\r?\n/)
?.map(str => [str, nldates.parseDate(str)]) || [];

if (!inputDates || inputDates.length === 0) {
LogTo.Debug("User inputted dates were null or empty");
settings.dropdownNaturalDates = {};
this.plugin.saveData(settings);
return;
}

const validDates = inputDates
.filter(([_, date]) => date != null && date.date)
.map(([s, _]) => s);

if (inputDates.length !== validDates.length) {
LogTo.Debug(`Ignoring ${inputDates.length - validDates.length} invalid natural language date strings.`);
}

const dateOptionsRecord: Record<string, string> = validDates
.reduce((acc: Record<string, string>, x: string) => acc[x]=x, {});
LogTo.Debug("Setting dropdown date options to " + JSON.stringify(dateOptionsRecord));
settings.dropdownNaturalDates = dateOptionsRecord;
this.plugin.saveData(settings);
}
}, 500, true)
)
})

//
// Default First Rep Date
// First Rep Date

new Setting(containerEl)
.setName("Default First Rep Date")
.setDesc(
"Sets the default first repetition date for new repetitions. Example: today, tomorrow, next week. **Requires that you have installed the Natural Language Dates plugin.**"
)
.addText((comp) => {
comp.setPlaceholder("Example: today, tomorrow, next week.");
const nldates = (<any>this.plugin.app).plugins.getPlugin(
"nldates-obsidian"
);
const hasNlDates = nldates != null;
comp.setValue(String(settings.defaultFirstRepDate)).onChange(
new NaturalDateSuggest(this.plugin, comp.inputEl);
comp.setValue(String(settings.defaultFirstRepDate))
.setPlaceholder("1970-01-01")
.onChange(
debounce(
(value) => {
if (hasNlDates) {
Expand Down

0 comments on commit 1460dfb

Please sign in to comment.