Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 89adfd8

Browse files
committed
Adds upload and insert image feature Closes kolkov#5
1 parent dc2974f commit 89adfd8

6 files changed

Lines changed: 56 additions & 4 deletions

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kolkov/angular-editor",
3-
"version": "0.7.0",
3+
"version": "0.8.0",
44
"author": "Andrey Kolkov <[email protected]>",
55
"repository": "https://github.com/kolkov/angular-editor",
66
"license": "MIT",

src/lib/angular-editor-toolbar.component.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@
113113
title="Unlink" [disabled]="htmlMode">
114114
<i class="fa fa-chain-broken"></i>
115115
</button>
116+
<input
117+
style="display: none"
118+
type="file" (change)="onFileChanged($event)"
119+
#fileInput>
120+
<button type="button" class="angular-editor-button" (click)="fileInput.click()" title="Insert Image" [disabled]="htmlMode"><i class="fa fa-image"></i></button>
116121
<button type="button" title="Horizontal Line" class="angular-editor-button"
117122
(click)="triggerCommand('insertHorizontalRule')" [disabled]="htmlMode"><i class="fa fa-minus"></i></button>
118123
</div>

src/lib/angular-editor-toolbar.component.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {Component, EventEmitter, Output, Renderer2} from "@angular/core";
2-
import {AngularEditorService} from "./angular-editor.service";
2+
import {AngularEditorService, UploadResponse} from "./angular-editor.service";
3+
import {Observable} from "rxjs";
4+
import {HttpEvent, HttpResponse} from "@angular/common/http";
35

46
@Component({
57
selector: 'angular-editor-toolbar',
@@ -8,7 +10,6 @@ import {AngularEditorService} from "./angular-editor.service";
810
})
911

1012
export class AngularEditorToolbarComponent {
11-
1213
id = '';
1314
htmlMode = false;
1415

@@ -146,4 +147,16 @@ export class AngularEditorToolbarComponent {
146147
this._renderer.removeClass(backgroundColorPickerWrapper, "disabled");
147148
}
148149
}
150+
151+
/**
152+
* Upload image when file is selected
153+
*/
154+
onFileChanged(event) {
155+
const file = event.target.files[0];
156+
this.editorService.uploadImage(file).subscribe(event => {
157+
if (event instanceof HttpResponse) {
158+
this.editorService.insertImage(event.body.imageUrl)
159+
}
160+
});
161+
}
149162
}

src/lib/angular-editor.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class AngularEditorComponent implements OnInit, ControlValueAccessor, Aft
5151

5252
ngOnInit() {
5353
this.editorToolbar.id = this.id;
54+
this.editorService.uploadUrl = this.config.uploadUrl;
5455
if (this.config.defaultParagraphSeparator) {
5556
document.execCommand("defaultParagraphSeparator", false, "p");
5657
}

src/lib/angular-editor.service.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import {Injectable} from '@angular/core';
2+
import {HttpClient, HttpEvent} from "@angular/common/http";
3+
import {Observable} from "rxjs";
4+
5+
export interface UploadResponse {
6+
imageUrl: string;
7+
}
28

39
@Injectable({
410
providedIn: 'root'
@@ -7,8 +13,9 @@ export class AngularEditorService {
713

814
savedSelection: Range | null;
915
selectedText: string;
16+
uploadUrl: string;
1017

11-
constructor() {
18+
constructor(private http: HttpClient) {
1219
}
1320

1421
/**
@@ -136,4 +143,28 @@ export class AngularEditorService {
136143

137144
return true;
138145
}
146+
147+
/**
148+
* Upload file to uploadUrl
149+
* @param file
150+
*/
151+
uploadImage(file: File): Observable<HttpEvent<UploadResponse>>{
152+
153+
const uploadData: FormData = new FormData();
154+
155+
uploadData.append('file', file, file.name);
156+
157+
return this.http.post<UploadResponse>(this.uploadUrl, uploadData, {
158+
reportProgress: true,
159+
observe: 'events',
160+
});
161+
}
162+
163+
/**
164+
* Insert image with Url
165+
* @param imageUrl
166+
*/
167+
insertImage(imageUrl: string){
168+
document.execCommand('insertImage', false, imageUrl);
169+
}
139170
}

src/lib/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface AngularEditorConfig {
1212
defaultParagraphSeparator?: string;
1313
defaultFontName?: string;
1414
defaultFontSize?: '1' | '2' | '3' | '4' | '5' | '6' | '7' |string;
15+
uploadUrl?: string;
1516
}
1617

1718
export const angularEditorConfig: AngularEditorConfig = {
@@ -28,4 +29,5 @@ export const angularEditorConfig: AngularEditorConfig = {
2829
defaultParagraphSeparator: '',
2930
defaultFontName: '',
3031
defaultFontSize: '',
32+
uploadUrl: 'v1/image',
3133
};

0 commit comments

Comments
 (0)