Skip to content

Commit

Permalink
Merge pull request #9 from jiyee/master
Browse files Browse the repository at this point in the history
Added features (selectable logging heading) and bugs (hotkey toggle & ignoring non-tasks)
  • Loading branch information
Gnopps authored Dec 25, 2023
2 parents d1b0a2f + 1cd5e52 commit ce39457
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 1,709 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ The following configuration options are possible
- _File with rewards_: The file (including folder if applicable) where the rewards are stored. Defaults to "Rewards.md".
- _Show popup when reward is awarded_: If activated a popup (modal) will be shown when a reward is awarded, you'll need to close this before you can continue your work. If deactivated only a timed notice will be shown.
- _Save rewards in daily note_: When active will amend any rewards received to the end of your daily note, one row per awarded reward.
- _The section heading of daily note used to save rewards_: This section heading is used as the place for saving rewards received in the daily note.
- _Save task in daily note_: When active will amend completed tasks to the end of your daily note, one row per completed task. The task will be listed with its name, time completed, and a link to the page it was completed on.
- _The section heading of daily note used to save tasks_: This section heading is used as the place for saving the completed tasks in the daily note.
- _Use with quotes instead of rewards_: Activate this if you are working with inspirational quotes instead of rewards. When active, your rewards are showing without any congratulations or other added text.

#### Reward settings
Expand Down
94 changes: 79 additions & 15 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
App,
TFile,
FileManager,
debounce,
Editor,
MarkdownView,
Expand All @@ -20,7 +22,9 @@ import {
getAllDailyNotes,
} from "obsidian-daily-notes-interface";

import { ObsidianRewarderSettings, DEFAULT_SETTINGS } from "./settings";
import { around } from "monkey-around";

import { ObsidianRewarderSettings, DEFAULT_SETTINGS, ObsidianRewarderSettingsTab } from "./settings";

// Add "batch-mode" where there is only call-out when award won and then all awards are stored in daily-note or batch file

