mirror of
https://github.com/jxpeng98/obsidian-to-NotionNext
synced 2026-07-29 08:08:34 +08:00
feat: enhance auto sync functionality to support multiple databases and improved user notifications
This commit is contained in:
54
src/main.ts
54
src/main.ts
@@ -226,41 +226,63 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find which database this file belongs to by checking for NotionID-{abName}
|
// Find all databases this file belongs to by checking for NotionID-{abName}
|
||||||
let foundDbDetails: DatabaseDetails | null = null;
|
const foundDatabases: Array<{ dbDetails: DatabaseDetails, notionId: string }> = [];
|
||||||
let notionId: string | null = null;
|
|
||||||
|
|
||||||
for (const key in this.settings.databaseDetails) {
|
for (const key in this.settings.databaseDetails) {
|
||||||
const dbDetails = this.settings.databaseDetails[key];
|
const dbDetails = this.settings.databaseDetails[key];
|
||||||
const notionIDKey = `NotionID-${dbDetails.abName}`;
|
const notionIDKey = `NotionID-${dbDetails.abName}`;
|
||||||
|
|
||||||
if (frontMatter[notionIDKey]) {
|
if (frontMatter[notionIDKey]) {
|
||||||
foundDbDetails = dbDetails;
|
foundDatabases.push({
|
||||||
notionId = String(frontMatter[notionIDKey]);
|
dbDetails: dbDetails,
|
||||||
break;
|
notionId: String(frontMatter[notionIDKey])
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no NotionID found, skip auto sync
|
// If no NotionID found, notify user to upload manually first
|
||||||
if (!foundDbDetails || !notionId) {
|
if (foundDatabases.length === 0) {
|
||||||
console.log(`[AutoSync] No NotionID found in ${file.path}, skipping auto sync`);
|
console.log(`[AutoSync] No NotionID found in ${file.path}, skipping auto sync`);
|
||||||
|
new Notice(i18nConfig.AutoSyncNoNotionID, 4000);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`[AutoSync] ${new Date().toISOString()} Auto syncing ${file.basename} to ${foundDbDetails.fullName}`);
|
// Notify user about multiple syncs if applicable
|
||||||
|
if (foundDatabases.length > 1) {
|
||||||
|
const message = i18nConfig.AutoSyncMultipleSync.replace('{count}', String(foundDatabases.length));
|
||||||
|
new Notice(message, 3000);
|
||||||
|
console.log(`[AutoSync] Found ${foundDatabases.length} NotionIDs in ${file.path}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sync to all found databases
|
||||||
|
for (const { dbDetails, notionId } of foundDatabases) {
|
||||||
|
console.log(`[AutoSync] ${new Date().toISOString()} Auto syncing ${file.basename} to ${dbDetails.fullName} (${dbDetails.abName})`);
|
||||||
|
|
||||||
|
try {
|
||||||
// Trigger appropriate upload command based on database format
|
// Trigger appropriate upload command based on database format
|
||||||
if (foundDbDetails.format === 'next') {
|
if (dbDetails.format === 'next') {
|
||||||
await uploadCommandNext(this, this.settings, foundDbDetails, this.app);
|
await uploadCommandNext(this, this.settings, dbDetails, this.app);
|
||||||
} else if (foundDbDetails.format === 'general') {
|
} else if (dbDetails.format === 'general') {
|
||||||
await uploadCommandGeneral(this, this.settings, foundDbDetails, this.app);
|
await uploadCommandGeneral(this, this.settings, dbDetails, this.app);
|
||||||
} else if (foundDbDetails.format === 'custom') {
|
} else if (dbDetails.format === 'custom') {
|
||||||
await uploadCommandCustom(this, this.settings, foundDbDetails, this.app);
|
await uploadCommandCustom(this, this.settings, dbDetails, this.app);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[AutoSync] Error syncing to ${dbDetails.fullName}:`, error);
|
||||||
|
const message = i18nConfig.AutoSyncFailed
|
||||||
|
.replace('{database}', dbDetails.fullName)
|
||||||
|
.replace('{error}', error.message);
|
||||||
|
new Notice(message, 5000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`[AutoSync] Error syncing file ${file.path}:`, error);
|
console.error(`[AutoSync] Error syncing file ${file.path}:`, error);
|
||||||
new Notice(`Auto sync failed for ${file.basename}: ${error.message}`);
|
const message = i18nConfig.AutoSyncError
|
||||||
|
.replace('{filename}', file.basename)
|
||||||
|
.replace('{error}', error.message);
|
||||||
|
new Notice(message);
|
||||||
} finally {
|
} finally {
|
||||||
this.syncingFiles.delete(file.path);
|
this.syncingFiles.delete(file.path);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user