feat: Add auto sync success notice setting and update upload commands to utilize it

This commit is contained in:
Jiaxin Peng
2026-03-04 09:05:41 +00:00
parent 4fb3b99996
commit 3e00f127e9
3 changed files with 56 additions and 9 deletions

View File

@@ -92,6 +92,9 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
if (typeof this.settings.autoSyncDelay !== 'number' || this.settings.autoSyncDelay < 2) { if (typeof this.settings.autoSyncDelay !== 'number' || this.settings.autoSyncDelay < 2) {
this.settings.autoSyncDelay = DEFAULT_SETTINGS.autoSyncDelay; this.settings.autoSyncDelay = DEFAULT_SETTINGS.autoSyncDelay;
} }
if (typeof this.settings.autoSyncSuccessNotice !== 'boolean') {
this.settings.autoSyncSuccessNotice = DEFAULT_SETTINGS.autoSyncSuccessNotice;
}
if (typeof this.settings.NotionLinkDisplay !== 'boolean') { if (typeof this.settings.NotionLinkDisplay !== 'boolean') {
this.settings.NotionLinkDisplay = DEFAULT_SETTINGS.NotionLinkDisplay; this.settings.NotionLinkDisplay = DEFAULT_SETTINGS.NotionLinkDisplay;
} }
@@ -112,6 +115,7 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
const needsSave = !loadedData || const needsSave = !loadedData ||
loadedData.autoSync === undefined || loadedData.autoSync === undefined ||
loadedData.autoSyncDelay === undefined || loadedData.autoSyncDelay === undefined ||
loadedData.autoSyncSuccessNotice === undefined ||
loadedData.NotionLinkDisplay === undefined || loadedData.NotionLinkDisplay === undefined ||
loadedData.autoCopyNotionLink === undefined || loadedData.autoCopyNotionLink === undefined ||
loadedData.autoSyncFrontmatterKey === undefined; loadedData.autoSyncFrontmatterKey === undefined;
@@ -123,6 +127,7 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
} else { } else {
if (loadedData.autoSync === undefined) migratedFields.push('autoSync'); if (loadedData.autoSync === undefined) migratedFields.push('autoSync');
if (loadedData.autoSyncDelay === undefined) migratedFields.push('autoSyncDelay'); if (loadedData.autoSyncDelay === undefined) migratedFields.push('autoSyncDelay');
if (loadedData.autoSyncSuccessNotice === undefined) migratedFields.push('autoSyncSuccessNotice');
if (loadedData.NotionLinkDisplay === undefined) migratedFields.push('NotionLinkDisplay'); if (loadedData.NotionLinkDisplay === undefined) migratedFields.push('NotionLinkDisplay');
if (loadedData.autoCopyNotionLink === undefined) migratedFields.push('autoCopyNotionLink'); if (loadedData.autoCopyNotionLink === undefined) migratedFields.push('autoCopyNotionLink');
@@ -146,6 +151,7 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
console.log('[Settings] Settings saved successfully', { console.log('[Settings] Settings saved successfully', {
autoSync: this.settings.autoSync, autoSync: this.settings.autoSync,
autoSyncDelay: this.settings.autoSyncDelay, autoSyncDelay: this.settings.autoSyncDelay,
autoSyncSuccessNotice: this.settings.autoSyncSuccessNotice,
NotionLinkDisplay: this.settings.NotionLinkDisplay, NotionLinkDisplay: this.settings.NotionLinkDisplay,
autoSyncFrontmatterKey: this.settings.autoSyncFrontmatterKey, autoSyncFrontmatterKey: this.settings.autoSyncFrontmatterKey,
databaseCount: Object.keys(this.settings.databaseDetails || {}).length databaseCount: Object.keys(this.settings.databaseDetails || {}).length
@@ -162,6 +168,10 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
console.warn('[Settings] Invalid autoSyncDelay value, resetting to default'); console.warn('[Settings] Invalid autoSyncDelay value, resetting to default');
this.settings.autoSyncDelay = DEFAULT_SETTINGS.autoSyncDelay; this.settings.autoSyncDelay = DEFAULT_SETTINGS.autoSyncDelay;
} }
if (typeof this.settings.autoSyncSuccessNotice !== 'boolean') {
console.warn('[Settings] Invalid autoSyncSuccessNotice value, resetting to default');
this.settings.autoSyncSuccessNotice = DEFAULT_SETTINGS.autoSyncSuccessNotice;
}
if (typeof this.settings.NotionLinkDisplay !== 'boolean') { if (typeof this.settings.NotionLinkDisplay !== 'boolean') {
console.warn('[Settings] Invalid NotionLinkDisplay value, resetting to default'); console.warn('[Settings] Invalid NotionLinkDisplay value, resetting to default');
this.settings.NotionLinkDisplay = DEFAULT_SETTINGS.NotionLinkDisplay; this.settings.NotionLinkDisplay = DEFAULT_SETTINGS.NotionLinkDisplay;
@@ -415,11 +425,11 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
try { try {
// Trigger appropriate upload command based on database format // Trigger appropriate upload command based on database format
if (dbDetails.format === 'next') { if (dbDetails.format === 'next') {
await uploadCommandNext(this, this.settings, dbDetails, this.app); await uploadCommandNext(this, this.settings, dbDetails, this.app, { isAutoSync: true });
} else if (dbDetails.format === 'general') { } else if (dbDetails.format === 'general') {
await uploadCommandGeneral(this, this.settings, dbDetails, this.app); await uploadCommandGeneral(this, this.settings, dbDetails, this.app, { isAutoSync: true });
} else if (dbDetails.format === 'custom') { } else if (dbDetails.format === 'custom') {
await uploadCommandCustom(this, this.settings, dbDetails, this.app); await uploadCommandCustom(this, this.settings, dbDetails, this.app, { isAutoSync: true });
} }
} catch (error) { } catch (error) {
console.error(`[AutoSync] Error syncing to ${dbDetails.fullName}:`, error); console.error(`[AutoSync] Error syncing to ${dbDetails.fullName}:`, error);

View File

@@ -17,6 +17,7 @@ export interface PluginSettings {
autoCopyNotionLink: boolean; autoCopyNotionLink: boolean;
autoSync: boolean; autoSync: boolean;
autoSyncDelay: number; autoSyncDelay: number;
autoSyncSuccessNotice: boolean;
autoSyncFrontmatterKey: string; autoSyncFrontmatterKey: string;
proxy: string; proxy: string;
GeneralButton: boolean; GeneralButton: boolean;
@@ -57,6 +58,7 @@ export const DEFAULT_SETTINGS: PluginSettings = {
autoCopyNotionLink: true, autoCopyNotionLink: true,
autoSync: false, autoSync: false,
autoSyncDelay: 5, autoSyncDelay: 5,
autoSyncSuccessNotice: false,
autoSyncFrontmatterKey: DEFAULT_AUTO_SYNC_DATABASE_KEY, autoSyncFrontmatterKey: DEFAULT_AUTO_SYNC_DATABASE_KEY,
proxy: "", proxy: "",
GeneralButton: true, GeneralButton: true,
@@ -100,6 +102,15 @@ export class ObsidianSettingTab extends PluginSettingTab {
this.createSettingEl(containerEl, i18nConfig.AutoCopyNotionLink, i18nConfig.AutoCopyNotionLinkDesc, 'toggle', i18nConfig.AutoCopyNotionLink, this.plugin.settings.autoCopyNotionLink, 'autoCopyNotionLink') this.createSettingEl(containerEl, i18nConfig.AutoCopyNotionLink, i18nConfig.AutoCopyNotionLinkDesc, 'toggle', i18nConfig.AutoCopyNotionLink, this.plugin.settings.autoCopyNotionLink, 'autoCopyNotionLink')
this.createSettingEl(containerEl, i18nConfig.AutoSync, i18nConfig.AutoSyncDesc, 'toggle', i18nConfig.AutoSync, this.plugin.settings.autoSync, 'autoSync') this.createSettingEl(containerEl, i18nConfig.AutoSync, i18nConfig.AutoSyncDesc, 'toggle', i18nConfig.AutoSync, this.plugin.settings.autoSync, 'autoSync')
this.createSettingEl(
containerEl,
i18nConfig.AutoSyncSuccessNotice,
i18nConfig.AutoSyncSuccessNoticeDesc,
'toggle',
i18nConfig.AutoSyncSuccessNotice,
this.plugin.settings.autoSyncSuccessNotice,
'autoSyncSuccessNotice'
)
new Setting(containerEl) new Setting(containerEl)
.setName(i18nConfig.AutoSyncFrontmatterKey) .setName(i18nConfig.AutoSyncFrontmatterKey)
@@ -369,4 +380,3 @@ export class ObsidianSettingTab extends PluginSettingTab {
} }
} }
} }

View File

@@ -10,6 +10,10 @@ import {getNowFileMarkdownContentCustom} from "./common/getMarkdownCustom";
const SYNC_ERROR_NOTICE_DURATION = 8000; const SYNC_ERROR_NOTICE_DURATION = 8000;
interface UploadCommandOptions {
isAutoSync?: boolean;
}
function extractErrorMessage(error: unknown): string { function extractErrorMessage(error: unknown): string {
if (error instanceof Error && error.message) { if (error instanceof Error && error.message) {
return error.message; return error.message;
@@ -17,6 +21,16 @@ function extractErrorMessage(error: unknown): string {
return String(error); return String(error);
} }
function shouldShowAutoSyncSuccessNotice(
plugin: ObsidianSyncNotionPlugin,
options?: UploadCommandOptions,
): boolean {
if (!options?.isAutoSync) {
return true;
}
return !!plugin.settings.autoSyncSuccessNotice;
}
function notifySyncError(prefix: string, basename: string, error: unknown): void { function notifySyncError(prefix: string, basename: string, error: unknown): void {
const errorMessage = extractErrorMessage(error); const errorMessage = extractErrorMessage(error);
console.error(`${prefix} Sync failed`, error); console.error(`${prefix} Sync failed`, error);
@@ -39,6 +53,7 @@ export async function uploadCommandNext(
settings: PluginSettings, settings: PluginSettings,
dbDetails: DatabaseDetails, dbDetails: DatabaseDetails,
app: App, app: App,
options?: UploadCommandOptions,
) { ) {
const {notionAPI, databaseID} = dbDetails; const {notionAPI, databaseID} = dbDetails;
@@ -118,7 +133,9 @@ export async function uploadCommandNext(
const {response} = res; const {response} = res;
if (response.status === 200) { if (response.status === 200) {
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green"; if (shouldShowAutoSyncSuccessNotice(plugin, options)) {
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green";
}
logCommandDebug("uploadCommandNext", "Sync succeeded", { logCommandDebug("uploadCommandNext", "Sync succeeded", {
filename: basename, filename: basename,
@@ -146,6 +163,7 @@ export async function uploadCommandGeneral(
settings: PluginSettings, settings: PluginSettings,
dbDetails: DatabaseDetails, dbDetails: DatabaseDetails,
app: App, app: App,
options?: UploadCommandOptions,
) { ) {
const {notionAPI, databaseID} = dbDetails; const {notionAPI, databaseID} = dbDetails;
@@ -160,7 +178,9 @@ export async function uploadCommandGeneral(
const {markDownData, nowFile, cover, tags} = await getNowFileMarkdownContentGeneral(app, settings) const {markDownData, nowFile, cover, tags} = await getNowFileMarkdownContentGeneral(app, settings)
new Notice(i18nConfig.StartUpload.replace('{filename}', nowFile.basename)); if (!options?.isAutoSync) {
new Notice(i18nConfig.StartUpload.replace('{filename}', nowFile.basename));
}
console.log(`Start upload ${nowFile.basename}`); console.log(`Start upload ${nowFile.basename}`);
if (markDownData) { if (markDownData) {
@@ -195,7 +215,9 @@ export async function uploadCommandGeneral(
const {response} = res; const {response} = res;
if (response.status === 200) { if (response.status === 200) {
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green"; if (shouldShowAutoSyncSuccessNotice(plugin, options)) {
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green";
}
logCommandDebug("uploadCommandGeneral", "Sync succeeded", { logCommandDebug("uploadCommandGeneral", "Sync succeeded", {
filename: basename, filename: basename,
@@ -223,6 +245,7 @@ export async function uploadCommandCustom(
settings: PluginSettings, settings: PluginSettings,
dbDetails: DatabaseDetails, dbDetails: DatabaseDetails,
app: App, app: App,
options?: UploadCommandOptions,
) { ) {
const {notionAPI, databaseID} = dbDetails; const {notionAPI, databaseID} = dbDetails;
@@ -237,7 +260,9 @@ export async function uploadCommandCustom(
const {markDownData, nowFile, cover, customValues} = await getNowFileMarkdownContentCustom(app, dbDetails) const {markDownData, nowFile, cover, customValues} = await getNowFileMarkdownContentCustom(app, dbDetails)
new Notice(i18nConfig.StartUpload.replace('{filename}', nowFile.basename)); if (!options?.isAutoSync) {
new Notice(i18nConfig.StartUpload.replace('{filename}', nowFile.basename));
}
console.log(`Start upload ${nowFile.basename}`); console.log(`Start upload ${nowFile.basename}`);
if (markDownData) { if (markDownData) {
@@ -272,7 +297,9 @@ export async function uploadCommandCustom(
const {response} = res; const {response} = res;
if (response.status === 200) { if (response.status === 200) {
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green"; if (shouldShowAutoSyncSuccessNotice(plugin, options)) {
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green";
}
logCommandDebug("uploadCommandCustom", "Sync succeeded", { logCommandDebug("uploadCommandCustom", "Sync succeeded", {
filename: basename, filename: basename,