mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-12-25 17:40:29 +00:00
Add support for macro definitions
This commit is contained in:
parent
79dcc9a557
commit
9278c43a91
55
core/modules/new_widgets/macrodef.js
Normal file
55
core/modules/new_widgets/macrodef.js
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/*\
|
||||||
|
title: $:/core/modules/new_widgets/macrodef.js
|
||||||
|
type: application/javascript
|
||||||
|
module-type: new_widget
|
||||||
|
|
||||||
|
Macro definition widget
|
||||||
|
|
||||||
|
\*/
|
||||||
|
(function(){
|
||||||
|
|
||||||
|
/*jslint node: true, browser: true */
|
||||||
|
/*global $tw: false */
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
var Widget = require("$:/core/modules/new_widgets/widget.js").widget;
|
||||||
|
|
||||||
|
var MacroDefWidget = function(parseTreeNode,options) {
|
||||||
|
this.initialise(parseTreeNode,options);
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Inherit from the base widget class
|
||||||
|
*/
|
||||||
|
MacroDefWidget.prototype = new Widget();
|
||||||
|
|
||||||
|
/*
|
||||||
|
Render this widget into the DOM
|
||||||
|
*/
|
||||||
|
MacroDefWidget.prototype.render = function(parent,nextSibling) {
|
||||||
|
this.parentDomNode = parent;
|
||||||
|
this.computeAttributes();
|
||||||
|
this.execute();
|
||||||
|
this.renderChildren(parent,nextSibling);
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Compute the internal state of the widget
|
||||||
|
*/
|
||||||
|
MacroDefWidget.prototype.execute = function() {
|
||||||
|
// Set macro definition
|
||||||
|
this.setVariable(this.parseTreeNode.name,this.parseTreeNode.text,this.parseTreeNode.params);
|
||||||
|
// Construct the child widgets
|
||||||
|
this.makeChildWidgets();
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
||||||
|
*/
|
||||||
|
MacroDefWidget.prototype.refresh = function(changedTiddlers) {
|
||||||
|
return this.refreshChildren(changedTiddlers);
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.macrodef = MacroDefWidget;
|
||||||
|
|
||||||
|
})();
|
@ -633,15 +633,31 @@ var tweakParseTreeNode = function(node) {
|
|||||||
node.type = node.tag.substr(1);
|
node.type = node.tag.substr(1);
|
||||||
}
|
}
|
||||||
tweakParseTreeNodes(node.children);
|
tweakParseTreeNodes(node.children);
|
||||||
},
|
};
|
||||||
tweakParseTreeNodes = function(nodeList) {
|
|
||||||
|
var tweakParseTreeNodes = function(nodeList) {
|
||||||
$tw.utils.each(nodeList,tweakParseTreeNode);
|
$tw.utils.each(nodeList,tweakParseTreeNode);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var tweakMacroDefinition = function(nodeList) {
|
||||||
|
if(nodeList && nodeList[0] && nodeList[0].type === "macrodef") {
|
||||||
|
nodeList[0].children = nodeList.slice(1);
|
||||||
|
nodeList.splice(1,nodeList.length-1);
|
||||||
|
tweakMacroDefinition(nodeList.children);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var tweakParser = function(parser) {
|
||||||
|
// Move any macro definitions to contain the body tree
|
||||||
|
tweakMacroDefinition(parser.tree);
|
||||||
|
// Tweak widgets
|
||||||
|
tweakParseTreeNodes(parser.tree);
|
||||||
|
};
|
||||||
|
|
||||||
exports.new_parseText = function(type,text,options) {
|
exports.new_parseText = function(type,text,options) {
|
||||||
var parser = this.parseText(type,text,options);
|
var parser = this.parseText(type,text,options);
|
||||||
if(parser) {
|
if(parser) {
|
||||||
tweakParseTreeNodes(parser.tree)
|
tweakParser(parser)
|
||||||
};
|
};
|
||||||
return parser;
|
return parser;
|
||||||
};
|
};
|
||||||
@ -649,7 +665,7 @@ exports.new_parseText = function(type,text,options) {
|
|||||||
exports.new_parseTiddler = function(title,options) {
|
exports.new_parseTiddler = function(title,options) {
|
||||||
var parser = this.parseTiddler(title,options);
|
var parser = this.parseTiddler(title,options);
|
||||||
if(parser) {
|
if(parser) {
|
||||||
tweakParseTreeNodes(parser.tree)
|
tweakParser(parser)
|
||||||
};
|
};
|
||||||
return parser;
|
return parser;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user