Adding text to a new tiddler with javascript

28 views
Skip to first unread message

Brady

unread,
May 23, 2007, 7:44:16 PM5/23/07
to TiddlyWiki
I'm trying to write a script that will prompt the user for a little
info, then create a tiddler with tags and text based on the user
input. I've gotten as far as prompting for the input, naming the
tiddler and adding the tags. All I have left to do is add the text to
the body of the tiddler. Here's what I have so far. This part works.

config.macros.newProject={};
config.macros.newProject.handler = function
(place,macroName,params,wikifier,paramString,tiddler)
{

if (!readOnly)
{
clearMessage();
var fieldName = prompt("Enter Field Name");
var projectDescription = prompt("Enter Project Description-");
var now = new Date();
var formatedDate = now.formatString("YYYY-0MM-0DD");
var newtitle = fieldName + " " + formatedDate + " " +
projectDescription;

if (store.tiddlerExists(newtitle))
{
alert("A tiddler with that name already exists!!!");
}
else
{
story.displayTiddler(null,newtitle,DEFAULT_EDIT_TEMPLATE);
story.setTiddlerTag(newtitle, fieldName, 0);
story.setTiddlerTag(newtitle, "@WorkUnFinishedProject", 0);
story.focusTiddler(newtitle,"title");
}
}

return false;
}

I have some programming experience, but its mainly just trial and
error hacking stuff together. I have no experience with DOM. I
bought Javascript for dummies, but the DOM section isn't much.
Looking through the TiddlyWiki source and others' macros, I've come
across a few ways that I might be able to add text to the tiddler, but
haven't gotten any to work yet. The one that I think I need is
wikify().
I've found the wikify function, but don't know what to put for the
place argument.
If I put the word "place" (ex wikify("[[MyTiddler]]", place)), the
text is placed in the calling wiki, which makes
sense, since "place" is an argument passed to the function from
the calling tiddler. So how do I reference the
new tiddler "place"?

Eric Shulman

unread,
May 23, 2007, 8:17:44 PM5/23/07
to TiddlyWiki
> input. I've gotten as far as prompting for the input, naming the
> tiddler and adding the tags. All I have left to do is add the text to
> the body of the tiddler. Here's what I have so far. This part works.
...

> story.displayTiddler(null,newtitle,DEFAULT_EDIT_TEMPLATE);
> story.setTiddlerTag(newtitle, fieldName, 0);
> story.setTiddlerTag(newtitle, "@WorkUnFinishedProject", 0);
> story.focusTiddler(newtitle,"title");
...

> across a few ways that I might be able to add text to the tiddler, but
> haven't gotten any to work yet. The one that I think I need is
> wikify().

wikify() is used to parse wiki-syntax and render DOM elements for
*viewing* a tiddler. However, since you are calling
displayTiddler(...,DEFAULT_EDIT_TEMPLATE) to display a tiddler
*editor*, you simply want to set the source content into the textarea
field that is already displayed, rather than creating any new DOM
elements.

The 'body' of a tiddler is stored in a tiddler field called 'text'.
You can retrieve the DOM element for the body textarea element that is
displayed in the tiddler editor by using story.getTiddlerField().
Then, simply set the value property of that DOM element to initialize
the editor's content:

story.getTiddlerField(newtitle,"text").value=newtext;

HTH,
-e
Eric Shulman
TiddlyTools / ELS Design Studios


Brady

unread,
May 23, 2007, 9:50:57 PM5/23/07
to TiddlyWiki
> wikify() is used to parse wiki-syntax and render DOM elements for
> *viewing* a tiddler. However, since you are calling
> displayTiddler(...,DEFAULT_EDIT_TEMPLATE) to display a tiddler
> *editor*, you simply want to set the source content into the textarea
> field that is already displayed, rather than creating any new DOM
> elements.
>
> The 'body' of a tiddler is stored in a tiddler field called 'text'.
> You can retrieve the DOM element for the body textarea element that is
> displayed in the tiddler editor by using story.getTiddlerField().
> Then, simply set the value property of that DOM element to initialize
> the editor's content:
>
> story.getTiddlerField(newtitle,"text").value=newtext;
>
> HTH,
> -e
> Eric Shulman
> TiddlyTools / ELS Design Studios

Thanks for the quick response.
That got me to the point I was trying to get to.
Now a crop of other problems, not sure that's what I wanted now that I
have it.
If I can't sort it out I'll post again.

Thanks again for the help....
Brady

Reply all
Reply to author
Forward
0 new messages