-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
294 lines (252 loc) · 9.86 KB
/
main.js
File metadata and controls
294 lines (252 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// == main.js ==
const { Plugin, PluginSettingTab, Setting, SuggestModal, Notice } = require('obsidian');
class PluginToggleSuggestModal extends SuggestModal {
constructor(app, items) {
super(app);
this.items = items;
this.resolve = null;
this.numberMap = new Map();
}
getSuggestions(query) {
return this.items.filter(it =>
it.display.toLowerCase().includes(query.toLowerCase()));
}
renderSuggestion(item, el) {
const index = this.items.indexOf(item);
const label = index < 9 ? `【${index + 1}】` : (index === 9 ? `【0】` : ` `);
el.setText(`${label} ${item.display}`);
this.numberMap.set((index + 1).toString(), item.id);
if (index === 9) this.numberMap.set('0', item.id);
}
onChooseSuggestion(item) {
this.resolve(item.id);
}
openAsync() {
return new Promise(res => {
this.resolve = res;
this.open();
});
}
onOpen() {
super.onOpen();
for (let i = 0; i <= 9; i++) {
const key = i.toString();
this.scope.register([], key, () => this.selectByNumber(key));
}
}
selectByNumber(key) {
const id = this.numberMap.get(key);
if (id) {
this.close();
this.resolve(id);
}
}
}
module.exports = class QuickTogglePlugin extends Plugin {
async onload() {
this.settings = Object.assign({ pluginIds: [] }, await this.loadData());
this.addCommand({
id: 'quick-toggle-plugin',
name: 'Quick Toggle Community Plugins',
callback: () => this.openMenu()
});
this.addSettingTab(new QuickToggleSettingTab(this.app, this));
}
async saveSettings() {
await this.saveData(this.settings);
}
async openMenu() {
let ids = this.settings.pluginIds;
if (!ids || ids.length === 0) {
new Notice('⚠️ 尚未在设置中选择要切换的插件');
return;
}
// 🔍 修复:过滤掉已经删除或无效的插件 ID
const manifests = this.app.plugins.manifests;
ids = ids.filter(id => manifests[id]);
if (ids.length === 0) {
new Notice('⚠️ 所有已选插件均不存在或已被删除');
this.settings.pluginIds = []; // 清理脏数据
await this.saveSettings();
return;
}
const cfgPath = '.obsidian/community-plugins.json';
const enabledList = JSON.parse(await this.app.vault.adapter.read(cfgPath));
const items = ids.map(id => {
const manifest = manifests[id];
const enabled = enabledList.includes(id);
const name = manifest?.name || id;
return { id, display: `${enabled ? '✅' : '⛔'} ${name} (${id})` };
});
const modal = new PluginToggleSuggestModal(this.app, items);
const chosenId = await modal.openAsync();
if (chosenId) await this.togglePlugin(chosenId);
}
async togglePlugin(pluginId) {
const cfgPath = '.obsidian/community-plugins.json';
let enabledList = JSON.parse(await this.app.vault.adapter.read(cfgPath));
const isEnabled = enabledList.includes(pluginId);
const name = this.app.plugins.manifests[pluginId]?.name || pluginId;
try {
if (isEnabled) {
await this.app.plugins.disablePlugin(pluginId);
enabledList = enabledList.filter(p => p !== pluginId);
new Notice(`🔴 已禁用插件:${name}`);
} else {
await this.app.plugins.enablePlugin(pluginId);
enabledList.push(pluginId);
new Notice(`💡 已启用插件:${name}`);
}
await this.app.vault.adapter.write(cfgPath, JSON.stringify(enabledList, null, 2));
} catch (err) {
new Notice('⚠️ 切换失败: ' + err.message);
}
}
};
class QuickToggleSettingTab extends PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
this.filterQuery = '';
this.sortByList = null;
this.sortByStatus = null;
}
async display() {
const { containerEl } = this;
containerEl.empty();
this.statContainer = containerEl.createDiv();
this.searchInput = containerEl.createEl('input', {
type: 'text',
placeholder: '🔍 搜索插件名称',
});
this.searchInput.style.cssText = `
margin: 1em 0;
width: 100%;
padding: 6px 10px;
border: 1px solid var(--interactive-accent);
border-radius: 6px;
font-size: 14px;
outline: none;
`;
this.searchInput.addEventListener('input', () => {
this.filterQuery = this.searchInput.value.trim().toLowerCase();
this.renderPluginList();
});
containerEl.createEl('h2', {
text: '快速开关社区插件设置',
attr: { style: 'margin-bottom: 0.2em; color: var(--interactive-accent);' }
});
containerEl.createEl('ul', {
attr: { style: 'margin-bottom: 1em; padding-left: 1.2em; color: var(--text-muted); font-size: 0.9em;' }
}).innerHTML = `
<li>在“加入列表”列点选将插件加入快速开关列表;</li>
<li>“插件状态”为插件当前状态,点击可“启用/禁用”插件;</li>
<li>点击“加入列表”和“插件状态”可快速分类插件。</li>
`;
this.listContainer = containerEl.createDiv();
await this.refreshData();
this.updateStats();
this.renderPluginList();
}
async refreshData() {
const enabledListRaw = await this.app.vault.adapter.read('.obsidian/community-plugins.json');
this.enabledList = JSON.parse(enabledListRaw);
}
updateStats() {
const allPluginIds = Object.keys(this.app.plugins.manifests);
const totalPlugins = allPluginIds.length;
const selectedCount = this.plugin.settings.pluginIds.filter(id => allPluginIds.includes(id)).length;
const disabledCount = allPluginIds.filter(id => !this.enabledList.includes(id)).length;
this.statContainer.empty();
this.statContainer.createEl('div', {
text: `插件安装总数: ${totalPlugins} | 禁用插件数量: ${disabledCount} | 加入快捷开关列表的插件数量: ${selectedCount}`,
cls: 'setting-item-description',
attr: { style: 'margin-bottom: 1em; font-weight: bold; color: var(--text-muted);' }
});
}
renderPluginList() {
this.listContainer.empty();
const header = this.listContainer.createDiv();
header.style.cssText = `
display: flex; justify-content: space-between;
font-weight: bold; padding: 6px 12px;
border-bottom: 2px solid var(--background-modifier-border);
`;
const getArrow = (sortState) => sortState === null ? '' : (sortState ? ' ▲' : ' ▼');
const nameHeader = header.createDiv({ text: '插件名称', attr: { style: 'flex:3;' } });
const toggleHeader = header.createDiv({ text: '加入列表' + getArrow(this.sortByList), attr: { style: 'flex:1;text-align:center;cursor:pointer;' } });
const statusHeader = header.createDiv({ text: '插件状态' + getArrow(this.sortByStatus), attr: { style: 'flex:1;text-align:center;cursor:pointer;' } });
toggleHeader.onclick = () => {
this.sortByList = this.sortByList === null ? true : !this.sortByList;
this.sortByStatus = null;
this.renderPluginList();
};
statusHeader.onclick = () => {
this.sortByStatus = this.sortByStatus === null ? true : !this.sortByStatus;
this.sortByList = null;
this.renderPluginList();
};
let all = Object.entries(this.app.plugins.manifests)
.filter(([id]) => id !== this.plugin.manifest.id)
.filter(([id, manifest]) => (manifest.name || id).toLowerCase().includes(this.filterQuery));
if (this.sortByList !== null) {
all.sort(([idA], [idB]) => {
const inListA = this.plugin.settings.pluginIds.includes(idA);
const inListB = this.plugin.settings.pluginIds.includes(idB);
return this.sortByList ? inListA - inListB : inListB - inListA;
});
} else if (this.sortByStatus !== null) {
all.sort(([idA], [idB]) => {
const enA = this.enabledList.includes(idA);
const enB = this.enabledList.includes(idB);
return this.sortByStatus ? enA - enB : enB - enA;
});
} else {
all.sort(([, a], [, b]) => a.name.localeCompare(b.name));
}
all.forEach(([id, manifest]) => {
const row = this.listContainer.createDiv({ cls: 'plugin-row' });
row.style.cssText = `
display: flex; align-items: center;
padding: 6px 12px;
border-bottom: 1px solid var(--background-modifier-border);
transition: background 0.2s ease;
`;
row.onmouseover = () => row.style.background = 'var(--background-modifier-hover)';
row.onmouseout = () => row.style.background = '';
row.createDiv({ text: `${manifest.name} (${manifest.version})`, attr: { style: 'flex:3;' } });
const toggleDiv = row.createDiv({ attr: { style: 'flex:1; text-align:center;' } });
new Setting(toggleDiv)
.addToggle(tg => {
tg.setValue(this.plugin.settings.pluginIds.includes(id))
.onChange(async val => {
const list = this.plugin.settings.pluginIds;
if (val && !list.includes(id)) list.push(id);
else if (!val) this.plugin.settings.pluginIds = list.filter(x => x !== id);
await this.plugin.saveSettings();
this.updateStats();
});
}).settingEl.querySelector('.setting-item-name')?.remove();
const statusDiv = row.createDiv({ attr: { style: 'flex:1; text-align:center;' } });
const iconBtn = statusDiv.createEl('button', {
text: this.enabledList.includes(id) ? '✅' : '⛔',
attr: {
style: `
cursor: pointer;
padding: 2px 6px;
font-size: 14px;
border: 1px solid var(--interactive-accent);
background: var(--background-modifier-form);
border-radius: 5px;
`
}
});
iconBtn.onclick = async () => {
await this.plugin.togglePlugin(id);
await this.refreshData();
iconBtn.setText(this.enabledList.includes(id) ? '✅' : '⛔');
this.updateStats();
};
});
}
}