> It's weird, I did not think plugin code executed until re-load.
> Anyway, the plugin works - just some unexpected behaviour on initial
> import.
Actually, it is precisely because the freshly imported plugin code has
*not* been executed that the odd behavior occurs. Here's why:
Unlike most plugins, EditFieldPlugin "hijacks" an *existing* core
macro, <<edit>>, to add the "view mode" handling. Thus, even though
the plugin code hasn't been executed, the core is still able to render
the edit fields contained in the "Examples" section of the plugin.
However, as soon as those edit fields are rendered, the core's
standard processing for config.macros.edit.handler() invokes
story.setDirty() for the tiddler, which tells the rest of the core
that the tiddler is now in "edit mode" (even though it really isn't!).
As a result of this "dirty" flag being set, when the core handler for
the "edit" toolbar command is subsequently invoked, the command simply
moves the focus to the rendered field that it believes is already part
of the editor (i.e., the textarea field in the example section),
instead of actually switching to the EditTemplate to truly enter into
edit mode.
The reason this problem goes away once the plugin has been properly
initialized (i.e., after a save-and-reload) is because of the
following two lines of code in the *hijacked* <<edit>> handler:
if (here && here.getAttribute("template").indexOf("ViewTemplate")!=-1)
{
story.setDirty(tiddler.title,false);
This code essentially forces the "dirty" flag set by core to be reset
when in "view mode", thereby permitting subsequent use of the "edit"
toolbar command to be properly processed so that the tiddler really
switches into "edit mode" when the toolbar command is invoked.
When I first wrote and tested this plugin last August, this very odd
behavior was the among the most subtle and puzzling problems that I
encountered. It took the rest of that day, and half of the next to
trace through the entire maze of tiddler "refresh" handling invoked by
the template switching logic to finally discover that the "dirty" flag
was the cause, and I distinctly remember having a gleefully self-
congratulatory "ah hah!" moment in which I reveled at my own
cleverness when I finally worked out the solution that made things
work as they were supposed to. :-)
Sometimes things are simple and obvious... and sometimes they are not.
-e