UNPKG

fastlion-amis

Version:

一种MIS页面生成工具

2 lines 13.3 kB
;/*!src/components/WangEditor.tsx*/ amis.define("accacc1",(function(t,e,n,l){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var o=t("9cad868"),a=(0,o.__importDefault)(t("11828c4")),i=t("809192b"),r=t("4e30285"),u=(0,o.__importDefault)(t("2ca21c7")),d=(0,o.__importDefault)(t("028a09d")),s=(0,o.__importDefault)(t("f410837")),c="TRANSLATEOLD",p="TRANSLATERULES",m=/^[-+]?\d+(\.\d+)?$/,f=/[¥$€元][-+]?\d*(\.\d+)?/,v=function(t){function e(){var e,n,l,o=t.apply(this,arguments)||this;return o.editorRef=a.default.createRef(),o.state={editor:null,originModel:"",clickCount:0,selectTempLate:null===(e=JSON.parse(localStorage.getItem(p)))||void 0===e?void 0:e.lastActiveRule,templateName:null===(n=JSON.parse(localStorage.getItem(p)))||void 0===n?void 0:n.lastActiveRule,showEmpty:null===(l=JSON.parse(localStorage.getItem(p)))||void 0===l?void 0:l.showEmpty},o.watchInput=function(t){"KeyE"===t.code&&o.setState({clickCount:o.state.clickCount+1}),o.state.clickCount>3&&localStorage.setItem(c,"1")},o.edit=!1,o.FontList=[{name:"小六(6.5)",value:"8px"},{name:"六号(7.5)",value:"10px"},{name:"小五(9)",value:"12px"},{name:"五号(10.5)",value:"14px"},{name:"小四(12)",value:"16px"},{name:"四号(14)",value:"18px"},{name:"小三(15)",value:"20px"},{name:"三号(16)",value:"21px"},{name:"小二(18)",value:"24px"},{name:"二号(22)",value:"29px"},{name:"小一(24)",value:"32px"},{name:"一号(26)",value:"34px"},{name:"小初(36)",value:"48px"},{name:"初号(42)",value:"59px"},"13px","15px","19px","22px","40px",{name:"10",value:"10pt"},{name:"30",value:"30pt"},{name:"50",value:"50pt"},{name:"70",value:"70pt"}],o}return(0,o.__extends)(e,t),e.prototype.componentDidMount=function(){this.setState({originModel:this.props.model}),document.addEventListener("keydown",this.watchInput),window.editorInstance=this},e.prototype.componentWillUnmount=function(){document.removeEventListener("keydown",this.watchInput)},e.prototype.componentDidUpdate=function(t,e,n){},e.prototype.strToHtmlEl=function(t){var e=document.createElement("div");return e.innerHTML=t,e},e.prototype.structuralTextDataToDescripbe=function(t,e){var n=this.structuralTextData(t),l={start:"居左",center:"居中",right:"居右"},o=e+"\n";return n.map((function(t,e){var n,a="第"+(e+1)+"行 "+(t.textAlign?"文本位置:"+l[t.textAlign||""]:"")+" 行高:"+(t.textLineHeight||"1")+" "+(null!==(n=t.textIndent)&&void 0!==n?n:"缩进"+t.textIndent)+"\n";t.children.map((function(t,e){var n=" 第"+(e+1)+"部分 字体:"+(t.textFont||"平台默认字体")+" 字号:"+(t.textSize||"平台字号")+" "+(t.bold?"加粗":"")+" "+(t.underLine?"下划线":"")+" 颜色:"+(t.color||"默认颜色")+" 文字前空格:"+(t.afterWhiteSpceCount||0)+" 文字后空格:"+(t.preWhiteSpceCount||0)+" 原文字:"+t.originText+" |";a+=n+"\t"})),o+=a+="\n"})),o},e.prototype.structuralTextData=function(t){var e=this.strToHtmlEl(t),n=function(t){var e=Array.from(t),n=[];e.map((function(t){var e=t.textContent,l=t.nodeName,a={nodeType:"text",children:[]},i=[];(null==e?void 0:e.trim().split(" "))&&!/^\s+$/.test(e)?i.push.apply(i,null==e?void 0:e.trim().split(/\s+/)):i.push(t),i.map((function(e,r){var u,d,s,c,p,v,h,g,x,y,T,S,C=(null===(u=String(t.textContent))||void 0===u?void 0:u.trim().match(/\s+/g))||[],E=e.textContent||e;if((null===(s=null===(d=String(E))||void 0===d?void 0:d.trim)||void 0===s?void 0:s.call(d))||"string"!=typeof e){var N=(0,o.__assign)({},a);if("#text"!==l){var L=t.style;N.bold=t.innerHTML.includes("<strong>"),N.underLine=t.innerHTML.includes("<u>"),N.textFont=L.fontFamily,N.textSize=L.fontSize,N.color=L.color,N.bgColor=L.backgroundColor,N.originTextWidth=(null==E?void 0:E.length)||0,N.originText="string"==typeof E&&E||"",N.preWhiteSpceCount=0===r?null===(v=null===(p=null===(c=null==t?void 0:t.textContent)||void 0===c?void 0:c.match(/^\s+/g))||void 0===p?void 0:p[0])||void 0===v?void 0:v.length:(null===(h=null==C?void 0:C.shift())||void 0===h?void 0:h.length)||0,N.afterWhiteSpceCount=r===i.length-1?null===(y=null===(x=null===(g=null==t?void 0:t.textContent)||void 0===g?void 0:g.match(/\s+$/g))||void 0===x?void 0:x[0])||void 0===y?void 0:y.length:0,m.test((null===(T=null==E?void 0:E.trim)||void 0===T?void 0:T.call(E))||"")&&(N.textType="pureNumber"),f.test((null===(S=null==E?void 0:E.trim)||void 0===S?void 0:S.call(E))||"")&&(N.textType="purePrice")}n.push(N)}}))}));var l=[],a=0;return n.map((function(t){var e;/^\s+$/.test(t.originText||"")?a+=(null===(e=t.originText)||void 0===e?void 0:e.length)||0:(t.preWhiteSpceCount=(t.preWhiteSpceCount||0)+a,a=0,l.push(t),t.afterWhiteSpceCount&&(a+=t.afterWhiteSpceCount))})),l},l=[];return Array.from(e.children).map((function(t){var e=t.style;l.push({nodeType:"P",textLineHeight:e.lineHeight,textAlign:e.textAlign,textIndent:e.textIndent,children:n(t.childNodes)})})),l},e.prototype.transFormatToHtml=function(t,e,n){void 0===n&&(n=!0);var l=JSON.parse(localStorage.getItem(p)).templates.find((function(e){return e.name===t})),o=this.strToHtmlEl(""),a=0,i=e?[""]:["#a1c4fd","#d4fc79","#f5576c","#fa71cd","#008080","#9B59B6","#03A9F4"];if(1===(null==e?void 0:e.length)){var r=[];null==l||l.htmlTemp.map((function(t){var n,l;r.push((null===(n=null==e?void 0:e[0])||void 0===n?void 0:n.splice(0,null===(l=null==t?void 0:t.children)||void 0===l?void 0:l.length))||[])})),e=r}return null==l||l.htmlTemp.map((function(t,l){var r,u=document.createElement("p");if(u.style.lineHeight=t.textLineHeight||"",u.style.textAlign=t.textAlign||"",u.style.textIndent=t.textIndent||"",u.style.margin="0px",t.children.map((function(t,o){var r,d,s,c=document.createElement("span");if(s=c,t.underLine){var p=document.createElement("u");s.append(p),s=p}if(t.bold){p=document.createElement("strong");s.append(p),s=p}var m=""+new Array(t.preWhiteSpceCount).fill("&nbsp;").join("")+((t.lockOriginText?t.originText:null===(r=null==e?void 0:e[l])||void 0===r?void 0:r[o])||"");s.innerHTML=n?(null===(d=null==e?void 0:e[l])||void 0===d?void 0:d[o])?m:"组"+(o+1)+":"+new Array(t.preWhiteSpceCount).fill("&nbsp;").join("")+(t.originText||""):m,c.style.fontFamily=t.textFont||"",c.style.fontSize=t.textSize||"",c.style.color=t.color||"",c.style.backgroundColor=t.bgColor||i[a],++a>i.length&&(a=0),u.append(c)})),((null===(r=null==e?void 0:e[l])||void 0===r?void 0:r.length)||0)>t.children.length){var d=u.lastChild;(null==d?void 0:d.innerText)&&(d.innerText+=(null==e?void 0:e[l].slice(t.children.length).join(""))||"")}o.appendChild(u)})),o.innerHTML},e.prototype.addMarginToParagraphs=function(t){for(var e=this.strToHtmlEl(t),n=e.querySelectorAll("p"),l=0;l<n.length;l++)n[l].style.margin="0";return e.innerHTML},e.prototype.transFromEditor=function(){var t,e,n,l=null===(t=this.state.editor)||void 0===t?void 0:t.getHtml(),o=this.strToHtmlEl(l||""),a=[];for(Array.from(o.childNodes).map((function(t,e){var n,l,o=[];(null===(n=null==t?void 0:t.textContent)||void 0===n?void 0:n.includes("|"))?o.push.apply(o,(null===(l=null==t?void 0:t.textContent)||void 0===l?void 0:l.split("|"))||[]):Array.from(t.childNodes).map((function(t){var e=[],n=null==t?void 0:t.textContent;/^\s+$/.test(n||"")||((null==n?void 0:n.trim().split(" "))&&!/^\s+$/.test(n)?e.push.apply(e,null==n?void 0:n.trim().split(/\s+/)):e.push(n||""),o.push.apply(o,e))})),a.push(o)}));1===a[0].length&&!a[0][0];)a.shift();for(;1===a[a.length-1].length&&!a[a.length-1][0];)a.pop();console.log(a),null===(n=(e=this.props).onModelChange)||void 0===n||n.call(e,this.transFormatToHtml(this.state.selectTempLate,a,this.state.showEmpty))},e.prototype.render=function(){var t=this,e=this.props,n=e.model,l=e.onModelChange,m=e.readOnly,f=e.disabled,v=e.onRef,h="rich-text-editor keyDownClass";this.props.readOnly&&(h+=" readOnly"),this.props.disabled&&(h+=" disabled");var g=localStorage.getItem(c),x={lastActiveRule:"",showEmpty:!1,templates:[]},y=x;return g&&((y=JSON.parse(localStorage.getItem(p)))||(localStorage.setItem(p,JSON.stringify(x)),y=x)),a.default.createElement(a.default.Fragment,null,g?a.default.createElement("div",{style:{position:"absolute",zIndex:1,bottom:0,right:0}},a.default.createElement(r.Button,{onClick:function(){t.setState({clickCount:0}),localStorage.removeItem(c)}},"隐藏"),a.default.createElement(r.Popconfirm,{okText:"确定",cancelText:"取消",title:function(){return a.default.createElement(d.default,{html:t.transFormatToHtml(t.state.selectTempLate)})}},a.default.createElement(r.Button,{onClick:function(){return console.log(t.transFormatToHtml(t.state.selectTempLate))}},"预览")),a.default.createElement(r.Button,{onClick:function(){return(0,o.__awaiter)(t,void 0,void 0,(function(){return(0,o.__generator)(this,(function(t){switch(t.label){case 0:return navigator.clipboard?[4,navigator.clipboard.writeText(localStorage.getItem(p)||"")]:(r.message.error("不支持复制到剪切板"),[2]);case 1:return t.sent(),r.message.success("导出成功,ctrl+v粘贴使用"),[2]}}))}))}},"导出"),a.default.createElement(r.Popconfirm,{okText:"确定",cancelText:"取消",onConfirm:function(){var e=JSON.parse(localStorage.getItem(p)),n=JSON.parse(t.templateStr);if(!t.templateStr||"object"!=typeof n||!n.templates)return r.message.warn("模板不合法");e.templates=(0,o.__spreadArray)((0,o.__spreadArray)([],e.templates,!0),n.templates,!0);var l={};e.templates.map((function(t){l[t.name]=t})),e.templates=Object.values(l),localStorage.setItem(p,t.templateStr)},title:function(){return a.default.createElement(u.default,{onChange:function(e){t.templateStr=e.target.value},placeholder:"请输入导出时复制的模板"})}},a.default.createElement(r.Button,null,"导入")),a.default.createElement(r.Tooltip,{overlay:function(){return a.default.createElement("div",null,(y.showEmpty?"保留空值":"过滤空值")+",可以在文本中加入|符号进行手工分段,分段样式参照模板预览")}},a.default.createElement(r.Button,{onClick:function(){return t.transFromEditor()}},"应用模板")),a.default.createElement(r.Popconfirm,{title:function(){return a.default.createElement("div",null,"请输入模板名",a.default.createElement(u.default,{style:{position:"relative",marginTop:"8px",left:"-22px"},value:t.state.templateName,onChange:function(e){t.setState({templateName:e.target.value})}}))},okText:"确定",onConfirm:function(){var e,n=y.templates.findIndex((function(e){return e.name===t.state.templateName})),l={name:t.state.templateName,htmlTemp:t.structuralTextData((null===(e=t.state.editor)||void 0===e?void 0:e.getHtml())||"")};n>=0?y.templates[n]=l:y.templates.push(l),localStorage.setItem(p,JSON.stringify(y)),t.setState({selectTempLate:t.state.templateName})},cancelText:"取消"},a.default.createElement(r.Button,null,"抽出模板")),a.default.createElement(r.Popconfirm,{title:"是否确认删除模板",okText:"确定",cancelText:"取消",onConfirm:function(){var e=y.templates.findIndex((function(e){return e.name===t.state.selectTempLate}));e>=0&&(y.templates.splice(e,1),localStorage.setItem(p,JSON.stringify(y)),t.setState({selectTempLate:y.templates[0].name,templateName:y.templates[0].name}))}},a.default.createElement(r.Button,null,"删除选中模板")),a.default.createElement(r.Select,{style:{width:"150px"},value:this.state.selectTempLate||"请选择模板,无模板请抽出模板",onChange:function(e){y.lastActiveRule=e,t.setState({selectTempLate:e,templateName:e}),localStorage.setItem(p,JSON.stringify(y))},placeholder:"请选择模板,无模板请抽出模板",options:y.templates.map((function(t){return{lable:t.name,value:t.name}}))||[]})):null,a.default.createElement(s.default,{errorText:"富文本无法解析,请联系开发人员"},a.default.createElement("div",{className:h,style:{position:"relative"},ref:this.editorRef,onTouchMove:function(t){return t.stopPropagation()}},a.default.createElement(i.Toolbar,{className:"email-edit-content-body-toolbar",editor:this.state.editor,mode:"simple",defaultConfig:{toolbarKeys:["undo","redo","|","fontFamily","fontSize","lineHeight","|","underline","italic","color","bgColor","indent","delIndent","|","bulletedList","numberedList","justifyLeft","justifyRight","justifyCenter"]}}),a.default.createElement(i.Editor,{className:"email-edit-content-body-editor",defaultConfig:{hoverbarKeys:{text:{menuKeys:["fontFamily","fontSize","lineHeight","|","underline","italic","color","bgColor","indent","delIndent","|","justifyLeft","justifyRight","justifyCenter"]}},MENU_CONF:{fontFamily:{fontFamilyList:["黑体","仿宋","楷体","标楷体","华文仿宋","华文楷体","宋体","微软雅黑","Arial","Tahoma","Verdana","Times New Roman","Courier New","思源黑体 CN Bold","思源黑体 CN Heavy","思源黑体 CN Medium","思源黑体 CN Normal","思源黑体 CN Regular","si"]},fontSize:{fontSizeList:this.FontList},lineHeight:{lineHeightList:["0","0.125","0.25","0.375","0.5","0.75","1","1.25","1.5","1.75","2","2.25","2.5","5pt","10pt","15pt","20pt","25pt","30pt","35pt","40pt"]}},readOnly:m||f,placeholder:"请输入"},mode:"default",value:n,onCreated:function(e){var l;null===(l=t.state.editor)||void 0===l||l.getAllMenuKeys(),e.setHtml(n||""),t.setState({editor:e}),e&&e.focus()},onChange:function(e){null==l||l(t.addMarginToParagraphs(e.getHtml())),v&&v(e)}}))))},e.defaultProps={disabled:!1,readOnly:!1},e}(a.default.Component);e.default=v}));