Instruct the excise.js to create question and answer tiddler if we click âperform excisionâ in excision drop down menu
- Remember to back up all of your tiddler
- Open the
$:/core/modules/editor/operations/text/excise.js
- I suggest to cut all the content and paste in somewhere else so you will have backup if something goes wrong
- Paste below code to replace the excise.js content
/*\
title: $:/core/modules/editor/operations/text/excise.js
type: application/javascript
module-type: texteditoroperation
Text editor operation to excise the selection to a new tiddler
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports["excise"] = function(event,operation) {
var editTiddler = this.wiki.getTiddler(this.editTitle),
editTiddlerTitle = this.editTitle;
if(editTiddler && editTiddler.fields["draft.of"]) {
editTiddlerTitle = editTiddler.fields["draft.of"];
}
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,
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}}",
tags:"flashcard"
}
));
break;
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;
}
operation.replacement = excisionTitle;
switch(event.paramObject.type || "transclude") {
case "transclude":
operation.replacement = "{{" + operation.replacement+ "}}";
break;
case "link":
operation.replacement = "[[" + operation.replacement+ "]]";
break;
case "macro":
operation.replacement = "<<" + (event.paramObject.macro || "translink") + " \"\"\"" + operation.replacement + "\"\"\">>";
break;
case "flashcard":
operation.replacement = "Answer "+ excisionTitle;
operation.replacement = "<<" + (event.paramObject.macro || "translink") + " \"\"\"" + operation.replacement + "\"\"\">>";
break;
}
operation.cutStart = operation.selStart;
operation.cutEnd = operation.selEnd;
operation.newSelStart = operation.selStart;
operation.newSelEnd = operation.selStart + operation.replacement.length;
};
})();
The explanation of above code can be interpreted step by step as below
Make a new tiddler for answer in flashcard with the predefined field
case "flashcard":
this.wiki.addTiddler(new $tw.Tiddler(
this.wiki.getCreationFields(),
this.wiki.getModificationFields(),
{
title: "Answer "+excisionTitle,
tags:"flashcard",
text: operation.selection + " [[Ref|"+editTiddlerTitle+"]]"
}
));
- For me I like to have tiddler with the title âAnswer âŚâ+excisionTitle. The excisionTitle (see below) will refer to the input text you type in excise dropdown menu > âTitle of new tiddlerâ. So for example I type the title âWhat is the meaning of tiddler?â in excise dropdown menu, excise.js will create the new tiddler with the title âAnswer What is the meaning of tiddler?â. You can play with your own preference with the title
- For the answer tiddler tags, just insert your own preference. Here I use tags: âflashcardâ so the new answer tiddler will have predefined tag âflashcardâ
- For the text area in answer tiddler, I write
text: operation.selection + " [[Ref|"+editTiddlerTitle+"]]"
. It means that the text in answer tiddler will have operation.selection which refers to the text that I want to excise from the source tiddler. At the end of that excised text, I want to have a word Ref which will direct me to the source tiddler. You can remove the + " [[Ref|"+editTiddlerTitle+"]]"
if you feel you donât need to know the source tiddler.
Make a new tiddler for question in flashcard with the predefined field
this.wiki.addTiddler(new $tw.Tiddler(
this.wiki.getCreationFields(),
this.wiki.getModificationFields(),
{
title: "Question "+excisionTitle,
text: "{{Answer " + excisionTitle + "||Flashcard template}}",
tags:"flashcard"
}
));
- For me I like to have tiddler with the title âQuestion âŚâ+excisionTitle. The excisionTitle will refer to the input text you type in excise dropdown menu. So for example I type the title âWhat is the meaning of tiddler?â in excise dropdown menu, excise.js will create the new tiddler with the title âAnswer What is the meaning of tiddler?â. You can play with your own preference with the title
- For the question tiddler tags, just insert your own preference. Here I use tags: âflashcardâ so the new answer tiddler will have predefined tag âflashcardâ. You can use tags like âflashcard questionâ, âquestionâ, etc as you like
- For the text area, I use this code
text: "{{Answer " + excisionTitle + "||Flashcard template}}"
. Using above example, It will write down {{Answer What is the meaning of tiddler?||Flashcard template}} in the text area of question tiddler. It will transclude the answer tiddler content into flashcard template and it will appear in question tiddler.
Last but not least, the translink macro in source tiddler
case "flashcard":
operation.replacement = "Answer "+ excisionTitle;
operation.replacement = "<<" + (event.paramObject.macro || "translink") + " \"\"\"" + operation.replacement + "\"\"\">>";
break;
The meaning of above code
In the source tiddler, replace (see below) the excised text with macro translink that directs you to the Answer Tiddler. If you have other kind macro you like, just change this part.
Reload your TW to activate the new excise.js
To be continued in below post