optimize sync config panel

This commit is contained in:
wanxp 2022-11-20 12:57:01 +08:00
parent eebe141ef0
commit 1adaecfaeb
8 changed files with 162 additions and 120 deletions

@ -1,10 +1,7 @@
import { import {
App, App,
ButtonComponent, ButtonComponent,
DropdownComponent,
Modal, SearchComponent, Setting, Modal, SearchComponent, Setting,
TextComponent,
ToggleComponent
} from "obsidian"; } from "obsidian";
import DoubanPlugin from "../../main"; import DoubanPlugin from "../../main";
@ -18,10 +15,7 @@ import {
} from "../../constant/DoubanUserState"; } from "../../constant/DoubanUserState";
import {SyncConfig} from "../sync/model/SyncConfig"; import {SyncConfig} from "../sync/model/SyncConfig";
import {clearInterval} from "timers"; import {clearInterval} from "timers";
import {statSync} from "fs";
import {CreateTemplateSelectParams} from "../setting/model/CreateTemplateSelectParams";
import {FolderSuggest} from "../setting/model/FolderSuggest"; import {FolderSuggest} from "../setting/model/FolderSuggest";
import SettingsManager from "../setting/SettingsManager";
import {DEFAULT_SETTINGS} from "../../constant/DefaultSettings"; import {DEFAULT_SETTINGS} from "../../constant/DefaultSettings";
export class DoubanSyncModal extends Modal { export class DoubanSyncModal extends Modal {
@ -105,8 +99,11 @@ export class DoubanSyncModal extends Modal {
const {settings} = this.plugin; const {settings} = this.plugin;
let syncConfig:SyncConfig = {syncType: SyncType.movie, scope: ALL, let syncConfig:SyncConfig = {syncType: SyncType.movie, scope: ALL,
force: false, force: false,
outputFolder: (settings.dataFilePath == '' || settings.dataFilePath == null) ? DEFAULT_SETTINGS.dataFilePath : settings.dataFilePath, dataFilePath: (settings.dataFilePath == '' || settings.dataFilePath == null) ? DEFAULT_SETTINGS.dataFilePath : settings.dataFilePath,
dataFileNamePath: (settings.dataFileNamePath == '' || settings.dataFileNamePath == null) ? DEFAULT_SETTINGS.dataFileNamePath : settings.dataFileNamePath}; dataFileNamePath: (settings.dataFileNamePath == '' || settings.dataFileNamePath == null) ? DEFAULT_SETTINGS.dataFileNamePath : settings.dataFileNamePath,
cacheImage: ( settings.cacheImage == null) ? DEFAULT_SETTINGS.cacheImage : settings.cacheImage,
attachmentPath: (settings.attachmentPath == '' || settings.attachmentPath == null) ? DEFAULT_SETTINGS.attachmentPath : settings.attachmentPath,
};
this.showConfigPan(contentEl, syncConfig, false); this.showConfigPan(contentEl, syncConfig, false);
const controls = contentEl.createDiv("controls"); const controls = contentEl.createDiv("controls");
@ -120,8 +117,9 @@ export class DoubanSyncModal extends Modal {
if(!this.plugin.statusHolder.startSync(syncConfig)) { if(!this.plugin.statusHolder.startSync(syncConfig)) {
return; return;
} }
this.updateContextByConfig(syncConfig);
this.show(contentEl); this.show(contentEl);
await this.plugin.sync(syncConfig, this.context); await this.plugin.sync(this.context);
}) })
const cancelButton = new ButtonComponent(controls) const cancelButton = new ButtonComponent(controls)
@ -133,57 +131,22 @@ export class DoubanSyncModal extends Modal {
syncButton.setClass("obsidian_douban_search_button"); syncButton.setClass("obsidian_douban_search_button");
} }
private updateContextByConfig(syncConfig: SyncConfig) {
const { context} = this;
context.syncConfig = syncConfig;
}
private showConfigPan(contentEl: HTMLElement, config:SyncConfig, disable:boolean) { private showConfigPan(contentEl: HTMLElement, config:SyncConfig, disable:boolean) {
const typeSelections = contentEl.createDiv('type-selection'); const typeSelections = contentEl.createDiv('type-selection');
const typeSelectionLabel = typeSelections.createEl('label');
typeSelectionLabel.setText(i18nHelper.getMessage('110030'))
typeSelectionLabel.addClass('obsidian_douban_settings_text')
const syncTypeDropdown = new DropdownComponent(typeSelections);
const scopeSelections = contentEl.createDiv("scope-selection");
syncTypeDropdown.addOptions(SyncTypeRecords)
.setValue(config.syncType)
.onChange((value) => {
config.syncType = value;
this.openScopeDropdown(scopeSelections, config, disable);
});
this.openScopeDropdown(scopeSelections, config, disable);
const forceSelections = contentEl.createDiv('force-selection');
let forceLabel = forceSelections.createEl('label');
forceLabel.setText(i18nHelper.getMessage('110031'));
forceLabel.addClass('obsidian_douban_settings_text');
forceLabel.addClass('obsidian_douban_sync_config_text');
const toggle:ToggleComponent = new ToggleComponent(forceSelections)
.setTooltip(i18nHelper.getMessage('500110'))
.setValue(config.force)
.onChange((value) => {
config.force = value;
});
if (disable) {
syncTypeDropdown.setDisabled(true);
toggle.setDisabled(true);
}
const folderSelections = contentEl.createDiv('folder-selection'); const folderSelections = contentEl.createDiv('folder-selection');
let folderLabel = folderSelections.createEl('label');
folderLabel.setText(i18nHelper.getMessage('110034'));
folderLabel.addClass('obsidian_douban_settings_text');
folderLabel.addClass('obsidian_douban_sync_config_text');
this.createFolderSetting(folderSelections, config, disable);
const fileName = contentEl.createDiv('fileName-item'); const fileName = contentEl.createDiv('fileName-item');
let fileNameLabel = fileName.createEl('label'); const attachments = contentEl.createDiv('attachments-item');
fileNameLabel.setText(i18nHelper.getMessage('110035')); const forceSelections = contentEl.createDiv('force-selection');
fileNameLabel.addClass('obsidian_douban_settings_text'); this.showTypeDropdown(typeSelections, config, disable);
fileNameLabel.addClass('obsidian_douban_sync_config_text'); this.showOutputFolderSelections(folderSelections, config, disable);
fileNameLabel.addClass('obsidian_douban_sync_config'); this.showOutiFleName(fileName, config, disable);
this.constructOutiFleName(fileName, config, disable); this.showAttachmentsFileConfig(attachments, config, disable);
this.showForceUpdateConfig(forceSelections, config, disable);
fileName.addClass('obsidian_douban_sync_config');
folderSelections.addClass('obsidian_douban_sync_config');
typeSelections.addClass('obsidian_douban_sync_config');
scopeSelections.addClass('obsidian_douban_sync_config');
forceSelections.addClass('obsidian_douban_sync_config');
} }
async onClose() { async onClose() {
@ -196,9 +159,6 @@ export class DoubanSyncModal extends Modal {
private openScopeDropdown(contentEl:HTMLDivElement, config: SyncConfig, disable:boolean) { private openScopeDropdown(contentEl:HTMLDivElement, config: SyncConfig, disable:boolean) {
contentEl.empty(); contentEl.empty();
let scopeLabel = contentEl.createEl('label');
scopeLabel.addClass('obsidian_douban_settings_text');
scopeLabel.setText(i18nHelper.getMessage('110032'));
switch (config.syncType) { switch (config.syncType) {
case SyncType.movie: case SyncType.movie:
this.showScopeDropdown(contentEl, DoubanSubjectStateRecords_MOVIE_SYNC, config, disable); this.showScopeDropdown(contentEl, DoubanSubjectStateRecords_MOVIE_SYNC, config, disable);
@ -218,53 +178,135 @@ export class DoubanSyncModal extends Modal {
} }
} }
private showScopeDropdown(contentEl:HTMLDivElement, scopeSelections: Record<string, string>, config: SyncConfig, disable:boolean) { private showTypeDropdown(containerEl:HTMLDivElement, config: SyncConfig, disable:boolean) {
const syncScopeTypeDropdown = new DropdownComponent(contentEl) containerEl.empty();
.addOptions(scopeSelections) const settings = new Setting(containerEl);
.setValue(config.scope) settings
.setName(i18nHelper.getMessage('110030'))
.addDropdown((dropdown) => {
dropdown.addOptions(SyncTypeRecords)
.setValue(config.syncType)
.onChange((value) => { .onChange((value) => {
config.syncType = value;
this.showTypeDropdown(containerEl, config, disable);
});
}).setDisabled(disable);
const scopeSelections = containerEl.createDiv("scope-selection");
this.openScopeDropdown(scopeSelections, config, disable);
}
private showScopeDropdown(containerEl:HTMLDivElement, scopeSelections: Record<string, string>, config: SyncConfig, disable:boolean) {
containerEl.empty();
new Setting(containerEl)
.setName(i18nHelper.getMessage('110032'))
.addDropdown((dropdown) => {
dropdown.addOptions(scopeSelections)
dropdown.setValue(config.scope)
.onChange(async (value: string) => {
config.scope = value; config.scope = value;
}); });
if (disable) { }).setDisabled(disable);
syncScopeTypeDropdown.setDisabled(true);
}
} }
private createFolderSetting(contentEl:HTMLDivElement, config: SyncConfig, disable:boolean) { private showOutiFleName(containerEl: HTMLElement, config: SyncConfig, disable:boolean) {
const {settings} = this.plugin; containerEl.empty();
const placeHolder = (settings.dataFilePath == '' || settings.dataFilePath == null) ? DEFAULT_SETTINGS.dataFilePath : settings.dataFilePath;
let outputFolder = placeHolder;
if (config.outputFolder) {
outputFolder = config.outputFolder;
}
const search = new TextComponent(contentEl);
new FolderSuggest(this.plugin.app, search.inputEl);
search.setValue(outputFolder)
.setPlaceholder(placeHolder)
.onChange(async (value:string) => {
config.outputFolder = value;
})
if (disable) {
search.setDisabled(true);
}
}
private constructOutiFleName(containerEl: HTMLElement, config: SyncConfig, disable:boolean) {
const {settings} = this.plugin; const {settings} = this.plugin;
const placeHolder =(settings.dataFileNamePath == '' || settings.dataFileNamePath == null) ? DEFAULT_SETTINGS.dataFileNamePath : settings.dataFileNamePath; const placeHolder =(settings.dataFileNamePath == '' || settings.dataFileNamePath == null) ? DEFAULT_SETTINGS.dataFileNamePath : settings.dataFileNamePath;
let dataFileNamePath = placeHolder; containerEl.empty();
if (config.dataFileNamePath) { const dataFilePathSetting = new Setting(containerEl)
dataFileNamePath = config.dataFileNamePath; .setName(i18nHelper.getMessage('121601'))
} .setDesc(i18nHelper.getMessage('121602'))
const textComponent = new TextComponent(containerEl); .addText((textField) => {
textComponent.setPlaceholder(placeHolder) textField.setPlaceholder(placeHolder)
.setValue(dataFileNamePath) .setValue(config.dataFileNamePath)
.onChange(async (value) => { .onChange(async (value) => {
config.dataFileNamePath = value; config.dataFileNamePath = value
}); });
if (disable) { })
textComponent.setDisabled(true); .setDisabled(disable);
dataFilePathSetting.addExtraButton((button) => {
button
.setIcon('reset')
.setTooltip(i18nHelper.getMessage('121902'))
.onClick(async () => {
config.dataFileNamePath = placeHolder
this.showOutiFleName(containerEl, config, disable);
});
})
} }
showOutputFolderSelections(containerEl: HTMLElement, config: SyncConfig, disable:boolean) {
containerEl.empty();
const placeHolder:string = this.plugin.settings.dataFilePath ? this.plugin.settings.dataFilePath : DEFAULT_SETTINGS.dataFilePath;
new Setting(containerEl)
.setName( i18nHelper.getMessage('121501'))
.setDesc( i18nHelper.getMessage('121502'))
.addSearch(async (search: SearchComponent) => {
new FolderSuggest(this.app, search.inputEl);
// @ts-ignore
search.setValue(config.dataFilePath)
// @ts-ignore
.setPlaceholder(placeHolder)
.onChange(async (value: string) => {
config.dataFilePath = value;
});
})
.setDisabled(disable);
}
showForceUpdateConfig(containerEl: HTMLElement, config: SyncConfig, disable:boolean) {
containerEl.empty();
new Setting(containerEl)
.setName(i18nHelper.getMessage('110031'))
.setDesc(i18nHelper.getMessage('500110'))
.addToggle((toggleComponent) => {
toggleComponent
// .setTooltip(i18nHelper.getMessage('121403'))
.setValue(config.force)
.onChange(async (value) => {
config.force = value;
});
})
.setDisabled(disable);
}
showAttachmentsFileConfig(containerEl: HTMLElement, config: SyncConfig, disable:boolean) {
containerEl.empty();
new Setting(containerEl)
.setName(i18nHelper.getMessage('121430'))
.setDesc(i18nHelper.getMessage('121431'))
.addToggle((toggleComponent) => {
toggleComponent
// .setTooltip(i18nHelper.getMessage('121403'))
.setValue(config.cacheImage)
.onChange(async (value) => {
config.cacheImage = value;
this.showAttachmentsFileConfig(containerEl, config, disable);
});
})
.setDisabled(disable);
if(config.cacheImage) {
this.showAttachmentPathSelections(containerEl, config, disable);
}
}
showAttachmentPathSelections(containerEl: HTMLElement, config: SyncConfig, disable:boolean) {
const placeHolder:string = this.plugin.settings.attachmentPath ? this.plugin.settings.attachmentPath : DEFAULT_SETTINGS.attachmentPath;
new Setting(containerEl)
.setName( i18nHelper.getMessage('121432'))
.setDesc( i18nHelper.getMessage('121433'))
.addSearch(async (search: SearchComponent) => {
new FolderSuggest(this.plugin.app, search.inputEl);
// @ts-ignore
search.setValue(config.attachmentPath)
// @ts-ignore
.setPlaceholder(placeHolder)
.onChange(async (value: string) => {
config.attachmentPath = value;
});
})
.setDisabled(disable);
} }
} }

@ -68,8 +68,9 @@ export default abstract class DoubanAbstractLoadHandler<T extends DoubanSubject>
} }
private getFileName(context: HandleContext): string { private getFileName(context: HandleContext): string {
if (context.dataFileNamePath) { const {syncConfig} = context;
return context.dataFileNamePath; if (syncConfig) {
return syncConfig.dataFileNamePath;
} }
const {dataFileNamePath} = context.settings; const {dataFileNamePath} = context.settings;
return dataFileNamePath ? dataFileNamePath : DEFAULT_SETTINGS.dataFileNamePath; return dataFileNamePath ? dataFileNamePath : DEFAULT_SETTINGS.dataFileNamePath;
@ -406,12 +407,13 @@ export default abstract class DoubanAbstractLoadHandler<T extends DoubanSubject>
} }
private async saveImage(extract: T, context: HandleContext) { private async saveImage(extract: T, context: HandleContext) {
if (!extract.image || !context.settings.cacheImage) { const {syncConfig} = context;
if (!extract.image || (syncConfig && !syncConfig.cacheImage) || !context.settings.cacheImage) {
return; return;
} }
let image = extract.image; let image = extract.image;
const filename = image.split('/').pop(); const filename = image.split('/').pop();
let folder = context.settings.attachmentPath; let folder = syncConfig? syncConfig.attachmentPath : context.settings.attachmentPath;
if (!folder) { if (!folder) {
folder = DEFAULT_SETTINGS.attachmentPath; folder = DEFAULT_SETTINGS.attachmentPath;
} }

@ -5,6 +5,7 @@ import UserComponent from "../../user/UserComponent";
import NetFileHandler from "src/org/wanxp/net/NetFileHandler"; import NetFileHandler from "src/org/wanxp/net/NetFileHandler";
import DoubanPlugin from "../../../main"; import DoubanPlugin from "../../../main";
import SyncStatusHolder from "../../sync/model/SyncStatusHolder"; import SyncStatusHolder from "../../sync/model/SyncStatusHolder";
import {SyncConfig} from "../../sync/model/SyncConfig";
export default interface HandleContext { export default interface HandleContext {
plugin:DoubanPlugin; plugin:DoubanPlugin;
@ -16,6 +17,5 @@ export default interface HandleContext {
showAfterCreate?:boolean; showAfterCreate?:boolean;
syncStatusHolder?:SyncStatusHolder; syncStatusHolder?:SyncStatusHolder;
action:string; action:string;
outputFolder?:string; syncConfig?: SyncConfig;
dataFileNamePath?:string;
} }

@ -15,7 +15,7 @@ export function constructOutUI(containerEl: HTMLElement, manager: SettingsManage
new Setting(containerEl).then(createFolderSelectionSetting({name: '121501', desc: '121502', placeholder: '121503', key: 'dataFilePath', manager: manager})); new Setting(containerEl).then(createFolderSelectionSetting({name: '121501', desc: '121502', placeholder: '121503', key: 'dataFilePath', manager: manager}));
let outfolder = containerEl.createDiv({ cls: 'settings-item' }); let outfolder = containerEl.createDiv({ cls: 'settings-item' });
constructOutFolderUI(outfolder, manager); constructOutputFileNameUI(outfolder, manager);
new Setting(containerEl) new Setting(containerEl)
.setName(i18nHelper.getMessage('120601')) .setName(i18nHelper.getMessage('120601'))
@ -58,7 +58,7 @@ export function constructOutUI(containerEl: HTMLElement, manager: SettingsManage
} }
export function constructOutFolderUI(containerEl: HTMLElement, manager: SettingsManager) { export function constructOutputFileNameUI(containerEl: HTMLElement, manager: SettingsManager) {
containerEl.empty(); containerEl.empty();
const dataFilePathSetting = new Setting(containerEl) const dataFilePathSetting = new Setting(containerEl)
.setName(i18nHelper.getMessage('121601')) .setName(i18nHelper.getMessage('121601'))
@ -78,7 +78,7 @@ export function constructOutFolderUI(containerEl: HTMLElement, manager: Settings
.onClick(async () => { .onClick(async () => {
manager.plugin.settings.dataFileNamePath = DEFAULT_SETTINGS.dataFileNamePath; manager.plugin.settings.dataFileNamePath = DEFAULT_SETTINGS.dataFileNamePath;
await manager.plugin.saveSettings(); await manager.plugin.saveSettings();
constructOutFolderUI(containerEl, manager) constructOutputFileNameUI(containerEl, manager)
}); });
}) })
} }

