FunctionEditorControls.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import React, { Suspense } from 'react';
  2. import { Icon, Tooltip } from '@grafana/ui';
  3. import { FuncInstance } from '../gfunc';
  4. export interface FunctionEditorControlsProps {
  5. onMoveLeft: (func: FuncInstance) => void;
  6. onMoveRight: (func: FuncInstance) => void;
  7. onRemove: (func: FuncInstance) => void;
  8. }
  9. const FunctionDescription = React.lazy(async () => {
  10. // @ts-ignore
  11. const { default: rst2html } = await import(/* webpackChunkName: "rst2html" */ 'rst2html');
  12. return {
  13. default(props: { description?: string }) {
  14. return <div dangerouslySetInnerHTML={{ __html: rst2html(props.description ?? '') }} />;
  15. },
  16. };
  17. });
  18. const FunctionHelpButton = (props: { description?: string; name: string }) => {
  19. if (props.description) {
  20. let tooltip = (
  21. <Suspense fallback={<span>Loading description...</span>}>
  22. <FunctionDescription description={props.description} />
  23. </Suspense>
  24. );
  25. return (
  26. <Tooltip content={tooltip} placement={'bottom-end'}>
  27. <Icon className={props.description ? undefined : 'pointer'} name="question-circle" />
  28. </Tooltip>
  29. );
  30. }
  31. return (
  32. <Icon
  33. className="pointer"
  34. name="question-circle"
  35. onClick={() => {
  36. window.open(
  37. 'http://graphite.readthedocs.org/en/latest/functions.html#graphite.render.functions.' + props.name,
  38. '_blank'
  39. );
  40. }}
  41. />
  42. );
  43. };
  44. export const FunctionEditorControls = (
  45. props: FunctionEditorControlsProps & {
  46. func: FuncInstance;
  47. }
  48. ) => {
  49. const { func, onMoveLeft, onMoveRight, onRemove } = props;
  50. return (
  51. <div
  52. style={{
  53. display: 'flex',
  54. width: '60px',
  55. justifyContent: 'space-between',
  56. }}
  57. >
  58. <Icon name="arrow-left" onClick={() => onMoveLeft(func)} />
  59. <FunctionHelpButton name={func.def.name} description={func.def.description} />
  60. <Icon name="times" onClick={() => onRemove(func)} />
  61. <Icon name="arrow-right" onClick={() => onMoveRight(func)} />
  62. </div>
  63. );
  64. };