UNPKG

react-editor-z

Version:

React wrapper for @monaco-editor. Default json.

2 lines (1 loc) 2.46 kB
import r,{useState as n,useRef as e,useEffect as t,useCallback as o}from"react";import a from"@monaco-editor/react";export{useMonaco}from"@monaco-editor/react";var i=function(){return i=Object.assign||function(r){for(var n,e=1,t=arguments.length;e<t;e++)for(var o in n=arguments[e])Object.prototype.hasOwnProperty.call(n,o)&&(r[o]=n[o]);return r},i.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var u=function(u){var c=u.language,l=void 0===c?"json":c,f=u.value,s=u.options,p=u.readOnly,d=u.rawJson,y=u.style,g=u.getRefs,v=u.onChange,O=function(r,n){var e={};for(var t in r)Object.prototype.hasOwnProperty.call(r,t)&&n.indexOf(t)<0&&(e[t]=r[t]);if(null!=r&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(t=Object.getOwnPropertySymbols(r);o<t.length;o++)n.indexOf(t[o])<0&&Object.prototype.propertyIsEnumerable.call(r,t[o])&&(e[t[o]]=r[t[o]])}return e}(u,["language","value","options","readOnly","rawJson","style","getRefs","onChange"]),m=n(""),h=m[0],b=m[1],w=e(null),S=e(!1),j=function(){var r=e(!1);return t((function(){return r.current=!0,function(){r.current=!1}}),[]),o((function(){return r.current}),[])}(),x="json"==="".concat(l).toLowerCase().trim(),C=(null==s?void 0:s.readOnly)||p,E=function(r){try{return x?JSON.stringify(JSON.parse(r),null,(null==s?void 0:s.tabSize)||4):r}catch(n){return r}},J=function(r,n){void 0===n&&(n=!1),v&&v(n?void 0:r,n)};return t((function(){j()&&(S.current=function(r){return null==r||("string"==typeof r?""===r.trim():Array.isArray(r)?0===r.length:"object"==typeof r&&0===Object.keys(r).length)}(f),x?b(E(f)):(S.current=!1,b(f)))}),[]),t((function(){if(j())if(S.current)S.current=!1;else{var r=E(f);b(r)}}),[j,f,S.current]),t((function(){var r;w.current&&(null===(r=w.current.getAction("editor.action.formatDocument"))||void 0===r||r.run(),g&&g(w))}),[w.current]),r.createElement("div",{style:i({borderRadius:"6px",boxShadow:"0 0 0 1px #d9d9d9",padding:"8px"},y||{})},r.createElement(a,i({theme:"vs-light",width:"100%",height:"300px",language:l,options:i({cursorStyle:"line",formatOnPaste:!0,formatOnType:!0,renderLineHighlight:"none",wordWrap:"on",autoIndent:"full",disableLayerHinting:!0,fontSize:14,readOnly:C,fontFamily:"Consolas, 'Courier New', monospace"},s||{})},O,{value:h,onChange:function(r){if(!C&&!S.current){if(x)try{JSON.parse(r),S.current=!0,J(r)}catch(n){J(d?r:void 0,!d)}else J(r);b(r)}},onMount:function(r){w.current=r}})))};export{u as CodeEditor,u as default};