This is a plugin which manages plugins for [Obsidian.md](https://obsidian.md/).
Features:
- Lazy Loading. Create a startup delay for a given plugin.
- Plugin toggling via command and/or hotkey.
Until the plugin has been published to the community tab, please use the below method to install:
- Download the latest `main.js` & `manifest.json` from the latest release.
- Create a folder–with any name–in `.obsidian/plugins` at your vaults location.
- Move the downloaded files into the created folder.
- After reloading the Obsidian plugin list, you will see the plugin disabled. Enable it.
This plugin is written as a literate program. If you’d like to read the source-code, you’ll find its entirety below with relevant descriptions.
using the Obsidian API, we load data saved from previous sessions.
Additionally, for new sessions we must provide a default set of our data in the case the previous state was deleted or never existed.
const DEFAULT_SETTINGS = {
pluginArr: function() {
let array = {}
Object.keys(app.plugins.manifests).forEach(
function(pluginId) {
array[pluginId] = { delay: "0", enabled: pluginStatus(pluginId) }
})
return array
}(),
}
const pluginSettings = Object.assign({}, DEFAULT_SETTINGS, await plugin.loadData());
const { pluginArr } = pluginSettings;
The original purpose of this plugin was to implement an easier variation of TftHacker’s lazy-loading.
Using a saved listed of plugins and their relevant on load delay, this is trivially achieved.
Notably the `app.plugins.enablePlugin` function does not cause the plugin to be enabled upon restart; therefore, I have no need to create any hooks on shutdown. (In order to for a plugin to stay enabled use `app.plugins.enablePluginAndSave`.)
Object.entries(pluginArr).forEach(
function([id, data]) {
if (data.enabled & data.delay > 0) {
setTimeout(
function() {
app.plugins.enablePlugin([id])
}, data.delay)
}
}
);
Based on the list of installed plugins, this code creates an array of `command` objects, which when activated, toggles the relevant plugin. If desired, the user can add a keybinding using the GUI.
The `command` object is specified by the Obsidian API and contains an `id` (save name for reference), `name` (unsafe name for viewing), and a callback function.
<<plugin-status>>
const commands = Object.entries(app.plugins.manifests).map(
function([id, data]) {
return { id: id,
name: `Toggle ${data.name}`,
callback:
<<command-callback>>
}
}
)
commands.forEach(
function(command) {
plugin.addCommand(command);
})
The callback function toggles the plugin between enabled & disabled depending on its current state.
function() {
const desiredState = ! app.plugins.plugins.hasOwnProperty(id);
togglePlugin(id, desiredState, pluginArr[id]);
}
const pluginStatus = function(pluginId) {
return app.plugins.plugins.hasOwnProperty(pluginId);
}
const getPluginData = function(key) {
const arr = app.plugins.manifests;
return Object.keys(arr).map(
function(item) {
return arr[item][key]
}
)
}
const togglePlugin = async function(id, state) {
if (state) {
if (pluginArr[id].delay > 0) {
app.plugins.enablePlugin(id);
} else {
app.plugins.enablePluginAndSave(id);
}
pluginArr[id].enabled = true;
await plugin.saveData(pluginSettings);
} else {
app.plugins.disablePluginAndSave(id);
pluginArr[id].enabled = false;
await plugin.saveData(pluginSettings);
}
}
The settings panel is a list of every installed plugin with a few options. The following loops between each plugin and adds it to the settings panel.
const MySettingTab = new obsidian.PluginSettingTab(app, plugin)
MySettingTab.display = async function() {
const { containerEl: El } = MySettingTab;
El.empty();
Object.entries(app.plugins.manifests).forEach(
function([id, pluginData], index, arr) {
if (! pluginArr[id]) {
pluginArr[id] = { delay: "0", enabled: pluginStatus(id) }
}
const data = pluginArr[id];
const st = new obsidian.Setting(El)
const manifest = app.plugins.manifests[id]
st.setName(manifest.name)
st.setDesc(manifest.description)
st.addToggle(
function(tg) {
tg.setValue(pluginStatus(id))
tg.onChange(
function(value) {
togglePlugin(id, value, pluginArr[id])
})
})
st.addText(
function(tx) {
tx.inputEl.type = "number"
tx.setValue(data.delay)
tx.onChange(async function(delay) {
pluginArr[id]["delay"] = delay
await plugin.saveData(pluginSettings)
if (app.plugins.enabledPlugins.has(id)) {
if (delay > 0) {
app.plugins.disablePluginAndSave(id);
app.plugins.enablePlugin(id);
}
} else if (delay == 0) {
if (pluginStatus(id) == true) {
app.plugins.enablePluginAndSave(id);
}
}
})
})
}
)
}
Now that we’ve created the settings panel we need to register it with the `addSettingTab` API function.
plugin.addSettingTab(MySettingTab);
To utilize the Obsidian API, we must extend the `Plugin` object. This object contains most the methods for interacting with the API.
To do so, it’s normally done with a class using the `extent` keyword to the Plugin class (`class MyPlugin extends Plugin`), but I’ve chosen to use a simple function which returns the a plugin object as classes are annoying to work with.
Furthermore, code put within the `plugin.onload` function will be our entry point.
function constructor(app, manifest) {
const plugin = new obsidian.Plugin(app, manifest)
plugin.onload = async function() {
<<helper-functions>>
<<load-settings>>
<<lazy-load>>
<<toggle-commands>>
<<settings-tab>>
}
return plugin; }
'use strict';
var obsidian = require('obsidian');
<<entry-point>>
module.exports = constructor;
{
"name": "obsidian-sample-plugin",
"version": "0.1.1",
"description": "",
"main": "main.js",
"scripts": {
"dev": "npx rollup --config rollup.config.js -w",
"build": "npx rollup --config rollup.config.js --environment BUILD:production",
"version": "node version-bump.mjs && git add manifest.json versions.json"
},
"keywords": [],
"author": "ohm-en",
"license": "MIT",
"devDependencies": {
"@types/node": "^16.11.6",
"builtin-modules": "^3.2.0",
"eslint": "^8.25.0",
"eslint-config-google": "^0.14.0",
"obsidian": "^0.12.17",
}
}
{
"id": "obsidian-plugin-manager",
"name": "Obsidian Plugin Manager",
"version": "0.1.1",
"minAppVersion": "0.13.14",
"description": "Better plugin management.",
"author": "ohm-en",
"authorUrl": "https://github.com/ohm-en",
"isDesktopOnly": false
}
import { readFileSync, writeFileSync } from "fs";
const targetVersion = process.env.npm_package_version;
// read minAppVersion from manifest.json and bump version to target version
let manifest = JSON.parse(readFileSync("manifest.json", "utf8"));
const { minAppVersion } = manifest;
manifest.version = targetVersion;
writeFileSync("manifest.json", JSON.stringify(manifest, null, "\t"));
// update versions.json with target version and minAppVersion from manifest.json
let versions = JSON.parse(readFileSync("versions.json", "utf8"));
versions[targetVersion] = minAppVersion;
writeFileSync("versions.json", JSON.stringify(versions, null, "\t"));
A huge thanks to @TfTHacker for creating the original implementation of lazy loading as found here.
MIT License
Copyright (c) 2022 ohm-en
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.