UNPKG

@figlinq/react-chart-editor

Version:
2 lines 4.32 kB
function ownKeys(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);r&&(o=o.filter(function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable})),t.push.apply(t,o)}return t}function _objectSpread(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?ownKeys(Object(t),!0).forEach(function(r){_defineProperty(e,r,t[r])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):ownKeys(Object(t)).forEach(function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))})}return e}function _defineProperty(e,r,t){return(r=_toPropertyKey(r))in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function _toPropertyKey(t){var i=_toPrimitive(t,"string");return"symbol"==typeof i?i:i+""}function _toPrimitive(t,r){if("object"!=typeof t||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var i=e.call(t,r||"default");if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}import Field from"./Field";import PropTypes from"prop-types";import{connectToContainer}from"../../lib";import nestedProperty from"plotly.js/src/lib/nested_property";import LaTeX from"../widgets/text_editors/LaTeX";import RichText from"../widgets/text_editors/RichText";import MultiFormat from"../widgets/text_editors/MultiFormat";import HTML from"../widgets/text_editors/HTML";import{jsx as _jsx}from"react/jsx-runtime";const TEMPLATE_STRING_REGEX=/%{([^\s%{}:]*)(:[^}]*)?}/g;const INDEX_IN_TEMPLATE_STRING_REGEX=/%{(meta(\[(\d+)]))}/;export const UnconnectedTextEditor=(props,context)=>{const hasTemplateStrings=value=>!value?false:value.match(TEMPLATE_STRING_REGEX);const updatePlot=value=>{const{updatePlot}=props;const templateStrings=hasTemplateStrings(value);let adjustedValue=value;if(templateStrings){adjustedValue=adjustedValue.replace(TEMPLATE_STRING_REGEX,match=>{const index=INDEX_IN_TEMPLATE_STRING_REGEX.exec(match);if(index){const adjustedIndex=parseInt(index[3],10)-1;if(!isNaN(adjustedIndex)){return`%{meta[${adjustedIndex<0?0:adjustedIndex}]}`}}return match})}updatePlot(adjustedValue)};const getAdjustedFullValue=fullValue=>{const templateStrings=hasTemplateStrings(fullValue);if(templateStrings){return fullValue.replace(TEMPLATE_STRING_REGEX,match=>{const index=INDEX_IN_TEMPLATE_STRING_REGEX.exec(match);if(index){const adjustedIndex=parseInt(index[3],10)+1;if(!isNaN(adjustedIndex)){return`%{meta[${adjustedIndex}]}`}return match}return match})}return fullValue};const{attr,container,htmlOnly,latexOnly,multiValued,richTextOnly}=props;const{localize:_}=context;let fullValue=getAdjustedFullValue(props.fullValue);let placeholder=props.placeholder;if(multiValued||fullValue&&(!container||!nestedProperty(container,attr))){placeholder=fullValue;fullValue=""}let editor;if(latexOnly){placeholder=_("Enter LaTeX formatted text");editor=_jsx(LaTeX,{value:fullValue,placeholder:placeholder,onChange:updatePlot})}else if(richTextOnly){editor=_jsx(RichText,{value:fullValue,placeholder:placeholder,onChange:updatePlot})}else if(htmlOnly){placeholder=_("Enter html formatted text");editor=_jsx(HTML,{value:fullValue,placeholder:placeholder,onChange:updatePlot})}else{editor=_jsx(MultiFormat,{value:fullValue,placeholder:placeholder,onChange:updatePlot})}return _jsx(Field,_objectSpread(_objectSpread({},props),{},{children:_jsx("div",{className:"text-editor",children:editor})}))};UnconnectedTextEditor.propTypes=_objectSpread(_objectSpread({},Field.propTypes),{},{fullValue:PropTypes.any,htmlOnly:PropTypes.bool,latexOnly:PropTypes.bool,richTextOnly:PropTypes.bool,updatePlot:PropTypes.func,placeholder:PropTypes.string});UnconnectedTextEditor.contextTypes={localize:PropTypes.func,fullLayout:PropTypes.object};UnconnectedTextEditor.displayName="UnconnectedTextEditor";export default connectToContainer(UnconnectedTextEditor,{modifyPlotProps:(props,context,plotProps)=>{if(plotProps.isVisible&&plotProps.multiValued){plotProps.isVisible=false}if(context.fullLayout&&context.fullLayout._dfltTitle&&Object.values(context.fullLayout._dfltTitle).includes(plotProps.fullValue)){plotProps.placeholder=plotProps.fullValue;plotProps.fullValue=""}}}); //# sourceMappingURL=TextEditor.js.map