Implement actual simple repeat commands

This commit is contained in:
Andre Perunicic 2022-09-18 01:31:50 -04:00
parent fe9a6bf53d
commit dcbef731c6

View file

@ -5,11 +5,12 @@ import {
PluginSettingTab,
Setting,
} from 'obsidian';
import { DateTime } from 'luxon';
import RepeatView, { REPEATING_NOTES_DUE_VIEW } from './repeat/obsidian/RepeatView';
import RepeatNoteSetupModal from './repeat/obsidian/RepeatNoteSetupModal';
import { RepeatPluginSettings, DEFAULT_SETTINGS } from './settings';
import { determineFrontmatterBounds, replaceOrInsertField } from './frontmatter';
export default class RepeatPlugin extends Plugin {
settings: RepeatPluginSettings;
@ -59,7 +60,7 @@ export default class RepeatPlugin extends Plugin {
'clock', 'Review repeating notes that are due', (evt: MouseEvent) => {
this.activateRepeatNotesDueView();
});
ribbonIconEl.addClass('repeat-pluggin-ribbon-icon');
ribbonIconEl.addClass('repeat-plugin-ribbon-icon');
this.addSettingTab(new RepeatPluginSettingTab(this.app, this));
@ -80,6 +81,53 @@ export default class RepeatPlugin extends Plugin {
return false;
}
});
['day', 'week', 'month', 'year'].map((unit) => {
this.addCommand({
id: `repeat-every-${unit}`,
name: `Repeat this note every ${unit}`,
checkCallback: (checking: boolean) => {
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (markdownView) {
if (!checking) {
const { editor } = markdownView;
let content = editor.getValue();
let bounds = determineFrontmatterBounds(content);
if (!bounds) {
// Create new frontmatter, and update content and bounds.
const newFrontmatter = '---\n---\n';
editor.replaceRange(
newFrontmatter, { line: 0, ch: 0 }, { line: 0, ch: 0 });
content = editor.getValue();
bounds = determineFrontmatterBounds(content);
if (!bounds) {
const filePath = markdownView.file.path;
throw Error(`Failed to create frontmatter in note ${filePath}.`);
}
}
// Insert repetition properties with the desired frequency.
const frontmatter = content.slice(...bounds);
const repeatValue = unit === 'day' ? 'daily' : `${unit}ly`;
let updatedFrontmatter = replaceOrInsertField(
frontmatter, 'repeat', repeatValue);
updatedFrontmatter = replaceOrInsertField(
updatedFrontmatter,
'repeat_due_at',
// TODO: Use logic utils to update due date.
DateTime.now().plus({ [unit]: 1 }).toISO());
editor.setValue([
content.slice(0, bounds[0]),
updatedFrontmatter,
content.slice(bounds[1]),
].join(''));
}
return true;
}
return false;
}
});
});
}
onunload() {