forked from chrisgrieser/obsidian-divide-and-conquer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
494 lines (439 loc) · 14.1 KB
/
main.ts
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import { DACSettingsTab, DEFAULT_SETTINGS } from "settings";
import { Notice, Plugin, PluginManifest } from "obsidian";
// add type safety for the undocumented methods
declare module "obsidian" {
interface App {
plugins: {
plugins: string[];
manifests: PluginManifest[];
enabledPlugins: Set<string>;
disablePluginAndSave: (id: string) => Promise<boolean>;
enablePluginAndSave: (id: string) => Promise<boolean>;
};
commands: {
executeCommandById: (commandID: string) => void;
};
customCss: {
enabledSnippets: Set<string>;
snippets: string[];
setCssEnabledStatus(snippet: string, enable: boolean): void;
};
}
}
export default class divideAndConquer extends Plugin {
settings: typeof DEFAULT_SETTINGS;
disabledState: Set<string>[];
manifests = this.app.plugins.manifests;
steps = 1;
async onunload() {
this.saveData();
console.log("Divide & Conquer Plugin unloaded.");
}
async onload() {
this.loadData();
console.log("Divide & Conquer Plugin loaded.");
let maybeReload = () => {
// if (this.settings.reloadAfterPluginChanges)
// // TODO: timeout isn't the best way to do this
// setTimeout(
// () => this.app.commands.executeCommandById("app:reload"),
// 2000
// ); // eslint-disable-line no-magic-numbers
};
const notice = () => {
if (this.steps === 1) new Notice("DAC: Now in the original state.");
if (this.steps === 0) new Notice("DAC: All Plugins Enabled");
};
// compose takes any number of functions, binds them to "this", and returns a function that calls them in order
let compose =
(...funcs: Function[]) =>
(...args: any[]) =>
funcs.reduce(
(promise, func) => promise.then(func.bind(this)),
Promise.resolve()
);
const composed = (func: () => any) => async () =>
compose(func, maybeReload, notice).bind(this)();
await this.loadData();
this.addSettingTab(new DACSettingsTab(this.app, this));
this.addCommand({
id: "count-enabled-and-disabled",
name: "Count enabled and disabled plugins",
callback: () => this.divideConquer("count"),
});
this.addCommand({
id: "disable-all",
name: "Disable all plugins",
callback: () => this.divideConquer("disable", "all"),
});
this.addCommand({
id: "enable-all",
name: "Enable all plugins",
callback: () => this.divideConquer("enable", "all"),
});
this.addCommand({
id: "toggle-all",
name: "Toggle all plugins (Disable enabled plugins & enable disabled ones)",
callback: () => this.divideConquer("toggle", "all"),
});
this.addCommand({
id: "bisect",
name: "Bisect - Disable half of the active plugins, or return to the original state if all plugins are active",
callback: composed(this.bisect),
});
this.addCommand({
id: "un-bisect",
name: "Un-Bisect - Undo the last bisection, or enable all plugins if in the original state",
callback: composed(this.unBisect),
});
this.addCommand({
id: "re-bisect",
name: "Re-Bisect - Undo the last bisection, then disable the other half",
callback: composed(this.reBisect),
});
this.addCommand({
id: "reset",
name: "Reset - forget the original state and set the current state as the new original state",
callback: composed(this.reset),
});
this.addCommand({
id: "restore",
name: "Restore - return to the original state",
callback: composed(this.restore),
});
this.addCommand({
id: "count-enabled-and-disabled-snippets",
name: "Count enabled and disabled snippets",
callback: () => this.divideConquerSnippets("count"),
});
this.addCommand({
id: "disable-all-snippets",
name: "Disable all snippets",
callback: () => this.divideConquerSnippets("disable", "all"),
});
this.addCommand({
id: "enable-all-snippets",
name: "Enable all snippets",
callback: () => this.divideConquerSnippets("enable", "all"),
});
this.addCommand({
id: "toggle-all-snippets",
name: "Toggle all plugins (Disable enabled snippets & enable disabled ones)",
callback: () => this.divideConquerSnippets("toggle", "all"),
});
this.addCommand({
id: "disable-half-snippets",
name: "Disable half of enabled snippets",
callback: () => this.divideConquerSnippets("disable", "half"),
});
this.addCommand({
id: "enable-half-snippets",
name: "Enable half of disabled snippets",
callback: () => this.divideConquerSnippets("enable", "half"),
});
}
async divideConquer(mode: string, scope?: string) {
console.log("Mode: " + mode + ", Scope: " + scope);
const reloadDelay = 2000;
const pluginsToIgnore = ["hot-reload", "obsidian-divide-and-conquer"];
const tplugins = this.app.plugins;
let noticeText;
const allPlugins = Object.keys(tplugins.manifests).filter(
(id) =>
!this.settings.filterRegexes.some((filter) =>
id.match(new RegExp(filter, "i"))
)
);
const enabledPlugins = Object.keys(tplugins.plugins).filter(
(id) =>
!this.settings.filterRegexes.some((filter) =>
id.match(new RegExp(filter, "i"))
)
);
const disabledPlugins = allPlugins.filter(
(id) => !enabledPlugins.includes(id)
);
if (mode === "count") {
noticeText =
"Total: " +
allPlugins.length +
"\nDisabled: " +
disabledPlugins.length +
"\nEnabled: " +
enabledPlugins.length;
}
if (scope === "all") {
if (mode === "enable") {
for (const id of disabledPlugins)
await tplugins.enablePluginAndSave(id);
} else if (mode === "disable") {
for (const id of enabledPlugins)
await tplugins.disablePluginAndSave(id);
} else if (mode === "toggle") {
for (const id of enabledPlugins)
await tplugins.disablePluginAndSave(id);
for (const id of disabledPlugins)
await tplugins.enablePluginAndSave(id);
}
noticeText =
mode.charAt(0).toUpperCase() +
mode.slice(1, -1) +
"ing all " +
allPlugins.length.toString() +
" plugins";
}
if (scope === "half") {
if (mode === "enable") {
const disabled = disabledPlugins.length;
const half = Math.ceil(disabled / 2);
const halfOfDisabled = disabledPlugins.slice(0, half);
for (const id of halfOfDisabled)
await tplugins.enablePluginAndSave(id);
noticeText =
"Enabling " +
half.toString() +
" out of " +
disabled.toString() +
" disabled plugins.";
} else if (mode === "disable") {
const enabled = enabledPlugins.length;
const half = Math.ceil(enabled / 2);
const halfOfEnabled = enabledPlugins.slice(0, half);
for (const id of halfOfEnabled)
await tplugins.disablePluginAndSave(id);
noticeText =
"Disabling " +
half.toString() +
" out of " +
enabled.toString() +
" enabled plugins.";
}
}
// Notify & reload
const reloadAfterwards = mode === "toggle" || mode === "disable";
if (reloadAfterwards) noticeText += "\n\nReloading Obsidian...";
new Notice(noticeText);
// if (reloadAfterwards) {
// setTimeout(() => {
// this.app.commands.executeCommandById("app:reload");
// }, reloadDelay);
// }
}
public async loadData() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await super.loadData()
);
this.steps = this.settings.steps;
this.disabledState = this.settings.disabledState
? JSON.parse(this.settings.disabledState).map(
(set: string[]) => new Set(set)
)
: undefined;
}
public async saveData(restore: boolean = true) {
if (this.disabledState?.length > 0)
this.settings.disabledState = JSON.stringify(
this.disabledState.map((set) => [...set])
);
else this.settings.disabledState = undefined;
this.settings.steps = this.steps;
if (restore) await this.restore();
await super.saveData(this.settings);
}
async bisect() {
if (++this.steps === 1) {
this.restore();
return;
}
const { enabled } = this.getCurrentEDPs();
const half = await this.disablePlugins(
enabled.slice(0, Math.floor(enabled.length / 2))
);
if (half.length > 0) this.disabledState.push(new Set(half));
this.saveData(false);
return half;
}
public async unBisect() {
this.steps = this.steps > 0 ? this.steps - 1 : 0;
const { disabled } = this.getCurrentEDPs();
await this.enablePlugins(disabled);
// this allows unbisect to turn on all plugins without losing the original state
if (this.disabledState.length > 1) {
const poppedDisabledState = this.disabledState.pop();
this.saveData(false);
return poppedDisabledState;
}
this.saveData(false);
return new Set();
}
public async reBisect() {
if (this.steps < 2) {
new Notice("Cannot re-bisect the original state.");
return;
}
const reenabled = await this.unBisect();
const { enabled } = this.getCurrentEDPs();
const toDisable = enabled.filter((id) => !reenabled.has(id));
await this.disablePlugins(toDisable);
if (toDisable.length > 0) this.disabledState.push(new Set(toDisable));
this.saveData(false);
}
public reset() {
this.disabledState = this.settings.disabledState = undefined;
this.steps = 1;
this.saveData(false);
// we don't need to set the original state here because it is lazily created
}
public async restore() {
if (this.disabledState === null) return;
for (let i = this.disabledState.length - 1; i >= 1; i--)
this.enablePlugins(this.disabledState[i]);
await this.disablePlugins(this.disabledState[0]);
this.reset();
this.saveData(false);
}
// EDPs = Enabled/Disabled Plugins
public getCurrentEDPs() {
const { enabled, disabled } = this.getVaultEDPsFrom(
this.getIncludedPlugins() as Set<string>
);
this.disabledState ??= [new Set(disabled)];
const currentDisabled = this.disabledState.last();
return { enabled, disabled: currentDisabled };
}
// EDPs = Enabled/Disabled Plugins
public getVaultEDPsFrom(from?: Set<string>) {
from ??= new Set(Object.keys(this.manifests));
// sort by display name rather than id
let included = Object.entries<PluginManifest>(this.manifests)
.filter(([key]) => from.has(key))
.sort((a, b) => b[1].name.localeCompare(a[1].name))
.map(([key, manifest]) => key);
return {
enabled: included.filter((id) =>
this.app.plugins.enabledPlugins.has(id)
),
disabled: included.filter(
(id) => !this.app.plugins.enabledPlugins.has(id)
),
};
}
public getIncludedPlugins(getPluginIds: boolean = true) {
const plugins = (
Object.values(this.manifests) as unknown as PluginManifest[]
).filter(
(p) =>
!this.settings.filterRegexes.some(
(filter) =>
p.id.match(new RegExp(filter, "i")) ||
(this.settings.filterUsingDisplayName &&
p.name.match(new RegExp(filter, "i"))) ||
(this.settings.filterUsingAuthor &&
p.author.match(new RegExp(filter, "i"))) ||
(this.settings.filterUsingDescription &&
p.description.match(new RegExp(filter, "i")))
)
);
return getPluginIds
? new Set(plugins.map((p) => p.id))
: new Set(plugins);
}
// enables in the reverse order that they were disabled (probably not necessary, but it's nice to be consistent)
async enablePlugins(plugins: string[] | Set<string>) {
if (plugins instanceof Set) plugins = [...plugins];
plugins
.reverse()
.forEach((id) => this.app.plugins.enablePluginAndSave(id));
return plugins;
}
async disablePlugins(plugins: string[] | Set<string>) {
if (plugins instanceof Set) plugins = [...plugins];
for (const id of plugins)
await this.app.plugins.disablePluginAndSave(id);
return plugins;
}
async divideConquerSnippets(mode: string, scope?: string) {
console.log("Mode: " + mode + ", Scope: " + scope);
const reloadDelay = 2000;
let noticeText;
/** Enabled can include snippets that were removed without disabling. */
/** This array is the list of currently loaded snippets. */
const allSnippets = this.app.customCss.snippets;
const enabledSnippets = allSnippets.filter((snippet) =>
this.app.customCss.enabledSnippets.has(snippet)
);
const disabledSnippets = allSnippets.filter(
(snippet) => !this.app.customCss.enabledSnippets.has(snippet)
);
if (mode === "count") {
noticeText =
"Total: " +
allSnippets.length +
"\nDisabled: " +
disabledSnippets.length +
"\nEnabled: " +
enabledSnippets.length;
}
if (scope === "all") {
if (mode === "enable") {
for (const snippet of disabledSnippets)
await this.app.customCss.setCssEnabledStatus(snippet, true);
} else if (mode === "disable") {
for (const snippet of enabledSnippets)
await this.app.customCss.setCssEnabledStatus(
snippet,
false
);
} else if (mode === "toggle") {
for (const snippet of enabledSnippets)
await this.app.customCss.setCssEnabledStatus(
snippet,
false
);
for (const snippet of disabledSnippets)
await this.app.customCss.setCssEnabledStatus(snippet, true);
}
noticeText =
mode.charAt(0).toUpperCase() +
mode.slice(1, -1) +
"ing all " +
allSnippets.length.toString() +
" snippets";
}
if (scope === "half") {
if (mode === "enable") {
const disabled = disabledSnippets.length;
const half = Math.ceil(disabled / 2);
const halfOfDisabled = disabledSnippets.slice(0, half);
for (const snippet of halfOfDisabled)
await this.app.customCss.setCssEnabledStatus(snippet, true);
noticeText =
"Enabling " +
half.toString() +
" out of " +
disabled.toString() +
" disabled snippets.";
} else if (mode === "disable") {
const enabled = enabledSnippets.length;
const half = Math.ceil(enabled / 2);
const halfOfEnabled = enabledSnippets.slice(0, half);
for (const snippet of halfOfEnabled)
await this.app.customCss.setCssEnabledStatus(
snippet,
false
);
noticeText =
"Disabling " +
half.toString() +
" out of " +
enabled.toString() +
" enabled snippets.";
}
}
// Notify
new Notice(noticeText);
// no need to reload for snippets
}
}