Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dartungar committed Jul 21, 2024
2 parents 29d8342 + ab3cdc0 commit 85955cb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
29 changes: 25 additions & 4 deletions src/filesIntegration/fileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,32 @@ export class FileService {
}

const result = this.getEntryAsString(entry);
const content = await this._plugin.app.vault.read(file);
const contentArray: string[] = content.split("\n");

let content = await this._plugin.app.vault.read(file);
// TODO: add in specific place, e.g after specified heading
content = content.replace(/\n+$/g, "");
this._plugin.app.vault.modify(file, content + "\n" + result);
let index: number = contentArray.indexOf(this._plugin.settings.journalPosition);

if (index != -1 && index != contentArray.length && index+1 != contentArray.length) {
while ( contentArray[index + 1].startsWith("-") ) {
index = index + 1;
if (index == contentArray.length || contentArray[index + 1] == undefined) {
break;
}
}
contentArray.splice(index + 1, 0, `${result}`);
this._plugin.app.vault.modify(file, contentArray.join("\n"));
} else {
if ( index+1 != contentArray.length ) {
this._plugin.showNotice(
`could not find the selected position in your journal-file -> Adding mood to the bottom.`,
5000,
`Mood Tracker`
);
}
let original_content = content.replace(/\n+$/g, "");
this._plugin.app.vault.modify(file, original_content + "\n" + result);
}
return;
}

private getEntryAsString(entry: MoodTrackerEntry): string {
Expand Down
13 changes: 11 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,17 @@ export default class MoodTrackerPlugin extends Plugin {
await this.saveEntries();
}

public showNotice(message: string, durationMs = 5000) {
new Notice(message, durationMs);
public showNotice(message: string, durationMs = 5000, title?: string) {
if (typeof title !== 'undefined') {
const notice = new Notice("", durationMs);
notice.noticeEl.append(
createEl("strong", { text: title, cls: "mood-tracker-notice-header"}),
createEl("br"),
message,
);
} else {
new Notice(message, durationMs);
}
}

async loadSettings() {
Expand Down
2 changes: 2 additions & 0 deletions src/settings/moodTrackerSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class MoodTrackerSettings {
trackerModalTitle: string;
useEmotions: boolean;
addToJournal: boolean;
journalPosition: string;
journalFilePath: string;
entryTemplate: string;
chartColor: string;
Expand All @@ -30,6 +31,7 @@ export const DEFAULT_SETTINGS: MoodTrackerSettings = {
entryTemplate: "- {{ICON}} {{NOTE}}",
trackerModalTitle: "How are you feeling?",
useEmotions: true,
journalPosition: "## Mood Tracker",
addToJournal: false,
journalFilePath: "",
chartColor: "#b26aba"
Expand Down
20 changes: 20 additions & 0 deletions src/settings/settingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class MoodTrackerSettingsTab extends PluginSettingTab {
this.addAddToNoteSettings();
if (this._plugin.settings.addToJournal) {
this.addJournalPathSetting();
this.addJournalLocation();
this.addTemplateSetting();
}

Expand Down Expand Up @@ -171,6 +172,25 @@ export class MoodTrackerSettingsTab extends PluginSettingTab {
})
}

private addJournalLocation() {
const setting = new Setting(this.containerEl);

setting.setName("Entry location");
setting.descEl.innerHTML = `Where in the journal should the Mood-Tracker entry be placed?<br>
Example: ## Mood Tracker
`

setting.addText((input) => {
input.inputEl.style.width = "min(400px, 35vw)";
input.setValue(this._plugin.settings.journalPosition)
.onChange(async (value) => {
this._plugin.settings.journalPosition = value;
await this._plugin.saveSettings();
});

})
}

private addTemplateSetting() {
const setting = new Setting(this.containerEl);

Expand Down
4 changes: 4 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
display: flex;
flex-direction: column;
align-items: center;
}

.mood-tracker-notice-header {
font-weight: 900;
}

0 comments on commit 85955cb

Please sign in to comment.