mirror of
https://github.com/jxpeng98/obsidian-to-NotionNext
synced 2026-07-29 08:08:34 +08:00
feat: Add auto sync success notice setting and update upload commands to utilize it
This commit is contained in:
16
src/main.ts
16
src/main.ts
@@ -92,6 +92,9 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
|
||||
if (typeof this.settings.autoSyncDelay !== 'number' || this.settings.autoSyncDelay < 2) {
|
||||
this.settings.autoSyncDelay = DEFAULT_SETTINGS.autoSyncDelay;
|
||||
}
|
||||
if (typeof this.settings.autoSyncSuccessNotice !== 'boolean') {
|
||||
this.settings.autoSyncSuccessNotice = DEFAULT_SETTINGS.autoSyncSuccessNotice;
|
||||
}
|
||||
if (typeof this.settings.NotionLinkDisplay !== 'boolean') {
|
||||
this.settings.NotionLinkDisplay = DEFAULT_SETTINGS.NotionLinkDisplay;
|
||||
}
|
||||
@@ -112,6 +115,7 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
|
||||
const needsSave = !loadedData ||
|
||||
loadedData.autoSync === undefined ||
|
||||
loadedData.autoSyncDelay === undefined ||
|
||||
loadedData.autoSyncSuccessNotice === undefined ||
|
||||
loadedData.NotionLinkDisplay === undefined ||
|
||||
loadedData.autoCopyNotionLink === undefined ||
|
||||
loadedData.autoSyncFrontmatterKey === undefined;
|
||||
@@ -123,6 +127,7 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
|
||||
} else {
|
||||
if (loadedData.autoSync === undefined) migratedFields.push('autoSync');
|
||||
if (loadedData.autoSyncDelay === undefined) migratedFields.push('autoSyncDelay');
|
||||
if (loadedData.autoSyncSuccessNotice === undefined) migratedFields.push('autoSyncSuccessNotice');
|
||||
if (loadedData.NotionLinkDisplay === undefined) migratedFields.push('NotionLinkDisplay');
|
||||
if (loadedData.autoCopyNotionLink === undefined) migratedFields.push('autoCopyNotionLink');
|
||||
|
||||
@@ -146,6 +151,7 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
|
||||
console.log('[Settings] Settings saved successfully', {
|
||||
autoSync: this.settings.autoSync,
|
||||
autoSyncDelay: this.settings.autoSyncDelay,
|
||||
autoSyncSuccessNotice: this.settings.autoSyncSuccessNotice,
|
||||
NotionLinkDisplay: this.settings.NotionLinkDisplay,
|
||||
autoSyncFrontmatterKey: this.settings.autoSyncFrontmatterKey,
|
||||
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');
|
||||
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') {
|
||||
console.warn('[Settings] Invalid NotionLinkDisplay value, resetting to default');
|
||||
this.settings.NotionLinkDisplay = DEFAULT_SETTINGS.NotionLinkDisplay;
|
||||
@@ -415,11 +425,11 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
|
||||
try {
|
||||
// Trigger appropriate upload command based on database format
|
||||
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') {
|
||||
await uploadCommandGeneral(this, this.settings, dbDetails, this.app);
|
||||
await uploadCommandGeneral(this, this.settings, dbDetails, this.app, { isAutoSync: true });
|
||||
} 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) {
|
||||
console.error(`[AutoSync] Error syncing to ${dbDetails.fullName}:`, error);
|
||||
|
||||
@@ -17,6 +17,7 @@ export interface PluginSettings {
|
||||
autoCopyNotionLink: boolean;
|
||||
autoSync: boolean;
|
||||
autoSyncDelay: number;
|
||||
autoSyncSuccessNotice: boolean;
|
||||
autoSyncFrontmatterKey: string;
|
||||
proxy: string;
|
||||
GeneralButton: boolean;
|
||||
@@ -57,6 +58,7 @@ export const DEFAULT_SETTINGS: PluginSettings = {
|
||||
autoCopyNotionLink: true,
|
||||
autoSync: false,
|
||||
autoSyncDelay: 5,
|
||||
autoSyncSuccessNotice: false,
|
||||
autoSyncFrontmatterKey: DEFAULT_AUTO_SYNC_DATABASE_KEY,
|
||||
proxy: "",
|
||||
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.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)
|
||||
.setName(i18nConfig.AutoSyncFrontmatterKey)
|
||||
@@ -369,4 +380,3 @@ export class ObsidianSettingTab extends PluginSettingTab {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ import {getNowFileMarkdownContentCustom} from "./common/getMarkdownCustom";
|
||||
|
||||
const SYNC_ERROR_NOTICE_DURATION = 8000;
|
||||
|
||||
interface UploadCommandOptions {
|
||||
isAutoSync?: boolean;
|
||||
}
|
||||
|
||||
function extractErrorMessage(error: unknown): string {
|
||||
if (error instanceof Error && error.message) {
|
||||
return error.message;
|
||||
@@ -17,6 +21,16 @@ function extractErrorMessage(error: unknown): string {
|
||||
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 {
|
||||
const errorMessage = extractErrorMessage(error);
|
||||
console.error(`${prefix} Sync failed`, error);
|
||||
@@ -39,6 +53,7 @@ export async function uploadCommandNext(
|
||||
settings: PluginSettings,
|
||||
dbDetails: DatabaseDetails,
|
||||
app: App,
|
||||
options?: UploadCommandOptions,
|
||||
) {
|
||||
|
||||
const {notionAPI, databaseID} = dbDetails;
|
||||
@@ -118,7 +133,9 @@ export async function uploadCommandNext(
|
||||
|
||||
const {response} = res;
|
||||
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", {
|
||||
filename: basename,
|
||||
@@ -146,6 +163,7 @@ export async function uploadCommandGeneral(
|
||||
settings: PluginSettings,
|
||||
dbDetails: DatabaseDetails,
|
||||
app: App,
|
||||
options?: UploadCommandOptions,
|
||||
) {
|
||||
|
||||
const {notionAPI, databaseID} = dbDetails;
|
||||
@@ -160,7 +178,9 @@ export async function uploadCommandGeneral(
|
||||
|
||||
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}`);
|
||||
|
||||
if (markDownData) {
|
||||
@@ -195,7 +215,9 @@ export async function uploadCommandGeneral(
|
||||
|
||||
const {response} = res;
|
||||
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", {
|
||||
filename: basename,
|
||||
@@ -223,6 +245,7 @@ export async function uploadCommandCustom(
|
||||
settings: PluginSettings,
|
||||
dbDetails: DatabaseDetails,
|
||||
app: App,
|
||||
options?: UploadCommandOptions,
|
||||
) {
|
||||
|
||||
const {notionAPI, databaseID} = dbDetails;
|
||||
@@ -237,7 +260,9 @@ export async function uploadCommandCustom(
|
||||
|
||||
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}`);
|
||||
|
||||
if (markDownData) {
|
||||
@@ -272,7 +297,9 @@ export async function uploadCommandCustom(
|
||||
const {response} = res;
|
||||
|
||||
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", {
|
||||
filename: basename,
|
||||
|
||||
Reference in New Issue
Block a user