Skip to content

Instantly share code, notes, and snippets.

@tmedwards
Created June 28, 2019 08:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmedwards/4c9061ab13ab01626e27c83d157a7311 to your computer and use it in GitHub Desktop.
Save tmedwards/4c9061ab13ab01626e27c83d157a7311 to your computer and use it in GitHub Desktop.
<<textboxplus>> macro for SugarCube v2
/*! <<textboxplus>> macro for SugarCube v2 */
!function(){"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&"function"==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};if("undefined"==typeof version||void 0===version.title||"SugarCube"!==version.title||void 0===version.major||version.major<2||void 0===version.minor||version.minor<22)throw new Error("<<textboxplus>> macro requires SugarCube 2.22.0 or greater, aborting load");Macro.add("textboxplus",{isAsync:!0,tags:null,handler:function(){var _this=this;if(this.args.length<2){var errors=[];return this.args.length<1&&errors.push("variable name"),this.args.length<2&&errors.push("default value"),this.error("no "+errors.join(" or ")+" specified")}if("string"!=typeof this.args[0])return this.error("variable name argument is not a string");var varName=this.args[0].trim();if("$"!==varName[0]&&"_"!==varName[0])return this.error('variable name "'+this.args[0]+'" is missing its sigil ($ or _)');Config.debug&&this.debugView.modes({block:!0});var varId=Util.slugify(varName),defaultValue=this.args[1],el=document.createElement("input"),autofocus=!1,passage=void 0;this.args.length>3?(passage=this.args[2],autofocus="autofocus"===this.args[3]):this.args.length>2&&("autofocus"===this.args[2]?autofocus=!0:passage=this.args[2]),"object"===(void 0===passage?"undefined":_typeof(passage))&&(passage=passage.link),jQuery(el).attr({id:this.name+"-"+varId,name:this.name+"-"+varId,type:"text",tabindex:0}).addClass("macro-"+this.name).on("change.macros",this.createShadowWrapper(function(){State.setVar(varName,this.value)})).on("keypress.macros",this.createShadowWrapper(function(){var contents=_this.payload[0].contents.trim();return function(ev){13===ev.which&&(ev.preventDefault(),State.setVar(varName,this.value),""!==contents&&Wikifier.wikifyEval(contents),null!=passage&&Engine.play(passage))}}())).appendTo(this.output),State.setVar(varName,defaultValue),el.value=defaultValue,autofocus&&(el.setAttribute("autofocus","autofocus"),postdisplay["#autofocus:"+el.id]=function(task){delete postdisplay[task],setTimeout(function(){return el.focus()},Engine.minDomActionDelay)})}})}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment