add function: sync personal movie/book/music info

This commit is contained in:
wanxp 2022-11-20 02:16:41 +08:00
parent 01a066f393
commit 80873784fc
11 changed files with 112 additions and 29 deletions

@ -97,6 +97,14 @@ export const DoubanSubjectStateRecords_NOTE_SYNC: { [key :string]: string } = {
[ALL]: i18nHelper.getMessage('500004'),
}
// @ts-ignore
export const DoubanSubjectStateRecords_MUSIC_SYNC: { [key in DoubanSubjectState]: string } = {
// @ts-ignore
[ALL]: i18nHelper.getMessage('500004'),
[DoubanSubjectState.wish]: i18nHelper.getMessage('500402'),
[DoubanSubjectState.do]: i18nHelper.getMessage('500403'),
[DoubanSubjectState.collect]: i18nHelper.getMessage('500404'),
}

@ -3,6 +3,7 @@ import {
ButtonComponent,
DropdownComponent,
Modal, SearchComponent, Setting,
TextComponent,
ToggleComponent
} from "obsidian";
@ -13,13 +14,15 @@ import {SyncType, SyncTypeRecords} from "../../constant/Constsant";
import {
ALL,
DoubanSubjectStateRecords_BOOK_SYNC, DoubanSubjectStateRecords_BROADCAST_SYNC,
DoubanSubjectStateRecords_MOVIE_SYNC, DoubanSubjectStateRecords_NOTE_SYNC
DoubanSubjectStateRecords_MOVIE_SYNC, DoubanSubjectStateRecords_MUSIC_SYNC, DoubanSubjectStateRecords_NOTE_SYNC
} from "../../constant/DoubanUserState";
import {SyncConfig} from "../sync/model/SyncConfig";
import {clearInterval} from "timers";
import {statSync} from "fs";
import {CreateTemplateSelectParams} from "../setting/model/CreateTemplateSelectParams";
import {FolderSuggest} from "../setting/model/FolderSuggest";
import SettingsManager from "../setting/SettingsManager";
import {DEFAULT_SETTINGS} from "../../constant/DefaultSettings";
export class DoubanSyncModal extends Modal {
plugin: DoubanPlugin;
@ -55,7 +58,6 @@ export class DoubanSyncModal extends Modal {
const sliderDiv = contentEl.createEl('div');
sliderDiv.addClass('obsidian_douban_sync_slider');
this.showProgress(sliderDiv);
const controls = contentEl.createDiv("controls");
const syncButton = new ButtonComponent(controls)
@ -72,16 +74,22 @@ export class DoubanSyncModal extends Modal {
});
cancelButton.setClass("obsidian_douban_search_button");
syncButton.setClass("obsidian_douban_search_button");
this.showProgress(sliderDiv, syncButton);
this.timer = setInterval(() => {
this.showProgress(sliderDiv);
this.showProgress(sliderDiv,syncButton);
}, 1000);
}
private showProgress(sliderDiv: HTMLDivElement) {
private showProgress(sliderDiv: HTMLDivElement, button:ButtonComponent) {
const {syncStatus} = this.plugin.statusHolder;
if (!syncStatus) {
if (!this.plugin.statusHolder.syncStarted) {
sliderDiv.innerHTML = `<p>
<label for="file">${i18nHelper.getMessage('110033')}</label>
<progress class="obsidian_douban_sync_slider" max="${syncStatus.getTotal()}" value="${syncStatus.getHandle()}"> </progress> <span> ${syncStatus.getHandle()}/${syncStatus.getTotal()}:${i18nHelper.getMessage('110036')} </span>
</p>`
button.setDisabled(true);
return;
}
sliderDiv.innerHTML = `<p>
@ -94,8 +102,11 @@ export class DoubanSyncModal extends Modal {
clearInterval(this.timer)
}
contentEl.createEl("h3", {text: i18nHelper.getMessage('500001')});
let syncConfig:SyncConfig = {syncType: SyncType.movie, scope: ALL, force: false, outputFolder: this.plugin.settings.dataFilePath};
const {settings} = this.plugin;
let syncConfig:SyncConfig = {syncType: SyncType.movie, scope: ALL,
force: false,
outputFolder: (settings.dataFilePath == '' || settings.dataFilePath == null) ? DEFAULT_SETTINGS.dataFilePath : settings.dataFilePath,
dataFileNamePath: (settings.dataFileNamePath == '' || settings.dataFileNamePath == null) ? DEFAULT_SETTINGS.dataFileNamePath : settings.dataFileNamePath};
this.showConfigPan(contentEl, syncConfig, false);
const controls = contentEl.createDiv("controls");
@ -157,6 +168,16 @@ export class DoubanSyncModal extends Modal {
folderLabel.addClass('obsidian_douban_sync_config_text');
this.createFolderSetting(folderSelections, config, disable);
const fileName = contentEl.createDiv('fileName-item');
let fileNameLabel = fileName.createEl('label');
fileNameLabel.setText(i18nHelper.getMessage('110035'));
fileNameLabel.addClass('obsidian_douban_settings_text');
fileNameLabel.addClass('obsidian_douban_sync_config_text');
fileNameLabel.addClass('obsidian_douban_sync_config');
this.constructOutiFleName(fileName, 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');
@ -188,6 +209,9 @@ export class DoubanSyncModal extends Modal {
case SyncType.note:
this.showScopeDropdown(contentEl, DoubanSubjectStateRecords_NOTE_SYNC, config, disable);
break;
case SyncType.music:
this.showScopeDropdown(contentEl, DoubanSubjectStateRecords_MUSIC_SYNC, config, disable);
break;
}
}
@ -204,14 +228,16 @@ export class DoubanSyncModal extends Modal {
}
private createFolderSetting(contentEl:HTMLDivElement, config: SyncConfig, disable:boolean) {
let outputFolder = this.plugin.settings.dataFilePath;
const {settings} = this.plugin;
const placeHolder = (settings.dataFilePath == '' || settings.dataFilePath == null) ? DEFAULT_SETTINGS.dataFilePath : settings.dataFilePath;
let outputFolder = placeHolder;
if (config.outputFolder) {
outputFolder = config.outputFolder;
}
const search = new SearchComponent(contentEl);
const search = new TextComponent(contentEl);
new FolderSuggest(this.plugin.app, search.inputEl);
search.setValue(outputFolder)
.setPlaceholder(i18nHelper.getMessage('121503'))
.setPlaceholder(placeHolder)
.onChange(async (value:string) => {
config.outputFolder = value;
})
@ -220,4 +246,22 @@ export class DoubanSyncModal extends Modal {
}
}
private constructOutiFleName(containerEl: HTMLElement, config: SyncConfig, disable:boolean) {
const {settings} = this.plugin;
const placeHolder =(settings.dataFileNamePath == '' || settings.dataFileNamePath == null) ? DEFAULT_SETTINGS.dataFileNamePath : settings.dataFileNamePath;
let dataFileNamePath = placeHolder;
if (config.dataFileNamePath) {
dataFileNamePath = config.dataFileNamePath;
}
const textComponent = new TextComponent(containerEl);
textComponent.setPlaceholder(placeHolder)
.setValue(dataFileNamePath)
.onChange(async (value) => {
config.dataFileNamePath = value;
});
if (disable) {
textComponent.setDisabled(true);
}
}
}

@ -68,6 +68,9 @@ export default abstract class DoubanAbstractLoadHandler<T extends DoubanSubject>
}
private getFileName(context: HandleContext): string {
if (context.dataFileNamePath) {
return context.dataFileNamePath;
}
const {dataFileNamePath} = context.settings;
return dataFileNamePath ? dataFileNamePath : DEFAULT_SETTINGS.dataFileNamePath;
}

@ -16,5 +16,6 @@ export default interface HandleContext {
showAfterCreate?:boolean;
syncStatusHolder?:SyncStatusHolder;
action:string;
outputFolder:string;
outputFolder?:string;
dataFileNamePath?:string;
}

@ -7,27 +7,32 @@ import {DoubanSubjectState} from "../../constant/DoubanUserState";
export default class GlobalStatusHolder {
public syncStatus:GlobalSyncStatusHolder;
public syncStarted:boolean;
public syncStartTime:number;
public completeSync() {
this.syncStatus = null;
this.syncStarted = false;
}
public startSync(syncConfigOut: SyncConfig):boolean {
if (this.syncStatus) {
if (this.syncStarted) {
const {syncConfig} = this.syncStatus;
// @ts-ignore
new Notice(i18nHelper.getMessage('110008'), SyncTypeRecords[syncConfig.syncType], DoubanSubjectState[syncConfig.scope]);
return false;
}
this.syncStatus = new GlobalSyncStatusHolder(syncConfigOut);
this.syncStarted = true;
this.syncStartTime = new Date().getTime();
return true;
}
public stopSync() {
this.syncStatus = null;
this.syncStarted = false;
}
public syncing() {
return this.syncStatus;
return this.syncStarted;
}
}

@ -5,13 +5,11 @@ export default class GlobalSyncStatusHolder {
public syncConfig: SyncConfig;
private total:number;
private handle:number;
private startTime:number;
constructor(syncConfig: SyncConfig) {
this.syncConfig = syncConfig;
this.startTime = new Date().getTime();
this.total = 0;
this.handle = 0;
this.total = 100;
this.handle = 1;
}
handled(num:number) {

@ -3,4 +3,5 @@ export interface SyncConfig {
scope: string,
force:boolean,
outputFolder:string;
dataFileNamePath:string;
}

@ -20,6 +20,8 @@ export default {
'110032': `Scope:`,
'110033': `Progress:`,
'110034': `OutputFolder:`,
'110035': `FileName: (Tip:Support Variables And Path)`,
'110036': `Complete`,
@ -183,7 +185,7 @@ export default {
'140207': `[OB-Douban]: [{0}/{1}] {2}`,
'140208': `[OB-Douban]: [{0}/{1}] {2}`,
'140301': `Douban: Syncing...`,
'140301': `Douban: Syncing[{0}]...`,
'140303': `Douban: User Info Expire, Please login again`,
'140302': `Douban: Sync complete`,

@ -20,6 +20,8 @@ export default {
'110032': `范围:`,
'110033': `进度:`,
'110034': `输出文件夹:`,
'110035': `文档名: (提示:支持参数化以及多级路径, 可用参数见配置界面)`,
'110036': `完成`,
'110201': `{0} 文件已经存在.`,
@ -189,7 +191,7 @@ export default {
'140208': `[OB-Douban]: [{0}/{1}] {2}`,
'140301': `Douban: 开始同步...`,
'140301': `Douban: 开始同步[{0}]...`,
'140302': `Douban: 同步完成`,
'140303': `Douban: 用户信息已过期,请至插件中重新登录`,

@ -8,7 +8,7 @@ import DoubanSubject from "./douban/data/model/DoubanSubject";
import Searcher from "./douban/data/search/Search";
import {i18nHelper} from './lang/helper';
import {log} from "src/org/wanxp/utils/Logutil";
import {Action, BasicConst, SearchHandleMode} from "./constant/Constsant";
import {Action, BasicConst, SearchHandleMode, SyncTypeRecords} from "./constant/Constsant";
import FileHandler from "./file/FileHandler";
import HandleContext from "./douban/data/model/HandleContext";
import HandleResult from "./douban/data/model/HandleResult";
@ -83,24 +83,28 @@ export default class DoubanPlugin extends Plugin {
async createFile(context: HandleContext, result: HandleResult) {
let filePath = this.settings.dataFilePath;
if (context.outputFolder) {
filePath = context.outputFolder;
}
filePath = filePath?filePath:DEFAULT_SETTINGS.dataFilePath;
filePath = FileUtil.join(filePath, result.fileName);
const {syncStatusHolder} = context;
const {subject} = result;
const {content} = result;
if (Action.Sync == context.action) {
if (context.syncStatusHolder.syncConfig.force) {
const exists:boolean = await this.fileHandler.createOrReplaceNewNoteWithData(filePath, result.content, context.showAfterCreate);
const exists:boolean = await this.fileHandler.createOrReplaceNewNoteWithData(filePath, content, context.showAfterCreate);
if (exists) {
syncStatusHolder != null ? syncStatusHolder.replace(subject.id, subject.title):null;
}else {
syncStatusHolder != null ?syncStatusHolder.create(subject.id, subject.title):null;
}
}else {
await this.fileHandler.createNewNoteWithData(filePath, result.content, context.showAfterCreate);
await this.fileHandler.createNewNoteWithData(filePath, content, context.showAfterCreate);
syncStatusHolder != null ?syncStatusHolder.create(subject.id, subject.title):null;
}
}else {
await this.fileHandler.createNewNoteWithData(filePath, result.content, context.showAfterCreate);
await this.fileHandler.createNewNoteWithData(filePath, content, context.showAfterCreate);
syncStatusHolder != null ?syncStatusHolder.create(subject.id, subject.title):null;
}
}
@ -243,15 +247,23 @@ export default class DoubanPlugin extends Plugin {
if (!result) {
return;
}
if (syncConfig.dataFileNamePath) {
context.dataFileNamePath = syncConfig.dataFileNamePath;
}
if (syncConfig.outputFolder) {
context.outputFolder = syncConfig.outputFolder;
}
context.syncStatusHolder = new SyncStatusHolder(syncConfig, this.statusHolder);
new Notice(i18nHelper.getMessage('140301'));
this.showStatus(i18nHelper.getMessage('140203', syncConfig.syncType));
// @ts-ignore
new Notice(i18nHelper.getMessage('140301', SyncTypeRecords[syncConfig.syncType]));
// @ts-ignore
this.showStatus(i18nHelper.getMessage('140203', SyncTypeRecords[syncConfig.syncType]));
const syncHandler = new SyncHandler(this.app, this, syncConfig, context);
await syncHandler.sync();
new Notice(i18nHelper.getMessage('140302'));
} catch (e) {
log.error(i18nHelper.getMessage('140206').replace('{0}', e.message), e);
log.error(i18nHelper.getMessage('140206', e.message), e);
} finally {
context.plugin.statusHolder.completeSync();
this.clearStatusBarDelay();

@ -50,6 +50,13 @@
.obsidian_douban_sync_config{
display: inline-block;
margin-left: 5px;
margin-bottom: 10px;
width: 90%;
}
.obsidian_douban_sync_selections{
width: 40px;
}