import { Input } from '@angular/core'; import { Component, OnInit } from '@angular/core'; import { DomSanitizer } from '@angular/platform-browser'; import { RhSafeAny } from 'rh-base/model'; type FileType = 'img' | 'pdf' | 'other'; @Component({ selector: 'rh-file-preview', templateUrl: './file-preview.component.html', styleUrls: ['./file-preview.component.less'] }) export class RhFilePreviewComponent implements OnInit { @Input() rhFileName!: string; @Input() rhFileUrl!: string; readLoading = false; fileType!: FileType; imgUrl: RhSafeAny = null; pdfUrl: RhSafeAny = null; otherUrl: RhSafeAny = null; constructor(private sanitizer: DomSanitizer) {} ngOnInit() { // } ngOnChanges() { if (this.rhFileName && this.rhFileUrl) { this.getFileViewer(); } } getFileViewer() { const imgReg = /\.(png|jpg|jpeg|bmp|gif|webp|psd|svg|tiff)/; const pdfReg = /\.pdf/; const toPdfReg = /\.(doc|docx|txt|ppt|pptx|xls|xlsx)/; this.readLoading = true; if (imgReg.test(this.rhFileName.toLowerCase())) { this.imgUrl = this.sanitizer.bypassSecurityTrustResourceUrl(this.rhFileUrl); this.fileType = 'img'; } else if (pdfReg.test(this.rhFileName.toLowerCase())) { this.pdfUrl = this.rhFileUrl; this.fileType = 'pdf'; // console.log(this.pdfUrl); } else if (toPdfReg.test(this.rhFileName.toLowerCase())) { this.pdfUrl = this.rhFileUrl; this.fileType = 'pdf'; } else { this.otherUrl = this.rhFileUrl; this.fileType = 'other'; const anchor = document.createElement('a'); anchor.download = this.rhFileName; anchor.href = this.otherUrl; anchor.click(); } this.readLoading = false; } // moveFile(){ // const el = document.getElementById('pdfFile'); // el.addEventListener('mousedown',(event) => { // const startX = event.clientX, // startY = event.clientY, // left = el.offsetLeft, // top = el.offsetTop; // const moveFun = (event) => { // const X = event.clientX - startX // const Y = event.clientY - startY; // el.style.left = `${left + X}px`; // el.style.top = `${top + Y}px`; // }; // document.addEventListener('mousemove',moveFun); // el.addEventListener('mouseup',() => { // document.removeEventListener('mousemove',moveFun); // }); // }); // } }