update at 2025-10-16 14:03:45

This commit is contained in:
douboer
2025-10-16 14:03:45 +08:00
parent b9feb2f764
commit 93afc99e7d
14 changed files with 78 additions and 72 deletions

View File

@@ -10,7 +10,7 @@
import { Plugin, WorkspaceLeaf, App, PluginManifest, Menu, Notice, TAbstractFile, TFile, TFolder } from 'obsidian';
import { PreviewView, VIEW_TYPE_NOTE_PREVIEW } from './preview-view';
import { NMPSettings } from './settings';
import { NoteToMpSettingTab } from './setting-tab';
import { Note2AnySettingTab } from './setting-tab';
import AssetsManager from './assets';
import { setVersion, uevent } from './utils';
import { WidgetsModal } from './widgets-modal';
@@ -20,7 +20,7 @@ import { XiaohongshuContentAdapter } from './xiaohongshu/adapter';
import { XiaohongshuAPIManager } from './xiaohongshu/api';
/**
* NoteToMpPlugin
* Note2AnyPlugin
*
* 中文说明:
* 这是插件的入口类,负责:
@@ -30,12 +30,12 @@ import { XiaohongshuAPIManager } from './xiaohongshu/api';
* - 提供文件右键菜单扩展,支持对单文件或文件夹进行发布操作
*
* 设计决策(简要):
* - 将批量发布的 UI 放在 `BatchPublishModal` 中,命令 `note-to-mp-batch-publish` 会打开该模态框
* - 将批量发布的 UI 放在 `BatchPublishModal` 中,命令 `note2any-batch-publish` 会打开该模态框
* - 单篇发布/文件夹批量发布仍复用 `NotePreview` 的发布逻辑,避免重复实现上传流程
*/
export default class NoteToMpPlugin extends Plugin {
export default class Note2AnyPlugin extends Plugin {
settings: NMPSettings;
assetsManager: AssetsManager;
ribbonIconEl: HTMLElement | null = null;
@@ -51,35 +51,35 @@ export default class NoteToMpPlugin extends Plugin {
}
async onload() {
console.log('Loading NoteToMP (plugin onload start)');
console.log('Loading Note2Any (plugin onload start)');
setVersion(this.manifest.version);
uevent('load');
console.log('[NoteToMpPlugin] workspace.layoutReady at onload =', this.app.workspace.layoutReady);
console.log('[Note2AnyPlugin] workspace.layoutReady at onload =', this.app.workspace.layoutReady);
// 先注册 view 之前,防止旧 snapshot 立即恢复创建大量视图:先临时卸载残留叶子(如果类型匹配)
try {
const legacyLeaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_NOTE_PREVIEW);
if (legacyLeaves.length > 0) {
console.log('[NoteToMpPlugin] detach legacy leaves early count=', legacyLeaves.length);
console.log('[Note2AnyPlugin] detach legacy leaves early count=', legacyLeaves.length);
this.app.workspace.detachLeavesOfType(VIEW_TYPE_NOTE_PREVIEW);
}
} catch (e) {
console.warn('[NoteToMpPlugin] early detach failed', e);
console.warn('[Note2AnyPlugin] early detach failed', e);
}
this.app.workspace.onLayoutReady(async () => {
console.log('[NoteToMpPlugin] onLayoutReady callback entered');
console.time('[NoteToMpPlugin] startup:onLayoutReady→loadResource');
console.log('[Note2AnyPlugin] onLayoutReady callback entered');
console.time('[Note2AnyPlugin] startup:onLayoutReady→loadResource');
try {
await this.loadResource(); // 确保资源完全加载完再继续,避免后续视图初始化反复等待
} catch (e) {
console.error('[NoteToMpPlugin] loadResource 失败', e);
console.error('[Note2AnyPlugin] loadResource 失败', e);
} finally {
console.timeEnd('[NoteToMpPlugin] startup:onLayoutReady→loadResource');
console.timeEnd('[Note2AnyPlugin] startup:onLayoutReady→loadResource');
}
// 清理旧视图
this.cleanupLegacyViews();
// 取消自动打开预览视图(用于排查启动卡顿)。用户可通过图标或命令手动打开。
// console.log('[NoteToMpPlugin] 已跳过自动打开预览视图调试模式');
// console.log('[Note2AnyPlugin] 已跳过自动打开预览视图调试模式');
});
this.registerView(
@@ -90,28 +90,28 @@ export default class NoteToMpPlugin extends Plugin {
this.ribbonIconEl = this.addRibbonIcon('clipboard-paste', '复制到公众号', (evt: MouseEvent) => {
this.activateView();
});
this.ribbonIconEl.addClass('note-to-mp-plugin-ribbon-class');
this.ribbonIconEl.addClass('note2any-plugin-ribbon-class');
this.addCommand({
id: 'note-to-mp-preview',
id: 'note2any-preview',
name: '复制到公众号',
callback: () => {
this.activateView();
}
});
this.addSettingTab(new NoteToMpSettingTab(this.app, this));
this.addSettingTab(new Note2AnySettingTab(this.app, this));
this.addCommand({
id: 'note-to-mp-widget',
name: '插入样式小部件',
id: 'note2any-widget',
name: '插入样式件',
callback: () => {
new WidgetsModal(this.app).open();
}
});
this.addCommand({
id: 'note-to-mp-batch-publish',
id: 'note2any-batch-publish',
name: '批量发布文章',
callback: () => {
new BatchPublishModal(this.app, this).open();
@@ -120,7 +120,7 @@ export default class NoteToMpPlugin extends Plugin {
// TODO: 重构后需要重新实现批量发布功能
// this.addCommand({
// id: 'note-to-mp-pub',
// id: 'note2any-pub',
// name: '发布公众号文章',
// callback: async () => {
// await this.activateView();
@@ -130,7 +130,7 @@ export default class NoteToMpPlugin extends Plugin {
// 命令:当前文件发布到微信草稿
this.addCommand({
id: 'note-to-mp-post-current',
id: 'note2any-post-current',
name: '发布当前文件到公众号草稿',
callback: async () => {
const file = this.app.workspace.getActiveFile();
@@ -181,7 +181,7 @@ export default class NoteToMpPlugin extends Plugin {
}
onunload() {
console.log('Unloading NoteToMP');
console.log('Unloading Note2Any');
// 移除 ribbon icon避免重载插件时重复创建
if (this.ribbonIconEl) {
this.ribbonIconEl.remove();
@@ -208,7 +208,7 @@ export default class NoteToMpPlugin extends Plugin {
}
});
} catch (e) {
console.warn('[NoteToMp] cleanupLegacyViews 失败', e);
console.warn('[Note2Any] cleanupLegacyViews 失败', e);
}
}