update at 2025-10-08 19:45:28
This commit is contained in:
142
src/main.ts
142
src/main.ts
@@ -8,7 +8,7 @@
|
||||
*/
|
||||
|
||||
import { Plugin, WorkspaceLeaf, App, PluginManifest, Menu, Notice, TAbstractFile, TFile, TFolder } from 'obsidian';
|
||||
import { NotePreview, VIEW_TYPE_NOTE_PREVIEW } from './note-preview';
|
||||
import { PreviewView, VIEW_TYPE_NOTE_PREVIEW } from './preview-view';
|
||||
import { NMPSettings } from './settings';
|
||||
import { NoteToMpSettingTab } from './setting-tab';
|
||||
import AssetsManager from './assets';
|
||||
@@ -51,22 +51,40 @@ export default class NoteToMpPlugin extends Plugin {
|
||||
}
|
||||
|
||||
async onload() {
|
||||
console.log('Loading NoteToMP');
|
||||
console.log('Loading NoteToMP (plugin onload start)');
|
||||
setVersion(this.manifest.version);
|
||||
uevent('load');
|
||||
this.app.workspace.onLayoutReady(()=>{
|
||||
this.loadResource();
|
||||
// 布局就绪后清理旧视图并自动打开一个新的标准预览(可选)
|
||||
this.cleanupLegacyViews();
|
||||
// 如果当前没有我们的预览叶子,自动激活一次,改善首次体验
|
||||
if (this.app.workspace.getLeavesOfType(VIEW_TYPE_NOTE_PREVIEW).length === 0) {
|
||||
this.activateView();
|
||||
console.log('[NoteToMpPlugin] 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);
|
||||
this.app.workspace.detachLeavesOfType(VIEW_TYPE_NOTE_PREVIEW);
|
||||
}
|
||||
})
|
||||
} catch (e) {
|
||||
console.warn('[NoteToMpPlugin] early detach failed', e);
|
||||
}
|
||||
this.app.workspace.onLayoutReady(async () => {
|
||||
console.log('[NoteToMpPlugin] onLayoutReady callback entered');
|
||||
console.time('[NoteToMpPlugin] startup:onLayoutReady→loadResource');
|
||||
try {
|
||||
await this.loadResource(); // 确保资源完全加载完再继续,避免后续视图初始化反复等待
|
||||
} catch (e) {
|
||||
console.error('[NoteToMpPlugin] loadResource 失败', e);
|
||||
} finally {
|
||||
console.timeEnd('[NoteToMpPlugin] startup:onLayoutReady→loadResource');
|
||||
}
|
||||
// 清理旧视图
|
||||
this.cleanupLegacyViews();
|
||||
// 取消自动打开预览视图(用于排查启动卡顿)。用户可通过图标或命令手动打开。
|
||||
// console.log('[NoteToMpPlugin] 已跳过自动打开预览视图调试模式');
|
||||
});
|
||||
|
||||
this.registerView(
|
||||
VIEW_TYPE_NOTE_PREVIEW,
|
||||
(leaf) => new NotePreview(leaf, this)
|
||||
(leaf) => new PreviewView(leaf, this)
|
||||
);
|
||||
|
||||
this.ribbonIconEl = this.addRibbonIcon('clipboard-paste', '复制到公众号', (evt: MouseEvent) => {
|
||||
@@ -100,56 +118,66 @@ export default class NoteToMpPlugin extends Plugin {
|
||||
}
|
||||
});
|
||||
|
||||
// TODO: 重构后需要重新实现批量发布功能
|
||||
// this.addCommand({
|
||||
// id: 'note-to-mp-pub',
|
||||
// name: '发布公众号文章',
|
||||
// callback: async () => {
|
||||
// await this.activateView();
|
||||
// this.getNotePreview()?.postArticle();
|
||||
// }
|
||||
// });
|
||||
|
||||
// 命令:当前文件发布到微信草稿
|
||||
this.addCommand({
|
||||
id: 'note-to-mp-pub',
|
||||
name: '发布公众号文章',
|
||||
id: 'note-to-mp-post-current',
|
||||
name: '发布当前文件到公众号草稿',
|
||||
callback: async () => {
|
||||
const file = this.app.workspace.getActiveFile();
|
||||
if (!file) { new Notice('没有活动文件'); return; }
|
||||
if (file.extension.toLowerCase() !== 'md') { new Notice('只能发布 Markdown 文件'); return; }
|
||||
await this.activateView();
|
||||
this.getNotePreview()?.postArticle();
|
||||
await this.getNotePreview()?.postWechatDraft(file);
|
||||
}
|
||||
});
|
||||
|
||||
// 监听右键菜单
|
||||
this.registerEvent(
|
||||
this.app.workspace.on('file-menu', (menu, file) => {
|
||||
// 发布到微信公众号
|
||||
menu.addItem((item) => {
|
||||
item
|
||||
.setTitle('发布到公众号')
|
||||
.setIcon('lucide-send')
|
||||
.onClick(async () => {
|
||||
if (file instanceof TFile) {
|
||||
if (file.extension.toLowerCase() !== 'md') {
|
||||
new Notice('只能发布 Markdown 文件');
|
||||
return;
|
||||
}
|
||||
await this.activateView();
|
||||
await this.getNotePreview()?.renderMarkdown(file);
|
||||
await this.getNotePreview()?.postArticle();
|
||||
} else if (file instanceof TFolder) {
|
||||
await this.activateView();
|
||||
await this.getNotePreview()?.batchPost(file);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 发布到小红书(新增)
|
||||
menu.addItem((item) => {
|
||||
item
|
||||
.setTitle('发布到小红书')
|
||||
.setIcon('lucide-heart')
|
||||
.onClick(async () => {
|
||||
if (file instanceof TFile) {
|
||||
if (file.extension.toLowerCase() !== 'md') {
|
||||
new Notice('只能发布 Markdown 文件');
|
||||
return;
|
||||
}
|
||||
await this.publishToXiaohongshu(file);
|
||||
}
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
// 监听右键菜单(文件浏览器)
|
||||
this.registerEvent(
|
||||
this.app.workspace.on('file-menu', (menu, file) => {
|
||||
// 发布到公众号草稿
|
||||
menu.addItem((item) => {
|
||||
item
|
||||
.setTitle('发布公众号')
|
||||
.setIcon('lucide-send')
|
||||
.onClick(async () => {
|
||||
if (file instanceof TFile) {
|
||||
if (file.extension.toLowerCase() !== 'md') {
|
||||
new Notice('只能发布 Markdown 文件');
|
||||
return;
|
||||
}
|
||||
await this.activateView();
|
||||
await this.getNotePreview()?.postWechatDraft(file);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 发布到小红书
|
||||
menu.addItem((item) => {
|
||||
item
|
||||
.setTitle('发布到小红书')
|
||||
.setIcon('lucide-heart')
|
||||
.onClick(async () => {
|
||||
if (file instanceof TFile) {
|
||||
if (file.extension.toLowerCase() !== 'md') {
|
||||
new Notice('只能发布 Markdown 文件');
|
||||
return;
|
||||
}
|
||||
await this.publishToXiaohongshu(file);
|
||||
}
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
onunload() {
|
||||
@@ -208,11 +236,11 @@ export default class NoteToMpPlugin extends Plugin {
|
||||
if (leaf) workspace.revealLeaf(leaf);
|
||||
}
|
||||
|
||||
getNotePreview(): NotePreview | null {
|
||||
getNotePreview(): PreviewView | null {
|
||||
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_NOTE_PREVIEW);
|
||||
if (leaves.length > 0) {
|
||||
const leaf = leaves[0];
|
||||
return leaf.view as NotePreview;
|
||||
return leaf.view as PreviewView;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user