Skip to content

Commit

Permalink
- Misc: Formatted with prettier.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesb committed Sep 25, 2021
1 parent 1460dfb commit 9c8d82f
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 92 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ If you would like to support my work, I have a [Patreon page](https://www.patreo
- This plugin is not supported on mobile! (yet)

### Important! Priorities

- Confusingly, low priority numbers correspond to high priorities! That means your daily queue of repetitions will be sorted from lowest priority number (most important) to highest priority number (least important). This is because this is the way priorities work in SuperMemo and having used it for a couple years I just got used to thinking about it like that. I didn't realise how confusing this could be until someone mentioned it in an issue. Apologies for any confusion!

### Features
Expand Down
7 changes: 6 additions & 1 deletion data.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@
"autoAddNewNotes": false,
"defaultFirstRepDate": "today",
"askForNextRepDate": false,
"dropdownNaturalDates": {"today": "today", "tomorrow": "tomorrow", "in two days": "in two days", "next week": "next week"}
"dropdownNaturalDates": {
"today": "today",
"tomorrow": "tomorrow",
"in two days": "in two days",
"next week": "next week"
}
}
10 changes: 5 additions & 5 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export const DefaultSettings: IWSettings = {
defaultFirstRepDate: "1970-01-01",
askForNextRepDate: false,
dropdownNaturalDates: {
"today": "today",
"tomorrow": "tomorrow",
"in two days": "in two days",
"next week": "next week",
}
today: "today",
tomorrow: "tomorrow",
"in two days": "in two days",
"next week": "next week",
},
};
22 changes: 14 additions & 8 deletions src/views/bulk-adding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,17 @@ export class BulkAdderModal extends ModalBase {
// Rep Dates

this.contentEl.appendText("Earliest Rep Date: ");
this.inputFirstRepMin = new TextComponent(contentEl)
.setPlaceholder("1970-01-01")
new NaturalDateSuggest(this.plugin, this.inputFirstRepMin.inputEl)
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)
.setPlaceholder("1970-01-01")
new NaturalDateSuggest(this.plugin, this.inputFirstRepMax.inputEl)
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 @@ -184,8 +186,12 @@ export class BulkAdderModal extends ModalBase {
const priMax = Number(this.maxPriorityComponent.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);
const dateMin = this.parseDate(
dateMinStr === "" ? "1970-01-01" : dateMinStr
);
const dateMax = this.parseDate(
dateMaxStr === "" ? "1970-01-01" : dateMaxStr
);

if (
!(
Expand Down
13 changes: 7 additions & 6 deletions src/views/date-suggest.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import IW from 'src/main';
import { TextInputSuggest } from './suggest'
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;
super(plugin.app, inputEl);
this.plugin = plugin;
}

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

renderSuggestion(date: string, el: HTMLElement): void {
Expand Down
14 changes: 10 additions & 4 deletions src/views/edit-data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { SliderComponent, TextComponent, ButtonComponent, DropdownComponent } from "obsidian";
import {
SliderComponent,
TextComponent,
ButtonComponent,
DropdownComponent,
} from "obsidian";
import IW from "../main";
import { ModalBase } from "./modal-base";
import { LogTo } from "../logger";
Expand Down Expand Up @@ -31,8 +36,9 @@ export class EditDataModal extends ModalBase {
// Next Rep Date

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

Expand Down Expand Up @@ -85,7 +91,7 @@ export class EditDataModal extends ModalBase {
}

async updateRepData() {
const dateStr = 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);
Expand Down
11 changes: 6 additions & 5 deletions src/views/modals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ abstract class ReviewModal extends ModalBase {

const firstRepDate = this.plugin.settings.defaultFirstRepDate;
contentEl.appendText("First Rep Date: ");
this.inputFirstRep = new TextComponent(contentEl)
.setPlaceholder(firstRepDate);
this.inputFirstRep = new TextComponent(contentEl).setPlaceholder(
firstRepDate
);
new NaturalDateSuggest(this.plugin, this.inputFirstRep.inputEl);
contentEl.createEl("br");

Expand Down Expand Up @@ -134,7 +135,7 @@ export class ReviewNoteModal extends ReviewModal {
}

async addToOutstanding() {
const dateStr = 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!");
Expand Down Expand Up @@ -172,7 +173,7 @@ export class ReviewFileModal extends ReviewModal {
}

async addToOutstanding() {
const dateStr = 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!");
Expand Down Expand Up @@ -224,7 +225,7 @@ export class ReviewBlockModal extends ReviewModal {
}

async addToOutstanding() {
const dateStr = 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!");
Expand Down
9 changes: 5 additions & 4 deletions src/views/next-rep-schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export class NextRepScheduler extends ModalBase {
// Date

contentEl.appendText("Next repetition date: ");
this.repDateComponent = new TextComponent(contentEl)
.setPlaceholder(this.curRep.nextRepDate.formatYYMMDD())
new NaturalDateSuggest(this.plugin, this.repDateComponent.inputEl)
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 @@ -84,7 +85,7 @@ export class NextRepScheduler extends ModalBase {
}

async schedule() {
const dateStr = this.repDateComponent.getValue();
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);
Expand Down
139 changes: 80 additions & 59 deletions src/views/settings-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,52 +88,72 @@ export class IWSettingsTab extends PluginSettingTab {
this.plugin.saveData(settings);
});
});


const nldates = (<any>this.plugin.app).plugins.getPlugin(
"nldates-obsidian"
);
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 => {
.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));
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)
)
})
},
500,
true
)
);
});

//
// First Rep Date
Expand All @@ -144,30 +164,31 @@ export class IWSettingsTab extends PluginSettingTab {
"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) => {
new NaturalDateSuggest(this.plugin, comp.inputEl);
comp.setValue(String(settings.defaultFirstRepDate))
.setPlaceholder("1970-01-01")
.onChange(
debounce(
(value) => {
if (hasNlDates) {
const dateString = String(value);
const date = nldates.parseDate(dateString);
if (date && date.date) {
LogTo.Debug(
"Setting default first rep date to " + dateString
);
settings.defaultFirstRepDate = dateString;
this.plugin.saveData(settings);
} else {
LogTo.Debug("Invalid natural language date string.");
new NaturalDateSuggest(this.plugin, comp.inputEl);
comp
.setValue(String(settings.defaultFirstRepDate))
.setPlaceholder("1970-01-01")
.onChange(
debounce(
(value) => {
if (hasNlDates) {
const dateString = String(value);
const date = nldates.parseDate(dateString);
if (date && date.date) {
LogTo.Debug(
"Setting default first rep date to " + dateString
);
settings.defaultFirstRepDate = dateString;
this.plugin.saveData(settings);
} else {
LogTo.Debug("Invalid natural language date string.");
}
}
}
},
500,
true
)
);
},
500,
true
)
);
});

//
Expand Down

0 comments on commit 9c8d82f

Please sign in to comment.