mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-11-04 04:57:51 +00:00 
			
		
		
		
	Fixes: CW-3602, CW-3606, CW-3605, CW-3601, CW-3603, CW-3600, CW-3598 - [CW-3602](https://linear.app/chatwoot/issue/CW-3602/chat-list-infinite-loader-fetching-only-odd-numbered-pages) Chat list pagination broken - [CW-3606](https://linear.app/chatwoot/issue/CW-3606/saving-greeting-message-is-not-working-in-inbox-settings) Greetings message not getting saved - [CW-3605](https://linear.app/chatwoot/issue/CW-3605/copy-and-paste-image-attachment-not-working-in-widget) Paste not working on widget - [CW-3601](https://linear.app/chatwoot/issue/CW-3601/edit-category-is-not-working-properly) Edit category not updating - [CW-3603](https://linear.app/chatwoot/issue/CW-3603/delete-filter-is-not-working) Delete filter modal not toggling - [CW-3600](https://linear.app/chatwoot/issue/CW-3600/portal-editor-is-not-working-properly) Portal editor events were flaky - [CW-3598](https://linear.app/chatwoot/issue/CW-3598/rearrange-of-pre-chat-form-fields-throws-an-error) Prechat form re-order bug --------- Co-authored-by: Vishnu Narayanan <iamwishnu@gmail.com>
		
			
				
	
	
		
			156 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			Vue
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			156 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			Vue
		
	
	
		
			Executable File
		
	
	
	
	
<script>
 | 
						|
import FileUpload from 'vue-upload-component';
 | 
						|
import Spinner from 'shared/components/Spinner.vue';
 | 
						|
import { checkFileSizeLimit } from 'shared/helpers/FileHelper';
 | 
						|
import {
 | 
						|
  MAXIMUM_FILE_UPLOAD_SIZE,
 | 
						|
  ALLOWED_FILE_TYPES,
 | 
						|
} from 'shared/constants/messages';
 | 
						|
import { BUS_EVENTS } from 'shared/constants/busEvents';
 | 
						|
import FluentIcon from 'shared/components/FluentIcon/Index.vue';
 | 
						|
import { DirectUpload } from 'activestorage';
 | 
						|
import { mapGetters } from 'vuex';
 | 
						|
import { emitter } from 'shared/helpers/mitt';
 | 
						|
 | 
						|
export default {
 | 
						|
  components: { FluentIcon, FileUpload, Spinner },
 | 
						|
  props: {
 | 
						|
    onAttach: {
 | 
						|
      type: Function,
 | 
						|
      default: () => {},
 | 
						|
    },
 | 
						|
  },
 | 
						|
  data() {
 | 
						|
    return { isUploading: false };
 | 
						|
  },
 | 
						|
  computed: {
 | 
						|
    ...mapGetters({ globalConfig: 'globalConfig/get' }),
 | 
						|
    fileUploadSizeLimit() {
 | 
						|
      return MAXIMUM_FILE_UPLOAD_SIZE;
 | 
						|
    },
 | 
						|
    allowedFileTypes() {
 | 
						|
      return ALLOWED_FILE_TYPES;
 | 
						|
    },
 | 
						|
  },
 | 
						|
  mounted() {
 | 
						|
    document.addEventListener('paste', this.handleClipboardPaste);
 | 
						|
  },
 | 
						|
  unmounted() {
 | 
						|
    document.removeEventListener('paste', this.handleClipboardPaste);
 | 
						|
  },
 | 
						|
  methods: {
 | 
						|
    handleClipboardPaste(e) {
 | 
						|
      const items = (e.clipboardData || e.originalEvent.clipboardData).items;
 | 
						|
      // items is a DataTransferItemList object which does not have forEach method
 | 
						|
      const itemsArray = Array.from(items);
 | 
						|
      itemsArray.forEach(item => {
 | 
						|
        if (item.kind === 'file') {
 | 
						|
          e.preventDefault();
 | 
						|
          const file = item.getAsFile();
 | 
						|
          this.$refs.upload.add(file);
 | 
						|
        }
 | 
						|
      });
 | 
						|
    },
 | 
						|
    getFileType(fileType) {
 | 
						|
      return fileType.includes('image') ? 'image' : 'file';
 | 
						|
    },
 | 
						|
    async onFileUpload(file) {
 | 
						|
      if (this.globalConfig.directUploadsEnabled) {
 | 
						|
        await this.onDirectFileUpload(file);
 | 
						|
      } else {
 | 
						|
        await this.onIndirectFileUpload(file);
 | 
						|
      }
 | 
						|
    },
 | 
						|
    async onDirectFileUpload(file) {
 | 
						|
      if (!file) {
 | 
						|
        return;
 | 
						|
      }
 | 
						|
      this.isUploading = true;
 | 
						|
      try {
 | 
						|
        if (checkFileSizeLimit(file, MAXIMUM_FILE_UPLOAD_SIZE)) {
 | 
						|
          const { websiteToken } = window.chatwootWebChannel;
 | 
						|
          const upload = new DirectUpload(
 | 
						|
            file.file,
 | 
						|
            `/api/v1/widget/direct_uploads?website_token=${websiteToken}`,
 | 
						|
            {
 | 
						|
              directUploadWillCreateBlobWithXHR: xhr => {
 | 
						|
                xhr.setRequestHeader('X-Auth-Token', window.authToken);
 | 
						|
              },
 | 
						|
            }
 | 
						|
          );
 | 
						|
 | 
						|
          upload.create((error, blob) => {
 | 
						|
            if (error) {
 | 
						|
              emitter.emit(BUS_EVENTS.SHOW_ALERT, {
 | 
						|
                message: error,
 | 
						|
              });
 | 
						|
            } else {
 | 
						|
              this.onAttach({
 | 
						|
                file: blob.signed_id,
 | 
						|
                ...this.getLocalFileAttributes(file),
 | 
						|
              });
 | 
						|
            }
 | 
						|
          });
 | 
						|
        } else {
 | 
						|
          emitter.emit(BUS_EVENTS.SHOW_ALERT, {
 | 
						|
            message: this.$t('FILE_SIZE_LIMIT', {
 | 
						|
              MAXIMUM_FILE_UPLOAD_SIZE: this.fileUploadSizeLimit,
 | 
						|
            }),
 | 
						|
          });
 | 
						|
        }
 | 
						|
      } catch (error) {
 | 
						|
        // Error
 | 
						|
      }
 | 
						|
      this.isUploading = false;
 | 
						|
    },
 | 
						|
    async onIndirectFileUpload(file) {
 | 
						|
      if (!file) {
 | 
						|
        return;
 | 
						|
      }
 | 
						|
      this.isUploading = true;
 | 
						|
      try {
 | 
						|
        if (checkFileSizeLimit(file, MAXIMUM_FILE_UPLOAD_SIZE)) {
 | 
						|
          await this.onAttach({
 | 
						|
            file: file.file,
 | 
						|
            ...this.getLocalFileAttributes(file),
 | 
						|
          });
 | 
						|
        } else {
 | 
						|
          emitter.emit(BUS_EVENTS.SHOW_ALERT, {
 | 
						|
            message: this.$t('FILE_SIZE_LIMIT', {
 | 
						|
              MAXIMUM_FILE_UPLOAD_SIZE: this.fileUploadSizeLimit,
 | 
						|
            }),
 | 
						|
          });
 | 
						|
        }
 | 
						|
      } catch (error) {
 | 
						|
        // Error
 | 
						|
      }
 | 
						|
      this.isUploading = false;
 | 
						|
    },
 | 
						|
    getLocalFileAttributes(file) {
 | 
						|
      return {
 | 
						|
        thumbUrl: window.URL.createObjectURL(file.file),
 | 
						|
        fileType: this.getFileType(file.type),
 | 
						|
      };
 | 
						|
    },
 | 
						|
  },
 | 
						|
};
 | 
						|
</script>
 | 
						|
 | 
						|
<template>
 | 
						|
  <FileUpload
 | 
						|
    ref="upload"
 | 
						|
    :size="4096 * 2048"
 | 
						|
    :accept="allowedFileTypes"
 | 
						|
    :data="{
 | 
						|
      direct_upload_url: '/api/v1/widget/direct_uploads',
 | 
						|
      direct_upload: true,
 | 
						|
    }"
 | 
						|
    @input-file="onFileUpload"
 | 
						|
  >
 | 
						|
    <button class="icon-button flex items-center justify-center">
 | 
						|
      <FluentIcon v-if="!isUploading.image" icon="attach" />
 | 
						|
      <Spinner v-if="isUploading" size="small" />
 | 
						|
    </button>
 | 
						|
  </FileUpload>
 | 
						|
</template>
 |