InlineEdit.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { css } from '@emotion/css';
  2. import React, { SyntheticEvent, useRef, useState } from 'react';
  3. import Draggable from 'react-draggable';
  4. import { Resizable, ResizeCallbackData } from 'react-resizable';
  5. import { Dimensions2D, GrafanaTheme2 } from '@grafana/data';
  6. import { IconButton, Portal, useStyles2 } from '@grafana/ui';
  7. import store from 'app/core/store';
  8. import { InlineEditBody } from './InlineEditBody';
  9. type Props = {
  10. onClose?: () => void;
  11. };
  12. const OFFSET_X = 70;
  13. export const InlineEdit = ({ onClose }: Props) => {
  14. const btnInlineEdit = document.querySelector('[data-btninlineedit]')!.getBoundingClientRect();
  15. const ref = useRef<HTMLDivElement>(null);
  16. const styles = useStyles2(getStyles);
  17. const inlineEditKey = 'inlineEditPanel';
  18. const defaultMeasurements = { width: 350, height: 400 };
  19. const defaultX = btnInlineEdit.x + OFFSET_X;
  20. const defaultY = btnInlineEdit.y - defaultMeasurements.height;
  21. const savedPlacement = store.getObject(inlineEditKey, {
  22. x: defaultX,
  23. y: defaultY,
  24. w: defaultMeasurements.width,
  25. h: defaultMeasurements.height,
  26. });
  27. const [measurements, setMeasurements] = useState<Dimensions2D>({ width: savedPlacement.w, height: savedPlacement.h });
  28. const [placement, setPlacement] = useState({ x: savedPlacement.x, y: savedPlacement.y });
  29. const onDragStop = (event: any, dragElement: any) => {
  30. let x = dragElement.x < 0 ? 0 : dragElement.x;
  31. let y = dragElement.y < 0 ? 0 : dragElement.y;
  32. setPlacement({ x: x, y: y });
  33. saveToStore(x, y, measurements.width, measurements.height);
  34. };
  35. const onResizeStop = (event: SyntheticEvent<Element, Event>, data: ResizeCallbackData) => {
  36. const { size } = data;
  37. setMeasurements({ width: size.width, height: size.height });
  38. saveToStore(placement.x, placement.y, size.width, size.height);
  39. };
  40. const saveToStore = (x: number, y: number, width: number, height: number) => {
  41. store.setObject(inlineEditKey, { x: x, y: y, w: width, h: height });
  42. };
  43. return (
  44. <Portal>
  45. <div className={styles.draggableWrapper}>
  46. <Draggable handle="strong" onStop={onDragStop} position={{ x: placement.x, y: placement.y }}>
  47. <Resizable height={measurements.height} width={measurements.width} onResize={onResizeStop}>
  48. <div
  49. className={styles.inlineEditorContainer}
  50. style={{ height: `${measurements.height}px`, width: `${measurements.width}px` }}
  51. ref={ref}
  52. >
  53. <strong className={styles.inlineEditorHeader}>
  54. <div className={styles.placeholder} />
  55. <div>Canvas Inline Editor</div>
  56. <IconButton name="times" size="xl" className={styles.inlineEditorClose} onClick={onClose} />
  57. </strong>
  58. <div className={styles.inlineEditorContentWrapper}>
  59. <div className={styles.inlineEditorContent}>
  60. <InlineEditBody />
  61. </div>
  62. </div>
  63. </div>
  64. </Resizable>
  65. </Draggable>
  66. </div>
  67. </Portal>
  68. );
  69. };
  70. const getStyles = (theme: GrafanaTheme2) => ({
  71. inlineEditorContainer: css`
  72. display: flex;
  73. flex-direction: column;
  74. background: ${theme.v1.colors.panelBg};
  75. box-shadow: 5px 5px 20px -5px #000000;
  76. z-index: 1000;
  77. opacity: 1;
  78. `,
  79. draggableWrapper: css`
  80. width: 0;
  81. height: 0;
  82. `,
  83. inlineEditorHeader: css`
  84. display: flex;
  85. align-items: center;
  86. justify-content: center;
  87. background: ${theme.colors.background.canvas};
  88. border: 1px solid ${theme.colors.border.weak};
  89. height: 40px;
  90. cursor: move;
  91. `,
  92. inlineEditorContent: css`
  93. white-space: pre-wrap;
  94. padding: 10px;
  95. `,
  96. inlineEditorClose: css`
  97. margin-left: auto;
  98. `,
  99. placeholder: css`
  100. width: 24px;
  101. height: 24px;
  102. visibility: hidden;
  103. margin-right: auto;
  104. `,
  105. inlineEditorContentWrapper: css`
  106. overflow: scroll;
  107. `,
  108. });