Skip to content

Commit 35c3d2f

Browse files
authored
Add ribbon; unload commands; fix weekly note formats (#22)
* Add ribbon; unload commands; fix weekly note formats * Minor style improvements * bump version * bump daily-notes-interface
1 parent 122118a commit 35c3d2f

File tree

8 files changed

+223
-156
lines changed

8 files changed

+223
-156
lines changed

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "periodic-notes",
33
"name": "Periodic Notes",
44
"description": "Create/manage your daily, weekly, and monthly notes",
5-
"version": "0.0.9",
5+
"version": "0.0.10",
66
"author": "Liam Cain",
77
"authorUrl": "https://github.com/liamcain/",
88
"isDesktopOnly": false,

package.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-periodic-notes",
3-
"version": "0.0.9",
3+
"version": "0.0.10",
44
"description": "Create/manage daily, weekly, and monthly notes",
55
"author": "liamcain",
66
"main": "main.js",
@@ -14,30 +14,30 @@
1414
"dependencies": {
1515
"@popperjs/core": "2.6.0",
1616
"obsidian": "obsidianmd/obsidian-api#master",
17-
"obsidian-daily-notes-interface": "0.7.3",
18-
"svelte": "3.32.1",
19-
"tslib": "2.0.3"
17+
"obsidian-daily-notes-interface": "0.7.6",
18+
"svelte": "3.35.0",
19+
"tslib": "2.1.0"
2020
},
2121
"devDependencies": {
22-
"@rollup/plugin-commonjs": "17.0.0",
23-
"@rollup/plugin-node-resolve": "11.0.0",
24-
"@rollup/plugin-typescript": "8.1.0",
22+
"@rollup/plugin-commonjs": "17.1.0",
23+
"@rollup/plugin-node-resolve": "11.2.0",
24+
"@rollup/plugin-typescript": "8.2.0",
2525
"@testing-library/jest-dom": "5.11.9",
2626
"@tsconfig/svelte": "1.0.10",
2727
"@types/jest": "26.0.20",
2828
"@types/moment": "2.13.0",
29-
"@types/node": "14.14.21",
30-
"@typescript-eslint/eslint-plugin": "4.13.0",
31-
"@typescript-eslint/parser": "4.13.0",
32-
"eslint": "7.18.0",
29+
"@types/node": "14.14.34",
30+
"@typescript-eslint/eslint-plugin": "4.17.0",
31+
"@typescript-eslint/parser": "4.17.0",
32+
"eslint": "7.22.0",
3333
"jest": "26.6.3",
3434
"moment": "2.29.1",
35-
"rollup": "2.38.4",
36-
"rollup-plugin-svelte": "7.0.0",
37-
"svelte-check": "1.1.32",
38-
"svelte-preprocess": "4.6.6",
39-
"ts-jest": "26.5.0",
40-
"typescript": "4.1.3"
35+
"rollup": "2.41.2",
36+
"rollup-plugin-svelte": "7.1.0",
37+
"svelte-check": "1.2.5",
38+
"svelte-preprocess": "4.6.9",
39+
"ts-jest": "26.5.3",
40+
"typescript": "4.2.3"
4141
},
4242
"jest": {
4343
"moduleNameMapper": {

src/commands.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ const periodConfigs: Record<IPeriodicity, IPeriodConfig> = {
4444
},
4545
};
4646

47-
async function openPeriodicNote(
47+
export async function openPeriodicNote(
4848
periodicity: IPeriodicity,
4949
date: Moment,
5050
inNewSplit: boolean
5151
): Promise<void> {
5252
const config = periodConfigs[periodicity];
5353
const startOfPeriod = date.clone().startOf(config.unitOfTime);
5454

55-
let allNotes;
55+
let allNotes: Record<string, TFile>;
5656
try {
5757
allNotes = config.getAllNotes();
5858
} catch (err) {
59-
console.error("failed to find your ${periodicity} notes folder", err);
60-
new Notice("Failed to find your ${periodicity} notes folder");
59+
console.error(`failed to find your ${periodicity} notes folder`, err);
60+
new Notice(`Failed to find your ${periodicity} notes folder`);
6161
return;
6262
}
6363

src/index.ts

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type moment from "moment";
22
import { App, Plugin } from "obsidian";
33

4-
import { getCommands } from "./commands";
4+
import { getCommands, openPeriodicNote } from "./commands";
55
import { SETTINGS_UPDATED } from "./events";
66
import {
77
DEFAULT_SETTINGS,
@@ -11,6 +11,7 @@ import {
1111
} from "./settings";
1212
import {
1313
getLegacyWeeklyNoteSettings,
14+
hasCoreDailyNotesPluginEnabled,
1415
hasLegacyWeeklyNoteSettings,
1516
} from "./utils";
1617

@@ -25,26 +26,28 @@ export default class PeriodicNotesPlugin extends Plugin {
2526
public settings: ISettings;
2627
public isInitialLoad: boolean;
2728

29+
private ribbonEls: HTMLElement[];
30+
2831
async onload(): Promise<void> {
32+
this.ribbonEls = [];
33+
2934
this.updateSettings = this.updateSettings.bind(this);
3035

3136
await this.loadSettings();
3237
this.addSettingTab(new PeriodicNotesSettingsTab(this.app, this));
3338

34-
if (this.app.workspace.layoutReady) {
35-
this.onLayoutReady();
36-
} else {
37-
this.app.workspace.on("layout-ready", this.onLayoutReady.bind(this));
38-
}
39+
this.app.workspace.onLayoutReady(this.onLayoutReady.bind(this));
3940
}
4041

41-
private onLayoutReady() {
42+
onLayoutReady(): void {
4243
// If the user has Calendar Weekly Notes settings, migrate them automatically,
4344
// since the functionality will be deprecated.
4445
if (this.isInitialLoad && hasLegacyWeeklyNoteSettings()) {
4546
this.migrateWeeklySettings();
4647
this.settings.weekly.enabled = true;
4748
}
49+
50+
this.configureRibbonIcons();
4851
this.configureCommands();
4952
}
5053

@@ -59,11 +62,37 @@ export default class PeriodicNotesPlugin extends Plugin {
5962
});
6063
}
6164

65+
private configureRibbonIcons() {
66+
for (const ribbonEl of this.ribbonEls) {
67+
ribbonEl.detach();
68+
}
69+
70+
// Only show ribbon icon if daily notes plugin is disabled to avoid confusion
71+
if (this.settings.daily.enabled && !hasCoreDailyNotesPluginEnabled()) {
72+
this.ribbonEls.push(
73+
this.addRibbonIcon("calendar-with-checkmark", "Open today's note", () =>
74+
openPeriodicNote("daily", window.moment(), false)
75+
)
76+
);
77+
}
78+
}
79+
6280
private configureCommands() {
63-
// TODO: There's currently no way to unload the commands when any of these
64-
// are toggled off
81+
// Remove disabled commands
82+
["daily", "weekly", "monthly"]
83+
.filter((periodicity) => !this.settings[periodicity].enabled)
84+
.forEach((periodicity: IPeriodicity) => {
85+
getCommands(periodicity).forEach((command) =>
86+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
87+
(this.app as any).commands.removeCommand(
88+
`periodic-notes:${command.id}`
89+
)
90+
);
91+
});
92+
93+
// register enabled commands
6594
["daily", "weekly", "monthly"]
66-
.filter((periodicity: IPeriodicity) => this.settings[periodicity].enabled)
95+
.filter((periodicity) => this.settings[periodicity].enabled)
6796
.forEach((periodicity: IPeriodicity) => {
6897
getCommands(periodicity).forEach(this.addCommand.bind(this));
6998
});
@@ -93,6 +122,7 @@ export default class PeriodicNotesPlugin extends Plugin {
93122

94123
private onSettingsUpdate(): void {
95124
this.configureCommands();
125+
this.configureRibbonIcons();
96126

97127
// Integrations (i.e. Calendar Plugin) can listen for changes to settings
98128
this.app.workspace.trigger(SETTINGS_UPDATED);

src/settings/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export class PeriodicNotesSettingsTab extends PluginSettingTab {
3838
}
3939

4040
unload(): void {
41+
super.unload();
4142
this.view?.$destroy();
4243
}
4344

src/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ function getDailyNotesPlugin(): IDailyNotesPlugin {
4242
return internalPlugins.getPluginById("daily-notes")?.instance;
4343
}
4444

45+
export function hasCoreDailyNotesPluginEnabled(): boolean {
46+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
47+
const { internalPlugins } = <any>window.app;
48+
return internalPlugins.getPluginById("daily-notes")?._loaded;
49+
}
50+
4551
export function capitalize(text: string): string {
4652
return text.charAt(0).toUpperCase() + text.slice(1);
4753
}

styles.css

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
.settings-banner {
2-
background-color: var(--background-secondary-alt);
3-
border-radius: 4px;
4-
margin-bottom: 2em;
2+
background-color: var(--background-primary-alt);
3+
border-radius: 8px;
4+
border: 1px solid var(--background-modifier-border);
5+
margin-bottom: 1em;
6+
margin-top: 1em;
57
padding: 1.5em;
68
text-align: left;
79
}

0 commit comments

Comments
 (0)