Excising a tiddler with predefined field by making conditional statement in javascript

Hi friends :grinning:

I have a small project to create flashcard using excise button. I aware that there are several plugins that could help but I wish to try by my self making my own solution so I could increase my learning and understanding.

I managed to add “flashcard” option when we click excise button (see below)

Now I want to excise a new tiddler when I click “flashcard” so that the text to be excised will be put in answer field but if I click other options such as “macro”, “link”, and “transclude” the text to be excised will be put in the body of the new tiddler.

I think I need to make a conditional statement in below this.wiki.getmodificationFields() but I am lacking knowledge how I can achieve this simple algorithm. I try to add

switch(event.paramObject.type||"transclude"){
case "flashcard": 
answer : operation.selection;
break;
}

after reload, I got a long message “internal javascript error”

I really appreciate if someone could guide me how to solve this problem
Thanks before
Sincerely yours

This may just be an exercise, but I would look at using tiddlywiki script, rather than JS. I presume it may be enough to have the current selection cut and pasted in the flashcard field?

This would suggest to me looking at how the excise dropdown works and including an additional option that moves the text and leaves behind a macro that allows the "answer to be toggled visable unless you are using another flashcard solution that already expects the answer in the field, then you need not leave anything behind.

Hi @TW_Tones ,

After several attempt, I managed to solve the conditional statement see below

var excisionTitle = event.paramObject.title || this.wiki.generateNewTitle("New Excision");
switch(event.paramObject.type || "transclude") {
	   case "flashcard":
	   this.wiki.addTiddler(new $tw.Tiddler(
	   this.wiki.getCreationFields(),
this.wiki.getModificationFields(),
     {
		    title: "Answer "+excisionTitle,
            answer: operation.selection,
            tags:"flashcard",
            text: operation.selection + " [[Ref|"+editTiddlerTitle+"]]"
      }
                                      ));
       this.wiki.addTiddler(new $tw.Tiddler(
	   this.wiki.getCreationFields(),
this.wiki.getModificationFields(),
     {
		    title: "Question "+excisionTitle,
            text: "{{Answer " + excisionTitle + "||Flashcard template}} {{||Source template}}",
            answer:editTiddlerTitle,
            tags:"flashcard"
      }
                                      ));
break;

         case "transclude": 
         this.wiki.addTiddler(new $tw.Tiddler(
         this.wiki.getCreationFields(),
		 this.wiki.getModificationFields(),
     {
		    title: excisionTitle,
            text: operation.selection,
            tags: event.paramObject.tagnew === "yes" ?  [editTiddlerTitle] : []
      }
                                      ));
break;

         case "link": 
         this.wiki.addTiddler(new $tw.Tiddler(
         this.wiki.getCreationFields(),
		 this.wiki.getModificationFields(),
     {
		    title: excisionTitle,
            text: operation.selection,
            tags: event.paramObject.tagnew === "yes" ?  [editTiddlerTitle] : []
      }
                                      ));
break;
 
         case "macro": 
         this.wiki.addTiddler(new $tw.Tiddler(
         this.wiki.getCreationFields(),
		 this.wiki.getModificationFields(),
     {
		    title: excisionTitle,
            text: operation.selection,
            tags: event.paramObject.tagnew === "yes" ?  [editTiddlerTitle] : []
      }
                                      ));
break;

}

It gives a clickable flashcard option in excise menu so it will create new question and answer tiddler with predefined field and transclusion template. I will create another topic to show case how my flashcard solution works. Hope it can inspire people on making flashcard in TW.

But meanwhile I still need advice, perhaps someone can offer simplification on my code above. I need to use 4 times switch-case which makes the code ugly. Case link, case macro and case transclude has similar syntax, I think it can be simplified.

It looks like all three cases do the exact same thing. Thus, you can simplify by allowing the transclude and link cases to just “fall through” to the macro case, so all three use the same code, like this:

case "transclude": 
case "link": 
case "macro": 
	this.wiki.addTiddler(new $tw.Tiddler(
		this.wiki.getCreationFields(),
		this.wiki.getModificationFields(),
		{
			title: excisionTitle,
			text: operation.selection,
			tags: event.paramObject.tagnew === "yes" ?  [editTiddlerTitle] : []
		}
	));
	break;

enjoy,
-e

2 Likes

Thanks @EricShulman

I tried it out and everything works well. The javascript code now neat and tidy