UNPKG

@wangeditor-next/editor-for-react

Version:
3 lines (2 loc) 3.05 kB
import*as t from"@wangeditor-next/editor";import{createEditor as e}from"@wangeditor-next/editor";import r,{useRef as n,useState as o,useCallback as a,useEffect as l}from"react";import i from"react-dom";var u=function(){return u=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},u.apply(this,arguments)};function c(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var n,o,a=r.call(t),l=[];try{for(;(void 0===e||e-- >0)&&!(n=a.next()).done;)l.push(n.value)}catch(t){o={error:t}}finally{try{n&&!n.done&&(r=a.return)&&r.call(a)}finally{if(o)throw o.error}}return l}function d(t){var d=t.defaultContent,f=void 0===d?[]:d,v=t.onCreated,s=t.defaultHtml,m=void 0===s?"":s,g=t.value,y=void 0===g?"":g,p=t.onChange,b=t.defaultConfig,h=void 0===b?{}:b,_=t.mode,C=void 0===_?"default":_,w=t.style,x=void 0===w?{}:w,E=t.className,H=t.loading,N=void 0!==H&&H,S=t.loadingText,j=void 0===S?"Loading...":S,A=n(null),O=c(o(null),2),T=O[0],D=O[1],I=n(""),P=n(!1),k=c(o(null),2),z=k[0],L=k[1],q=a(function(t){A.current=t,D(t)},[]),B=a(function(t){v&&v(t);var e=h.onCreated;e&&e(t)},[h,v]),F=a(function(t){var e=h.onDestroyed;L(null),e&&e(t)},[h]);return l(function(){if(null!=z)return z.__react_on_change=function(t){var e=t.getHtml(),r=I.current;if(I.current=e,!P.current&&e!==r){p&&p(t);var n=h.onChange;n&&n(t)}},function(){z.__react_on_change=void 0}},[z,h,p]),l(function(){if(null!=z&&y!==I.current)try{P.current=!0,z.setHtml(y),I.current=z.getHtml()}catch(t){console.error(t)}finally{P.current=!1}},[z,y]),l(function(){var t;if(null!=A.current&&null==z&&!(null===(t=A.current)||void 0===t?void 0:t.getAttribute("data-w-e-textarea"))){var r=e({selector:A.current,config:u(u({},h),{onCreated:B,onChange:function(t){var e;return null===(e=null==r?void 0:r.__react_on_change)||void 0===e?void 0:e.call(r,t)},onDestroyed:F}),content:f,html:m||y,mode:C});I.current=r.getHtml(),L(r)}},[z,h,f,m,B,F,C,y]),r.createElement("div",{ref:q,style:u(u({},x),{position:x.position||"relative"}),className:E,"data-w-e-react-editor-container":"true"},N&&T&&i.createPortal(r.createElement("div",{"data-w-e-loading-overlay":"true",style:{position:"absolute",top:0,right:0,bottom:0,left:0,display:"flex",alignItems:"center",justifyContent:"center",background:"rgba(255, 255, 255, 0.6)",zIndex:10}},j),T))}function f(e){var o=e.editor,a=e.defaultConfig,i=void 0===a?{}:a,u=e.mode,c=void 0===u?"default":u,d=e.style,f=void 0===d?{}:d,v=e.className,s=n(null);return l(function(){var e;if(null!=s.current&&null!=o&&!(null===(e=s.current)||void 0===e?void 0:e.getAttribute("data-w-e-toolbar"))){var r=t.createToolbar({editor:o,selector:s.current,config:i,mode:c});return function(){var t;r&&(r.destroy(),null===(t=s.current)||void 0===t||t.removeAttribute("data-w-e-toolbar"))}}},[o,i,c]),r.createElement("div",{style:f,ref:s,className:v})}"function"==typeof SuppressedError&&SuppressedError;export{d as Editor,f as Toolbar}; //# sourceMappingURL=index.mjs.map