Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

2.8 KB
Raw Blame
import type { draggable } from '@atlaskit/pragmatic-drag-and-drop/element/adapter';
import { setCustomNativeDragPreview } from '@atlaskit/pragmatic-drag-and-drop/element/set-custom-native-drag-preview';
import { Flex, Heading } from '@invoke-ai/ui-library';
import type { MultipleImageDndSourceData } from 'features/dnd/dnd';
import { DND_IMAGE_DRAG_PREVIEW_SIZE, preserveOffsetOnSourceFallbackCentered } from 'features/dnd/util';
import { memo } from 'react';
import { createPortal } from 'react-dom';
import { useTranslation } from 'react-i18next';
import type { Param0 } from 'tsafe';

const DndDragPreviewMultipleImage = memo(
  ({ image_names, video_names }: { image_names: string[]; video_names: string[] }) => {
    const { t } = useTranslation();
    // The whole mixed selection moves on drop (the board drop handler dispatches both
    // mutations), so the preview counts both kinds to match the "Images/Videos" label.
    const count = image_names.length + video_names.length;
    return (
      <Flex
        w={DND_IMAGE_DRAG_PREVIEW_SIZE}
        h={DND_IMAGE_DRAG_PREVIEW_SIZE}
        alignItems="center"
        justifyContent="center"
        flexDir="column"
        bg="base.900"
        borderRadius="base"
      >
        <Heading>{count}</Heading>
        <Heading size="sm">{t('parameters.images_withCount', { count })}</Heading>
      </Flex>
    );
  }
);

DndDragPreviewMultipleImage.displayName = 'DndDragPreviewMultipleImage';

export type DndDragPreviewMultipleImageState = {
  type: 'multiple-image';
  container: HTMLElement;
  image_names: string[];
  video_names: string[];
};

export const createMultipleImageDragPreview = (arg: DndDragPreviewMultipleImageState) =>
  createPortal(
    <DndDragPreviewMultipleImage image_names={arg.image_names} video_names={arg.video_names} />,
    arg.container
  );

type SetMultipleDragPreviewArg = {
  multipleImageDndData: MultipleImageDndSourceData;
  setDragPreviewState: (dragPreviewState: DndDragPreviewMultipleImageState | null) => void;
  onGenerateDragPreviewArgs: Param0<Param0<typeof draggable>['onGenerateDragPreview']>;
};

export const setMultipleImageDragPreview = ({
  multipleImageDndData,
  onGenerateDragPreviewArgs,
  setDragPreviewState,
}: SetMultipleDragPreviewArg) => {
  const { nativeSetDragImage, source, location } = onGenerateDragPreviewArgs;
  setCustomNativeDragPreview({
    render({ container }) {
      setDragPreviewState({
        type: 'multiple-image',
        container,
        image_names: multipleImageDndData.payload.image_names,
        video_names: multipleImageDndData.payload.video_names,
      });
      return () => setDragPreviewState(null);
    },
    nativeSetDragImage,
    getOffset: preserveOffsetOnSourceFallbackCentered({
      element: source.element,
      input: location.current.input,
    }),
  });
};