1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-20 00:34:50 +00:00
TiddlyWiki5/core/modules/startup/plugins.js
Jeremy Ruston 1c23059204
Dynamic loading/unloading of plugins (#4259)
* First pass at dynamic loading/unloading

* Show warning for changes to plugins containing JS modules

* Use $:/config/RegisterPluginType/* for configuring whether a plugin type is automatically registered

Where "registered" means "the constituent shadows are loaded".

* Fix the info plugin

The previous mechanism re-read all plugin info during startup

* Don't prettify JSON in the plugin library

* Indicate in plugin library whether a plugin requires reloading

* Display the highlighted plugin name in the plugin chooser

And if there's no name field fall back to the part of the title after the final slash.
2019-09-16 12:15:39 +01:00

60 lines
1.9 KiB
JavaScript

/*\
title: $:/core/modules/startup/plugins.js
type: application/javascript
module-type: startup
Startup logic concerned with managing plugins
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
// Export name and synchronous status
exports.name = "plugins";
exports.after = ["load-modules"];
exports.synchronous = true;
var TITLE_REQUIRE_RELOAD_DUE_TO_PLUGIN_CHANGE = "$:/status/RequireReloadDueToPluginChange";
var PREFIX_CONFIG_REGISTER_PLUGIN_TYPE = "$:/config/RegisterPluginType/";
exports.startup = function() {
$tw.wiki.addTiddler({title: TITLE_REQUIRE_RELOAD_DUE_TO_PLUGIN_CHANGE,text: "no"});
$tw.wiki.addEventListener("change",function(changes) {
var changesToProcess = [],
requireReloadDueToPluginChange = false;
$tw.utils.each(Object.keys(changes),function(title) {
var tiddler = $tw.wiki.getTiddler(title),
containsModules = $tw.wiki.doesPluginContainModules(title);
if(containsModules) {
requireReloadDueToPluginChange = true;
} else if(tiddler) {
var pluginType = tiddler.fields["plugin-type"];
if($tw.wiki.getTiddlerText(PREFIX_CONFIG_REGISTER_PLUGIN_TYPE + (tiddler.fields["plugin-type"] || ""),"no") === "yes") {
changesToProcess.push(title);
}
}
});
if(requireReloadDueToPluginChange) {
$tw.wiki.addTiddler({title: TITLE_REQUIRE_RELOAD_DUE_TO_PLUGIN_CHANGE,text: "yes"});
}
// Read or delete the plugin info of the changed tiddlers
if(changesToProcess.length > 0) {
var changes = $tw.wiki.readPluginInfo(changesToProcess);
if(changes.modifiedPlugins.length > 0 || changes.deletedPlugins.length > 0) {
// (Re-)register any modified plugins
$tw.wiki.registerPluginTiddlers(null,changes.modifiedPlugins);
// Unregister any deleted plugins
$tw.wiki.unregisterPluginTiddlers(null,changes.deletedPlugins);
// Unpack the shadow tiddlers
$tw.wiki.unpackPluginTiddlers();
}
}
});
};
})();