@blockly/field-multilineinput
Version:
3 lines • 12.2 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 n in i)("object"==typeof exports?exports:t)[n]=i[n]}}(this,(t,e,i,n,l,s)=>(()=>{"use strict";var o={370(e){e.exports=t},379(t){t.exports=i},127(t){t.exports=e},157(t){t.exports=n},537(t){t.exports=l},557(t){t.exports=s}},r={};function a(t){var e=r[t];if(void 0!==e)return e.exports;var i=r[t]={exports:{}};return o[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 c={};a.r(c),a.d(c,{FieldMultilineInput:()=>y,installAllBlocks:()=>L,registerFieldMultilineInput:()=>f,textMultiline:()=>d});var d={};a.r(d),a.d(d,{BLOCK_NAME:()=>g,blockDefinition:()=>v,installBlock:()=>k,toDart:()=>I,toJavascript:()=>b,toLua:()=>x,toPhp:()=>A,toPython:()=>D});var h=a(370),u=a(127),p=a(379),_=a(157),m=a(537),E=a(557);class y extends h.FieldTextInput{constructor(t,e,i){super(h.Field.SKIP_SETUP),this.textGroup=null,this.maxLines_=1/0,this.isOverflowedY_=!1,this.hintElement=null,this.cachedHintWidth=null,t!==h.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(y);return null!==t?t:this.getValue()}loadState(t){this.loadLegacyState(h.Field,t)||this.setValue(t)}initView(){this.createBorderRect_(),this.textGroup=h.utils.dom.createSvgElement(h.utils.Svg.G,{class:"blocklyEditableField"},this.fieldGroup_),this.fieldGroup_&&h.utils.dom.addClass(this.fieldGroup_,"blocklyField")}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 h.Field.NBSP;const i=e.split("\n");e="";const n=this.isOverflowedY_?this.maxLines_:i.length;for(let t=0;t<n;t++){let l=i[t];l.length>this.maxDisplayLength?l=l.substring(0,this.maxDisplayLength-4)+"...":this.isOverflowedY_&&t===n-1&&(l=l.substring(0,l.length-3)+"..."),l=l.replace(/\s/g,h.Field.NBSP),e+=l,t!==n-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 n=this.getConstants();if(!n)throw Error("Constants not found");const l=this.getDisplayText_().split("\n");let s=0;for(let t=0;t<l.length;t++){const e=n.FIELD_TEXT_HEIGHT+n.FIELD_BORDER_RECT_Y_PADDING;h.utils.dom.createSvgElement(h.utils.Svg.TEXT,{class:"blocklyText blocklyMultilineText",x:n.FIELD_BORDER_RECT_X_PADDING,y:s+n.FIELD_BORDER_RECT_Y_PADDING,dy:n.FIELD_TEXT_BASELINE},i).appendChild(document.createTextNode(l[t])),s+=e}if(this.isBeingEdited_){const t=this.htmlInput_;this.isOverflowedY_?h.utils.dom.addClass(t,"blocklyHtmlTextAreaInputOverflowedY"):h.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_?(h.utils.dom.removeClass(e,"blocklyInvalidInput"),h.utils.aria.setState(e,h.utils.aria.State.INVALID,!1)):(h.utils.dom.addClass(e,"blocklyInvalidInput"),h.utils.aria.setState(e,h.utils.aria.State.INVALID,!0))}}updateSize_(){var t;const e=this.getConstants();if(!e)throw Error("Constants not found");const i=this.textGroup.childNodes;let n=0,l=0;for(let t=0;t<i.length;t++){const s=i[t],o=h.utils.dom.getTextWidth(s);o>n&&(n=o),l+=e.FIELD_TEXT_HEIGHT+(t>0?e.FIELD_BORDER_RECT_Y_PADDING:0)}if(this.isBeingEdited_){const t=String(this.value_).split("\n"),i=h.utils.dom.createSvgElement(h.utils.Svg.TEXT,{class:"blocklyText blocklyMultilineText"});for(let e=0;e<t.length;e++){t[e].length>this.maxDisplayLength&&(t[e]=t[e].substring(0,this.maxDisplayLength)),i.textContent=t[e];const l=h.utils.dom.getTextWidth(i);l>n&&(n=l)}const s=this.htmlInput_;n+=s.offsetWidth-s.clientWidth,y.showHint&&(l+=e.FIELD_TEXT_HEIGHT+e.FIELD_BORDER_RECT_Y_PADDING)}if(this.borderRect_&&(l+=2*e.FIELD_BORDER_RECT_Y_PADDING,n+=2*e.FIELD_BORDER_RECT_X_PADDING+1),this.isBeingEdited_&&(n=Math.max(n,y.EDITOR_MIN_WIDTH),y.showHint&&(null===(t=this.hintElement)||void 0===t?void 0:t.isConnected))){const t=this.workspace_.getScale();if(!this.cachedHintWidth||this.cachedHintWidth.scale!==t){this.hintElement.style.width="max-content";const e=this.hintElement.offsetWidth;this.hintElement.style.width="",e>0&&(this.cachedHintWidth={scale:t,widthPx:e})}this.cachedHintWidth&&(n=Math.max(n,this.cachedHintWidth.widthPx/t))}this.borderRect_&&(this.borderRect_.setAttribute("width",`${n}`),this.borderRect_.setAttribute("height",`${l}`)),this.size_.width=n,this.size_.height=l,this.positionBorderRect_()}showEditor_(t,e){super.showEditor_(t,e),this.forceRerender()}widgetCreate_(){const t=h.WidgetDiv.getDiv(),e=this.workspace_.getScale(),i=this.getConstants();if(!i)throw Error("Constants not found");const n=document.createElement("textarea");n.className="blocklyHtmlInput blocklyHtmlTextAreaInput",n.setAttribute("spellcheck",String(this.spellcheck_));const l=i.FIELD_TEXT_FONTSIZE*e+"pt";t.style.fontSize=l,n.style.fontSize=l;const s=h.FieldTextInput.BORDERRADIUS*e+"px";n.style.borderRadius=s;const o=i.FIELD_BORDER_RECT_X_PADDING*e,r=i.FIELD_BORDER_RECT_Y_PADDING*e/2,a=i.FIELD_TEXT_HEIGHT+i.FIELD_BORDER_RECT_Y_PADDING,c=Math.ceil(a*e),d=y.showHint?r+c:r;return n.style.padding=`${r}px ${o}px ${d}px ${o}px`,n.style.lineHeight=a*e+"px",t.appendChild(n),n.value=n.defaultValue=this.getEditorText_(this.value_),n.setAttribute("data-untyped-default-value",String(this.value_)),n.setAttribute("data-old-value",""),h.utils.userAgent.GECKO?setTimeout(this.resizeEditor_.bind(this),0):this.resizeEditor_(),this.bindInputEvents_(n),y.showHint&&(this.hintElement=this.createHint(c),this.hintElement.style.fontFamily=i.FIELD_TEXT_FONTFAMILY,t.appendChild(this.hintElement)),n}createHint(t){const e=document.createElement("div");e.className="blocklyMultilineHint",e.setAttribute("aria-hidden","true"),e.style.height=t+"px";const i=document.createElement("div");i.className="blocklyMultilineHintGroup",i.appendChild(this.createHintKeycap("⏎")),i.appendChild(this.createHintColon()),i.appendChild(this.createHintLabel(y.enterCommits?"commit":"newline")),e.appendChild(i);const n=document.createElement("div");return n.className="blocklyMultilineHintGroup",n.appendChild(this.createHintKeycap("⇧")),n.appendChild(this.createHintKeycap("⏎")),n.appendChild(this.createHintColon()),n.appendChild(this.createHintLabel(y.enterCommits?"newline":"commit")),e.appendChild(n),e}createHintColon(){const t=document.createElement("span");return t.className="blocklyMultilineHintColon",t.textContent=":",t}createHintKeycap(t){const e=document.createElement("span");return e.className="blocklyMultilineHintKey",e.textContent=t,e}createHintLabel(t){const e=document.createElement("span");return e.className="blocklyMultilineHintLabel",e.textContent="commit"===t?h.Msg.FIELD_MULTILINEINPUT_FINISH_EDITING||"Finish editing":h.Msg.FIELD_MULTILINEINPUT_NEW_LINE||"New line",e}setMaxLines(t){"number"==typeof t&&t>0&&t!==this.maxLines_&&(this.maxLines_=t,this.forceRerender())}getMaxLines(){return this.maxLines_}onHtmlInputKeyDown_(t){if("Enter"===t.key){if(t.isComposing)return;const e=t.shiftKey;(y.enterCommits?!e:e)?super.onHtmlInputKeyDown_(t):(this.insertNewline(),t.preventDefault(),t.stopPropagation())}else super.onHtmlInputKeyDown_(t)}insertNewline(){var t,e;const i=this.htmlInput_;if(!i)return;const n=null!==(t=i.selectionStart)&&void 0!==t?t:i.value.length,l=null!==(e=i.selectionEnd)&&void 0!==e?e:n;i.value=i.value.substring(0,n)+"\n"+i.value.substring(l),i.selectionStart=i.selectionEnd=n+1,i.dispatchEvent(new Event("input"))}static fromJson(t){return new this(h.utils.parsing.replaceMessageReferences(t.text),void 0,t)}}function f(){h.fieldRegistry.register("field_multilinetext",y)}y.EDITOR_MIN_WIDTH=150,y.enterCommits=!0,y.showHint=!0,h.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\n.blocklyMultilineHint {\n position: absolute;\n bottom: 0;\n left: 0;\n width: 100%;\n display: flex;\n justify-content: space-around;\n align-items: center;\n padding: 0 4px;\n box-sizing: border-box;\n font-size: 0.85em;\n color: rgba(0, 0, 0, 0.6);\n background-color: white;\n border-top: 1px solid rgba(0, 0, 0, 0.15);\n border-radius: 0 0 4px 4px;\n user-select: none;\n pointer-events: none;\n}\n\n.blocklyMultilineHintGroup {\n display: inline-flex;\n align-items: center;\n}\n\n.blocklyMultilineHintKey {\n display: inline-flex;\n align-items: center;\n justify-content: center;\n min-width: 1.3em;\n height: 1.3em;\n padding: 0 0.25em;\n margin: 0 0.1em;\n border: 1px solid rgba(0, 0, 0, 0.35);\n border-radius: 3px;\n background-color: #f5f5f5;\n line-height: 1;\n box-sizing: border-box;\n}\n\n.blocklyMultilineHintColon {\n margin: 0 0.15em;\n}\n\n.blocklyMultilineHintLabel {\n white-space: nowrap;\n}\n");const g="text_multiline",T={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 b(t,e){const i=e.multiline_quote_(t.getFieldValue("TEXT")),n=-1!==i.indexOf("+")?u.Order.ADDITION:u.Order.ATOMIC;return[i,n]}function I(t,e){const i=e.multiline_quote_(t.getFieldValue("TEXT")),n=-1!==i.indexOf("+")?p.Order.ADDITIVE:p.Order.ATOMIC;return[i,n]}function x(t,e){const i=e.multiline_quote_(t.getFieldValue("TEXT")),n=-1!==i.indexOf("..")?_.Order.CONCATENATION:_.Order.ATOMIC;return[i,n]}function A(t,e){const i=e.multiline_quote_(t.getFieldValue("TEXT")),n=-1!==i.indexOf(".")?m.Order.STRING_CONCAT:m.Order.ATOMIC;return[i,n]}function D(t,e){const i=e.multiline_quote_(t.getFieldValue("TEXT")),n=-1!==i.indexOf("+")?E.Order.ADDITIVE:E.Order.ATOMIC;return[i,n]}const C=h.common.createBlockDefinitionsFromJsonArray([T]),v=C[g];function k(t={}){f(),h.common.defineBlocks(C),t.javascript&&(t.javascript.forBlock[g]=b),t.dart&&(t.dart.forBlock[g]=I),t.lua&&(t.lua.forBlock[g]=x),t.php&&(t.php.forBlock[g]=A),t.python&&(t.python.forBlock[g]=D)}const L=k;return c})());
//# sourceMappingURL=index.js.map