Hi Jed,
while I was unable to exactly understand what you are trying to
accomplish, I am able to answer your two questions.
Please be aware that this will be a very long answer, but I hope you can
follow along. Please feel free to ask any questions that arise.
---------------
> //Okay I am in a node repl and I just booted TiddlyWiki. So lets see
what parseText() actually does
undefined
> // I will use an example text that includes a widget, some wikitext
and also a variable that is printed out (named 'currentTest')
undefined
> var parsed = $tw.wiki.parseText("text/vnd.tiddlywiki",
"<<currentTest>><$button>''Test''</$button>", {});
undefined
> //Let's see what we got:
undefined
> parsed
{ wiki:
{ addTiddler: [Function],
deleteTiddler: [Function],
getTiddler: [Function],
allTitles: [Function],
each: [Function],
allShadowTitles: [Function],
eachShadow: [Function],
eachTiddlerPlusShadows: [Function],
eachShadowPlusTiddlers: [Function],
tiddlerExists: [Function],
isShadowTiddler: [Function],
getShadowSource: [Function],
readPluginInfo: [Function],
getPluginInfo: [Function],
registerPluginTiddlers: [Function],
unregisterPluginTiddlers: [Function],
unpackPluginTiddlers: [Function],
caches: {},
globalCache: {},
changedTiddlers: {},
changeCount:
{ '$:/StoryList': 1,
'$:/HistoryList': 1 },
eventListeners: [ change: [Object] ],
eventsTriggered: false },
type: 'text/vnd.tiddlywiki',
source: '<<currentTest>><$button>\'\'Test\'\'</$button>',
sourceLength: 42,
pos: 42,
pragmaRules: [],
blockRules: [ { rule: [Object], matchIndex: 13 } ],
inlineRules:
[ { rule: [Object], matchIndex: undefined },
{ rule: [Object], matchIndex: undefined },
{ rule: [Object], matchIndex: undefined } ],
tree:
[ { type: 'element',
tag: 'p',
children: [Object] } ] }
> /*
... As you can see parseText() actually returns a parser which can parse
the text we provided and in fact it already did
parse it and put the resulting parseTree in the attribute named 'tree'*/
undefined
> var util = require('util');
undefined
> console.log(util.inspect(parsed.tree, false,null));
[ { type: 'element',
tag: 'p',
children:
[ { type: 'macrocall', name: 'currentTest', params: [] },
{ type: 'button',
start: 15,
attributes: {},
tag: '$button',
end: 24,
isBlock: false,
children:
[ { type: 'element',
tag: 'strong',
children: [ { type: 'text', text: 'Test' } ] } ] } ] } ]
undefined
> /* So really it is not much more than that, it also didn't execute
ANY widget code or even touched widgets.*/
undefined
> /* The process I am about to show you is closely related to what
happens in the renderText() function of tiddlywiki. B
ut to make this work the way we want, we first have to make the widgets
themselves, which can be generated from the pars
eTree, we just got*/
undefined
> var widgets = $tw.wiki.makeWidget(parsed, {});
undefined
> // This resulted in:
undefined
> console.log(widgets);
{ parseTreeNode: { type: 'widget', children: [ [Object] ] },
wiki:
{ addTiddler: [Function],
deleteTiddler: [Function],
getTiddler: [Function],
allTitles: [Function],
each: [Function],
allShadowTitles: [Function],
eachShadow: [Function],
eachTiddlerPlusShadows: [Function],
eachShadowPlusTiddlers: [Function],
tiddlerExists: [Function],
isShadowTiddler: [Function],
getShadowSource: [Function],
readPluginInfo: [Function],
getPluginInfo: [Function],
registerPluginTiddlers: [Function],
unregisterPluginTiddlers: [Function],
unpackPluginTiddlers: [Function],
caches: {},
globalCache: {},
changedTiddlers: {},
changeCount: { '$:/StoryList': 1, '$:/HistoryList': 1 },
eventListeners: [ change: [Object] ],
eventsTriggered: false },
parentWidget: undefined,
variablesConstructor: [Function],
variables: {},
document:
{ setSequenceNumber: [Function],
createElementNS: [Function],
createElement: [Function],
createTextNode: [Function],
compatMode: 'CSS1Compat',
isTiddlyWikiFakeDom: true },
attributes: {},
children: [],
domNodes: [],
eventListeners: {} }
undefined
> /* This looks more like a Widget to me and in fact this is the
vanilla widget which doesn't do much, but has our parse
Tree woven into it. You will also notice that the children's of the
widget object we got are still empty, which is becau
se our widgets weren't actually built, but rather a Root widget was
constructed around it. The real widgets we care abo
ut will be built by the makeChildWidgets() call upron rendering this
thing. For this render, we need some sort of HTML D
OM container. Unfortunately we aren't in the browser so we just have to
fake this.*/
undefined
> var container = $tw.fakeDocument.createElement("div");
undefined
> /* And also render it:*/
undefined
> widgets.render(container, null);
undefined
> // Apparently nothing happened.. Oh no wait, the container is now filled:
undefined
> console.log(container.innerHTML);
<p><button class=''><strong>Test</strong></button></p>
undefined
> // Perfect .. also we have now a fully built widget tree in widgets:
undefined
> console.log(widgets.children);
[{ parseTreeNode: { type: 'element', tag: 'p', children: [Object] },
wiki:
{ addTiddler: [Function],
deleteTiddler: [Function],
getTiddler: [Function],
allTitles: [Function],
each: [Function],
allShadowTitles: [Function],
eachShadow: [Function],
eachTiddlerPlusShadows: [Function],
eachShadowPlusTiddlers: [Function],
tiddlerExists: [Function],
isShadowTiddler: [Function],
getShadowSource: [Function],
readPluginInfo: [Function],
getPluginInfo: [Function],
registerPluginTiddlers: [Function],
unregisterPluginTiddlers: [Function],
unpackPluginTiddlers: [Function],
caches: {},
globalCache: {},
changedTiddlers: {},
changeCount: [Object],
eventListeners: [Object],
eventsTriggered: false },
parentWidget:
{ parseTreeNode: [Object],
wiki: [Object],
parentWidget: undefined,
variablesConstructor: [Function],
variables: {},
document: [Object],
attributes: {},
children: [Circular],
domNodes: [],
eventListeners: {},
parentDomNode: [Object] },
variablesConstructor: [Function],
variables: {},
document:
{ setSequenceNumber: [Function],
createElementNS: [Function],
createElement: [Function],
createTextNode: [Function],
compatMode: 'CSS1Compat',
isTiddlyWikiFakeDom: true },
attributes: {},
children: [ [Object], [Object] ],
domNodes: [ [Object] ],
eventListeners: {},
parentDomNode:
{ isTiddlyWikiFakeDom: true,
tag: 'div',
attributes: {},
isRaw: false,
children: [Object],
style: {},
namespaceURI: 'http://www.w3.org/1999/xhtml' },
namespace: 'http://www.w3.org/1999/xhtml' } ]
undefined
> //Now it is just a matter of invoking the desired action with the
normal method:
undefined
> widgets.invokeActions(/* ..whatever the event will be */);
false
> // In this case it returns false, because there were no children
where it was able to invoke an action.
undefined
> /* However, before we dig deeper into that, notice that our variable
we printed out in our original text just got tran
slated to an empty string, luckily the makeWidget function accepts
options to set the variables for the widget tree.*/
undefined
> var container = $tw.fakeDocument.createElement("div");
undefined
> var widgets = $tw.wiki.makeWidget(parsed, {variables: {"currentTest":
"Hello Jed, I am a variable"}});
undefined
> widgets.render(container, null);
undefined
> console.log(container.innerHTML);
<p>Hello Jed, I am a variable<button
class=''><strong>Test</strong></button></p>
undefined
> /* So that is how you do that part. Finally, lets test that the above
process really works with a more elaborate examp
le, however, I'll have to improvise a bit since this loaded tiddlywiki
actually dies some time ago since i didn't suppl
y it with a command line parameter*/
undefined
> $tw.wiki.addTiddler({title: "targetTiddler", text: "Empty"});
undefined
> $tw.wiki.getTiddler("targetTiddler");
{ fields: { title: 'targetTiddler', text: 'Empty' } }
> var parsed = $tw.wiki.parseText("text/vnd.tiddlywiki",
"<$action-setfield $tiddler='targetTiddler' $value='This really
works =)' />", {});
undefined
> var widgets = $tw.wiki.makeWidget(parsed, {});
undefined
> var container = $tw.fakeDocument.createElement("div");
undefined
> widgets.render(container, null);
undefined
> widgets.invokeActions({});
false
> //this is because I am not 5.1.7 where action only propagate to
immediate children, so lets invoke it deeper
undefined
> console.log(widgets.children[0]);
{ parseTreeNode: { type: 'element', tag: 'p', children: [ [Object] ] },
wiki:
{...},
parentWidget:
{ ..},
attributes: {},
children:
[ { parseTreeNode: [Object],
wiki: [Object],
parentWidget: [Circular],
variablesConstructor: [Function],
variables: {},
document: [Object],
attributes: [Object],
children: [],
domNodes: [],
eventListeners: {},
actionTiddler: 'targetTiddler',
actionField: undefined,
actionIndex: undefined,
actionValue: 'This really works =)' } ],
domNodes:
[ { isTiddlyWikiFakeDom: true,
tag: 'p',
attributes: {},
isRaw: false,
children: [],
style: {},
namespaceURI: 'http://www.w3.org/1999/xhtml',
parentNode: [Object] } ],
eventListeners: {},
parentDomNode:
{..}
undefined
> // as you can see this is the direct parent of the action widget
undefined
> widgets.children[0].invokeActions({});
true
> // that looks good.. let's take a look at the tiddler
undefined
> $tw.wiki.getTiddler("targetTiddler");
{ fields:
{ title: 'targetTiddler',
text: 'This really works =)',
modified: Thu Feb 19 2015 05:52:06 GMT+0100 (Mitteleuropäische
Zeit) } }
> Done.
I hope this helps, even though it is quite long.
/Andreas
Am 19.02.2015 um 04:05 schrieb Jed Carty:
After a conversation with Tobais
https://groups.google.com/forum/#!topic/tiddlywikidev/p0epN2XlgZk it
looks like it would be much more useful for me to create a general 'if
any of these things change perform these actions' daemon instead of
one that just does math. Most of it turned out to be very simple,
except for the actually useful part where it evaluates the actions you
give it. What I have is on github here
https://github.com/inmysocks/TW5-TriggerActions and will give you a
nice list of the action widgets/macros you would like it to evaluate
in the console, without actually doing anything useful.
So, to make this do something useful I need to figure out two things:
1) Is it possible to get javascript to trigger an action widget when
given the plain text string. I currently have it pull the text out of
fields and each field is supposed to have an action widget to trigger,
but unfortunately
$tw.wiki.parseText("text/vnd.tiddlywiki",actionItem,"") doesn't
trigger whatever action widget expression is stored in actionItem. I
didn't really expect it to but I was hoping to get lucky. I am not
sure that what I want to do is actually possible without significant
work, but I am hoping that someone who knows more about how the core
works can tell me.
2) In the same context, is it possible to set the currentTiddler
variable? Since I am not making a widget I don't think I can use the
widget.setVariable function that the set widget uses, or at least I
haven't figured out how to yet.
Said differently, I want to do the equivalent of
<$tiddler tiddler=<<someTiddler>>>
<$action-setfield text=test/>
(some list of more action widgets/macros)
</$tiddler>
in javascript where someTiddler and the action widget expressions are
given as strings, and the action widgets are triggered by the daemon
when a tiddler changes.
--
You received this message because you are subscribed to the Google
Groups "TiddlyWikiDev" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
Visit this group at http://groups.google.com/group/tiddlywikidev.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"TiddlyWikiDev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/tiddlywikidev.
For more options, visit https://groups.google.com/d/optout.