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.

4.2 KB
Raw Blame
<template>
  <div class="container">
    <EditorAppHeader icon="image" :title="t('markdown.image.title')" :subtitle="t('markdown.image.subtitle')" />
    <div class="content">
      <AppDrop ref="dropComponent" @select="submitFile" />
      <Loader v-if="isLoading" style="margin: 12px auto" />
      <p v-if="uploadError" class="error">{{ uploadError }}</p>
      <div class="search-bar">
        <input v-model="searchQuery" :placeholder="t('markdown.image.searchPlaceholder')" />
      </div>
      <div class="images-grid">
        <div v-for="image in filteredImages" :key="image.id" class="image-item" @click="selectImage(image)">
          <img :src="resolvePreviewUrl(image)" :alt="image.name" class="image-preview" @error="handleImageError" />
          <div class="image-info">
            <span class="image-name">{{ image.name }}</span>
            <span class="image-size">{{ readableFileSize(image.size ?? 0) }}</span>
          </div>
        </div>
      </div>

      <div v-if="filteredImages.length === 0" class="no-images">
        <p>{{ t('markdown.image.noImages') }}</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import EditorAppHeader from './EditorAppHeader.vue';
import { readableFileSize, isImageFile, resolvePreviewUrl } from '~/helpers/resources';
import type { Node } from '~/stores';

const props = defineProps<{ onImageSelect: (imageUrl: string, altText: string) => void; nodeId?: string }>();
const emit = defineEmits<{ (e: 'close'): void }>();

const { t } = useI18nT();
const resourcesStore = useResourcesStore();
const nodesStore = useNodesStore();

const searchQuery = ref('');
const isLoading = ref(false);
const uploadError = ref<string | null>(null);
const dropComponent = ref();
const { resourceURL } = useApi();

const filteredImages = computed(() => {
  if (!searchQuery.value.trim()) {
    return nodesStore.resources.filter(img => isImageFile(img.metadata?.filetype as string));
  }
  return nodesStore.resources.filter(img => isImageFile(img.metadata?.filetype as string) && img.name.toLowerCase().includes(searchQuery.value.toLowerCase()));
});

const submitFile = (selectedFile: File | File[] | null) => {
  if (!selectedFile) return;
  isLoading.value = true;
  const body = new FormData();
  body.append('parent_id', props.nodeId || '');
  body.append('file', Array.isArray(selectedFile) ? selectedFile[0]! : selectedFile);
  dropComponent.value.reset(); // Reset drop component
  resourcesStore
    .post(body)
    .catch(e => (uploadError.value = e || t('markdown.image.uploadError')))
    .finally(() => (isLoading.value = false));
};

const handleImageError = (event: Event) => {
  const img = event.target as HTMLImageElement;
  img.style.display = 'none';
};

const selectImage = (image: Node) => {
  const altText = image.name.replace(/\.[^/.]+$/, '');
  props.onImageSelect(resourceURL(image), altText);
  emit('close');
};
</script>

<style scoped lang="scss">
.container {
  display: flex;
  flex-direction: column;
}

.content {
  gap: 24px;
  min-height: 0;
  padding: 0;
  padding-right: 8px;
  overflow-y: auto;
}

.search-bar {
  flex-shrink: 0;
  padding: 16px 0;
  border-bottom: 1px solid var(--border);
}

.images-grid {
  display: grid;
  flex: 1;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  padding: 16px 0;
}

.image-item {
  border: 2px solid transparent;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition:
    border-color $transition-fast ease,
    box-shadow $transition-fast ease,
    transform $transition-fast ease;

  &:hover {
    border-color: var(--primary);
    box-shadow: var(--shadow-lg);
    transform: translateY(-2px);
  }

  .image-preview {
    width: 100%;
    height: 150px;
    border-radius: var(--radius-sm);
  }

  .image-info {
    padding: 12px;

    .image-name {
      display: block;
      margin-bottom: 4px;
      font-size: 14px;
      font-weight: 500;
      color: var(--text-primary);
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }

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

.no-images {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
  font-size: 16px;
  color: var(--text-secondary);
}
</style>