Skip to content
Snippets Groups Projects
CreateEntityMenu.vue 3.95 KiB
Newer Older
malyusgun's avatar
malyusgun committed
import { useFileDialog, useWindowSize } from '@vueuse/core';
import { useAuthorizationStore } from '@/app/stores/authorization';
import { useFilesWebsocketStore } from '@/app/stores/filesWebsocket';
import { useDataStore } from '@/app/stores/data';
malyusgun's avatar
malyusgun committed
const emit = defineEmits(['createEntity']);
const { open: uploadFile, onChange } = useFileDialog({
malyusgun's avatar
malyusgun committed
const authorizationStore = useAuthorizationStore();
const dataStore = useDataStore();
malyusgun's avatar
malyusgun committed
const userNickName = computed(() => authorizationStore.userNickName);
const entitiesCount = computed(() => dataStore.entities.length);
const addImage = async (files: FileList) => {
  let image = new Image();
  if (!files[0]) return;
  const url = URL.createObjectURL(files[0]);
  image.src = url;
  image.onload = async () => {
malyusgun's avatar
malyusgun committed
    const filesWebsocketStore = useFilesWebsocketStore();
    filesWebsocketStore.saveImageUrl(url);
    const response = await fetch(url);
    const blob = await response.blob();
    const buffer = await blob.arrayBuffer();
malyusgun's avatar
malyusgun committed
    const { width: windowWidth } = useWindowSize();
    const maxWidth = windowWidth.value - 128;
malyusgun's avatar
malyusgun committed
    if (image.width > maxWidth) {
      image.height = Math.floor((maxWidth / image.width) * image.height);
malyusgun's avatar
malyusgun committed
      image.width = maxWidth;
    }
    if (image.height > maxHeight) {
      image.width = Math.floor((maxHeight / image.height) * image.width);
      image.height = maxHeight;
    }
malyusgun's avatar
malyusgun committed
    emit('createEntity', {
      entity_type: 'image',
      entity_order: entitiesCount.value + 1,
malyusgun's avatar
malyusgun committed
      image_buffer: buffer,
      entity_position: 'left',
      entity_title_position: 'center',
      image_width: image.width,
      image_height: image.height,
      image_scale: '1'
    });
  };
};
onChange((files) => {
  if (files && files?.length > 0) {
    addImage(files);
  }
});

  {
    label: 'Divider',
    icon: 'pi pi-pause',
    command: () => {
      emit('createEntity', {
        entity_type: 'divider',
        entity_order: entitiesCount.value + 1,
        divider_height: 1,
        divider_type: 'solid'
      });
    }
  },
    label: 'Paragraph',
malyusgun's avatar
malyusgun committed
      emit('createEntity', {
        entity_type: 'paragraph',
        entity_order: entitiesCount.value + 1,
        text: '',
        paragraph_size: 'full',
        font_size: '24',
        entity_position: 'left'
    command: () => uploadFile()
malyusgun's avatar
malyusgun committed
      emit('createEntity', {
        entity_type: 'table',
        entity_order: entitiesCount.value + 1,
        table_columns: [
          {
            name: 'Name',
            type: 'text'
          },
          {
            name: 'Description',
            type: 'text'
          },
          {
            name: 'Status',
            type: 'status'
          }
        ]
      });
    }
  }
]);
</script>

<template>
  <SpeedDial
    :model="speedDialItems"
    direction="right"
    :button-props="{ severity: 'primary', rounded: true }"
    style="position: absolute; left: 5%; top: 0"
  >
    <template #item="{ item, toggleCallback }">
      <div
        class="flex flex-col items-center justify-between -translate-8 gap-2 p-2 border rounded border-surface-200 dark:border-surface-700 w-20 cursor-pointer"
        @click="toggleCallback"
      >
        <span v-show="item.label !== 'Divider'" :class="item.icon" />
        <svg
          v-show="item.label === 'Divider'"
          fill="#fff"
          width="20px"
          height="20px"
          viewBox="0 0 24 24"
          xmlns="http://www.w3.org/2000/svg"
        >
          <path
            d="M3.293,20.707a1,1,0,0,1,0-1.414l16-16a1,1,0,1,1,1.414,1.414l-16,16A1,1,0,0,1,3.293,20.707Z"
          />
        </svg>
        <span>
          {{ item.label }}
        </span>
      </div>
    </template>
  </SpeedDial>
</template>

<style scoped></style>