UNPKG

@figlinq/react-chart-editor

Version:
2 lines 6.05 kB
import{Component}from"react";import PropTypes from"prop-types";import{CompositeDecorator,Editor,EditorState,Entity,RichUtils}from"draft-js";import{stateToHTML}from"@plotly/draft-js-export-html";import{stateFromHTML}from"draft-js-import-html";import{SUPERSCRIPT,SUBSCRIPT,LINK,STYLES_TO_HTML_TAGS,STYLE_MAP,INLINE_STYLES}from"./configuration";import LinkDecorator from"./LinkDecorator";import LinkEditor from"./LinkEditor";import StyleButtonGroup from"./StyleButtonGroup";import debounce from"./debounce";import{getEntityKeyAt,getEntityByKey,toggleInlineStyle,handleKeyCommand,insertSoftNewline,cursorHasLink}from"./DraftCommands";import{findLinkEntities}from"./decoratorStrategies";import getCoordinates from"./getSelectionCoordinates";import{jsx as _jsx,jsxs as _jsxs}from"react/jsx-runtime";class RichText extends Component{constructor(props,context){super(props,context);this.state={editorState:props.value.toString().trim().length?this.createEditorStateFromHTML(props.value):EditorState.createEmpty(this.getDecorator())};this.getDecorator=this.getDecorator.bind(this);this.createEditorStateFromHTML=this.createEditorStateFromHTML.bind(this);this.getEditorStateAsHTML=this.getEditorStateAsHTML.bind(this);this.focus=this.focus.bind(this);this.getParentContainerVerticalOffset=this.getParentContainerVerticalOffset.bind(this);this.onChange=this.onChange.bind(this);this.onBlur=this.onBlur.bind(this);this.onFocus=this.onFocus.bind(this);this.onLinkEditorBlur=this.onLinkEditorBlur.bind(this);this.onLinkEditorFocus=this.onLinkEditorFocus.bind(this);this.onLinkEditorChange=this.onLinkEditorChange.bind(this);this.onLinkEditorClose=this.onLinkEditorClose.bind(this);this.onKeyCommand=this.onKeyCommand.bind(this);this.onReturnPressed=this.onReturnPressed.bind(this);this.onStyleButtonToggle=this.onStyleButtonToggle.bind(this);this.renderLinkEditor=this.renderLinkEditor.bind(this)}UNSAFE_componentWillReceiveProps(nextProps){const{linkEditorFocus,editorFocus}=this.state;if(linkEditorFocus||editorFocus){return}const editorState=this.createEditorStateFromHTML(nextProps.value);this.setState({editorState})}shouldComponentUpdate(nextProps,nextState){const{placeholder,value}=this.props;const{editorState,linkEditorFocus}=this.state;if(linkEditorFocus||placeholder!==nextProps.placeholder||value!==nextProps.value||editorState!==nextState.editorState){return true}return nextProps.value!==this.getEditorStateAsHTML(editorState)}getDecorator(){return new CompositeDecorator([{strategy:findLinkEntities,component:LinkDecorator,props:{style:STYLE_MAP[LINK]}}])}createEditorStateFromHTML(html){const contentState=stateFromHTML(html,{inlineStyles:{[SUPERSCRIPT]:{element:"sup"},[SUBSCRIPT]:{element:"sub"}},defaultBlockTag:null});const decorator=this.getDecorator();EditorState.createWithContent(contentState);return EditorState.createWithContent(contentState,decorator)}getEditorStateAsHTML(editorState){const contentState=editorState.getCurrentContent();return stateToHTML(contentState,{defaultBlockTag:null,inlineStyles:STYLES_TO_HTML_TAGS})}focus(){this.editorInput.focus()}getParentContainerVerticalOffset(){return document.querySelector(".panel").scrollTop}onChange(editorState){const{selectedLinkID}=this.state;const selection=editorState.getSelection();const entityKey=getEntityKeyAt(editorState,selection);const newState={editorState};if(!cursorHasLink(editorState,selection)){Object.assign(newState,{selectedLinkID:null})}else if(selectedLinkID!==entityKey){Object.assign(newState,{selectedLinkID:entityKey})}this.setState(newState);const htmlContent=this.getEditorStateAsHTML(editorState).replace(/<br>\n*/,"<br>");if(this.props.value!==htmlContent){debounce(this.props.onChange,[htmlContent])}}onBlur(){this.setState({editorFocus:false})}onFocus(){this.setState({editorFocus:true})}onLinkEditorBlur(){this.setState({linkEditorFocus:false})}onLinkEditorFocus(){this.setState({linkEditorFocus:true})}onLinkEditorChange(linkID,urlValue){const{editorState}=this.state;const selectionState=editorState.getSelection();Entity.replaceData(linkID,{url:urlValue});const updatedEditorState=RichUtils.toggleLink(editorState,selectionState,linkID);this.onChange(updatedEditorState)}onLinkEditorClose(){this.focus();this.setState({linkEditorFocus:false,selectedLinkID:null})}onKeyCommand(command){const newEditorState=handleKeyCommand(this.state.editorState,command);if(newEditorState){this.onChange(newEditorState);return true}return false}onReturnPressed(){const newEditorState=insertSoftNewline(this.state.editorState);this.onChange(newEditorState);return true}onStyleButtonToggle(inlineStyle){const newEditorState=toggleInlineStyle(this.state.editorState,inlineStyle);if(newEditorState){this.onChange(newEditorState)}}renderLinkEditor(selectedLinkID){if(!selectedLinkID){return null}const linkEntity=getEntityByKey(selectedLinkID);const linkURL=linkEntity.getData().url;const coordinates=getCoordinates();return _jsx(LinkEditor,{onFocus:this.onLinkEditorFocus,onURLChange:this.onLinkEditorChange,onBlur:this.onLinkEditorBlur,onClose:this.onLinkEditorClose,coordinates:coordinates,linkID:selectedLinkID,linkURL:linkURL})}render(){const{editorState,selectedLinkID}=this.state;const linkIsSelected=Boolean(selectedLinkID);return _jsxs("div",{className:"rich-text-editor__root",children:[_jsx(StyleButtonGroup,{styles:INLINE_STYLES,currentStyle:editorState.getCurrentInlineStyle(),linkIsSelected:linkIsSelected,onToggle:this.onStyleButtonToggle}),_jsx("div",{className:"rich-text-editor__editor",onClick:this.focus,children:_jsx(Editor,{customStyleMap:STYLE_MAP,editorState:editorState,handleReturn:this.onReturnPressed,handleKeyCommand:this.onKeyCommand,onChange:this.onChange,onBlur:this.onBlur,onFocus:this.onFocus,placeholder:this.props.placeholder,spellCheck:false,ref:input=>this.editorInput=input})}),this.renderLinkEditor(selectedLinkID)]})}}RichText.propTypes={onChange:PropTypes.func.isRequired,placeholder:PropTypes.string,value:PropTypes.any};RichText.defaultProps={placeholder:"",value:""};export default RichText; //# sourceMappingURL=index.js.map