-
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tanner Harrison
committed
May 6, 2024
1 parent
ef3f269
commit de2ff37
Showing
5 changed files
with
160 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import type { Moment } from "moment"; | ||
import type { TFile } from "obsidian"; | ||
import { | ||
createDailyNote, | ||
createWeeklyNote, | ||
createMonthlyNote, | ||
createYearlyNote, | ||
getDailyNoteSettings, | ||
getWeeklyNoteSettings, | ||
getMonthlyNoteSettings, | ||
getYearlyNoteSettings, | ||
} from "obsidian-daily-notes-interface"; | ||
|
||
import type { ISettings } from "src/settings"; | ||
import { createConfirmationDialog } from "src/ui/modal"; | ||
|
||
export enum Period { | ||
Daily = "Daily", | ||
Weekly = "Weekly", | ||
Monthly = "Monthly", | ||
Yearly = "Yearly" | ||
} | ||
|
||
export const getDateForPeriodicNote = (date: Moment, noteType: Period) => { | ||
switch (noteType) { | ||
case Period.Daily: | ||
return date; | ||
case Period.Weekly: | ||
return date.startOf("week"); | ||
case Period.Monthly: | ||
return date.startOf("month"); | ||
case Period.Yearly: | ||
return date.startOf("year"); | ||
} | ||
} | ||
const getCreateNoteFunction = (noteType: Period) => { | ||
switch (noteType) { | ||
case Period.Daily: | ||
return createDailyNote; | ||
case Period.Weekly: | ||
return createWeeklyNote; | ||
case Period.Monthly: | ||
return createMonthlyNote; | ||
case Period.Yearly: | ||
return createYearlyNote; | ||
} | ||
}; | ||
|
||
const getNoteSettingsGetter = (noteType: Period) => { | ||
switch (noteType) { | ||
case Period.Daily: | ||
return getDailyNoteSettings; | ||
case Period.Weekly: | ||
return getWeeklyNoteSettings; | ||
case Period.Monthly: | ||
return getMonthlyNoteSettings; | ||
case Period.Yearly: | ||
return getYearlyNoteSettings; | ||
} | ||
}; | ||
|
||
/** | ||
* Create a Daily Note for a given date. | ||
*/ | ||
export async function tryToCreatePeriodicNote( | ||
date: Moment, | ||
inNewSplit: boolean, | ||
settings: ISettings, | ||
noteType: Period, | ||
cb?: (newFile: TFile) => void | ||
): Promise<void> { | ||
const noteCreator = getCreateNoteFunction(noteType); | ||
const noteSettingsGetter = getNoteSettingsGetter(noteType); | ||
|
||
const { workspace } = window.app; | ||
const { format } = noteSettingsGetter(); | ||
const filename = date.format(format); | ||
|
||
|
||
const createFile = async () => { | ||
const note = await noteCreator(date); | ||
const leaf = inNewSplit | ||
? workspace.splitActiveLeaf() | ||
: workspace.getUnpinnedLeaf(); | ||
|
||
await leaf.openFile(note, { active: true }); | ||
cb?.(note); | ||
}; | ||
|
||
if (settings.shouldConfirmBeforeCreate) { | ||
createConfirmationDialog({ | ||
cta: "Create", | ||
onAccept: createFile, | ||
text: `File ${filename} does not exist. Would you like to create it?`, | ||
title: `New ${noteType} Note test`, | ||
}); | ||
} else { | ||
await createFile(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -602,6 +602,13 @@ | |
dependencies: | ||
"@types/tern" "*" | ||
|
||
"@types/[email protected]": | ||
version "5.60.8" | ||
resolved "https://registry.yarnpkg.com/@types/codemirror/-/codemirror-5.60.8.tgz#b647d04b470e8e1836dd84b2879988fc55c9de68" | ||
integrity sha512-VjFgDF/eB+Aklcy15TtOTLQeMjTo07k7KAjql8OK5Dirr7a6sJY4T1uVBDuTVG9VEmn1uUsohOpYnVfgC6/jyw== | ||
dependencies: | ||
"@types/tern" "*" | ||
|
||
"@types/estree@*": | ||
version "0.0.47" | ||
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.47.tgz#d7a51db20f0650efec24cd04994f523d93172ed4" | ||
|
@@ -3074,6 +3081,11 @@ moment@*, [email protected]: | |
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3" | ||
integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ== | ||
|
||
[email protected]: | ||
version "2.29.4" | ||
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108" | ||
integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w== | ||
|
||
[email protected]: | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" | ||
|
@@ -3219,14 +3231,21 @@ [email protected]: | |
obsidian obsidianmd/obsidian-api#master | ||
tslib "2.1.0" | ||
|
||
[email protected].0: | ||
version "0.9.0" | ||
resolved "https://registry.yarnpkg.com/obsidian-daily-notes-interface/-/obsidian-daily-notes-interface-0.9.0.tgz#6a8996917899097c99ed62264a28e231a297d3c6" | ||
integrity sha512-vzqOLgjTJi+jeT654eRgyEA2PfqeNI23Fe955VwqnRpAjkarr6qcllpC9cs1XBgpjmRm/XRIqXFdLsO+1z1RGg== | ||
[email protected].4: | ||
version "0.9.4" | ||
resolved "https://registry.yarnpkg.com/obsidian-daily-notes-interface/-/obsidian-daily-notes-interface-0.9.4.tgz#df085c5aa4c8c2b2b810185d4d9dc24344cb7604" | ||
integrity sha512-PILoRtZUB5wEeGnDQAPMlkVlXwDYoxkLR8Wl4STU2zLNwhcq9kKvQexiXi7sfjGlpTnL+LeAOfEVWyeVndneKg== | ||
dependencies: | ||
obsidian obsidianmd/obsidian-api#master | ||
tslib "2.1.0" | ||
|
||
"obsidian@github:obsidianmd/obsidian-api#master": | ||
version "1.5.7" | ||
resolved "https://codeload.github.com/obsidianmd/obsidian-api/tar.gz/8b2eda0f24285636c8aa116972643e5233a23dc1" | ||
dependencies: | ||
"@types/codemirror" "5.60.8" | ||
moment "2.29.4" | ||
|
||
obsidian@obsidianmd/obsidian-api#master: | ||
version "0.11.11" | ||
resolved "https://codeload.github.com/obsidianmd/obsidian-api/tar.gz/dbfa19ad7aa6557f0ecff962065c3f540bc77e27" | ||
|