Dropzone parse text and create new or append to existing tiddler

Hello all,

I want to drag plain text (sometimes HTML) vocabulary term-definition to create a tiddler with term as title and definition as content. If the tiddler exists, the new content is appended to the existing tiddler otherwise create new. (keeping only the text. )

My code creates a new tiddler the first time. On second try it creates yet another tiddler -with the same name. What am I doing wrong?


\define vocabSplitPattern()  [¦|\/]
\define droppable-text-actions()
  /* use vocabSplitPattern()  [¦|\/] to split at ¦ or /  */
  <$wikify name=import-text text=<<actionTiddler>>>
    <$vars newTitle={{{ [<import-text>splitregexp<vocabSplitPattern>first[]] }}} 
      newDefn={{{ [<import-text>splitregexp<vocabSplitPattern>last[]] }}}  
      lf="""
    <hr/>
    """>
      
    <!-- If tiddler exists, append new definition -->
    <$list filter="[<newTitle>has[text]]" variable="oldText">
      <$vars oldText={{{ [<currentTiddler>get[text]] }}}   >
        <$action-setfield $tiddler=<<currentTiddler>>    text={{{ [<newDefn>addsuffix<oldText>] }}}  />
          <$action-navigate $to=<<currentTiddler>>/>
        </$action-setfield>
      </$vars>
    </$list>

    <!-- If tiddler does not exist, create it -->
    <$list filter="[<newTitle>!has[title] ]">
      <$action-createtiddler $basetitle=<<newTitle>> $template=<<actionTiddler>> text=<<newDefn>> tags="vocab" >
        <$action-navigate $to=<<createTiddler-title>>/>
      </$action-createtiddler>
    </$list>

    </$vars>
  </$wikify>
\end


<$droppable actions=<<droppable-text-actions>>>
  <$button style="width:10em;height:3em;">Drop vocab here</$button>
</$droppable>

Test data–

banana ¦ fruit i eat . hmmm.

banana / another line

I am not 100% sure, what you want. But from the test-data and your existing code, I think that’s what you want.

I would use conditional if instead of list if a tiddler exists. IMO it is clearer.

I would also use $let instead of $vars.

 \define vocabSplitPattern()  [¦|\/]

\define droppable-text-actions()
  /* use vocabSplitPattern()  [¦|\/] to split at ¦ or /  */
  <$wikify name=import-text text=<<actionTiddler>>>
    <$let  newTitle={{{ [<import-text>splitregexp<vocabSplitPattern>first[]] }}} 
      newDefn={{{ [<import-text>splitregexp<vocabSplitPattern>last[]] }}}  
      lf="""
    <hr/>
    """>
      
    <!-- If tiddler exists, append new definition -->
    <% if [<newTitle>is[tiddler]] %>
      <$let oldText={{{ [<newTitle>get[text]] }}}   >
        <$action-setfield $tiddler=<<newTitle>>    text={{{ [<oldText>addsuffix<lf>addsuffix<newDefn>] }}}  />
          <$action-navigate $to=<<newTitle>>/>
        </$action-setfield>
      </$let>
    <%endif%>

    <!-- If tiddler does not exist, create it -->
    <% if [<newTitle>!is[tiddler]] %>
      <$action-createtiddler $basetitle=<<newTitle>> $template=<<actionTiddler>> text=<<newDefn>> tags="vocab" $overwrite=yes >
        <$action-navigate $to=<<createTiddler-title>>/>
      </$action-createtiddler>
    <%endif%>

    </$let>
  </$wikify>
\end


<$droppable actions=<<droppable-text-actions>>>
  <$button style="width:10em;height:3em;">Drop vocab here</$button>
</$droppable>

Thanks for the pointer to conditional if.

The problem still persists where the definition is not appended to existing tiddler.

using the same test data- if I drop the text banana ¦ fruit i eat . hmmm. in the drop zone, I get:
Screenshot 2025-02-22 at 8.24.49 AM

Next, if I use the second line from the test data banana / another line and drop it on the drop zone, I get:
Screenshot 2025-02-22 at 8.25.19 AM

Two separate tiddlers have been created with the same title. I’m expecting to keep the first tiddler and append the content which should look like this:

Any ideas?

edit: When I test this on a new empty on tiddlyhost, the final result is a single tiddler. The second drop overwrites the first.

Use my code. It works for me.

This is very nice. I’ve not done much drag-and-drop in TW. But I always had the impression that it was fiddly and complex. This makes it look quite simple. Thanks for the instructional post!

One very minor question: Why do you choose to use two opposite conditions rather than else?

There are several possibilities to optimize the whole thing. I did use a similar structure as the original code. There should be a possibility to use 2 times the action-createtiddler widget instead of action-setfield.

@pmario Thanks for confirming.

I tried again on a new empty on Tiddlyhost and it now works.
I tried again on my wiki and it now works.

Not sure why it was misbehaving.

@Scott_Sauyet Combining the if statements with else also works. I suspect he kept it that way since my original list filter code had two separate checks.

Thanks.

So it does! For some reason, I only skimmed the original post, probably because it was asking about drag-and-drop, and I don’t really have any insight to add about that. But it looks like your post already had the simple-looking drag-and-drop code. So thank you too!