Angular JS and Froala WYSIWYG Editor
Angular 1 Plugin
Angular 1 plugin for the Froala WYSIWYG HTML Editor is also available.
Installation instructions
Install angular-froala-wysiwyg
from npm
npm install angular-froala-wysiwyg
You will need CSS styles
<!-- index.html --> <link href="node_modules/froala-editor/css/froala_editor.pkgd.min.css" rel="stylesheet">
Update editor instructions
npm update froala-editor --save
Integration
Use with Angular CLI
Installing @angular/cli
Note: you can skip this part if you already have application generated.
npm install -g @angular/cli ng new my-app cd my-app
Add angular-froala-wysiwyg
- install
angular-froala-wysiwyg
npm install angular-froala-wysiwyg --save
- open
src/app/app.module.ts
and add
// Import Angular plugin. import { FroalaEditorModule, FroalaViewModule } from 'angular-froala-wysiwyg'; ... @NgModule({ ... imports: [FroalaEditorModule.forRoot(), FroalaViewModule.forRoot() ... ], ... })
- open
angular.json
file and insert a new entry into thestyles
array
"styles": [ "./node_modules/froala-editor/css/froala_editor.pkgd.min.css", "./node_modules/froala-editor/css/froala_style.min.css", ]
- in
angular.json
file insert a new entry into thescripts
array
"scripts": [ "./node_modules/froala-editor/js/froala_editor.pkgd.min.js" ]
- open
src/app/app.component.html
and add
<div id="edit" [froalaEditor]>Hello, Froala!</div>
Run angular-cli
ng serve
Use with ionic v2 or v3
Create Ionic app
Note: you can skip this part if you already have application generated.
npm install -g cordova ionic ionic start sample blank cd sample
Add angular-froala-wysiwyg
For v3 make sure that you use the latest version of ionic and also the latest version of angular.
Installing Froala Wysiwyg Editor in Ionic is fairly easy, it can be done using npm:
bash
npm install angular-froala-wysiwyg --save
- Inside
src/app/app.component.html
add
<ion-app> <ion-router-outlet></ion-router-outlet> <div id="edit" [froalaEditor]>Hello, Froala!</div> </ion-app>
- open
src/app/app.module.ts
and add
// Import Angular2 plugin. import { FroalaEditorModule, FroalaViewModule } from 'angular-froala-wysiwyg'; ...
Replace
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule]
with
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule,FroalaEditorModule.forRoot(), FroalaViewModule.forRoot()]
Inside
src/app/app-routing.module.ts
remove the line{ path: '', redirectTo: 'home', pathMatch: 'full' }
Inside
src/index.html
<script src="assets/js/froala_editor.pkgd.webpack.min.js" > </script> <link rel="stylesheet" href="assets/css/font-awesome.min.css"> <link rel="stylesheet" href="assets/css/froala_editor.pkgd.min.css"> <link rel="stylesheet" href="assets/css/froala_style.min.css">
- In
angular.json
change outpath of build to "outputPath": "src/assets" and insert following inside assets of build:javascript { "glob": "**/*", "input": "node_modules/froala-editor/css", "output": "css" }, { "glob": "**/*", "input": "node_modules/font-awesome/css", "output": "css" }, { "glob": "**/*", "input": "node_modules/font-awesome/fonts", "output": "fonts" }, { "glob": "**/*", "input": "node_modules/froala-editor/js", "output": "js" }
Run your App
ionic build ionic serve
Use with webpack
Create webpack app
Note: you can skip this part if you already have application generated.
git clone --depth 1 https://github.com/AngularClass/angular-starter.git cd angular-starter npm install
Add angular-froala-wysiwyg
- install
angular-froala-wysiwyg
npm install angular-froala-wysiwyg --save
- open
src/app/app.module.ts
and add
// Import the Froala Editor plugin. import "FroalaEditor"; // Import Angular plugin. import { FroalaEditorModule, FroalaViewModule } from 'angular-froala-wysiwyg'; ... @NgModule({ ... imports: [FroalaEditorModule.forRoot(), FroalaViewModule.forRoot(), ... ], ... })
- open
src/app/app.component.ts
and add to the template
<div id="edit" [froalaEditor]>Hello, Froala!</div>
- open
config/webpack.dev.js
and add the following toCopyWebpackPlugin
const path=require("path"); const webpack=require("webpack"); const srcDir=path.resolve(__dirname,'src'); ... resolve: { extensions: ['.js'], modules:[srcDir,'../node_modules/froala-editor/js','node_modules'], alias: { "FroalaEditor": 'froala_editor.pkgd.min.js' } }, plugins: [ new webpack.ProvidePlugin({ FroalaEditor: 'froala_editor.pkgd.min.js' }), ...
- open
config/webpack.common.js
var webpack = require('webpack');
- open
config/webpack.common.js
and add the following toCopyWebpackPlugin
{ from: 'node_modules/froala-editor/css/', to: 'assets/froala-editor/css/', },
- open
config/head-config.common.js
and add a new entry to link
{ rel: 'stylesheet', href: '/assets/froala-editor/css/froala_editor.pkgd.min.css' }, { rel: 'stylesheet', href: '/assets/froala-editor/css/froala_style.min.css' }
Run webpack app
npm run start
Use with angular-seed
Create angular-seed app
Note: you can skip this part if you already have application generated. For more details please also read: https://github.com/mgechev/angular-seed.
git clone --depth 1 https://github.com/mgechev/angular-seed.git cd angular-seed npm install
Add angular-froala-wysiwyg
- install
angular-froala-wysiwyg
npm install angular-froala-wysiwyg --save
- open
tools/config/project.config.ts
file and uncomment the following line from the top of the file
import { ExtendPackages } from './seed.config.interfaces';
- in
tools/config/project.config.ts
file add
... this.NPM_DEPENDENCIES = [ ...this.NPM_DEPENDENCIES, { src: 'froala-editor/js/froala_editor.pkgd.min.js', inject: 'libs' }, { src: 'froala-editor/css/froala_editor.pkgd.min.css', inject: true }, { src: 'froala-editor/css/froala_style.min.css', inject: true } ]; ... let additionalPackages: ExtendPackages[] = [ // required for dev build { name:'angular-froala-wysiwyg', path:'node_modules/angular-froala-wysiwyg/bundles/angular-froala-wysiwyg.umd.min.js' }, // required for prod build { name:'angular-froala-wysiwyg/*', path:'node_modules/angular-froala-wysiwyg/bundles/angular-froala-wysiwyg.umd.min.js' } ] this.addPackagesBundles(additionalPackages);
- open
src/client/app/home/home.module.ts
and add
// Import Angular2 plugin. import { FroalaEditorModule, FroalaViewModule } from 'angular-froala-wysiwyg'; ... @NgModule({ ... imports: [FroalaEditorModule.forRoot(), FroalaViewModule.forRoot() ... ], ... })
- open
src/client/app/home/home.component.html
and add
<div id="edit" [froalaEditor]>Hello, Froala!</div>
Run webpack app
npm run start
Use with system.js
and JIT
Create Angular app
Note: you can skip this part if you already have application generated.
git clone https://github.com/angular/quickstart.git angular-quickstart cd angular-quickstart npm install
Add angular-froala-wysiwyg
- install
angular-froala-wysiwyg
npm install angular-froala-wysiwyg --save
- open
src/index.html
and add
<link rel="stylesheet" href="node_modules/froala-editor/css/froala_editor.pkgd.min.css"> <link rel="stylesheet" href="node_modules/froala-editor/css/froala_style.min.css"> <script src="node_modules/froala-editor/js/froala_editor.pkgd.min.js"></script>
- open
src/app/app.module.ts
and add
// Import Angular2 plugin. import { FroalaEditorModule, FroalaViewModule } from 'angular-froala-wysiwyg'; ... @NgModule({ ... imports: [FroalaEditorModule.forRoot(), FroalaViewModule.forRoot(), ... ], ... })
- open
src/app/app.component.ts
file and add to the template
<div id="edit" [froalaEditor]>Hello, Froala!</div>
- open
src/systemjs.config.js
file and add to map
map: { ... 'angular-froala-wysiwyg': 'npm:angular-froala-wysiwyg/bundles/angular-froala-wysiwyg.umd.js', ... }
-
Run app
npm run start
Use with aot
Create Angular app
ng new froala-aot
npm install font-awesome
npm install froala-editor
- Go to
angular.json
and changearchitect.build.outputPath
tosrc/dist
and add following code toarchitect.build.options.assets
javascript { "glob": "**/*", "input": "./node_modules/froala-editor", "output": "assets/froala-editor/" }, { "glob": "**/*", "input": "./node_modules/font-awesome", "output": "assets/font-awesome/" }, { "glob": "**/*", "input": "./node_modules/jquery", "output": "assets/jquery/" }
- Go to
package.json
and updatescripts.build
tong build --aot
andscripts.start
tong serve --aot
Add angular-froala-wysiwyg
- install
angular-froala-wysiwyg
npm install angular-froala-wysiwyg --save
- open
src/index.html
and add
<link rel="stylesheet" href="assets/font-awesome/css/font-awesome.min.css"> <link rel="stylesheet" href="assets/froala-editor/css/froala_editor.pkgd.min.css"> <link rel="stylesheet" href="assets/froala-editor/css/froala_style.min.css"> <script src="assets/froala-editor/js/froala_editor.pkgd.min.js"></script>
- open
src/app/app.module.ts
and add
// Import Angular2 plugin. import { FroalaEditorModule, FroalaViewModule } from 'angular-froala-wysiwyg'; ... @NgModule({ ... imports: [FroalaEditorModule.forRoot(), FroalaViewModule.forRoot(), ... ], ... })
- open
src/app/app.component.ts
file and add to the template
<div id="edit" [froalaEditor]>Hello, Froala!</div>
Run app
npm run build npm run start
Usage
Options
You can pass editor options as Input (optional).
[froalaEditor]='options'
You can pass any existing Froala option. Consult the Froala documentation to view the list of all the available options:
public options: Object = { placeholderText: 'Edit Your Content Here!', charCounterCount: false }
Aditional option is used: * immediateAngularModelUpdate: (default: false) This option synchronizes the angular model as soon as a key is released in the editor. Note that it may affect performances.
Events and Methods
Events can be passed in with the options, with a key events and object where the key is the event name and the value is the callback function.
public options: Object = { placeholder: "Edit Me", events : { 'froalaEditor.focus' : function(e, editor) { console.log(editor.selection.get()); } } }
Using the editor instance from the arguments of the callback you can call editor methods as described in the method docs.
Froala events are described in the events docs.
Model
The WYSIWYG HTML editor content model.
[(froalaModel)]="editorContent"
Pass initial content:
public editorContent: string = 'My Document\'s Title'
Use the content in other places:
<input [ngModel]="editorContent"/> <input [(ngModel)]="editorContent"/> <!-- For two way binding -->
Other two way binding example:
<div [froalaEditor] [(froalaModel)]="editorContent"></div> <div [froalaEditor] [(froalaModel)]="editorContent"></div>
Use it with reactive forms:
<form [formGroup]="form" (ngSubmit)="onSubmit()"> <textarea [froalaEditor] formControlName="formModel"></textarea> <button type="submit">Submit</button> </form>
If you want to use two-way binding to display the form model in other places you must include [(froalaModel)]
:
<form [formGroup]="form" (ngSubmit)="onSubmit()"> <textarea [froalaEditor] formControlName="formModel" [(froalaModel)]="form.formModel"></textarea> <div [froalaView]="form.formModel"></div> <button type="submit">Submit</button> </form>
If you want to wrap froalaEditor directive into a component that supports reactive forms please see https://froala.component.ts from demo.
Extend functionality
You can extend the functionality by adding a custom button like bellow:
// We will make usage of the Init hook and make the implementation there. import { Component, OnInit } from '@angular/core'; declare var $ :any; @Component({ selector: 'app-demo', template: `<div class="sample"> <h2>Sample 11: Add Custom Button</h2> <div [froalaEditor]="options" [(froalaModel)]="content" ></div> </div>`, export class AppComponent implements OnInit{ ngOnInit () { $.FroalaEditor.DefineIcon('alert', {NAME: 'info'}); $.FroalaEditor.RegisterCommand('alert', { title: 'Hello', focus: false, undo: false, refreshAfterCallback: false, callback: () => { alert('Hello!', this); } }); } public options: Object = { charCounterCount: true, toolbarButtons: ['bold', 'italic', 'underline', 'paragraphFormat','alert'], toolbarButtonsXS: ['bold', 'italic', 'underline', 'paragraphFormat','alert'], toolbarButtonsSM: ['bold', 'italic', 'underline', 'paragraphFormat','alert'], toolbarButtonsMD: ['bold', 'italic', 'underline', 'paragraphFormat','alert'], }; }
Special tags
You can also use the editor on img, button, input and a tags:
<img [froalaEditor] [(froalaModel)]="imgObj"/>
The model must be an object containing the attributes for your special tags. Example:
public imgObj: Object = { src: 'path/to/image.jpg' };
The froalaModel will change as the attributes change during usage.
- froalaModel can contain a special attribute named innerHTML which inserts innerHTML in the element: If you are using 'button' tag, you can specify the button text like this:
public buttonModel: Object = { innerHTML: 'Click Me' };
As the button text is modified by the editor, the innerHTML attribute from buttonModel model will be modified too.
Specific option for special tags
- angularIgnoreAttrs: (default: null) This option is an array of attributes that you want to ignore when the editor updates the froalaModel:
public inputOptions: Object = { angularIgnoreAttrs: ['class', 'id'] };
Manual Initialization
Gets the functionality to operate on the editor: create, destroy and get editor instance. Use it if you want to manually initialize the editor.
(froalaInit)="initialize($event)"
Where initialize
is the name of a function in your component which will receive an object with different methods to control the editor initialization process.
public initialize(initControls) { this.initControls = initControls; this.deleteAll = function() { this.initControls.getEditor()('html.set', ''); }; }
The object received by the function will contain the following methods:
- initialize: Call this method to initialize the Froala Editor
- destroy: Call this method to destroy the Froala Editor
- getEditor: Call this method to retrieve the editor that was created. This method will return null if the editor was not yet created
Displaying HTML
To display content created with the froala editor use the froalaView directive.
[froalaView]="editorContent"
<div [froalaEditor] [(froalaModel)]="editorContent"></div> <div [froalaView]="editorContent"></div>