Skip to content

Commit

Permalink
Closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
zachmueller committed Sep 29, 2024
1 parent fb85901 commit 9f79b24
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 63 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "spaced-everything",
"name": "Spaced everything",
"version": "1.2.0",
"version": "1.3.0",
"minAppVersion": "1.6.7",
"description": "Apply spaced repetition algorithms to everything in your vault.",
"author": "Zach Mueller",
Expand Down
124 changes: 101 additions & 23 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ export default class SpacedEverythingPlugin extends Plugin {
async onload() {
await this.loadSettings();
this.logger = new Logger(this.app, this.settings);

// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SpacedEverythingSettingTab(this.app, this));

// Command to log the review outcome
this.addCommand({
id: 'log-review-outcome',
Expand All @@ -53,7 +53,7 @@ export default class SpacedEverythingPlugin extends Plugin {
this.logReviewOutcome(editor, view);
}
});

// Command to open the next review item
this.addCommand({
id: 'open-next-review-item',
Expand All @@ -63,7 +63,7 @@ export default class SpacedEverythingPlugin extends Plugin {
}
});

// Add a new command to toggle contexts for a note
// Command to toggle contexts for a note
this.addCommand({
id: 'toggle-note-contexts',
name: 'Toggle note contexts',
Expand All @@ -72,14 +72,23 @@ export default class SpacedEverythingPlugin extends Plugin {
}
});

// Add a new command to capture thoughts
// Command to capture thoughts
this.addCommand({
id: 'capture-thought',
name: 'Capture thought',
callback: () => {
this.captureThought()
}
});

// Command to update the spacing method for a note
this.addCommand({
id: 'update-spacing-method',
name: 'Update spacing method',
editorCallback: (editor: Editor, view: MarkdownView) => {
this.updateSpacingMethod(editor, view);
}
});
}

async captureThought() {
Expand Down Expand Up @@ -386,13 +395,31 @@ export default class SpacedEverythingPlugin extends Plugin {
// prompt user to select contexts
await this.toggleNoteContexts();

const activeSpacingMethod = this.getActiveSpacingMethod(file, frontmatter);
let activeSpacingMethod: SpacingMethod;
const spacingMethods = this.settings.spacingMethods;

// If there's only one spacing method, use that
if (spacingMethods.length === 1) {
activeSpacingMethod = spacingMethods[0];
} else {
// Prompt the user to select a spacing method
const spacingMethodNames = spacingMethods.map(method => method.name);
const selectedMethod = await suggester(spacingMethodNames, 'Select a spacing method for this note:');

if (selectedMethod) {
activeSpacingMethod = spacingMethods.find(method => method.name === selectedMethod)!;
} else {
new Notice('Onboarding cancelled by user.');
return false;
}
}

// add standard Spaced Everything frontmatter properties and values
await this.app.fileManager.processFrontMatter(file, async (frontmatter: any) => {
frontmatter["se-interval"] = activeSpacingMethod?.defaultInterval || 1;
frontmatter["se-last-reviewed"] = now;
frontmatter["se-ease"] = activeSpacingMethod?.defaultEaseFactor; // TODO::make sure this is optional::
frontmatter['se-interval'] = activeSpacingMethod.defaultInterval;
frontmatter['se-last-reviewed'] = now;
frontmatter['se-ease'] = activeSpacingMethod.defaultEaseFactor;
frontmatter['se-method'] = activeSpacingMethod.name;
});

if (this.settings.logOnboardAction) {
Expand Down Expand Up @@ -459,26 +486,77 @@ export default class SpacedEverythingPlugin extends Plugin {
}

getActiveSpacingMethod(file: TFile, frontmatter: any): SpacingMethod | undefined {
const noteContexts = frontmatter?.['se-contexts'] || [];
const seMethod = frontmatter?.['se-method'];

// If se-method is set, try to find the corresponding spacing method
let activeSpacingMethod = this.settings.spacingMethods.find(method => method.name === seMethod);

// If se-method doesn't match any existing spacing method, proceed with fallback logic
if (!activeSpacingMethod) {
const noteContexts = frontmatter?.['se-contexts'] || [];

// If no contexts are defined for the note, use the first spacing method
if (noteContexts.length === 0) {
return this.settings.spacingMethods[0];
// If no contexts are defined for the note, use the first spacing method
if (noteContexts.length === 0) {
activeSpacingMethod = this.settings.spacingMethods[0];
new Notice(`Set 'se-method' to '${activeSpacingMethod.name}' for this note (no context defined).`);
this.setFrontmatterProperty(file, 'se-method', activeSpacingMethod.name);
return activeSpacingMethod;
}

// Get the first context from the list
const firstContext = noteContexts[0];

// Find the spacing method associated with the first context
const contextSpacingMethod = this.settings.contexts.find(
context => context.name === firstContext
)?.spacingMethodName;

if (contextSpacingMethod) {
activeSpacingMethod = this.settings.spacingMethods.find(method => method.name === contextSpacingMethod);
if (activeSpacingMethod) {
new Notice(`Set 'se-method' to '${activeSpacingMethod.name}' for this note (based on '${firstContext}' context).`);
this.setFrontmatterProperty(file, 'se-method', activeSpacingMethod.name);
return activeSpacingMethod;
}
}

// If no context is mapped to a spacing method, use the first spacing method
activeSpacingMethod = this.settings.spacingMethods[0];
new Notice(`Set 'se-method' to '${activeSpacingMethod.name}' for this note (no context mapped to a spacing method).`);
this.setFrontmatterProperty(file, 'se-method', activeSpacingMethod.name);
return activeSpacingMethod;
}

// Find the first context that matches an active context in the settings
const activeContext = this.settings.contexts.find(
(context) => context.isActive && noteContexts.includes(context.name)
);
// If se-method matches an existing spacing method, return it
return activeSpacingMethod;
}

// If no active context is found, use the first spacing method
if (!activeContext) {
return this.settings.spacingMethods[0];
// Helper function to set a frontmatter property
private setFrontmatterProperty(file: TFile, property: string, value: any) {
this.app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter[property] = value;
});
}

async updateSpacingMethod(editor: Editor, view: MarkdownView) {
const activeFile = this.app.workspace.getActiveFile();
if (!activeFile) {
new Notice('No active file to update spacing method.');
return;
}

// Use the spacing method associated with the active context
const spacingMethodName = activeContext.spacingMethodName;
return this.settings.spacingMethods.find((method) => method.name === spacingMethodName);
const spacingMethodNames = this.settings.spacingMethods.map(method => method.name);
const selectedMethod = await suggester(spacingMethodNames, 'Select a spacing method:');

if (selectedMethod) {
await this.app.fileManager.processFrontMatter(activeFile, (frontmatter) => {
frontmatter['se-method'] = selectedMethod;
});

new Notice(`Updated spacing method to '${selectedMethod}' for ${activeFile.basename}`);
} else {
new Notice('Spacing method update cancelled by user.');
}
}

onunload() {
Expand Down
39 changes: 1 addition & 38 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export class SpacedEverythingSettingTab extends PluginSettingTab {
this.plugin.settings.spacingMethods.push(newSpacingMethod);
await this.plugin.saveSettings();
this.renderSpacingMethodSetting(spacingMethodsDiv, newSpacingMethod, this.plugin.settings.spacingMethods.length - 1);
this.display(); // Re-render the settings tab to ensure new method shows up for context dropdown
})
);

Expand Down Expand Up @@ -107,7 +106,6 @@ export class SpacedEverythingSettingTab extends PluginSettingTab {
const newContext: Context = {
name: '',
isActive: false,
spacingMethodName: this.plugin.settings.spacingMethods[0].name, // Set the default spacing method name
};
this.plugin.settings.contexts.push(newContext);
await this.plugin.saveSettings();
Expand Down Expand Up @@ -350,8 +348,7 @@ export class SpacedEverythingSettingTab extends PluginSettingTab {
.setDesc('Configure the settings for this spacing method.');

const generalSettingsDiv = settingBody.createDiv('general-settings');

let oldName = spacingMethod.name;

new Setting(generalSettingsDiv)
.setName('Name')
.setDesc('Enter a name for this spacing method')
Expand All @@ -368,20 +365,6 @@ export class SpacedEverythingSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
});

textComponent.inputEl.addEventListener('blur', () => {
// Update contexts that were previously mapped to the old name
if (oldName && oldName !== spacingMethod.name) {
this.plugin.settings.contexts.forEach((context) => {
if (context.spacingMethodName === oldName) {
context.spacingMethodName = spacingMethod.name;
}
});
this.plugin.saveSettings();
}

this.display(); // Re-render the settings tab
});

return textComponent;
});

Expand Down Expand Up @@ -489,15 +472,8 @@ export class SpacedEverythingSettingTab extends PluginSettingTab {
cb.setIcon('cross')
.setTooltip('Delete spacing method')
.onClick(async () => {
const name = spacingMethod.name;
const isInUse = this.plugin.settings.contexts.some(
(context) => context.spacingMethodName === name
);

if (this.plugin.settings.spacingMethods.length === 1) {
new Notice('Cannot delete the last spacing method');
} else if (isInUse) {
new Notice(`Spacing method in use by one or more contexts, cannot delete`);
} else {
this.plugin.settings.spacingMethods.splice(index, 1);
await this.plugin.saveSettings();
Expand All @@ -507,10 +483,6 @@ export class SpacedEverythingSettingTab extends PluginSettingTab {
});
}

private getSpacingMethodDropdownOptions(): Record<string, string> {
return Object.fromEntries(this.plugin.settings.spacingMethods.map((method) => [method.name, method.name]));
}

renderContextSetting(containerEl: HTMLElement, context: Context, index: number) {
const settingEl = containerEl.createDiv('context-settings-items');

Expand All @@ -532,15 +504,6 @@ export class SpacedEverythingSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
})
)
.addDropdown((dropdown) =>
dropdown
.addOptions(this.getSpacingMethodDropdownOptions())
.setValue(context.spacingMethodName)
.onChange(async (value) => {
context.spacingMethodName = value;
await this.plugin.saveSettings();
})
)
.addExtraButton((cb) => {
cb.setIcon('cross')
.setTooltip('Delete')
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export interface Context {
name: string;
isActive: boolean;
spacingMethodName: string;
spacingMethodName?: string;
}

export interface ReviewOption {
Expand Down

0 comments on commit 9f79b24

Please sign in to comment.