Expand All @@ -37,14 +41,22 @@ export async function getDailyNoteFile(): Promise<TFile> {
export default class ObsidianRewarder extends Plugin {
settings: ObsidianRewarderSettings;

async handleReward(clickedTaskText) {
async handleReward(clickedTaskText: String) {
let arrayOfCleanedRewards = [];
let chosenReward;
let rewardsByOccurrence = {};

// Read contents of rewards file
const { vault } = this.app;
let rewardsFile = vault.getAbstractFileByPath(this.settings.rewardsFile);
const rewardsFile = vault.getAbstractFileByPath(this.settings.rewardsFile);


if (rewardsFile === null || !(rewardsFile instanceof TFile)) {
new Notice(
"Obsidian Rewards couldn't open the rewards file.\nPlease check the path in the settings."
);
return;
}

let contents;

Expand Down Expand Up @@ -105,7 +117,7 @@ export default class ObsidianRewarder extends Plugin {
) {
imageLink = metadataValues[i];
} else if (/^\d+$/.test(metadataValues[i])) {
rewardsLeft = metadataValues[i];
rewardsLeft = parseInt(metadataValues[i]);
} else {
occurrence = metadataValues[i];
}
Expand Down Expand Up @@ -268,7 +280,7 @@ export default class ObsidianRewarder extends Plugin {
async logToDailyNote(clickedTaskText, chosenReward, logTaskOnly) {
// Log to daily note, partly taken from https://github.com/kzhovn/statusbar-pomo-obsidian/blob/master/src/timer.ts

let logText = this.settings.saveTaskToDaily
let logTasksText = this.settings.saveTaskToDaily
? this.settings.completedTaskCharacter +
clickedTaskText +
" ([[" +
Expand All @@ -278,27 +290,54 @@ export default class ObsidianRewarder extends Plugin {
moment().format("HH:mm")
: "";

logText =
logText +
let logRewardsText =
(this.settings.saveRewardToDaily && logTaskOnly === false
? (logText.length > 0 ? "\r" : "") +
"Earned reward: " +
? "Earned reward: " +
chosenReward.rewardName
: "");

if (
(this.settings.saveRewardToDaily === true ||
this.settings.saveTaskToDaily === true) &&
(this.settings.saveRewardToDaily === true || this.settings.saveTaskToDaily === true) &&
appHasDailyNotesPluginLoaded() === true
) {
let file = (await getDailyNoteFile()).path;
//from Note Refactor plugin by James Lynch, https://github.com/lynchjames/note-refactor-obsidian/blob/80c1a23a1352b5d22c70f1b1d915b4e0a1b2b33f/src/obsidian-file.ts#L69

let existingContent = await this.app.vault.adapter.read(file);
if (existingContent.length > 0) {
existingContent = existingContent + "\r";
if (this.settings.saveTaskToDaily) {
const saveTaskSectionHeading = this.settings.saveTaskSectionHeading;
if (saveTaskSectionHeading) {
// from https://stackoverflow.com/questions/66616065/markdown-regex-to-find-all-content-following-an-heading-2-but-stop-at-another
const matches = existingContent.match(new RegExp("(?:^|\n)" + saveTaskSectionHeading + "[^\n]*\n.*?(?=\n*##?\s?|$)", 'gs'));
if (matches && matches.length > 0) {
const match = matches[0];
existingContent = existingContent.replace(match, match + "\n" + logTasksText);
} else {
existingContent = existingContent + "\n" + logTasksText;
}
} else {
existingContent = existingContent + "\n" + logTasksText;
}
}

if (this.settings.saveRewardToDaily && logTaskOnly === false) {
const saveRewardSectionHeading = this.settings.saveRewardSectionHeading;
if (saveRewardSectionHeading) {
const matches = existingContent.match(new RegExp("(?:^|\n)" + saveRewardSectionHeading + "[^\n]*\n.*?(?=\n*##?\s?|$)", 'gs'));
if (matches && matches.length > 0) {
const match = matches[0];
existingContent = existingContent.replace(match, match + "\n" + logRewardsText);
} else {
existingContent = existingContent + "\n" + logRewardsText;
}
} else {
existingContent = existingContent + "\n" + logRewardsText;
}
}
}
await this.app.vault.adapter.write(file, existingContent + logText);

await this.app.vault.adapter.write(file, existingContent);
}
}

Expand Down Expand Up @@ -339,7 +378,7 @@ export default class ObsidianRewarder extends Plugin {
async onload() {
await this.loadSettings();

this.addSettingTab(new ObsidianRewarderSettings(this.app, this));
this.addSettingTab(new ObsidianRewarderSettingsTab(this.app, this));

this.addCommand({
id: "create-sample-rewards-note",
Expand All @@ -353,7 +392,8 @@ export default class ObsidianRewarder extends Plugin {
if (
evt.target instanceof HTMLInputElement &&
evt.target.type === "checkbox" &&
evt.target.checked
evt.target.checked &&
(evt.target.parentNode.innerText.length > 0 || evt.target.parentNode.parentNode.innerText.length > 0)
) {
evt.target.parentNode.innerText.length > 0 // Check where task name is, depends on if is using Obsidian Tasks or not
? this.handleReward(evt.target.parentNode.innerText)
Expand All @@ -367,6 +407,30 @@ export default class ObsidianRewarder extends Plugin {
this.register(() =>
window.removeEventListener("click", callback, { capture: true })
);

if (this.app.workspace.layoutReady) {
this.onLayoutReady();
} else {
this.app.workspace.on("layout-ready", this.onLayoutReady.bind(this));
}
}

onLayoutReady() {
const toggleCheckboxStatusCommand = this.app.commands.findCommand("editor:toggle-checklist-status");
const that = this;
around(toggleCheckboxStatusCommand, {
editorCallback: (next) => function(editor: Editor) {
next.call(this, editor);
let cursor = editor.getCursor();
let lineText = editor.getLine(cursor.line);

const idx = lineText.search(/- \[x\] /); // only checked status
if (idx >= 0) {
const checkedTaskText = lineText.substring(idx + 6);
that.handleReward(checkedTaskText);
}
}
});
}

async loadSettings() {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-rewarder",
"name": "Rewarder",
"version": "0.3.1",
"version": "0.4.0",
"minAppVersion": "0.12.0",
"description": "Gives you rewards for completing tasks/todos, highly configurable.",
"author": "Obsidian",
Expand Down
Loading

0 comments on commit ce39457

Please sign in to comment.