mirror of
https://github.com/yeban8090/mp-preview.git
synced 2026-07-22 05:46:27 +00:00
插件视图创建优化
This commit is contained in:
parent
ed41d1d150
commit
a76e1f6104
1 changed files with 59 additions and 54 deletions
113
src/main.ts
113
src/main.ts
|
|
@ -6,72 +6,77 @@ import { MPConverter } from './converter';
|
|||
import { DonateManager } from './donateManager';
|
||||
import { MPSettingTab } from './settings/MPSettingTab';
|
||||
export default class MPPlugin extends Plugin {
|
||||
settingsManager: SettingsManager;
|
||||
templateManager: TemplateManager;
|
||||
async onload() {
|
||||
// 初始化设置管理器
|
||||
this.settingsManager = new SettingsManager(this);
|
||||
await this.settingsManager.loadSettings();
|
||||
settingsManager: SettingsManager;
|
||||
templateManager: TemplateManager;
|
||||
async onload() {
|
||||
// 初始化设置管理器
|
||||
this.settingsManager = new SettingsManager(this);
|
||||
await this.settingsManager.loadSettings();
|
||||
|
||||
// 初始化模板管理器
|
||||
this.templateManager = new TemplateManager(this.app, this.settingsManager);
|
||||
// 初始化模板管理器
|
||||
this.templateManager = new TemplateManager(this.app, this.settingsManager);
|
||||
|
||||
// 初始化转换器
|
||||
MPConverter.initialize(this.app);
|
||||
// 初始化转换器
|
||||
MPConverter.initialize(this.app);
|
||||
|
||||
DonateManager.initialize(this.app, this);
|
||||
DonateManager.initialize(this.app, this);
|
||||
|
||||
// 注册视图
|
||||
this.registerView(
|
||||
VIEW_TYPE_MP,
|
||||
(leaf) => new MPView(leaf, this.templateManager, this.settingsManager)
|
||||
);
|
||||
// 注册视图
|
||||
this.registerView(
|
||||
VIEW_TYPE_MP,
|
||||
(leaf) => new MPView(leaf, this.templateManager, this.settingsManager)
|
||||
);
|
||||
|
||||
// 自动打开视图但不聚焦
|
||||
this.app.workspace.onLayoutReady(() => {
|
||||
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_MP);
|
||||
if (leaves.length === 0) {
|
||||
const rightLeaf = this.app.workspace.getRightLeaf(false);
|
||||
if (rightLeaf) {
|
||||
rightLeaf.setViewState({
|
||||
type: VIEW_TYPE_MP,
|
||||
active: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
// 自动打开视图但不聚焦
|
||||
// this.app.workspace.onLayoutReady(() => {
|
||||
// const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_MP);
|
||||
// if (leaves.length === 0) {
|
||||
// const rightLeaf = this.app.workspace.getRightLeaf(false);
|
||||
// if (rightLeaf) {
|
||||
// rightLeaf.setViewState({
|
||||
// type: VIEW_TYPE_MP,
|
||||
// active: false,
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
|
||||
// 添加命令到命令面板
|
||||
this.addCommand({
|
||||
// 添加一个功能按钮用于打开所有面板
|
||||
this.addRibbonIcon("eye", "打开公众号预览", () => {
|
||||
this.activateView();
|
||||
});
|
||||
|
||||
// 添加命令到命令面板
|
||||
this.addCommand({
|
||||
id: 'open-mp-preview',
|
||||
name: '打开公众号预览插件',
|
||||
callback: async () => {
|
||||
await this.activateView();
|
||||
callback: async () => {
|
||||
await this.activateView();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 在插件的 onload 方法中添加:
|
||||
this.addSettingTab(new MPSettingTab(this.app, this));
|
||||
// 在插件的 onload 方法中添加:
|
||||
this.addSettingTab(new MPSettingTab(this.app, this));
|
||||
}
|
||||
|
||||
async activateView() {
|
||||
// 如果视图已经存在,激活它
|
||||
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_MP);
|
||||
if (leaves.length > 0) {
|
||||
this.app.workspace.revealLeaf(leaves[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
async activateView() {
|
||||
// 如果视图已经存在,激活它
|
||||
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_MP);
|
||||
if (leaves.length > 0) {
|
||||
this.app.workspace.revealLeaf(leaves[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
// 创建新视图
|
||||
const rightLeaf = this.app.workspace.getRightLeaf(false);
|
||||
if (rightLeaf) {
|
||||
await rightLeaf.setViewState({
|
||||
type: VIEW_TYPE_MP,
|
||||
active: true,
|
||||
});
|
||||
} else {
|
||||
// 如果无法获取右侧面板,显示错误提示
|
||||
// 创建新视图
|
||||
const rightLeaf = this.app.workspace.getRightLeaf(false);
|
||||
if (rightLeaf) {
|
||||
await rightLeaf.setViewState({
|
||||
type: VIEW_TYPE_MP,
|
||||
active: true,
|
||||
});
|
||||
} else {
|
||||
// 如果无法获取右侧面板,显示错误提示
|
||||
new Notice('无法创建视图面板');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue