Skip to content

Commit

Permalink
Remove weekly notes from core, add Periodic Notes support (liamcain#129)
Browse files Browse the repository at this point in the history
* Remove weekly notes from core

* UI improvements to direct users to download Periodic Notes

* Update readme

* use v0.3.1 of calendar-ui

* minor style fixes

* Hide weekly settings if periodic notes - weekly is enabled

* Update jest config

* bump version
  • Loading branch information
liamcain authored Feb 9, 2021
1 parent 726dd6e commit c937b71
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 311 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ If you want to style weekends to be distinguishable from weekdays, you can set t

#### Weekly notes have a new home

The weekly note functionality has been split out into its [very own plugin](https://github.com/liamcain/obsidian-weekly-notes). In the future, the functionality will be removed from the Calendar plugin; so if you're currently using weekly notes, I encourage you to make the switch. Don't worry, the behavior is functionally identical and will still integrate with the calendar view!
The weekly note functionality has been split out into its [very own plugin](https://github.com/liamcain/obsidian-periodic-notes/). In the future, the functionality will be removed from the Calendar plugin; so if you're currently using weekly notes, I encourage you to make the switch. Don't worry, the behavior is functionally identical and will still integrate with the calendar view!

This split was inspired by the [One Thing Well](https://en.wikipedia.org/wiki/Unix_philosophy) philosophy. Plugins should be as modular. Some users might want weekly notes and have no use for a calendar view. And Vice versa.
This split was inspired by the [One Thing Well](https://en.wikipedia.org/wiki/Unix_philosophy) philosophy. Plugins should be as modular. Some users might want weekly notes and have no use for a calendar view. And vice versa.

If you are currently using weekly notes within the Calendar plugin, the new Weekly Notes plugin will migrate your settings for you automatically.
If you are currently using weekly notes within the Calendar plugin, the new Periodic Notes plugin will migrate your settings for you automatically.

### Usage

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "calendar",
"name": "Calendar",
"description": "Calendar view of your daily notes",
"version": "1.4.19",
"version": "1.5.0",
"author": "Liam Cain",
"authorUrl": "https://github.com/liamcain/",
"isDesktopOnly": false,
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"name": "calendar",
"version": "1.4.19",
"version": "1.5.0",
"description": "Calendar view of your daily notes",
"author": "liamcain",
"main": "main.js",
"license": "MIT",
"scripts": {
"lint": "svelte-check && eslint . --ext .ts",
"build": "npm run lint && rollup -c",
"test": "jest",
"test": "jest --passWithNoTests",
"test:watch": "yarn test -- --watch"
},
"dependencies": {
"obsidian": "obsidianmd/obsidian-api#master",
"obsidian-calendar-ui": "0.3.0",
"obsidian-daily-notes-interface": "0.5.1",
"obsidian-calendar-ui": "0.3.1",
"obsidian-daily-notes-interface": "0.7.0",
"svelte": "3.31.0",
"tslib": "2.0.3"
},
Expand Down
134 changes: 0 additions & 134 deletions src/io/__tests__/weeklyNotes.spec.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/io/path.ts

This file was deleted.

87 changes: 8 additions & 79 deletions src/io/weeklyNotes.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,13 @@
import type { Moment } from "moment";
import { Notice, TFile } from "obsidian";
import { getTemplateContents } from "obsidian-daily-notes-interface";
import type { TFile } from "obsidian";
import {
createWeeklyNote,
getWeeklyNoteSettings,
} from "obsidian-daily-notes-interface";

import { getWeeklyNoteSettings, ISettings } from "src/settings";
import type { ISettings } from "src/settings";
import { createConfirmationDialog } from "src/ui/modal";

import { getNotePath } from "./path";

function getDaysOfWeek(): string[] {
const { moment } = window;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let weekStart = (<any>moment.localeData())._week.dow;
const daysOfWeek = [
"sunday",
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
];

while (weekStart) {
daysOfWeek.push(daysOfWeek.shift());
weekStart--;
}
return daysOfWeek;
}

export function getDayOfWeekNumericalValue(dayOfWeekName: string): number {
return getDaysOfWeek().indexOf(dayOfWeekName.toLowerCase());
}

export async function createWeeklyNote(
date: Moment,
settings: ISettings
): Promise<TFile> {
const { vault } = window.app;
const { template, format, folder } = getWeeklyNoteSettings(settings);
const templateContents = await getTemplateContents(template);
const filename = date.format(format);
const normalizedPath = getNotePath(folder, filename);

try {
const createdFile = await vault.create(
normalizedPath,
templateContents
.replace(
/{{\s*(date|time)\s*:(.*?)}}/gi,
(_, _timeOrDate, momentFormat) => {
return date.format(momentFormat.trim());
}
)
.replace(/{{\s*title\s*}}/gi, filename)
.replace(
/{{\s*(sunday|monday|tuesday|wednesday|thursday|friday|saturday)\s*:(.*?)}}/gi,
(_, dayOfWeek, momentFormat) => {
const day = getDayOfWeekNumericalValue(dayOfWeek);
return date.weekday(day).format(momentFormat.trim());
}
)
);
return createdFile;
} catch (err) {
console.error(`Failed to create file: '${normalizedPath}'`, err);
new Notice("Unable to create new file.");
}
}

export function getWeeklyNote(date: Moment, settings: ISettings): TFile {
const { vault } = window.app;
const { format, folder } = getWeeklyNoteSettings(settings);

const startOfWeek = date.clone().startOf("week");
const baseFilename = startOfWeek.format(format);

const fullPath = getNotePath(folder, baseFilename);
return vault.getAbstractFileByPath(fullPath) as TFile;
}

/**
* Create a Weekly Note for a given date.
*/
Expand All @@ -89,11 +18,11 @@ export async function tryToCreateWeeklyNote(
cb?: (file: TFile) => void
): Promise<void> {
const { workspace } = window.app;
const { format } = getWeeklyNoteSettings(settings);
const { format } = getWeeklyNoteSettings();
const filename = date.format(format);

const createFile = async () => {
const dailyNote = await createWeeklyNote(date, settings);
const dailyNote = await createWeeklyNote(date);
const leaf = inNewSplit
? workspace.splitActiveLeaf()
: workspace.getUnpinnedLeaf();
Expand Down
46 changes: 25 additions & 21 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { App, PluginSettingTab, Setting } from "obsidian";
import {
appHasDailyNotesPluginLoaded,
IDailyNoteSettings,
} from "obsidian-daily-notes-interface";
import { appHasDailyNotesPluginLoaded } from "obsidian-daily-notes-interface";
import type { ILocaleOverride, IWeekStartOption } from "obsidian-calendar-ui";

import { DEFAULT_WEEK_FORMAT, DEFAULT_WORDS_PER_DOT } from "src/constants";
Expand Down Expand Up @@ -33,16 +30,6 @@ const weekdays = [
"saturday",
];

export function getWeeklyNoteSettings(settings: ISettings): IDailyNoteSettings {
return {
format: settings.weeklyNoteFormat || DEFAULT_WEEK_FORMAT,
folder: settings.weeklyNoteFolder ? settings.weeklyNoteFolder.trim() : "",
template: settings.weeklyNoteTemplate
? settings.weeklyNoteTemplate.trim()
: "",
};
}

export const defaultSettings = Object.freeze({
shouldConfirmBeforeCreate: true,
weekStart: "locale" as IWeekStartOption,
Expand All @@ -57,6 +44,12 @@ export const defaultSettings = Object.freeze({
localeOverride: "system-default",
});

function appHasPeriodicNotesPluginLoaded() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const periodicNotes = (<any>window.app).plugins.getPlugin("periodic-notes");
return periodicNotes && periodicNotes.settings?.weekly?.enabled;
}

export class CalendarSettingsTab extends PluginSettingTab {
private plugin: CalendarPlugin;

Expand All @@ -69,12 +62,15 @@ export class CalendarSettingsTab extends PluginSettingTab {
this.containerEl.empty();

if (!appHasDailyNotesPluginLoaded()) {
this.containerEl.createEl("h3", {
text: "⚠️ Daily Notes plugin not enabled",
});
this.containerEl.createEl("p", {
text:
"The calendar is best used in conjunction with the Daily Notes plugin. Enable it in your plugin settings for a more optimal experience.",
this.containerEl.createDiv("settings-banner", (banner) => {
banner.createEl("h3", {
text: "⚠️ Daily Notes plugin not enabled",
});
banner.createEl("p", {
cls: "setting-item-description",
text:
"The calendar is best used in conjunction with either the Daily Notes plugin or the Periodic Notes plugin (available in the Community Plugins catalog).",
});
});
}

Expand All @@ -86,10 +82,18 @@ export class CalendarSettingsTab extends PluginSettingTab {
this.addConfirmCreateSetting();
this.addShowWeeklyNoteSetting();

if (this.plugin.options.showWeeklyNote) {
if (
this.plugin.options.showWeeklyNote &&
!appHasPeriodicNotesPluginLoaded()
) {
this.containerEl.createEl("h3", {
text: "Weekly Note Settings",
});
this.containerEl.createEl("p", {
cls: "setting-item-description",
text:
"Note: Weekly Note settings are moving. You are encouraged to install the 'Periodic Notes' plugin to keep the functionality in the future.",
});
this.addWeeklyNoteFormatSetting();
this.addWeeklyNoteTemplateSetting();
this.addWeeklyNoteFolderSetting();
Expand Down
3 changes: 2 additions & 1 deletion src/ui/Calendar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { onDestroy } from "svelte";
import type { ISettings } from "src/settings";
import { activeFile, dailyNotes, settings } from "./stores";
import { activeFile, dailyNotes, settings, weeklyNotes } from "./stores";
let today: Moment;
Expand All @@ -32,6 +32,7 @@
function getToday(settings: ISettings) {
configureGlobalMomentLocale(settings.localeOverride, settings.weekStart);
dailyNotes.reindex();
weeklyNotes.reindex();
return window.moment();
}
Expand Down
Loading

0 comments on commit c937b71

Please sign in to comment.