2019-09-03 03:29:02 -05:00
|
|
|
import React, { Suspense } from 'react';
|
2020-08-12 02:40:06 -05:00
|
|
|
import { PopoverController, Popover, ClickOutsideWrapper } from '@grafana/ui';
|
2019-09-03 03:29:02 -05:00
|
|
|
import { FunctionDescriptor, FunctionEditorControls, FunctionEditorControlsProps } from './FunctionEditorControls';
|
2019-02-18 10:55:38 -06:00
|
|
|
|
|
|
|
interface FunctionEditorProps extends FunctionEditorControlsProps {
|
|
|
|
func: FunctionDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FunctionEditorState {
|
|
|
|
showingDescription: boolean;
|
|
|
|
}
|
2019-09-03 03:29:02 -05:00
|
|
|
const FunctionDescription = React.lazy(async () => {
|
|
|
|
// @ts-ignore
|
|
|
|
const { default: rst2html } = await import(/* webpackChunkName: "rst2html" */ 'rst2html');
|
|
|
|
return {
|
2020-12-01 09:19:52 -06:00
|
|
|
default(props: { description?: string }) {
|
|
|
|
return <div dangerouslySetInnerHTML={{ __html: rst2html(props.description ?? '') }} />;
|
|
|
|
},
|
2019-09-03 03:29:02 -05:00
|
|
|
};
|
|
|
|
});
|
2019-02-18 10:55:38 -06:00
|
|
|
|
|
|
|
class FunctionEditor extends React.PureComponent<FunctionEditorProps, FunctionEditorState> {
|
|
|
|
private triggerRef = React.createRef<HTMLSpanElement>();
|
|
|
|
|
|
|
|
constructor(props: FunctionEditorProps) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
showingDescription: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-05 09:46:46 -05:00
|
|
|
renderContent = ({ updatePopperPosition }: any) => {
|
2019-02-21 02:09:24 -06:00
|
|
|
const {
|
|
|
|
onMoveLeft,
|
|
|
|
onMoveRight,
|
|
|
|
func: {
|
|
|
|
def: { name, description },
|
|
|
|
},
|
|
|
|
} = this.props;
|
2019-02-18 10:55:38 -06:00
|
|
|
const { showingDescription } = this.state;
|
|
|
|
|
|
|
|
if (showingDescription) {
|
|
|
|
return (
|
|
|
|
<div style={{ overflow: 'auto', maxHeight: '30rem', textAlign: 'left', fontWeight: 'normal' }}>
|
|
|
|
<h4 style={{ color: 'white' }}> {name} </h4>
|
2019-09-03 03:29:02 -05:00
|
|
|
<Suspense fallback={<span>Loading description...</span>}>
|
|
|
|
<FunctionDescription description={description} />
|
|
|
|
</Suspense>
|
2019-02-18 10:55:38 -06:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FunctionEditorControls
|
|
|
|
{...this.props}
|
|
|
|
onMoveLeft={() => {
|
|
|
|
onMoveLeft(this.props.func);
|
|
|
|
updatePopperPosition();
|
|
|
|
}}
|
|
|
|
onMoveRight={() => {
|
|
|
|
onMoveRight(this.props.func);
|
|
|
|
updatePopperPosition();
|
|
|
|
}}
|
|
|
|
onDescriptionShow={() => {
|
|
|
|
this.setState({ showingDescription: true }, () => {
|
|
|
|
updatePopperPosition();
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2020-08-12 02:40:06 -05:00
|
|
|
<PopoverController content={this.renderContent} placement="top" hideAfter={100}>
|
2019-02-18 10:55:38 -06:00
|
|
|
{(showPopper, hidePopper, popperProps) => {
|
|
|
|
return (
|
|
|
|
<>
|
2020-07-08 04:05:20 -05:00
|
|
|
{this.triggerRef.current && (
|
2019-08-19 04:40:47 -05:00
|
|
|
<Popover
|
2019-02-18 10:55:38 -06:00
|
|
|
{...popperProps}
|
|
|
|
referenceElement={this.triggerRef.current}
|
|
|
|
wrapperClassName="popper"
|
|
|
|
className="popper__background"
|
|
|
|
onMouseLeave={() => {
|
|
|
|
this.setState({ showingDescription: false });
|
|
|
|
}}
|
|
|
|
renderArrow={({ arrowProps, placement }) => (
|
|
|
|
<div className="popper__arrow" data-placement={placement} {...arrowProps} />
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
)}
|
2020-08-12 02:40:06 -05:00
|
|
|
<ClickOutsideWrapper
|
|
|
|
onClick={() => {
|
|
|
|
if (popperProps.show) {
|
|
|
|
hidePopper();
|
|
|
|
}
|
2019-02-18 10:55:38 -06:00
|
|
|
}}
|
|
|
|
>
|
2020-08-12 02:40:06 -05:00
|
|
|
<span
|
|
|
|
ref={this.triggerRef}
|
|
|
|
onClick={popperProps.show ? hidePopper : showPopper}
|
|
|
|
onMouseLeave={() => {
|
|
|
|
this.setState({ showingDescription: false });
|
|
|
|
}}
|
|
|
|
style={{ cursor: 'pointer' }}
|
|
|
|
>
|
|
|
|
{this.props.func.def.name}
|
|
|
|
</span>
|
|
|
|
</ClickOutsideWrapper>
|
2019-02-18 10:55:38 -06:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
2019-08-19 04:40:47 -05:00
|
|
|
</PopoverController>
|
2019-02-18 10:55:38 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export { FunctionEditor };
|