@ -2,6 +2,8 @@ export interface SyncConfig {
syncType: string, syncType: string,
scope: string, scope: string,
force:boolean, force:boolean,
outputFolder:string; dataFilePath:string;
dataFileNamePath:string; dataFileNamePath:string;
cacheImage:boolean;
attachmentPath:string;
} }

@ -233,7 +233,7 @@ export default {
'310107': `内容简介`, '310107': `内容简介`,
'310108': `出版社`, '310108': `出版社`,
'310109': `出版时间`, '310109': `出版时间`,
'310110': `其他`, '310110': ``,
'310111': `author:原作者`, '310111': `author:原作者`,
'310112': `translator:译者`, '310112': `translator:译者`,
'310113': `isbn:isbn`, '310113': `isbn:isbn`,

@ -231,7 +231,7 @@ export default {
'122004': `以下参数使用时请用'{{}}'包裹, 举例: 参数title, 则使用时为{{title}}. `, '122004': `以下参数使用时请用'{{}}'包裹, 举例: 参数title, 则使用时为{{title}}. `,
'122010': `我的状态参数`, '122010': `我的状态参数`,
'410101': `其他`, '410101': ``,
'410102': `未知`, '410102': `未知`,
@ -256,7 +256,7 @@ export default {
'310107': `内容简介`, '310107': `内容简介`,
'310108': `出版社`, '310108': `出版社`,
'310109': `出版时间`, '310109': `出版时间`,
'310110': `(固定值:其他)`, '310110': ``,
'310111': `author:原作者`, '310111': `author:原作者`,
'310112': `translator:译者`, '310112': `translator:译者`,
'310113': `isbn:isbn`, '310113': `isbn:isbn`,

@ -83,8 +83,9 @@ export default class DoubanPlugin extends Plugin {
async createFile(context: HandleContext, result: HandleResult) { async createFile(context: HandleContext, result: HandleResult) {
let filePath = this.settings.dataFilePath; let filePath = this.settings.dataFilePath;
if (context.outputFolder) { const {syncConfig} = context;
filePath = context.outputFolder; if (syncConfig) {
filePath = syncConfig.dataFilePath;
} }
filePath = filePath?filePath:DEFAULT_SETTINGS.dataFilePath; filePath = filePath?filePath:DEFAULT_SETTINGS.dataFilePath;
filePath = FileUtil.join(filePath, result.fileName); filePath = FileUtil.join(filePath, result.fileName);
@ -241,18 +242,13 @@ export default class DoubanPlugin extends Plugin {
setTimeout(() => this.doubanStatusBar.empty(), BasicConst.CLEAN_STATUS_BAR_DELAY) setTimeout(() => this.doubanStatusBar.empty(), BasicConst.CLEAN_STATUS_BAR_DELAY)
} }
async sync(syncConfig: SyncConfig, context: HandleContext) { async sync(context: HandleContext) {
const {syncConfig} = context;
try { try {
const result:boolean = await this.checkLogin(context); const result:boolean = await this.checkLogin(context);
if (!result) { if (!result) {
return; return;
} }
if (syncConfig.dataFileNamePath) {
context.dataFileNamePath = syncConfig.dataFileNamePath;
}
if (syncConfig.outputFolder) {
context.outputFolder = syncConfig.outputFolder;
}
context.syncStatusHolder = new SyncStatusHolder(syncConfig, this.statusHolder); context.syncStatusHolder = new SyncStatusHolder(syncConfig, this.statusHolder);
// @ts-ignore // @ts-ignore
new Notice(i18nHelper.getMessage('140301', SyncTypeRecords[syncConfig.syncType])); new Notice(i18nHelper.getMessage('140301', SyncTypeRecords[syncConfig.syncType]));