draggableUtils.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import type {Translate} from '@dnd-kit/core';
  2. import {space} from 'sentry/styles/space';
  3. export type WidgetDragPositioning = {
  4. initialTranslate: Translate;
  5. translate: Translate;
  6. left?: number;
  7. top?: number;
  8. };
  9. export const WIDGET_PREVIEW_DRAG_ID = 'widget-preview-draggable';
  10. export const SIDEBAR_HEIGHT = 54;
  11. export const DEFAULT_TRANSLATE_COORDINATES = {x: 0, y: 0};
  12. const DEFAULT_SPACING = parseInt(space(2).replace('px', ''), 10);
  13. export const DEFAULT_LEFT = DEFAULT_SPACING;
  14. export const DEFAULT_TOP = SIDEBAR_HEIGHT + DEFAULT_SPACING; // 54px sidebar + 16px space
  15. export const DEFAULT_WIDGET_DRAG_POSITIONING: WidgetDragPositioning = {
  16. initialTranslate: DEFAULT_TRANSLATE_COORDINATES,
  17. translate: DEFAULT_TRANSLATE_COORDINATES,
  18. left: DEFAULT_LEFT,
  19. top: DEFAULT_TOP,
  20. };
  21. export const DRAGGABLE_PREVIEW_HEIGHT = 200;
  22. export const DRAGGABLE_PREVIEW_WIDTH = 300;
  23. export const PREVIEW_HEIGHT = 400;
  24. export const DRAGGABLE_PREVIEW_HEIGHT_PX = `${DRAGGABLE_PREVIEW_HEIGHT}px`;
  25. export const DRAGGABLE_PREVIEW_WIDTH_PX = `${DRAGGABLE_PREVIEW_WIDTH}px`;
  26. export const PREVIEW_HEIGHT_PX = `${PREVIEW_HEIGHT}px`;
  27. /**
  28. * Snaps the preview to the visible corners of the slideout
  29. * @param over - The droppable area object from the drag end event
  30. * @returns The new position of the preview
  31. */
  32. export function snapPreviewToCorners(over: any | null): WidgetDragPositioning {
  33. const selectedCorner = over?.id?.toString().split('-');
  34. return {
  35. translate: DEFAULT_TRANSLATE_COORDINATES,
  36. initialTranslate: DEFAULT_TRANSLATE_COORDINATES,
  37. left: over?.rect
  38. ? selectedCorner?.[1] === 'left'
  39. ? over?.rect.left
  40. : over?.rect.right - DRAGGABLE_PREVIEW_WIDTH
  41. : undefined,
  42. top: over?.rect
  43. ? selectedCorner?.[0] === 'top'
  44. ? over?.rect.top
  45. : over?.rect.bottom - DRAGGABLE_PREVIEW_HEIGHT
  46. : undefined,
  47. };
  48. }