react-chart-editor
Version:
plotly.js chart editor react component UI
2 lines • 13.2 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(obj){return obj&&obj.__esModule?obj:{default:obj}}function _getRequireWildcardCache(e){if("function"!=typeof WeakMap)return null;var r=new WeakMap,t=new WeakMap;return(_getRequireWildcardCache=function _getRequireWildcardCache(e){return e?t:r})(e)}function _interopRequireWildcard(e,r){if(!r&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var t=_getRequireWildcardCache(r);if(t&&t.has(e))return t.get(e);var n={__proto__:null},a=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var u in e)if("default"!==u&&Object.prototype.hasOwnProperty.call(e,u)){var i=a?Object.getOwnPropertyDescriptor(e,u):null;i&&(i.get||i.set)?Object.defineProperty(n,u,i):n[u]=e[u]}return n.default=e,t&&t.set(e,n),n}(function(){var enterModule=typeof reactHotLoaderGlobal!=="undefined"?reactHotLoaderGlobal.enterModule:undefined;enterModule&&enterModule(module)})();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,_toPropertyKey(descriptor.key),descriptor)}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);Object.defineProperty(Constructor,"prototype",{writable:false});return Constructor}function _toPropertyKey(arg){var key=_toPrimitive(arg,"string");return typeof key==="symbol"?key:String(key)}function _toPrimitive(input,hint){if(typeof input!=="object"||input===null)return input;var prim=input[Symbol.toPrimitive];if(prim!==undefined){var res=prim.call(input,hint||"default");if(typeof res!=="object")return res;throw new TypeError("@@toPrimitive must return a primitive value.")}return(hint==="string"?String:Number)(input)}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function")}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,writable:true,configurable:true}});Object.defineProperty(subClass,"prototype",{writable:false});if(superClass)_setPrototypeOf(subClass,superClass)}function _setPrototypeOf(o,p){_setPrototypeOf=Object.setPrototypeOf?Object.setPrototypeOf.bind():function _setPrototypeOf(o,p){o.__proto__=p;return o};return _setPrototypeOf(o,p)}function _createSuper(Derived){var hasNativeReflectConstruct=_isNativeReflectConstruct();return function _createSuperInternal(){var Super=_getPrototypeOf(Derived),result;if(hasNativeReflectConstruct){var NewTarget=_getPrototypeOf(this).constructor;result=Reflect.construct(Super,arguments,NewTarget)}else{result=Super.apply(this,arguments)}return _possibleConstructorReturn(this,result)}}function _possibleConstructorReturn(self,call){if(call&&(typeof call==="object"||typeof call==="function")){return call}else if(call!==void 0){throw new TypeError("Derived constructors may only return object or undefined")}return _assertThisInitialized(self)}function _assertThisInitialized(self){if(self===void 0){throw new ReferenceError("this hasn't been initialised - super() hasn't been called")}return self}function _isNativeReflectConstruct(){if(typeof Reflect==="undefined"||!Reflect.construct)return false;if(Reflect.construct.sham)return false;if(typeof Proxy==="function")return true;try{Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}));return true}catch(e){return false}}function _getPrototypeOf(o){_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf.bind():function _getPrototypeOf(o){return o.__proto__||Object.getPrototypeOf(o)};return _getPrototypeOf(o)}var __signature__=typeof reactHotLoaderGlobal!=="undefined"?reactHotLoaderGlobal.default.signature:function(a){return a};var RichText=function(_Component){_inherits(RichText,_Component);var _super=_createSuper(RichText);function RichText(props,context){var _this;_classCallCheck(this,RichText);_this=_super.call(this,props,context);_this.state={editorState:props.value.toString().trim().length?_this.createEditorStateFromHTML(props.value):_draftJs.EditorState.createEmpty(_this.getDecorator())};_this.getDecorator=_this.getDecorator.bind(_assertThisInitialized(_this));_this.createEditorStateFromHTML=_this.createEditorStateFromHTML.bind(_assertThisInitialized(_this));_this.getEditorStateAsHTML=_this.getEditorStateAsHTML.bind(_assertThisInitialized(_this));_this.focus=_this.focus.bind(_assertThisInitialized(_this));_this.getParentContainerVerticalOffset=_this.getParentContainerVerticalOffset.bind(_assertThisInitialized(_this));_this.onChange=_this.onChange.bind(_assertThisInitialized(_this));_this.onBlur=_this.onBlur.bind(_assertThisInitialized(_this));_this.onFocus=_this.onFocus.bind(_assertThisInitialized(_this));_this.onLinkEditorBlur=_this.onLinkEditorBlur.bind(_assertThisInitialized(_this));_this.onLinkEditorFocus=_this.onLinkEditorFocus.bind(_assertThisInitialized(_this));_this.onLinkEditorChange=_this.onLinkEditorChange.bind(_assertThisInitialized(_this));_this.onLinkEditorClose=_this.onLinkEditorClose.bind(_assertThisInitialized(_this));_this.onKeyCommand=_this.onKeyCommand.bind(_assertThisInitialized(_this));_this.onReturnPressed=_this.onReturnPressed.bind(_assertThisInitialized(_this));_this.onStyleButtonToggle=_this.onStyleButtonToggle.bind(_assertThisInitialized(_this));_this.renderLinkEditor=_this.renderLinkEditor.bind(_assertThisInitialized(_this));return _this}_createClass(RichText,[{key:"UNSAFE_componentWillReceiveProps",value:function UNSAFE_componentWillReceiveProps(nextProps){var _this$state=this.state,linkEditorFocus=_this$state.linkEditorFocus,editorFocus=_this$state.editorFocus;if(linkEditorFocus||editorFocus){return}var editorState=this.createEditorStateFromHTML(nextProps.value);this.setState({editorState})}},{key:"shouldComponentUpdate",value:function shouldComponentUpdate(nextProps,nextState){var _this$props=this.props,placeholder=_this$props.placeholder,value=_this$props.value;var _this$state2=this.state,editorState=_this$state2.editorState,linkEditorFocus=_this$state2.linkEditorFocus;if(linkEditorFocus||placeholder!==nextProps.placeholder||value!==nextProps.value||editorState!==nextState.editorState){return true}return nextProps.value!==this.getEditorStateAsHTML(editorState)}},{key:"getDecorator",value:function getDecorator(){return new _draftJs.CompositeDecorator([{strategy:_decoratorStrategies.findLinkEntities,component:_LinkDecorator.default,props:{style:_configuration.STYLE_MAP[_configuration.LINK]}}])}},{key:"createEditorStateFromHTML",value:function createEditorStateFromHTML(html){var contentState=(0,_draftJsImportHtml.stateFromHTML)(html,{inlineStyles:{[_configuration.SUPERSCRIPT]:{element:"sup"},[_configuration.SUBSCRIPT]:{element:"sub"}},defaultBlockTag:null});var decorator=this.getDecorator();_draftJs.EditorState.createWithContent(contentState);return _draftJs.EditorState.createWithContent(contentState,decorator)}},{key:"getEditorStateAsHTML",value:function getEditorStateAsHTML(editorState){var contentState=editorState.getCurrentContent();return(0,_draftJsExportHtml.stateToHTML)(contentState,{defaultBlockTag:null,inlineStyles:_configuration.STYLES_TO_HTML_TAGS})}},{key:"focus",value:function focus(){this.editorInput.focus()}},{key:"getParentContainerVerticalOffset",value:function getParentContainerVerticalOffset(){return document.querySelector(".panel").scrollTop}},{key:"onChange",value:function onChange(editorState){var selectedLinkID=this.state.selectedLinkID;var selection=editorState.getSelection();var entityKey=(0,_DraftCommands.getEntityKeyAt)(editorState,selection);var 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);var htmlContent=this.getEditorStateAsHTML(editorState).replace(/<br>\n*/,"<br>");if(this.props.value!==htmlContent){(0,_debounce.default)(this.props.onChange,[htmlContent])}}},{key:"onBlur",value:function onBlur(){this.setState({editorFocus:false})}},{key:"onFocus",value:function onFocus(){this.setState({editorFocus:true})}},{key:"onLinkEditorBlur",value:function onLinkEditorBlur(){this.setState({linkEditorFocus:false})}},{key:"onLinkEditorFocus",value:function onLinkEditorFocus(){this.setState({linkEditorFocus:true})}},{key:"onLinkEditorChange",value:function onLinkEditorChange(linkID,urlValue){var editorState=this.state.editorState;var selectionState=editorState.getSelection();_draftJs.Entity.replaceData(linkID,{url:urlValue});var updatedEditorState=_draftJs.RichUtils.toggleLink(editorState,selectionState,linkID);this.onChange(updatedEditorState)}},{key:"onLinkEditorClose",value:function onLinkEditorClose(){this.focus();this.setState({linkEditorFocus:false,selectedLinkID:null})}},{key:"onKeyCommand",value:function onKeyCommand(command){var newEditorState=(0,_DraftCommands.handleKeyCommand)(this.state.editorState,command);if(newEditorState){this.onChange(newEditorState);return true}return false}},{key:"onReturnPressed",value:function onReturnPressed(){var newEditorState=(0,_DraftCommands.insertSoftNewline)(this.state.editorState);this.onChange(newEditorState);return true}},{key:"onStyleButtonToggle",value:function onStyleButtonToggle(inlineStyle){var newEditorState=(0,_DraftCommands.toggleInlineStyle)(this.state.editorState,inlineStyle);if(newEditorState){this.onChange(newEditorState)}}},{key:"renderLinkEditor",value:function renderLinkEditor(selectedLinkID){if(!selectedLinkID){return null}var linkEntity=(0,_DraftCommands.getEntityByKey)(selectedLinkID);var linkURL=linkEntity.getData().url;var 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})}},{key:"render",value:function render(){var _this2=this;var _this$state3=this.state,editorState=_this$state3.editorState,selectedLinkID=_this$state3.selectedLinkID;var 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:function ref(input){return _this2.editorInput=input}})),this.renderLinkEditor(selectedLinkID))}},{key:"__reactstandin__regenerateByEval",value:function __reactstandin__regenerateByEval(key,code){this[key]=eval(code)}}]);return RichText}(_react.Component);RichText.propTypes={onChange:_propTypes.default.func.isRequired,placeholder:_propTypes.default.string,value:_propTypes.default.any};RichText.defaultProps={placeholder:"",value:""};var _default=RichText;var _default2=exports.default=_default;;(function(){var reactHotLoader=typeof reactHotLoaderGlobal!=="undefined"?reactHotLoaderGlobal.default:undefined;if(!reactHotLoader){return}reactHotLoader.register(RichText,"RichText","/Users/dima/plotly/react-chart-editor/src/components/widgets/text_editors/RichText/index.js");reactHotLoader.register(_default,"default","/Users/dima/plotly/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