diff --git a/docs/additional-documentation/code-editor.html b/docs/additional-documentation/code-editor.html index 833ac1b7..ca43c6e2 100644 --- a/docs/additional-documentation/code-editor.html +++ b/docs/additional-documentation/code-editor.html @@ -67,12 +67,18 @@
To Upload images, you must specify an endpoint to which the image has to be uploaded. The POST request sent is a FORMDATA with name file
.
The Response is a json
and it should be in the following format
{
+ "url": "http://myApiEndPoint"
+}
The Input for the endpoint is a string can be provided directly or via config
property
<app-ngx-editor imageEndPoint="http://myApiEndPoint"></app-ngx-editor>
+ You must install all the following peerDependencies
in order for `ngx-editor to work
Bootstrap
is a temporary dependency and it will be removed sooner.
Import codemirror`s css into your project
+node_modules/codemirror/lib/codemirror.css
For ngModel
and image upload
work properly, you must inlcude FormsModule
and HttpClientModule
respectively in your project.
import { FormsModule } from '@angular/forms';
+import { HttpClientModule } from '@angular/common/http';
+ ElementRef
- api to access dom element
-ElementRef
+ api to access dom element
+
+ MessageService
- service to send message to the editor message component
-MessageService
+ service to send message to the editor message component
+
+ CommandExecutorService
- executes command from the toolbar
-CommandExecutorService
+ executes command from the toolbar
+
+ Renderer2
- access and manipulate the dom element
-Renderer2
+ access and manipulate the dom element
+
+ editable
Specifies weather the textarea to be editable or not
+Specifies weather the textarea to be editable or not
Type: boolean
@@ -727,7 +762,7 @@
enableToolbar
Weather to enable or disable the toolbar
+Weather to enable or disable the toolbar
Type: boolean
@@ -750,7 +785,7 @@
height
+
+ imageEndPoint
+ |
+
+ Endpoint for which the image to be uploaded
Type: Inputs | |||||||
- + |
- Sets minimum height for the editor +Sets minimum height for the editor
Type: Inputs
-
+
|
InputsminWidth
|
- Sets minimum width of the editor +Sets minimum width of the editor
Type: Inputs
-
+
|
Inputsplaceholder
|
- Placeholder for the textArea +Placeholder for the textArea
Type: Inputs
-
+
|
Inputs
-
+
|
InputsshowToolbar
|
- Weather to show or hide toolbar +Weather to show or hide toolbar
Type: Inputs
-
+
|
Inputsspellcheck
|
- The spellcheck property specifies whether the element is to have its spelling and grammar checked or not. +The spellcheck property specifies whether the element is to have its spelling and grammar checked or not.
Type: Inputs
-
+
|
Inputs
-
+
|
Inputs
-
+
|
Inputswidth
|
- Sets Width of the editor +Sets Width of the editor
Type: Inputs
-
+
|
Outputsblur
|
- emits emits $event type: EventEmitter<string>
@@ -1009,7 +1067,7 @@ Outputs
-
+
|
Outputsfocus
|
- emits emits $event type: EventEmitter<string>
@@ -1030,7 +1088,7 @@ Outputs
-
+
|
HostListeners
-
+
|
-
+
|
Name |
- Type |
+ Type |
Optional |
- Default value |
- Description |
|
any
-
- any
+ string
-
- html string from contenteditable
-string
+ html string from contenteditable
+
+ string
-
- html string from the editor
-string
+ html string from the editor
+
+ any
-
- a function
-any
+ a function
+
+ any
-
- a function
-any
+ a function
+
+ number
-
- vertical height of the eidtable portion of the editor
-number
+ vertical height of the eidtable portion of the editor
+
+ any
-
- value to be executed when there is a change in contenteditable
-any
+ value to be executed when there is a change in contenteditable
+
+ MessageService
- service to send message to the editor
-MessageService
+ service to send message to the editor
+
+ property that holds the message to be displayed on the editor
+PopoverConfig
- PopoverConfig
+ FormBuilder
- FormBuilder
+ MessageService
- MessageService
+ CommandExecutorService
- CommandExecutorService
+ + + + + buildInsertImageForm + + + + | +
+ buildInsertImageForm()
+ |
+
+ + | +
+ create insert image form +
+ Returns :
+ void
+
+ |
+
- + | |||||||||||||||
- + | |||||||||||||||
Name | -Type | Optional | -Default value | -Description | +Description | ||||||||||
value | -- | -- no - | -
-
- |
- name of the toolbar buttons - |
+ value | + + ++ no + | + + +
+
+ |
+ + + + insertImage + + + + | +
+ insertImage()
+ |
+
+ + | +
+ insert image in the editor +
+ Returns :
+ void
+
+ |
+
- + | |
- insert link +inserts link in the editor
@@ -800,6 +920,78 @@ |
+ + + + onFileChange + + + + | +||||||
+ onFileChange(e: )
+ |
+ ||||||
+ + | +||||||
+ Executed when file is selected +
+ Parameters :
+
+
+
+
+ Returns :
+ void
+
+
+
+
+ |
+
- + | ||||||||||||||||
Name | -Type | +Type | Optional | -Default value | -Description | +Description | ||||||||||
command | -
- string
- |
- - no - | -
-
- |
- name of the command to be executed - |
+ command | + +
+ string
+ |
+
+ + no + | + + +
+
+ |
+ + + + imageForm + + + + | +
+ imageForm:
+ |
+
+ Type : FormGroup
+
+ |
+
+ + | +
+ holds values of the insert image form + |
+
+ + + + imagePopover + + + + | +
+ imagePopover:
+ |
+
+ Decorators : ViewChild
+ |
+
+ + | +
+ + + + isUploading + + + + | +
+ isUploading:
+ |
+
+ Default value : false
+ |
+
+ + | +
+ set to true when the image is being uploaded + |
+
+ + + + updloadPercentage + + + + | +
+ updloadPercentage:
+ |
+
+ Type : number
+
+ |
+
+ Default value : 0
+ |
+
+ + | +
+ upload percentage + |
+
+ + + + uploadComplete + + + + | +
+ uploadComplete:
+ |
+
+ Default value : true
+ |
+
+ + | +
+ set to false when image is being uploaded + |
+
- + |
+ holds values of the insert link form + |
+
import { Component, Input, Output, EventEmitter, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
+import { HttpEvent, HttpEventType, HttpResponse } from '@angular/common/http';
import { PopoverConfig } from 'ngx-bootstrap';
import { CommandExecutorService } from '../common/services/command-executor.service';
import { MessageService } from '../common/services/message.service';
@@ -970,13 +1367,23 @@
export class NgxEditorToolbarComponent implements OnInit {
+ /** holds values of the insert link form */
urlForm: FormGroup;
+ /** holds values of the insert image form */
+ imageForm: FormGroup;
+ /** set to false when image is being uploaded */
+ uploadComplete = true;
+ /** upload percentage */
+ updloadPercentage = 0;
+ /** set to true when the image is being uploaded */
+ isUploading = false;
/**
* Editor configuration
*/
@Input() config: any;
@ViewChild('urlPopover') urlPopover;
+ @ViewChild('imagePopover') imagePopover;
/**
* Emits an event when a toolbar button is clicked
*/
@@ -1013,17 +1420,20 @@
* create URL insert form
*/
buildUrlForm(): void {
+
this.urlForm = this._formBuilder.group({
urlLink: ['', [Validators.required]],
urlText: ['', [Validators.required]],
urlNewTab: [true]
});
+
+ return;
}
/**
- * insert link
+ * inserts link in the editor
*/
- insertLink() {
+ insertLink(): void {
try {
this._commandExecutorService.createLink(this.urlForm.value);
@@ -1031,11 +1441,84 @@
this._messageService.sendMessage(error.message);
}
+ /** reset form to default */
+ this.buildUrlForm();
+ /** close inset URL pop up */
this.urlPopover.hide();
+
+ return;
+ }
+
+ /**
+ * create insert image form
+ */
+ buildInsertImageForm(): void {
+
+ this.imageForm = this._formBuilder.group({
+ imageUrl: ['', [Validators.required]]
+ });
+
+ return;
+ }
+
+ /**
+ * Executed when file is selected
+ *
+ * @param e onChange event
+ */
+ onFileChange(e): void {
+
+ this.uploadComplete = false;
+ this.isUploading = true;
+
+ if (e.target.files.length > 0) {
+ const file = e.target.files[0];
+
+ try {
+ this._commandExecutorService.uploadImage(file, this.config.imageEndPoint).subscribe(event => {
+
+ if (event.type) {
+ this.updloadPercentage = Math.round(100 * event.loaded / event.total);
+ }
+
+ if (event instanceof HttpResponse) {
+ try {
+ this._commandExecutorService.insertImage(event.body.url);
+ } catch (error) {
+ this._messageService.sendMessage(error.message);
+ }
+ this.uploadComplete = true;
+ this.isUploading = false;
+ }
+ });
+ } catch (error) {
+ this._messageService.sendMessage(error.message);
+ this.uploadComplete = true;
+ this.isUploading = false;
+ }
+
+ }
+
+ return;
+ }
+
+ /** insert image in the editor */
+ insertImage(): void {
+ try {
+ this._commandExecutorService.insertImage(this.imageForm.value.imageUrl);
+ } catch (error) {
+ this._messageService.sendMessage(error.message);
+ }
+
+ /** reset form to default */
+ this.buildInsertImageForm();
+ /** close inset URL pop up */
+ this.imagePopover.hide();
}
ngOnInit() {
this.buildUrlForm();
+ this.buildInsertImageForm();
}
}
@@ -1149,16 +1632,16 @@
</button>
</div>
<div class="ngx-toolbar-set">
- <button type="button" class="ngx-editor-button" *ngIf="canEnableToolbarOptions('link')" popoverTitle="Insert Link" [popover]="insertLinkTemplate"
- title="Insert/Edit Link" #urlPopover="bs-popover" [disabled]="!config['enableToolbar']">
+ <button type="button" class="ngx-editor-button" *ngIf="canEnableToolbarOptions('link')" (click)="buildUrlForm()" [popover]="insertLinkTemplate"
+ title="Insert Link" #urlPopover="bs-popover" [disabled]="!config['enableToolbar']">
<i class="fa fa-link" aria-hidden="true"></i>
</button>
<button type="button" class="ngx-editor-button" *ngIf="canEnableToolbarOptions('unlink')" (click)="triggerCommand('unlink')"
title="Unlink" [disabled]="!config['enableToolbar']">
<i class="fa fa-chain-broken" aria-hidden="true"></i>
</button>
- <button type="button" class="ngx-editor-button" *ngIf="canEnableToolbarOptions('image')" (click)="triggerCommand('image')"
- title="Insert Image" [disabled]="!config['enableToolbar']">
+ <button type="button" class="ngx-editor-button" *ngIf="canEnableToolbarOptions('image')" (click)="buildInsertImageForm()"
+ title="Insert Image" [popover]="insertImageTemplate" #imagePopover="bs-popover" [disabled]="!config['enableToolbar']">
<i class="fa fa-picture-o" aria-hidden="true"></i>
</button>
</div>
@@ -1170,24 +1653,69 @@
</div>
</div>
-<!-- Popover templates -->
+<!-- URL Popover template -->
<ng-template #insertLinkTemplate>
- <form class="ngxe-popover-form" [formGroup]="urlForm" (ngSubmit)="urlForm.valid && insertLink()" autocomplete="off">
- <div class="form-group">
- <label for="urlInput" class="small">URL</label>
- <input type="text" class="form-control-sm" id="URLInput" aria-describedby="emailHelp" placeholder="URL" formControlName="urlLink"
- required>
- </div>
- <div class="form-group">
- <label for="urlTextInput" class="small">Text</label>
- <input type="text" class="form-control-sm" id="urlTextInput" placeholder="Text" formControlName="urlText" required>
+ <div class="ngxe-popover-form extra-gt">
+ <form [formGroup]="urlForm" (ngSubmit)="urlForm.valid && insertLink()" autocomplete="off">
+ <div class="form-group">
+ <label for="urlInput" class="small">URL</label>
+ <input type="text" class="form-control-sm" id="URLInput" placeholder="URL" formControlName="urlLink" required>
+ </div>
+ <div class="form-group">
+ <label for="urlTextInput" class="small">Text</label>
+ <input type="text" class="form-control-sm" id="urlTextInput" placeholder="Text" formControlName="urlText" required>
+ </div>
+ <div class="form-check">
+ <input type="checkbox" class="form-check-input" id="urlNewTab" formControlName="urlNewTab">
+ <label class="form-check-label" for="urlNewTab">Open in new tab</label>
+ </div>
+ <button type="submit" class="btn-primary btn-sm btn">Submit</button>
+ </form>
+ </div>
+</ng-template>
+
+<!-- Image Uploader Popover template -->
+<ng-template #insertImageTemplate>
+ <div class="ngxe-popover-form image-ctnr">
+ <div class="imgc-topbar">
+ <button type="button" class="btn" (click)="isImageUploader = true">
+ <i class="fa fa-upload"></i>
+ </button>
+ <button type="button" class="btn" (click)="isImageUploader = false">
+ <i class="fa fa-link"></i>
+ </button>
</div>
- <div class="form-check">
- <input type="checkbox" class="form-check-input" id="urlNewTab" formControlName="urlNewTab">
- <label class="form-check-label" for="urlNewTab">Open in new tab</label>
+ <div class="imgc-ctnt">
+ <div *ngIf="isImageUploader; else insertImageLink"> </div>
+ <div *ngIf="!isImageUploader; else imageUploder"> </div>
+ <ng-template #imageUploder>
+ <div class="ngx-insert-img-ph">
+ <p *ngIf="uploadComplete">Choose Image</p>
+ <p *ngIf="!uploadComplete">
+ <span>Uploading Image</span>
+ <br>
+ <span>{{ updloadPercentage }} %</span>
+ </p>
+ <div class="ngxe-img-upl-frm">
+ <input type="file" (change)="onFileChange($event)" accept="image/*" [disabled]="isUploading" [style.cursor]="isUploading ? 'not-allowed': 'allowed'">
+ </div>
+ </div>
+ </ng-template>
+ <ng-template #insertImageLink>
+ <form class="extra-gt" [formGroup]="imageForm" (ngSubmit)="imageForm.valid && insertImage()" autocomplete="off">
+ <div class="form-group">
+ <label for="imageURLInput" class="small">URL</label>
+ <input type="text" class="form-control-sm" id="imageURLInput" placeholder="URL" formControlName="imageUrl" required>
+ </div>
+ <button type="submit" class="btn-primary btn-sm btn">Submit</button>
+ </form>
+ </ng-template>
+ <div class="progress" *ngIf="!uploadComplete">
+ <div class="progress-bar progress-bar-striped progress-bar-animated bg-success" [ngClass]="{'bg-danger': updloadPercentage<20, 'bg-warning': updloadPercentage<50, 'bg-success': updloadPercentage>=100}"
+ [style.width.%]="updloadPercentage"></div>
+ </div>
</div>
- <button type="submit" class="btn-primary btn-sm btn">Submit</button>
- </form>
+ </div>
</ng-template>