@blockly/block-plus-minus
Version:
A group of blocks that replace the built-in mutator UI with a +/- based UI.
3 lines • 14.8 kB
JavaScript
/*! For license information please see index.js.LICENSE.txt */
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e(require("blockly/core"));else if("function"==typeof define&&define.amd)define(["blockly/core"],e);else{var n="object"==typeof exports?e(require("blockly/core")):e(t.Blockly);for(var i in n)("object"==typeof exports?exports:t)[i]=n[i]}}(this,(t=>(()=>{"use strict";var e={370:e=>{e.exports=t}},n={},i=function t(i){var s=n[i];if(void 0!==s)return s.exports;var a=n[i]={exports:{}};return e[i](a,a.exports,t),a.exports}(370);function s(t){if(t.saveExtraState){const e=t.saveExtraState();return e?JSON.stringify(e):""}if(t.mutationToDom){const e=t.mutationToDom();return e?i.Xml.domToText(e):""}return""}function a(t=void 0){const e=new i.FieldImage(r,15,15,void 0,o);return e.args_=t,e}function o(t){const e=t.getSourceBlock();if(e.isInFlyout)return;i.Events.setGroup(!0);const n=s(e);e.minus(t.args_);const a=s(e);n!=a&&i.Events.fire(new i.Events.BlockChange(e,"mutation",null,n,a)),i.Events.setGroup(!1)}const r="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgd2lkdGg9IjI0IiBoZWlnaHQ9IjI0Ij48cGF0aCBkPSJNMTggMTFoLTEyYy0xLjEwNCAwLTIgLjg5Ni0yIDJzLjg5NiAyIDIgMmgxMmMxLjEwNCAwIDItLjg5NiAyLTJzLS44OTYtMi0yLTJ6IiBmaWxsPSJ3aGl0ZSIgLz48L3N2Zz4K";function u(t=void 0){const e=new i.FieldImage(d,15,15,void 0,l);return e.args_=t,e}function l(t){const e=t.getSourceBlock();if(e.isInFlyout)return;i.Events.setGroup(!0);const n=s(e);e.plus(t.args_);const a=s(e);n!=a&&i.Events.fire(new i.Events.BlockChange(e,"mutation",null,n,a)),i.Events.setGroup(!1)}const d="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgd2lkdGg9IjI0IiBoZWlnaHQ9IjI0Ij48cGF0aCBkPSJNMTggMTBoLTR2LTRjMC0xLjEwNC0uODk2LTItMi0ycy0yIC44OTYtMiAybC4wNzEgNGgtNC4wNzFjLTEuMTA0IDAtMiAuODk2LTIgMnMuODk2IDIgMiAybDQuMDcxLS4wNzEtLjA3MSA0LjA3MWMwIDEuMTA0Ljg5NiAyIDIgMnMyLS44OTYgMi0ydi00LjA3MWw0IC4wNzFjMS4xMDQgMCAyLS44OTYgMi0ycy0uODk2LTItMi0yeiIgZmlsbD0id2hpdGUiIC8+PC9zdmc+Cg==",_={elseIfCount_:0,hasElse_:!1,mutationToDom:function(){if(!this.elseIfCount_&&!this.hasElse_)return null;const t=i.utils.xml.createElement("mutation");return t.setAttribute("elseif",this.elseIfCount_),this.hasElse_&&t.setAttribute("else",1),t},domToMutation:function(t){const e=parseInt(t.getAttribute("elseif"),10)||0;this.hasElse_=!!parseInt(t.getAttribute("else"),10)||0,this.hasElse_&&!this.getInput("ELSE")&&this.appendStatementInput("ELSE").appendField(i.Msg.CONTROLS_IF_MSG_ELSE),this.updateShape_(e)},saveExtraState:function(){if(!this.elseIfCount_&&!this.hasElse_)return null;const t=Object.create(null);return this.elseIfCount_&&(t.elseIfCount=this.elseIfCount_),this.hasElse_&&(t.hasElse=!0),t},loadExtraState:function(t){const e=t.elseIfCount||0;this.hasElse_=t.hasElse||!1,this.hasElse_&&!this.getInput("ELSE")&&this.appendStatementInput("ELSE").appendField(i.Msg.CONTROLS_IF_MSG_ELSE),this.updateShape_(e)},updateShape_:function(t){for(;this.elseIfCount_<t;)this.addElseIf_();for(;this.elseIfCount_>t;)this.removeElseIf_()},plus:function(){this.addElseIf_()},minus:function(t){0!=this.elseIfCount_&&this.removeElseIf_(t)},addElseIf_:function(){this.elseIfCount_++,this.appendValueInput("IF"+this.elseIfCount_).setCheck("Boolean").appendField(i.Msg.CONTROLS_IF_MSG_ELSEIF).appendField(a(this.elseIfCount_),"MINUS"+this.elseIfCount_),this.appendStatementInput("DO"+this.elseIfCount_).appendField(i.Msg.CONTROLS_IF_MSG_THEN),this.getInput("ELSE")&&this.moveInputBefore("ELSE",null)},removeElseIf_:function(t=void 0){if(void 0!==t&&t!=this.elseIfCount_){const e=2*t,n=this.inputList;let i=n[e].connection;i.isConnected()&&i.disconnect(),i=n[e+1].connection,i.isConnected()&&i.disconnect(),this.bumpNeighbours();for(let t,n=e+2;(t=this.inputList[n])&&"ELSE"!=t.name;n++){const e=t.connection.targetConnection;e&&this.inputList[n-2].connection.connect(e)}}this.removeInput("IF"+this.elseIfCount_),this.removeInput("DO"+this.elseIfCount_),this.elseIfCount_--}};i.Extensions.isRegistered("controls_if_mutator")&&i.Extensions.unregister("controls_if_mutator"),i.Extensions.registerMutator("controls_if_mutator",_,(function(){this.getInput("IF0").insertFieldAt(0,u(),"PLUS")})),delete i.Blocks.lists_create_with,i.defineBlocksWithJsonArray([{type:"lists_create_with",message0:"%{BKY_LISTS_CREATE_EMPTY_TITLE} %1",args0:[{type:"input_dummy",name:"EMPTY"}],output:"Array",style:"list_blocks",helpUrl:"%{BKY_LISTS_CREATE_WITH_HELPURL}",tooltip:"%{BKY_LISTS_CREATE_WITH_TOOLTIP}",mutator:"new_list_create_with_mutator"}]);const h={itemCount_:0,mutationToDom:function(){const t=i.utils.xml.createElement("mutation");return t.setAttribute("items",this.itemCount_),t},domToMutation:function(t){const e=parseInt(t.getAttribute("items"),10);this.updateShape_(e)},saveExtraState:function(){return{itemCount:this.itemCount_}},loadExtraState:function(t){this.updateShape_(t.itemCount)},updateShape_:function(t){for(;this.itemCount_<t;)this.addPart_();for(;this.itemCount_>t;)this.removePart_();this.updateMinus_()},plus:function(){this.addPart_(),this.updateMinus_()},minus:function(){0!=this.itemCount_&&(this.removePart_(),this.updateMinus_())},addPart_:function(){0==this.itemCount_?(this.removeInput("EMPTY"),this.topInput_=this.appendValueInput("ADD"+this.itemCount_).appendField(u(),"PLUS").appendField(i.Msg.LISTS_CREATE_WITH_INPUT_WITH)):this.appendValueInput("ADD"+this.itemCount_),this.itemCount_++},removePart_:function(){this.itemCount_--,this.removeInput("ADD"+this.itemCount_),0==this.itemCount_&&(this.topInput_=this.appendDummyInput("EMPTY").appendField(u(),"PLUS").appendField(i.Msg.LISTS_CREATE_EMPTY_TITLE))},updateMinus_:function(){const t=this.getField("MINUS");!t&&this.itemCount_>0?this.topInput_.insertFieldAt(1,a(),"MINUS"):t&&this.itemCount_<1&&this.topInput_.removeField("MINUS")}};i.Extensions.registerMutator("new_list_create_with_mutator",h,(function(){this.getInput("EMPTY").insertFieldAt(0,u(),"PLUS"),this.updateShape_(3)})),i.Msg.PROCEDURE_VARIABLE="variable:",delete i.Blocks.procedures_defnoreturn,delete i.Blocks.procedures_defreturn,i.defineBlocksWithJsonArray([{type:"procedures_defnoreturn",message0:"%{BKY_PROCEDURES_DEFNORETURN_TITLE} %1 %2",message1:"%{BKY_PROCEDURES_DEFNORETURN_DO} %1",args0:[{type:"field_input",name:"NAME",text:""},{type:"input_dummy",name:"TOP"}],args1:[{type:"input_statement",name:"STACK"}],style:"procedure_blocks",helpUrl:"%{BKY_PROCEDURES_DEFNORETURN_HELPURL}",tooltip:"%{BKY_PROCEDURES_DEFNORETURN_TOOLTIP}",extensions:["get_procedure_def_no_return","procedure_context_menu","procedure_rename","procedure_vars"],mutator:"procedure_def_mutator"},{type:"procedures_defreturn",message0:"%{BKY_PROCEDURES_DEFRETURN_TITLE} %1 %2",message1:"%{BKY_PROCEDURES_DEFRETURN_DO} %1",message2:"%{BKY_PROCEDURES_DEFRETURN_RETURN} %1",args0:[{type:"field_input",name:"NAME",text:""},{type:"input_dummy",name:"TOP"}],args1:[{type:"input_statement",name:"STACK"}],args2:[{type:"input_value",align:"right",name:"RETURN"}],style:"procedure_blocks",helpUrl:"%{BKY_PROCEDURES_DEFRETURN_HELPURL}",tooltip:"%{BKY_PROCEDURES_DEFRETURN_TOOLTIP}",extensions:["get_procedure_def_return","procedure_context_menu","procedure_rename","procedure_vars"],mutator:"procedure_def_mutator"}]),i.Extensions.registerMixin("get_procedure_def_no_return",{getProcedureDef:function(){const t=this.argData_.map((t=>t.model.name));return[this.getFieldValue("NAME"),t,!1]},callType_:"procedures_callnoreturn"}),i.Extensions.registerMixin("get_procedure_def_return",{getProcedureDef:function(){const t=this.argData_.map((t=>t.model.name));return[this.getFieldValue("NAME"),t,!0]},callType_:"procedures_callreturn"});const p={customContextMenu:function(t){if(this.isInFlyout)return;const e=this.getFieldValue("NAME"),n=i.Msg.PROCEDURES_CREATE_DO.replace("%1",e),s=i.utils.xml.createElement("block");s.setAttribute("type",this.callType_),s.appendChild(this.mutationToDom(!0));const a=i.ContextMenu.callbackFactory(this,s);if(t.push({enabled:!0,text:n,callback:a}),this.isCollapsed())return;const o=this.getVarModels();for(const e of o){const n=i.Msg.VARIABLES_SET_CREATE_GET.replace("%1",e.name),s=i.utils.xml.createElement("block");s.setAttribute("type","variables_get"),s.appendChild(i.Variables.generateVariableFieldDom(e));const a=i.ContextMenu.callbackFactory(this,s);t.push({enabled:!0,text:n,callback:a})}}};i.Extensions.registerMixin("procedure_context_menu",p);const c={mutationToDom:function(t=!1){const e=i.utils.xml.createElement("mutation");return t&&e.setAttribute("name",this.getFieldValue("NAME")),this.argData_.forEach((n=>{const s=i.utils.xml.createElement("arg"),a=n.model;s.setAttribute("name",a.name),s.setAttribute("varid",a.getId()),s.setAttribute("argid",n.argId),t&&s.setAttribute("paramid",n.argId),e.appendChild(s)})),this.hasStatements_||e.setAttribute("statements","false"),e},domToMutation:function(t){this.hasStatements_="false"!==t.getAttribute("statements"),this.hasStatements_||this.removeInput("STACK");const e=[],n=[],i=[];for(const s of t.childNodes)"arg"==s.nodeName.toLowerCase()&&(e.push(s.getAttribute("name")),n.push(s.getAttribute("varid")||s.getAttribute("varId")),i.push(s.getAttribute("argid")));this.updateShape_(e,n,i)},saveExtraState:function(){if(!this.argData_.length&&this.hasStatements_)return null;const t=Object.create(null);return this.argData_.length&&(t.params=[],this.argData_.forEach((e=>{const n=e.model;t.params.push({name:n.name,id:n.getId(),argId:e.argId})}))),this.hasStatements_||(t.hasStatements=!1),t},loadExtraState:function(t){this.hasStatements_=!1!==t.hasStatements,this.hasStatements_||this.removeInput("STACK");const e=[],n=[],i=[];if(t.params)for(let s=0;s<t.params.length;s++){const a=t.params[s];e.push(a.name),n.push(a.id),i.push(a.argId)}this.updateShape_(e,n,i)},updateShape_:function(t,e,n){if(t.length!=e.length)throw Error("names and varIds must have the same length.");for(let t=this.argData_.length-1;t>=0;t--)this.removeArg_(this.argData_[t].argId);this.argData_=[];const s=e.length;for(let i=0;i<s;i++)this.addArg_(t[i],e[i],n[i]);i.Procedures.mutateCallers(this)},plus:function(){this.addArg_(),i.Procedures.mutateCallers(this)},minus:function(t){this.argData_.length&&(this.removeArg_(t),i.Procedures.mutateCallers(this))},addArg_:function(t=null,e=null,n=null){if(!this.argData_.length){const t=new i.FieldLabel(i.Msg.PROCEDURES_BEFORE_PARAMS);this.getInput("TOP").appendField(t,"WITH")}const s=this.argData_.map((t=>t.model.name));t=t||i.Variables.generateUniqueNameFromOptions(i.Procedures.DEFAULT_ARG,s);const a=i.Variables.getOrCreateVariablePackage(this.workspace,e,t,"");n=n||i.utils.idGenerator.genUid(),this.addVarInput_(t,n),this.getInput("STACK")?this.moveInputBefore(n,"STACK"):this.moveInputBefore(n,"RETURN"),this.argData_.push({model:a,argId:n})},removeArg_:function(t){this.removeInput(t,!0)&&(1==this.argData_.length&&this.getInput("TOP").removeField("WITH"),this.argData_=this.argData_.filter((e=>e.argId!=t)))},addVarInput_:function(t,e){const n=new i.FieldTextInput(t,this.validator_);n.onFinishEditing_=this.finishEditing_.bind(n),n.varIdsToDelete_=[],n.preEditVarModel_=null,this.appendDummyInput(e).setAlign(i.inputs.Align.RIGHT).appendField(a(e)).appendField(i.Msg.PROCEDURE_VARIABLE).appendField(n,e)},validator_:function(t){const e=this.getSourceBlock(),n=e.workspace,s=e.argData_,a=e.argData_.find((t=>t.argId==this.name)),o=a.model.getId(),r=(t=t.replace(/[\s\xa0]+/g," ").trim()).toLowerCase();if(!t||!s.every((t=>t.argId==this.name||r!=t.model.name.toLowerCase())))return this.preEditVarModel_&&(a.model=this.preEditVarModel_,this.preEditVarModel_=null),i.Procedures.mutateCallers(e),null;this.varIdsToDelete_.length||(this.preEditVarModel_=a.model,n.getVariableUsesById(o).every((t=>t.id==e.id||t.getProcedureCall&&t.getProcedureCall()==e.getProcedureDef()[0]))&&this.varIdsToDelete_.push(o));let u=n.getVariable(t,"");return u?u.name!=t&&n.renameVariableById(u.getId(),t):(u=n.createVariable(t,""),this.varIdsToDelete_.push(u.getId())),u.getId()!=o&&(a.model=u),i.Procedures.mutateCallers(e),t},finishEditing_:function(t){const e=this.getSourceBlock(),n=e.argData_.find((t=>t.argId==this.name)).model.getId();this.varIdsToDelete_.forEach((t=>{t!=n&&e.workspace.deleteVariableById(t)})),this.varIdsToDelete_.length=0,this.preEditVarModel_=null}};i.Extensions.registerMutator("procedure_def_mutator",c,(function(){this.argData_=[],this.hasStatements_=!0,this.getInput("TOP").insertFieldAt(0,u(),"PLUS")})),i.Extensions.register("procedure_rename",(function(){this.getField("NAME").setValidator(i.Procedures.rename)})),i.Extensions.register("procedure_vars",(function(){const t={getVars:function(){return this.argData_.map((t=>t.model.name))},getVarModels:function(){return this.argData_.map((t=>t.model))},renameVarById:function(t,e){const n=this.argData_.find((e=>e.model.getId()==t));if(!n)return;const s=this.workspace.getVariableById(e),a=s.name;this.addVarInput_(a,e),this.moveInputBefore(e,t),this.removeInput(t),n.model=s,i.Procedures.mutateCallers(this)},updateVarName:function(t){const e=t.getId(),n=this.argData_.find((t=>t.model.getId()==e));n&&(this.setFieldValue(t.name,n.argId),n.model=t)}};this.mixin(t,!0)}));const m={itemCount_:0,mutationToDom:function(){const t=i.utils.xml.createElement("mutation");return t.setAttribute("items",this.itemCount_),t},domToMutation:function(t){const e=parseInt(t.getAttribute("items"),10);this.updateShape_(e)},saveExtraState:function(){return{itemCount:this.itemCount_}},loadExtraState:function(t){this.updateShape_(t.itemCount)},updateShape_:function(t){for(;this.itemCount_<t;)this.addPart_();for(;this.itemCount_>t;)this.removePart_();this.updateMinus_()},plus:function(){this.addPart_(),this.updateMinus_()},minus:function(){0!=this.itemCount_&&(this.removePart_(),this.updateMinus_())},addPart_:function(){0==this.itemCount_?(this.getInput("EMPTY")&&this.removeInput("EMPTY"),this.topInput_=this.appendValueInput("ADD"+this.itemCount_).appendField(u(),"PLUS").appendField(i.Msg.TEXT_JOIN_TITLE_CREATEWITH)):this.appendValueInput("ADD"+this.itemCount_),this.itemCount_++},removePart_:function(){this.itemCount_--,this.removeInput("ADD"+this.itemCount_),0==this.itemCount_&&(this.topInput_=this.appendDummyInput("EMPTY").appendField(u(),"PLUS").appendField(this.newQuote_(!0)).appendField(this.newQuote_(!1)))},updateMinus_:function(){const t=this.getField("MINUS");!t&&this.itemCount_>0?this.topInput_.insertFieldAt(1,a(),"MINUS"):t&&this.itemCount_<1&&this.topInput_.removeField("MINUS")}};return i.Extensions.isRegistered("text_join_mutator")&&i.Extensions.unregister("text_join_mutator"),i.Extensions.registerMutator("text_join_mutator",m,(function(){i.Extensions.apply("quote_image_mixin",this,!1),this.updateShape_(2)})),{}})()));
//# sourceMappingURL=index.js.map