( app: App, timeblockId: string, oldDate: string, newDate: string, newStartTime: string, newEndTime: string )
| 793 | * Updates a timeblock in a daily note's frontmatter |
| 794 | */ |
| 795 | export async function updateTimeblockInDailyNote( |
| 796 | app: App, |
| 797 | timeblockId: string, |
| 798 | oldDate: string, |
| 799 | newDate: string, |
| 800 | newStartTime: string, |
| 801 | newEndTime: string |
| 802 | ): Promise<void> { |
| 803 | const { getDailyNote, getAllDailyNotes, appHasDailyNotesPluginLoaded } = await import( |
| 804 | "obsidian-daily-notes-interface" |
| 805 | ); |
| 806 | |
| 807 | if (!appHasDailyNotesPluginLoaded()) { |
| 808 | throw new Error("Daily Notes plugin is not enabled"); |
| 809 | } |
| 810 | |
| 811 | const allDailyNotes = getAllDailyNotes(); |
| 812 | |
| 813 | // Get the timeblock from the old date |
| 814 | const oldMoment = getWindowMoment(oldDate); |
| 815 | const oldDailyNote = getDailyNote(oldMoment, allDailyNotes); |
| 816 | |
| 817 | if (!oldDailyNote) { |
| 818 | throw new Error(`Daily note for ${oldDate} not found`); |
| 819 | } |
| 820 | |
| 821 | const oldContent = await app.vault.read(oldDailyNote); |
| 822 | const timeblocks = extractTimeblocksFromNote(oldContent, oldDailyNote.path); |
| 823 | |
| 824 | // Find the timeblock to move |
| 825 | const timeblockIndex = timeblocks.findIndex((tb) => tb.id === timeblockId); |
| 826 | if (timeblockIndex === -1) { |
| 827 | throw new Error(`Timeblock ${timeblockId} not found`); |
| 828 | } |
| 829 | |
| 830 | const timeblock = timeblocks[timeblockIndex]; |
| 831 | |
| 832 | // If moving to same date, just update times |
| 833 | if (oldDate === newDate) { |
| 834 | await updateTimeblockTimes(app, oldDailyNote, timeblockId, newStartTime, newEndTime); |
| 835 | return; |
| 836 | } |
| 837 | |
| 838 | // Remove from old date |
| 839 | await removeTimeblockFromDailyNote(app, oldDailyNote, timeblockId); |
| 840 | |
| 841 | // Add to new date with updated times |
| 842 | const updatedTimeblock: TimeBlock = { |
| 843 | ...timeblock, |
| 844 | startTime: newStartTime, |
| 845 | endTime: newEndTime, |
| 846 | }; |
| 847 | |
| 848 | await addTimeblockToDailyNote(app, newDate, updatedTimeblock); |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * Copies a timeblock into a daily note with a new ID and time range. |
no test coverage detected