1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-24 17:10:29 +00:00

Introduce extensible viewer modules for the view macro

This commit is contained in:
Jeremy Ruston 2012-10-18 18:27:27 +01:00
parent 7d60c8f55e
commit cd04fa22c6
8 changed files with 186 additions and 71 deletions

View File

@ -25,14 +25,11 @@ exports.info = {
exports.executeMacro = function() {
var tiddler = this.wiki.getTiddler(this.tiddlerTitle),
field = this.hasParameter("field") ? this.params.field : "title",
value,
children,
t,
childrenClone = [],
parents = this.parents;
value;
// Get the value to display
if(tiddler) {
value = tiddler.fields[field];
} else {
} else { // Use a special value if the tiddler is missing
switch(field) {
case "text":
value = "";
@ -49,72 +46,16 @@ exports.executeMacro = function() {
break;
}
}
switch(this.params.format) {
case "link":
if(value === undefined) {
return $tw.Tree.Text("");
} else {
var link = $tw.Tree.Macro("link",{
srcParams: {to: value},
content: [$tw.Tree.Text(value)],
isBlock: this.isBlock,
wiki: this.wiki
});
link.execute(parents,this.tiddlerTitle);
return link;
}
break;
case "transclude":
if(tiddler && this.params.field && (this.params.field in tiddler.fields)) {
children = this.wiki.parseTiddler(tiddler.fields[this.params.field]).tree;
for(t=0; t<children.length; t++) {
childrenClone.push(children[t].clone());
}
for(t=0; t<childrenClone.length; t++) {
childrenClone[t].execute(parents,this.tiddlerTitle);
}
return $tw.Tree.Element(this.isBlock ? "div" : "span",{},childrenClone);
}
break;
case "wikified":
if(tiddler && this.params.field === "text") {
if(parents.indexOf(tiddler.fields.title) !== -1) {
children = [$tw.Tree.errorNode("Tiddler recursion error in <<view>> macro")];
} else {
children = this.wiki.parseTiddler(tiddler.fields.title).tree;
}
parents = parents.slice(0);
parents.push(tiddler.fields.title);
} else {
children = this.wiki.parseText("text/x-tiddlywiki",value).tree;
}
for(t=0; t<children.length; t++) {
childrenClone.push(children[t].clone());
}
for(t=0; t<childrenClone.length; t++) {
childrenClone[t].execute(parents,this.tiddlerTitle);
}
return $tw.Tree.Element(this.isBlock ? "div" : "span",{},childrenClone);
case "date":
var template = this.params.template || "DD MMM YYYY";
if(value === undefined) {
return $tw.Tree.Text("");
} else {
return $tw.Tree.Text($tw.utils.formatDateString(value,template));
}
break;
default: // "text"
// Get the stringified version of the field value
if(field !== "text" && tiddler) {
value = tiddler.getFieldString(field);
}
if(value === undefined || value === null) {
return $tw.Tree.Text("");
} else {
return $tw.Tree.Text(value);
}
// Figure out which viewer to use
// TODO: Tiddler field modules should be able to specify a field type from which the viewer is derived
if(tiddler && this.params.format) {
var viewer = this.wiki.macros.view.viewers[this.params.format];
}
return $tw.Tree.Text("");
if(!viewer) {
viewer = this.wiki.macros.view.viewers["text"];
}
// Call the viewer to generate the content
return viewer(tiddler,field,value,this);
};
})();

View File

@ -0,0 +1,26 @@
/*\
title: $:/core/modules/macros/view/viewers/date.js
type: application/javascript
module-type: viewer
A viewer for viewing tiddler fields as a date
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
function renderValue(tiddler,field,value,viewMacro) {
var template = viewMacro.params.template || "DD MMM YYYY";
if(value === undefined) {
return $tw.Tree.Text("");
} else {
return $tw.Tree.Text($tw.utils.formatDateString(value,template));
}
}
exports["date"] = renderValue;
})();

View File

@ -0,0 +1,32 @@
/*\
title: $:/core/modules/macros/view/viewers/link.js
type: application/javascript
module-type: viewer
A viewer for viewing tiddler fields as a link
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
function renderValue(tiddler,field,value,viewMacro) {
if(value === undefined) {
return $tw.Tree.Text("");
} else {
var link = $tw.Tree.Macro("link",{
srcParams: {to: value},
content: [$tw.Tree.Text(value)],
isBlock: viewMacro.isBlock,
wiki: viewMacro.wiki
});
link.execute(viewMacro.parents,viewMacro.tiddlerTitle);
return link;
}
}
exports["link"] = renderValue;
})();

View File

@ -0,0 +1,30 @@
/*\
title: $:/core/modules/macros/view/viewers/text.js
type: application/javascript
module-type: viewer
A viewer for viewing tiddler fields as plain text
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
function renderValue(tiddler,field,value,viewMacro) {
// Get the value as a string
if(field !== "text" && tiddler) {
value = tiddler.getFieldString(field);
}
// Return the text
if(value === undefined || value === null) {
return $tw.Tree.Text("");
} else {
return $tw.Tree.Text(value);
}
}
exports["text"] = renderValue;
})();

View File

@ -0,0 +1,31 @@
/*\
title: $:/core/modules/macros/view/viewers/transclude.js
type: application/javascript
module-type: viewer
A viewer that transcludes the tiddler whose title is specified in the viewed field
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
function renderValue(tiddler,field,value,viewMacro) {
if(tiddler && viewMacro.params.field && (viewMacro.params.field in tiddler.fields)) {
var children = viewMacro.wiki.parseTiddler(tiddler.fields[viewMacro.params.field]).tree,
childrenClone = [];
for(t=0; t<children.length; t++) {
childrenClone.push(children[t].clone());
}
for(t=0; t<childrenClone.length; t++) {
childrenClone[t].execute(parents,viewMacro.tiddlerTitle);
}
return $tw.Tree.Element(viewMacro.isBlock ? "div" : "span",{},childrenClone);
}
}
exports["transclude"] = renderValue;
})();

View File

@ -0,0 +1,42 @@
/*\
title: $:/core/modules/macros/view/viewers/wikified.js
type: application/javascript
module-type: viewer
A viewer for viewing tiddler fields as wikified text
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
function renderValue(tiddler,field,value,viewMacro) {
// Check for recursion
var parents = viewMacro.parents,
children,t,childrenClone = [];
if(tiddler && viewMacro.params.field === "text") {
if(parents.indexOf(tiddler.fields.title) !== -1) {
children = [$tw.Tree.errorNode("Tiddler recursion error in <<view>> macro")];
} else {
children = viewMacro.wiki.parseTiddler(tiddler.fields.title).tree;
}
parents = parents.slice(0);
parents.push(tiddler.fields.title);
} else {
children = viewMacro.wiki.parseText("text/x-tiddlywiki",value).tree;
}
// Clone and execute the parsed wikitext
for(t=0; t<children.length; t++) {
childrenClone.push(children[t].clone());
}
for(t=0; t<childrenClone.length; t++) {
childrenClone[t].execute(parents,viewMacro.tiddlerTitle);
}
return $tw.Tree.Element(viewMacro.isBlock ? "div" : "span",{},childrenClone);
}
exports["wikified"] = renderValue;
})();

View File

@ -33,6 +33,7 @@ exports.startup = function() {
// Set up the wiki store
$tw.wiki.initMacros();
$tw.wiki.initEditors();
$tw.wiki.initViewers();
$tw.wiki.initStoryViews();
$tw.wiki.initParsers();
// Set up the command modules

View File

@ -523,6 +523,18 @@ exports.initEditors = function(moduleType) {
}
};
/*
Install viewer modules for the edit macro
*/
exports.initViewers = function(moduleType) {
moduleType = moduleType || "viewer";
var viewMacro = this.macros.view;
if(viewMacro) {
viewMacro.viewers = {};
$tw.modules.applyMethods(moduleType,viewMacro.viewers);
}
};
/*
Install view modules for the story macro
*/