|
| 1 | + |
| 2 | +// Import the core angular services. |
| 3 | +import { Component } from "@angular/core"; |
| 4 | +import { DomSanitizer } from "@angular/platform-browser"; |
| 5 | +import { SafeUrl } from "@angular/platform-browser"; |
| 6 | + |
| 7 | +// ----------------------------------------------------------------------------------- // |
| 8 | +// ----------------------------------------------------------------------------------- // |
| 9 | + |
| 10 | +@Component({ |
| 11 | + selector: "my-app", |
| 12 | + host: { |
| 13 | + "(window:paste)": "handlePaste( $event )" |
| 14 | + }, |
| 15 | + styleUrls: [ "./app.component.less" ], |
| 16 | + template: |
| 17 | + ` |
| 18 | + <h2> |
| 19 | + Sample Images (That You Can Right-Click And Copy) |
| 20 | + </h2> |
| 21 | +
|
| 22 | + <p class="sample-images"> |
| 23 | + <img src="./img/image-1.jpg" class="sample-image" /> |
| 24 | + <img src="./img/image-2.jpg" class="sample-image" /> |
| 25 | + <img src="./img/image-3.png" class="sample-image" /> |
| 26 | + <img src="./img/image-4.jpg" class="sample-image" /> |
| 27 | + <img src="./img/image-5.jpg" class="sample-image" /> |
| 28 | + </p> |
| 29 | +
|
| 30 | + <h2> |
| 31 | + Pasted Images |
| 32 | + </h2> |
| 33 | +
|
| 34 | + <p class="images"> |
| 35 | + <ng-template ngFor let-imageUrl [ngForOf]="imageUrls"> |
| 36 | +
|
| 37 | + <img [src]="imageUrl" class="image" /> |
| 38 | +
|
| 39 | + </ng-template> |
| 40 | + </p> |
| 41 | + ` |
| 42 | +}) |
| 43 | +export class AppComponent { |
| 44 | + |
| 45 | + public imageUrls: SafeUrl[]; |
| 46 | + |
| 47 | + private lastObjectUrl: string; |
| 48 | + private sanitizer: DomSanitizer; |
| 49 | + |
| 50 | + // I initialize the app component. |
| 51 | + constructor( sanitizer: DomSanitizer ) { |
| 52 | + |
| 53 | + this.sanitizer = sanitizer; |
| 54 | + |
| 55 | + this.imageUrls = []; |
| 56 | + this.lastObjectUrl = ""; |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + // --- |
| 61 | + // PUBLIC METHODS. |
| 62 | + // --- |
| 63 | + |
| 64 | + // I handle the paste event on the Window (see host bindings). |
| 65 | + public handlePaste( event: ClipboardEvent ) : void { |
| 66 | + |
| 67 | + var pastedImage = this.getPastedImage( event ); |
| 68 | + |
| 69 | + if ( ! pastedImage ) { |
| 70 | + |
| 71 | + return; |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + // When we create Object URLs, the browser will keep them in memory until the |
| 76 | + // document is unloaded or until the URL is explicitly released. Since we are |
| 77 | + // going to create a new URL every time the user pastes an image into the app (in |
| 78 | + // this particular demo), we need to be sure to release the previous Object URL |
| 79 | + // before we create the new one. |
| 80 | + // -- |
| 81 | + // NOTE: One the Image is rendered in the DOM, releasing the Object URL will not |
| 82 | + // affect the rendering. |
| 83 | + if ( this.lastObjectUrl ) { |
| 84 | + |
| 85 | + URL.revokeObjectURL( this.lastObjectUrl ); |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + // At this point, the "pastedImage" is a File object, which is a specialized type |
| 90 | + // of "Blob". We can now generate a "blob:" URL using the given File. |
| 91 | + this.lastObjectUrl = URL.createObjectURL( pastedImage ); |
| 92 | + |
| 93 | + // By default, Angular WILL NOT TRUST this "blob:" style URLs. However, since we |
| 94 | + // know these are going to be expected, we can use the DOM Sanitizer to bypass |
| 95 | + // the security checks on these images. |
| 96 | + // -- |
| 97 | + // NOTE: The sanitizer doesn't return Strings - it returns SafeUrls. |
| 98 | + this.imageUrls.unshift( |
| 99 | + this.sanitizer.bypassSecurityTrustUrl( this.lastObjectUrl ) |
| 100 | + ); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + // --- |
| 105 | + // PRIVATE METHODS. |
| 106 | + // --- |
| 107 | + |
| 108 | + // I return the first Image File from the given paste event (or null). |
| 109 | + private getPastedImage( event: ClipboardEvent ) : File | null { |
| 110 | + |
| 111 | + // NOTE: I am not very familiar with the Paste Event. As such, I am probably |
| 112 | + // being more cautious here than I need to be. However, in an abundance of |
| 113 | + // caution, I am checking each part of the targeted object path. |
| 114 | + if ( |
| 115 | + event.clipboardData && |
| 116 | + event.clipboardData.files && |
| 117 | + event.clipboardData.files.length && |
| 118 | + this.isImageFile( event.clipboardData.files[ 0 ] ) |
| 119 | + ) { |
| 120 | + |
| 121 | + return( event.clipboardData.files[ 0 ] ); |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + return( null ); |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + // I determine if the given File is an Image (according do its Mime-Type). |
| 131 | + private isImageFile( file: File ) : boolean { |
| 132 | + |
| 133 | + return( file.type.search( /^image\//i ) === 0 ); |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments