Skip to content

Commit 515af60

Browse files
Added support for migrating notes from Simple Calendar
1 parent 3867f99 commit 515af60

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/classes/applications/configuration-app.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
GameWorldTimeIntegrations,
1010
Icons,
1111
LeapYearRules,
12+
LegacyModuleName,
1213
ModuleName,
1314
MoonYearResetOptions,
1415
NoteReminderNotificationType,
@@ -1854,13 +1855,31 @@ export default class ConfigurationApp extends FormApplication {
18541855
const calendarId = (newCalId || importCalendar?.id || calId).replace("_temp", "");
18551856
for (let n = 0; n < data.notes[calId].length; n++) {
18561857
const journalEntryData = data.notes[calId][n];
1857-
const noteImportedIntoDifferentCalendar = !(journalEntryData.flags[ModuleName].noteData.calendarId === calendarId);
1858-
journalEntryData.flags[ModuleName].noteData.calendarId = calendarId;
1859-
journalEntryData.folder = NManager.noteDirectory?.id;
1860-
if (noteImportedIntoDifferentCalendar || !(<Game>game).journal?.has(journalEntryData._id)) {
1861-
await JournalEntry.create(journalEntryData, { keepId: !noteImportedIntoDifferentCalendar });
1862-
} else {
1863-
(<Game>game).journal?.get(journalEntryData._id)?.update(journalEntryData);
1858+
const noteData = journalEntryData.flags[ModuleName]?.noteData || journalEntryData.flags[LegacyModuleName]?.noteData;
1859+
if (noteData) {
1860+
const noteImportedIntoDifferentCalendar = !(noteData.calendarId === calendarId);
1861+
// add logic to determine if this entry came from the original version of SC, and if so, convert it to the new format
1862+
const isConverted = !journalEntryData.flags[ModuleName];
1863+
const legacyEntryExists = isConverted && (<Game>game).journal?.has(journalEntryData._id);
1864+
if (isConverted) {
1865+
journalEntryData.flags[ModuleName] = journalEntryData.flags[LegacyModuleName];
1866+
journalEntryData.flags["core"].sheetClass = ModuleName + ".NoteSheet";
1867+
journalEntryData.flags[LegacyModuleName] = undefined;
1868+
}
1869+
1870+
journalEntryData.flags[ModuleName].noteData.calendarId = calendarId;
1871+
journalEntryData.folder = NManager.noteDirectory?.id;
1872+
if (legacyEntryExists) {
1873+
// Clean up the legacy entry if it exists.
1874+
delete journalEntryData._id;
1875+
await JournalEntry.create(journalEntryData);
1876+
} else if (noteImportedIntoDifferentCalendar || !(<Game>game).journal?.has(journalEntryData._id)) {
1877+
// import a note into a new calendar OR create one if it doesn't exist.
1878+
await JournalEntry.create(journalEntryData, { keepId: !noteImportedIntoDifferentCalendar });
1879+
} else {
1880+
// update existing journal entry.
1881+
(<Game>game).journal?.get(journalEntryData._id)?.update(journalEntryData);
1882+
}
18641883
}
18651884
}
18661885
}

src/constants.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
* @internal
44
*/
55
export const ModuleName = "foundryvtt-simple-calendar-reborn";
6-
6+
/**
7+
* The original name of this module, used for backwards compatibility.
8+
* @internal
9+
*/
10+
export const LegacyModuleName = "foundryvtt-simple-calendar";
711
/**
812
* The name of the module specific socket
913
* @internal

0 commit comments

Comments
 (0)