@eeacms/react-chart-editor
Version:
plotly.js chart editor react component UI
2 lines • 8.31 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _react=_interopRequireWildcard(require("react"));var _propTypes=_interopRequireDefault(require("prop-types"));var _draftJs=require("draft-js");var _draftJsExportHtml=require("@plotly/draft-js-export-html");var _draftJsImportHtml=require("draft-js-import-html");var _configuration=require("./configuration");var _LinkDecorator=_interopRequireDefault(require("./LinkDecorator"));var _LinkEditor=_interopRequireDefault(require("./LinkEditor"));var _StyleButtonGroup=_interopRequireDefault(require("./StyleButtonGroup"));var _debounce=_interopRequireDefault(require("./debounce"));var _DraftCommands=require("./DraftCommands");var _decoratorStrategies=require("./decoratorStrategies");var _getSelectionCoordinates=_interopRequireDefault(require("./getSelectionCoordinates"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _interopRequireWildcard(e,t){if("function"==typeof WeakMap)var r=new WeakMap,n=new WeakMap;return(_interopRequireWildcard=function _interopRequireWildcard(e,t){if(!t&&e&&e.__esModule)return e;var o,i,f={__proto__:null,default:e};if(null===e||"object"!=typeof e&&"function"!=typeof e)return f;if(o=t?n:r){if(o.has(e))return o.get(e);o.set(e,f)}for(const t in e)"default"!==t&&{}.hasOwnProperty.call(e,t)&&((i=(o=Object.defineProperty)&&Object.getOwnPropertyDescriptor(e,t))&&(i.get||i.set)?o(f,t,i):f[t]=e[t]);return f})(e,t)}(function(){var enterModule=typeof reactHotLoaderGlobal!=="undefined"?reactHotLoaderGlobal.enterModule:undefined;enterModule&&enterModule(module)})();var __signature__=typeof reactHotLoaderGlobal!=="undefined"?reactHotLoaderGlobal.default.signature:function(a){return a};class RichText extends _react.Component{constructor(props,context){super(props,context);this.state={editorState:props.value.toString().trim().length?this.createEditorStateFromHTML(props.value):_draftJs.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 _draftJs.CompositeDecorator([{strategy:_decoratorStrategies.findLinkEntities,component:_LinkDecorator.default,props:{style:_configuration.STYLE_MAP[_configuration.LINK]}}])}createEditorStateFromHTML(html){const contentState=(0,_draftJsImportHtml.stateFromHTML)(html,{inlineStyles:{[_configuration.SUPERSCRIPT]:{element:"sup"},[_configuration.SUBSCRIPT]:{element:"sub"}},defaultBlockTag:null});const decorator=this.getDecorator();_draftJs.EditorState.createWithContent(contentState);return _draftJs.EditorState.createWithContent(contentState,decorator)}getEditorStateAsHTML(editorState){const contentState=editorState.getCurrentContent();return(0,_draftJsExportHtml.stateToHTML)(contentState,{defaultBlockTag:null,inlineStyles:_configuration.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=(0,_DraftCommands.getEntityKeyAt)(editorState,selection);const newState={editorState};if(!(0,_DraftCommands.cursorHasLink)(editorState,selection)){Object.assign(newState,{selectedLinkID:null})}else if(selectedLinkID!==entityKey){Object.assign(newState,{selectedLinkID:entityKey})}this.setState(newState);let htmlContent=this.getEditorStateAsHTML(editorState).replace(/<br>\n*/,"<br>");if(htmlContent==="<br>"||htmlContent===""){htmlContent=this.props.emptyValue}if(this.props.value!==htmlContent){(0,_debounce.default)(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();_draftJs.Entity.replaceData(linkID,{url:urlValue});const updatedEditorState=_draftJs.RichUtils.toggleLink(editorState,selectionState,linkID);this.onChange(updatedEditorState)}onLinkEditorClose(){this.focus();this.setState({linkEditorFocus:false,selectedLinkID:null})}onKeyCommand(command){const newEditorState=(0,_DraftCommands.handleKeyCommand)(this.state.editorState,command);if(newEditorState){this.onChange(newEditorState);return true}return false}onReturnPressed(){const newEditorState=(0,_DraftCommands.insertSoftNewline)(this.state.editorState);this.onChange(newEditorState);return true}onStyleButtonToggle(inlineStyle){const newEditorState=(0,_DraftCommands.toggleInlineStyle)(this.state.editorState,inlineStyle);if(newEditorState){this.onChange(newEditorState)}}renderLinkEditor(selectedLinkID){if(!selectedLinkID){return null}const linkEntity=(0,_DraftCommands.getEntityByKey)(selectedLinkID);const linkURL=linkEntity.getData().url;const coordinates=(0,_getSelectionCoordinates.default)();return _react.default.createElement(_LinkEditor.default,{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 _react.default.createElement("div",{className:"rich-text-editor__root"},_react.default.createElement(_StyleButtonGroup.default,{styles:_configuration.INLINE_STYLES,currentStyle:editorState.getCurrentInlineStyle(),linkIsSelected:linkIsSelected,onToggle:this.onStyleButtonToggle}),_react.default.createElement("div",{className:"rich-text-editor__editor",onClick:this.focus},_react.default.createElement(_draftJs.Editor,{customStyleMap:_configuration.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))}__reactstandin__regenerateByEval(key,code){this[key]=eval(code)}}RichText.propTypes={onChange:_propTypes.default.func.isRequired,placeholder:_propTypes.default.string,value:_propTypes.default.any,emptyValue:_propTypes.default.any};RichText.defaultProps={placeholder:"",value:"",emptyValue:null};const _default=RichText;var _default2=exports.default=_default;;(function(){var reactHotLoader=typeof reactHotLoaderGlobal!=="undefined"?reactHotLoaderGlobal.default:undefined;if(!reactHotLoader){return}reactHotLoader.register(RichText,"RichText","/react-chart-editor/src/components/widgets/text_editors/RichText/index.js");reactHotLoader.register(_default,"default","/react-chart-editor/src/components/widgets/text_editors/RichText/index.js")})();;(function(){var leaveModule=typeof reactHotLoaderGlobal!=="undefined"?reactHotLoaderGlobal.leaveModule:undefined;leaveModule&&leaveModule(module)})();
//# sourceMappingURL=index.js.map