update at 2025-10-08 17:06:31
This commit is contained in:
41
src/main.ts
41
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 './mp-preview';
|
||||
import { NotePreview, VIEW_TYPE_NOTE_PREVIEW } from './note-preview';
|
||||
import { NMPSettings } from './settings';
|
||||
import { NoteToMpSettingTab } from './setting-tab';
|
||||
import AssetsManager from './assets';
|
||||
@@ -38,6 +38,7 @@ import { XiaohongshuAPIManager } from './xiaohongshu/api';
|
||||
export default class NoteToMpPlugin extends Plugin {
|
||||
settings: NMPSettings;
|
||||
assetsManager: AssetsManager;
|
||||
ribbonIconEl: HTMLElement | null = null;
|
||||
constructor(app: App, manifest: PluginManifest) {
|
||||
super(app, manifest);
|
||||
AssetsManager.setup(app, manifest);
|
||||
@@ -55,6 +56,12 @@ export default class NoteToMpPlugin extends Plugin {
|
||||
uevent('load');
|
||||
this.app.workspace.onLayoutReady(()=>{
|
||||
this.loadResource();
|
||||
// 布局就绪后清理旧视图并自动打开一个新的标准预览(可选)
|
||||
this.cleanupLegacyViews();
|
||||
// 如果当前没有我们的预览叶子,自动激活一次,改善首次体验
|
||||
if (this.app.workspace.getLeavesOfType(VIEW_TYPE_NOTE_PREVIEW).length === 0) {
|
||||
this.activateView();
|
||||
}
|
||||
})
|
||||
|
||||
this.registerView(
|
||||
@@ -62,10 +69,10 @@ export default class NoteToMpPlugin extends Plugin {
|
||||
(leaf) => new NotePreview(leaf, this)
|
||||
);
|
||||
|
||||
const ribbonIconEl = this.addRibbonIcon('clipboard-paste', '复制到公众号', (evt: MouseEvent) => {
|
||||
this.ribbonIconEl = this.addRibbonIcon('clipboard-paste', '复制到公众号', (evt: MouseEvent) => {
|
||||
this.activateView();
|
||||
});
|
||||
ribbonIconEl.addClass('note-to-mp-plugin-ribbon-class');
|
||||
this.ribbonIconEl.addClass('note-to-mp-plugin-ribbon-class');
|
||||
|
||||
this.addCommand({
|
||||
id: 'note-to-mp-preview',
|
||||
@@ -146,7 +153,35 @@ export default class NoteToMpPlugin extends Plugin {
|
||||
}
|
||||
|
||||
onunload() {
|
||||
console.log('Unloading NoteToMP');
|
||||
// 移除 ribbon icon,避免重载插件时重复创建
|
||||
if (this.ribbonIconEl) {
|
||||
this.ribbonIconEl.remove();
|
||||
this.ribbonIconEl = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理历史失效视图:
|
||||
* 某些用户可能曾使用过旧插件构建(例如 note-mp-preview-manager),升级后残留的标签页会提示“插件不再活动”。
|
||||
* 这里做一次性清理,避免用户手动关标签造成困扰。
|
||||
*/
|
||||
private cleanupLegacyViews() {
|
||||
try {
|
||||
const legacyIds = ['note-mp-preview-manager']; // 可扩展
|
||||
const { workspace } = this.app;
|
||||
// 遍历所有叶子,关闭可能的失效 view(无法直接匹配 id 时,仅检测报错视图类型)
|
||||
workspace.getLeavesOfType(VIEW_TYPE_NOTE_PREVIEW).forEach(l => {
|
||||
// 如果 view 的 plugin 不存在或 manifest id 不匹配我们当前的 id,则关闭
|
||||
const anyView: any = l.view;
|
||||
const vid = (anyView?.plugin?.manifest?.id) || '';
|
||||
if (vid && vid !== this.manifest.id && legacyIds.includes(vid)) {
|
||||
workspace.detachLeavesOfType(VIEW_TYPE_NOTE_PREVIEW);
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
console.warn('[NoteToMp] cleanupLegacyViews 失败', e);
|
||||
}
|
||||
}
|
||||
|
||||
async loadSettings() {
|
||||
|
||||
Reference in New Issue
Block a user