@wpmvc/classic-editor
Version:
A React component for integrating WordPress' classic editor using wp.editor API
74 lines (61 loc) • 2.54 kB
JavaScript
(function(t,e){typeof exports=="object"&&typeof module<"u"?module.exports=e(require("react/jsx-runtime"),require("@wordpress/element"),require("styled-components")):typeof define=="function"&&define.amd?define(["react/jsx-runtime","@wordpress/element","styled-components"],e):(t=typeof globalThis<"u"?globalThis:t||self,t["classic-editor"]=e(t.jsxRuntime,t.wp.element,t.styledComponents))})(this,function(t,e,a){"use strict";const f=a.div`
${o=>o.$extend&&`
border: 1px solid #dadbdd;
border-radius: 8px;
overflow: hidden;
position: relative;
background-color: #fff;
.wp-editor-tabs,
.wp-media-buttons {
padding: 8px;
}
.wp-editor-container {
border: 0;
}
.wp-switch-editor {
height: auto;
margin: 0 0 0 4px;
background-color: transparent;
padding: 7px 14px;
font-size: 14px;
border-radius: 5px;
line-height: inherit;
border-color: transparent;
float: none;
font-weight: 500;
}
.tmce-active .switch-tmce,
.html-active .switch-html {
background-color: #ededed;
border-color: #dadbdd;
color: #1e1f21;
}
.mce-top-part {
background: #f1f1f0;
&:before {
box-shadow: none;
}
}
.mce-toolbar-grp {
border: 0;
}
.wpmvc-classic-editor {
padding: 15px;
border: none;
&:focus {
box-shadow: none;
border: none;
}
}
`}
.mce-toolbar .mce-btn i {
font-family: 'dashicons' !important;
}
.mce-ico {
font-family: 'tinymce', Arial !important;
}
.wpmvc-classic-editor {
visibility: visible !important;
width: 100%;
}
`;function p({value:o,onChange:u,height:l=250,useExtendStyles:m=!1,hasMedia:w=!0}){var c;const[s,b]=e.useState(!1),r=e.useRef(null),n=e.useMemo(()=>`wp_editor_${Date.now()+Math.floor(Math.random()*1e3)}`,[]),d=!!((c=window.wp)!=null&&c.editor),h=i=>{r.current=i,i.on("init",()=>{i.setContent(o)}),i.on("change",()=>{u(i.getContent())}),b(!0)},x=()=>{window.wp.editor.remove(n),window.wp.editor.initialize(n,{tinymce:{height:l,toolbar1:"formatselect,table,bold,italic,bullist,numlist,link,blockquote,alignleft,aligncenter,alignright,underline,strikethrough,forecolor,removeformat,codeformat,outdent,indent,undo,redo",menubar:!1,setup:h},quicktags:!0,mediaButtons:w})};return e.useEffect(()=>{r.current&&o!==r.current.getContent()&&r.current.setContent(o)},[o]),e.useEffect(()=>{s&&setTimeout(()=>{r.current.setContent(o)},500)},[s]),e.useEffect(()=>(d&&x(),()=>{d&&window.wp.editor.remove(n)}),[d]),t.jsx(f,{$extend:m,children:t.jsx("textarea",{className:"wpmvc-classic-editor",id:n})})}return p});