Skip to content

πŸ“š The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

6.0 KB
Raw Blame
<template>
  <div
    class="drop"
    :class="{ dragging: isDragOver, 'has-files': selectedFiles.length }"
    @dragover.prevent
    @drop.prevent="handleFileDrop"
    @dragenter.prevent="dragEnter"
    @dragleave.prevent="dragLeave"
    @paste.prevent="handlePaste"
    @click="handleDropAreaClick"
  >
    <input ref="fileInput" type="file" :multiple="multiple" :webkitdirectory="allowFolders" @change="handleFileSelect" />

    <div v-if="selectedFiles.length" class="files">
      <div class="list">
        <FileInline
          v-for="(file, index) in selectedFiles"
          :key="file.name + '-' + file.size + '-' + file.lastModified"
          :file="file"
          :remove-file="() => removeFile(index)"
          class="file-card"
        />
      </div>
      <footer>
        <span class="total">{{ selectedFiles.length }} {{ t('cdn.appdrop.file', { n: selectedFiles.length }) }} β€’ {{ readableFileSize(totalSize) }}</span>
        <span class="link" @click="triggerFileSelect">+ {{ t('cdn.appdrop.addMore') }}</span>
      </footer>
    </div>

    <div v-else class="empty">
      <Icon name="layers" size="40px" />
      <p v-if="isMobile" class="mobile-prompt">
        {{ t('cdn.appdrop.promptMobile') }}
      </p>
      <p v-else-if="multiple">
        <i18n-t scope="global" keypath="cdn.appdrop.promptPlural">
          <template #link>
            <span class="link" @click="triggerFileSelect">{{ t('cdn.appdrop.link') }}</span>
          </template>
        </i18n-t>
      </p>
      <p v-else>
        <i18n-t scope="global" keypath="cdn.appdrop.prompt">
          <template #link>
            <span class="link" @click="triggerFileSelect">{{ t('cdn.appdrop.link') }}</span>
          </template>
        </i18n-t>
      </p>
      <span v-if="multiple" class="hint">{{ t('cdn.appdrop.max', { n: props.maxFiles }) }}</span>
    </div>
  </div>
</template>
<script setup lang="ts">
import { readableFileSize } from '~/helpers/resources';

const props = withDefaults(
  defineProps<{
    multiple?: boolean;
    allowFolders?: boolean;
    maxFiles?: number;
  }>(),
  {
    maxFiles: 10,
    multiple: false,
    allowFolders: false,
  },
);

const emit = defineEmits<{
  select: [files: File | File[] | null];
}>();

const { t } = useI18nT();
const { isMobile } = useDevice();

const selectedFiles = ref<File[]>([]);
const isDragOver = ref(false);
const fileInput = ref<HTMLInputElement | null>(null);

const totalSize = computed(() => selectedFiles.value.reduce((acc, file) => acc + file.size, 0));

// Actions
const triggerFileSelect = () => fileInput.value!.click();

const handleDropAreaClick = () => {
  if (isMobile.value && !selectedFiles.value.length) {
    triggerFileSelect();
  }
};

const addFiles = (files?: FileList | File[] | null) => {
  if (!files) return;
  const fileArray = Array.from(files);
  if (props.multiple) {
    const newFiles = fileArray.slice(0, props.maxFiles - selectedFiles.value.length);
    for (const file of newFiles) {
      if (file && !selectedFiles.value.some(f => f.name === file.name && f.size === file.size && f.lastModified === file.lastModified)) {
        selectedFiles.value.push(file);
      }
    }
    emit('select', selectedFiles.value);
  } else {
    const file = fileArray[0];
    if (!file) return;
    selectedFiles.value = [file];
    emit('select', file);
  }
};

const handleFileSelect = (event: Event) => {
  const files = (event.target as HTMLInputElement | null)?.files;
  addFiles(files);
};

const handleFileDrop = (event: DragEvent) => {
  if (event.dataTransfer?.files && event.dataTransfer.files.length > 0) {
    const files = event.dataTransfer.files;
    addFiles(files);
    isDragOver.value = false;
  }
};

const handlePaste = (event: ClipboardEvent) => {
  const items = event.clipboardData?.items;
  if (!items) return;

  const pastedFiles: File[] = [];
  for (const item of items) {
    const file = item.getAsFile();
    if (!file) continue;
    pastedFiles.push(file);
    if (!props.multiple) break;
  }
  addFiles(pastedFiles);
};

const dragEnter = () => (isDragOver.value = true);
const dragLeave = () => (isDragOver.value = false);

const removeFile = (index: number) => {
  selectedFiles.value.splice(index, 1);
  if (fileInput.value) fileInput.value.value = '';
  if (props.multiple) {
    emit('select', selectedFiles.value.length ? selectedFiles.value : null);
  } else {
    emit('select', null);
  }
};

const reset = () => {
  selectedFiles.value = [];
  if (fileInput.value) fileInput.value.value = '';
};

defineExpose({ reset });
</script>

<style scoped lang="scss">
.drop {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  min-height: 150px;
  border: 2px dashed var(--border);
  border-radius: var(--radius-md);
  font-size: 14px;
  color: var(--text-secondary);
  background-color: transparent;
  transition:
    border-color $transition-base ease,
    background-color $transition-base ease;

  &:hover {
    border-color: var(--border-strong);
    background-color: var(--surface-raised);
  }

  &.has-files {
    align-items: stretch;
    padding: 12px;
  }

  &.dragging {
    border-color: var(--primary);
    background-color: var(--surface-raised);
  }

  input[type='file'] {
    display: none;
  }
}

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;

  p {
    margin: 0;
  }

  .mobile-prompt {
    max-width: 160px;
    text-align: center;
  }

  .hint {
    font-size: 12px;
    color: var(--text-secondary);
  }
}

.files {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
}

.list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 300px;
  overflow-y: auto;
}

footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.total {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
}

.link {
  font-size: 13px;
  color: var(--primary);
  cursor: pointer;

  &:hover {
    text-decoration: underline;
  }
}

</style>