mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-23 10:07:19 +00:00
Add support for data modules
Modules can now be JSON or tiddler dictionaries, as well as executable code
This commit is contained in:
parent
fc61b9aca7
commit
3f58ead593
20
core/boot.js
20
core/boot.js
@ -196,6 +196,7 @@ $tw.utils.parseStringArray = function(value) {
|
||||
|
||||
// Parse a block of name:value fields. The `fields` object is used as the basis for the return value
|
||||
$tw.utils.parseFields = function(text,fields) {
|
||||
fields = fields || {};
|
||||
text.split(/\r?\n/mg).forEach(function(line) {
|
||||
var p = line.indexOf(":");
|
||||
if(p !== -1) {
|
||||
@ -540,10 +541,22 @@ Define all modules stored in ordinary tiddlers
|
||||
*/
|
||||
$tw.Wiki.prototype.defineTiddlerModules = function() {
|
||||
$tw.utils.each(this.tiddlers,function(tiddler,title,object) {
|
||||
if(tiddler.fields.type === "application/javascript" && tiddler.hasField("module-type")) {
|
||||
// Define the module
|
||||
if(tiddler.hasField("module-type")) {
|
||||
switch (tiddler.fields.type) {
|
||||
case "application/javascript":
|
||||
// We don't need to register JavaScript tiddlers in the browser
|
||||
if(!$tw.browser) {
|
||||
$tw.modules.define(tiddler.fields.title,tiddler.fields["module-type"],tiddler.fields.text);
|
||||
}
|
||||
break;
|
||||
case "application/json":
|
||||
$tw.modules.define(tiddler.fields.title,tiddler.fields["module-type"],JSON.parse(tiddler.fields.text));
|
||||
break;
|
||||
case "application/x-tiddler-dictionary":
|
||||
$tw.modules.define(tiddler.fields.title,tiddler.fields["module-type"],$tw.utils.parseFields(tiddler.fields.text));
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -1034,9 +1047,8 @@ $tw.boot.startup = function() {
|
||||
// Unpack bundle tiddlers
|
||||
$tw.wiki.unpackBundleTiddlers();
|
||||
// Register typed modules from the tiddlers we've just loaded
|
||||
if(!$tw.browser) {
|
||||
$tw.wiki.defineTiddlerModules();
|
||||
}
|
||||
// And any modules within bundles
|
||||
$tw.wiki.defineBundledModules();
|
||||
// Run any startup modules
|
||||
$tw.modules.forEachModuleOfType("startup",function(title,module) {
|
||||
|
@ -51,6 +51,10 @@ $tw.modules.define = function(moduleName,moduleType,definition) {
|
||||
definition: definition,
|
||||
exports: undefined
|
||||
};
|
||||
// If the definition is already an object we can use it as the exports
|
||||
if(typeof moduleInfo.definition === "object") {
|
||||
moduleInfo.exports = definition;
|
||||
}
|
||||
// Store the module in the titles hashmap
|
||||
if(Object.prototype.hasOwnProperty.call($tw.modules.titles,moduleName)) {
|
||||
console.log("Warning: Redefined module - " + moduleName);
|
||||
|
Loading…
Reference in New Issue
Block a user