@blockly/field-multilineinput
Version:
3 lines • 8.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"),require("blockly/javascript"),require("blockly/dart"),require("blockly/lua"),require("blockly/php"),require("blockly/python"));else if("function"==typeof define&&define.amd)define(["blockly/core","blockly/javascript","blockly/dart","blockly/lua","blockly/php","blockly/python"],e);else{var i="object"==typeof exports?e(require("blockly/core"),require("blockly/javascript"),require("blockly/dart"),require("blockly/lua"),require("blockly/php"),require("blockly/python")):e(t.Blockly,t["Blockly.JavaScript"],t["Blockly.Dart"],t["Blockly.Lua"],t["Blockly.PHP"],t["Blockly.Python"]);for(var l in i)("object"==typeof exports?exports:t)[l]=i[l]}}(this,(t,e,i,l,o,s)=>(()=>{"use strict";var r={127:t=>{t.exports=e},157:t=>{t.exports=l},370:e=>{e.exports=t},379:t=>{t.exports=i},537:t=>{t.exports=o},557:t=>{t.exports=s}},n={};function a(t){var e=n[t];if(void 0!==e)return e.exports;var i=n[t]={exports:{}};return r[t](i,i.exports,a),i.exports}a.d=(t,e)=>{for(var i in e)a.o(e,i)&&!a.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},a.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),a.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var u={};a.r(u),a.d(u,{FieldMultilineInput:()=>f,installAllBlocks:()=>R,registerFieldMultilineInput:()=>y,textMultiline:()=>d});var d={};a.r(d),a.d(d,{BLOCK_NAME:()=>g,blockDefinition:()=>k,installBlock:()=>v,toDart:()=>I,toJavascript:()=>m,toLua:()=>x,toPhp:()=>D,toPython:()=>b});var c=a(370),h=a(127),p=a(379),_=a(157),T=a(537),E=a(557);class f extends c.FieldTextInput{constructor(t,e,i){super(c.Field.SKIP_SETUP),this.textGroup=null,this.maxLines_=1/0,this.isOverflowedY_=!1,t!==c.Field.SKIP_SETUP&&(i&&this.configure_(i),this.setValue(t),e&&this.setValidator(e))}configure_(t){super.configure_(t),t.maxLines&&this.setMaxLines(t.maxLines)}toXml(t){return t.textContent=this.getValue().replace(/\n/g," "),t}fromXml(t){this.setValue(t.textContent.replace(/ /g,"\n"))}saveState(){const t=this.saveLegacyState(f);return null!==t?t:this.getValue()}loadState(t){this.loadLegacyState(c.Field,t)||this.setValue(t)}initView(){this.createBorderRect_(),this.textGroup=c.utils.dom.createSvgElement(c.utils.Svg.G,{class:"blocklyEditableField"},this.fieldGroup_)}getDisplayText_(){const t=this.getSourceBlock();if(!t)throw new Error("The field has not yet been attached to its input. Call appendField to attach it.");let e=this.getText();if(!e)return c.Field.NBSP;const i=e.split("\n");e="";const l=this.isOverflowedY_?this.maxLines_:i.length;for(let t=0;t<l;t++){let o=i[t];o.length>this.maxDisplayLength?o=o.substring(0,this.maxDisplayLength-4)+"...":this.isOverflowedY_&&t===l-1&&(o=o.substring(0,o.length-3)+"..."),o=o.replace(/\s/g,c.Field.NBSP),e+=o,t!==l-1&&(e+="\n")}return t.RTL&&(e+=""),e}doValueUpdate_(t){super.doValueUpdate_(t),null!==this.value_&&(this.isOverflowedY_=this.value_.split("\n").length>this.maxLines_)}render_(){const t=this.getSourceBlock();if(!t)throw new Error("The field has not yet been attached to its input. Call appendField to attach it.");let e;const i=this.textGroup;for(;e=i.firstChild;)i.removeChild(e);const l=this.getConstants();if(!l)throw Error("Constants not found");const o=this.getDisplayText_().split("\n");let s=0;for(let t=0;t<o.length;t++){const e=l.FIELD_TEXT_HEIGHT+l.FIELD_BORDER_RECT_Y_PADDING;c.utils.dom.createSvgElement(c.utils.Svg.TEXT,{class:"blocklyText blocklyMultilineText",x:l.FIELD_BORDER_RECT_X_PADDING,y:s+l.FIELD_BORDER_RECT_Y_PADDING,dy:l.FIELD_TEXT_BASELINE},i).appendChild(document.createTextNode(o[t])),s+=e}if(this.isBeingEdited_){const t=this.htmlInput_;this.isOverflowedY_?c.utils.dom.addClass(t,"blocklyHtmlTextAreaInputOverflowedY"):c.utils.dom.removeClass(t,"blocklyHtmlTextAreaInputOverflowedY")}if(this.updateSize_(),this.isBeingEdited_){t.RTL?setTimeout(this.resizeEditor_.bind(this),0):this.resizeEditor_();const e=this.htmlInput_;this.isTextValid_?(c.utils.dom.removeClass(e,"blocklyInvalidInput"),c.utils.aria.setState(e,c.utils.aria.State.INVALID,!1)):(c.utils.dom.addClass(e,"blocklyInvalidInput"),c.utils.aria.setState(e,c.utils.aria.State.INVALID,!0))}}updateSize_(){const t=this.getConstants();if(!t)throw Error("Constants not found");const e=this.textGroup.childNodes;t.FIELD_TEXT_FONTSIZE,t.FIELD_TEXT_FONTWEIGHT,t.FIELD_TEXT_FONTFAMILY;let i=0,l=0;for(let o=0;o<e.length;o++){const s=e[o],r=c.utils.dom.getTextWidth(s);r>i&&(i=r),l+=t.FIELD_TEXT_HEIGHT+(o>0?t.FIELD_BORDER_RECT_Y_PADDING:0)}if(this.isBeingEdited_){const t=String(this.value_).split("\n"),e=c.utils.dom.createSvgElement(c.utils.Svg.TEXT,{class:"blocklyText blocklyMultilineText"});for(let l=0;l<t.length;l++){t[l].length>this.maxDisplayLength&&(t[l]=t[l].substring(0,this.maxDisplayLength)),e.textContent=t[l];const o=c.utils.dom.getTextWidth(e);o>i&&(i=o)}const l=this.htmlInput_;i+=l.offsetWidth-l.clientWidth}this.borderRect_&&(l+=2*t.FIELD_BORDER_RECT_Y_PADDING,i+=2*t.FIELD_BORDER_RECT_X_PADDING+1,this.borderRect_.setAttribute("width",`${i}`),this.borderRect_.setAttribute("height",`${l}`)),this.size_.width=i,this.size_.height=l,this.positionBorderRect_()}showEditor_(t,e){super.showEditor_(t,e),this.forceRerender()}widgetCreate_(){const t=c.WidgetDiv.getDiv(),e=this.workspace_.getScale(),i=this.getConstants();if(!i)throw Error("Constants not found");const l=document.createElement("textarea");l.className="blocklyHtmlInput blocklyHtmlTextAreaInput",l.setAttribute("spellcheck",String(this.spellcheck_));const o=i.FIELD_TEXT_FONTSIZE*e+"pt";t.style.fontSize=o,l.style.fontSize=o;const s=c.FieldTextInput.BORDERRADIUS*e+"px";l.style.borderRadius=s;const r=i.FIELD_BORDER_RECT_X_PADDING*e,n=i.FIELD_BORDER_RECT_Y_PADDING*e/2;l.style.padding=n+"px "+r+"px "+n+"px "+r+"px";const a=i.FIELD_TEXT_HEIGHT+i.FIELD_BORDER_RECT_Y_PADDING;return l.style.lineHeight=a*e+"px",t.appendChild(l),l.value=l.defaultValue=this.getEditorText_(this.value_),l.setAttribute("data-untyped-default-value",String(this.value_)),l.setAttribute("data-old-value",""),c.utils.userAgent.GECKO?setTimeout(this.resizeEditor_.bind(this),0):this.resizeEditor_(),this.bindInputEvents_(l),l}setMaxLines(t){"number"==typeof t&&t>0&&t!==this.maxLines_&&(this.maxLines_=t,this.forceRerender())}getMaxLines(){return this.maxLines_}onHtmlInputKeyDown_(t){"Enter"!==t.key&&super.onHtmlInputKeyDown_(t)}static fromJson(t){return new this(c.utils.parsing.replaceMessageReferences(t.text),void 0,t)}}function y(){c.fieldRegistry.register("field_multilinetext",f)}c.Css.register("\n.blocklyHtmlTextAreaInput {\n font-family: monospace;\n resize: none;\n overflow: hidden;\n height: 100%;\n text-align: left;\n}\n\n.blocklyHtmlTextAreaInputOverflowedY {\n overflow-y: scroll;\n}\n");const g="text_multiline",A={type:g,message0:"%1 %2",args0:[{type:"field_image",src:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAARCAYAAADpPU2iAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAdhgAAHYYBXaITgQAAABh0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMS42/U4J6AAAAP1JREFUOE+Vks0KQUEYhjmRIja4ABtZ2dm5A3t3Ia6AUm7CylYuQRaUhZSlLZJiQbFAyRnPN33y01HOW08z8873zpwzM4F3GWOCruvGIE4/rLaV+Nq1hVGMBqzhqlxgCys4wJA65xnogMHsQ5lujnYHTejBBCK2mE4abjCgMGhNxHgDFWjDSG07kdfVa2pZMf4ZyMAdWmpZMfYOsLiDMYMjlMB+K613QISRhTnITnsYg5yUd0DETmEoMlkFOeIT/A58iyK5E18BuTBfgYXfwNJv4P9/oEBerLylOnRhygmGdPpTTBZAPkde61lbQe4moWUvYUZYLfUNftIY4zwA5X2Z9AYnQrEAAAAASUVORK5CYII=",width:12,height:17,alt:"¶"},{type:"field_multilinetext",name:"TEXT",text:""}],output:"String",style:"text_blocks",helpUrl:"%{BKY_TEXT_TEXT_HELPURL}",tooltip:"%{BKY_TEXT_TEXT_TOOLTIP}",extensions:["parent_tooltip_when_inline"]};function m(t,e){const i=e.multiline_quote_(t.getFieldValue("TEXT")),l=-1!==i.indexOf("+")?h.Order.ADDITION:h.Order.ATOMIC;return[i,l]}function I(t,e){const i=e.multiline_quote_(t.getFieldValue("TEXT")),l=-1!==i.indexOf("+")?p.Order.ADDITIVE:p.Order.ATOMIC;return[i,l]}function x(t,e){const i=e.multiline_quote_(t.getFieldValue("TEXT")),l=-1!==i.indexOf("..")?_.Order.CONCATENATION:_.Order.ATOMIC;return[i,l]}function D(t,e){const i=e.multiline_quote_(t.getFieldValue("TEXT")),l=-1!==i.indexOf(".")?T.Order.STRING_CONCAT:T.Order.ATOMIC;return[i,l]}function b(t,e){const i=e.multiline_quote_(t.getFieldValue("TEXT")),l=-1!==i.indexOf("+")?E.Order.ADDITIVE:E.Order.ATOMIC;return[i,l]}const O=c.common.createBlockDefinitionsFromJsonArray([A]),k=O[g];function v(t={}){y(),c.common.defineBlocks(O),t.javascript&&(t.javascript.forBlock[g]=m),t.dart&&(t.dart.forBlock[g]=I),t.lua&&(t.lua.forBlock[g]=x),t.php&&(t.php.forBlock[g]=D),t.python&&(t.python.forBlock[g]=b)}const R=v;return u})());
//# sourceMappingURL=index.js.map