|
| 1 | +--- |
| 2 | +sidebar_label: Integration with Angular |
| 3 | +title: Integration with Angular |
| 4 | +description: You can learn about the integration with Angular in the documentation of the DHTMLX JavaScript RichText library. Browse developer guides and API reference, try out code examples and live demos, and download a free 30-day evaluation version of DHTMLX RichText. |
| 5 | +--- |
| 6 | + |
| 7 | +# Integration with Angular |
| 8 | + |
| 9 | +:::tip |
| 10 | +You should be familiar with basic concepts and patterns of **Angular** before reading this documentation. To refresh your knowledge, please refer to the [**Angular documentation**](https://angular.io/docs). |
| 11 | +::: |
| 12 | + |
| 13 | +DHTMLX RichText is compatible with **Angular**. We have prepared code examples on how to use DHTMLX RichText with **Angular**. For more information, refer to the corresponding [**Example on GitHub**](https://github.com/DHTMLX/angular-richtext-demo). |
| 14 | + |
| 15 | +## Creating a project |
| 16 | + |
| 17 | +:::info |
| 18 | +Before you start to create a new project, install [**Angular CLI**](https://angular.io/cli) and [**Node.js**](https://nodejs.org/en/). |
| 19 | +::: |
| 20 | + |
| 21 | +Create a new **my-angular-richtext-app** project using Angular CLI. Run the following command for this purpose: |
| 22 | + |
| 23 | +~~~json |
| 24 | +ng new my-angular-richtext-app |
| 25 | +~~~ |
| 26 | + |
| 27 | +:::note |
| 28 | +If you want to follow this guide, disable Server-Side Rendering (SSR) and Static Site Generation (SSG/Prerendering) when creating new Angular app! |
| 29 | +::: |
| 30 | + |
| 31 | +The command above installs all the necessary tools, so you don't need to run any additional commands. |
| 32 | + |
| 33 | +### Installation of dependencies |
| 34 | + |
| 35 | +Go to the new created app directory: |
| 36 | + |
| 37 | +~~~json |
| 38 | +cd my-angular-richtext-app |
| 39 | +~~~ |
| 40 | + |
| 41 | +Install dependencies and start the dev server. For this, use the [**yarn**](https://yarnpkg.com/) package manager: |
| 42 | + |
| 43 | +~~~json |
| 44 | +yarn |
| 45 | +yarn start |
| 46 | +~~~ |
| 47 | + |
| 48 | +The app should run on a localhost (for instance `http://localhost:3000`). |
| 49 | + |
| 50 | +## Creating RichText |
| 51 | + |
| 52 | +Now you should get the DHTMLX RichText source code. First of all, stop the app and proceed with installing the RichText package. |
| 53 | + |
| 54 | +### Step 1. Package installation |
| 55 | + |
| 56 | +Download the [**trial RichText package**](/how_to_start/#installing-richtext-via-npm-or-yarn) and follow steps mentioned in the README file. Note that trial RichText is available 30 days only. |
| 57 | + |
| 58 | +### Step 2. Component creation |
| 59 | + |
| 60 | +Now you need to create an Angular component, to add Richtext into the application. Create the **richtext** folder in the **src/app/** directory, add a new file into it and name it **richtext.component.ts**. |
| 61 | + |
| 62 | +#### Import source files |
| 63 | + |
| 64 | +Open the **richtext.component.ts** file and import RichText source files. Note that: |
| 65 | + |
| 66 | +- if you use PRO version and install the RichText package from a local folder, the imported path looks like this: |
| 67 | + |
| 68 | +~~~jsx |
| 69 | +import { Richtext} from 'dhx-richtext-package'; |
| 70 | +~~~ |
| 71 | + |
| 72 | +- if you use the trial version of RichText, specify the following path: |
| 73 | + |
| 74 | +~~~jsx |
| 75 | +import { Richtext} from '@dhx/trial-richtext'; |
| 76 | +~~~ |
| 77 | + |
| 78 | +In this tutorial you can see how to configure the **trial** version of RichText. |
| 79 | + |
| 80 | +#### Set containers and initialize the Richtext |
| 81 | + |
| 82 | +To display RichText on the page, you need to set a container for RichText, and initialize the component using the corresponding constructor: |
| 83 | + |
| 84 | +~~~jsx {1,8-11,15-18,24-31} title="Richtext.component.ts" |
| 85 | +import { Richtext} from '@dhx/trial-richtext'; |
| 86 | +import { Component, ElementRef, OnInit, ViewChild, OnDestroy, ViewEncapsulation} from '@angular/core'; |
| 87 | + |
| 88 | +@Component({ |
| 89 | + encapsulation: ViewEncapsulation.None, |
| 90 | + selector: "richtext", // a template name used in the "app.component.ts" file as <richtext /> |
| 91 | + styleUrls: ["./richtext.component.css"], // include the css file |
| 92 | + template: `<div class = "component_container"> |
| 93 | + <div #richtext_container class = "widget"></div> |
| 94 | + </div>` |
| 95 | +}) |
| 96 | + |
| 97 | +export class RichTextComponent implements OnInit, OnDestroy { |
| 98 | + // initialize container for RichText |
| 99 | + @ViewChild("richtext_container", { static: true }) richtext_container!: ElementRef; |
| 100 | + |
| 101 | + private _editor!: Richtext; |
| 102 | + |
| 103 | + ngOnInit() { |
| 104 | + // initialize the RichText component |
| 105 | + this._editor = new Richtext(this.richtext_container.nativeElement, {}); |
| 106 | + } |
| 107 | + |
| 108 | + ngOnDestroy(): void { |
| 109 | + this._editor.destructor(); // destruct RichText |
| 110 | + } |
| 111 | +} |
| 112 | +~~~ |
| 113 | + |
| 114 | +#### Adding styles |
| 115 | + |
| 116 | +To display RichText correctly, you need to provide the corresponding styles. For this purpose, you can create the **richtext.component.css** file in the **src/app/richtext/** directory and specify important styles for RichText and its container: |
| 117 | + |
| 118 | +~~~css title="Richtext.component.css" |
| 119 | +/* import RichText styles */ |
| 120 | +@import "@dhx/trial-richtext/codebase/richtext.css"; |
| 121 | + |
| 122 | +/* specify styles for initial page */ |
| 123 | +html, |
| 124 | +body{ |
| 125 | + height: 100%; |
| 126 | + padding: 0; |
| 127 | + margin: 0; |
| 128 | +} |
| 129 | + |
| 130 | +/* specify styles for RichText container */ |
| 131 | +.component_container { |
| 132 | + height: 100%; |
| 133 | + margin: 0 auto; |
| 134 | +} |
| 135 | + |
| 136 | +/* specify styles for RichText widget */ |
| 137 | +.widget { |
| 138 | + height: calc(100% - 56px); |
| 139 | +} |
| 140 | +~~~ |
| 141 | + |
| 142 | +#### Loading data |
| 143 | + |
| 144 | +To add data into RichText, you need to provide a data set. You can create the **data.ts** file in the **src/app/richtext/** directory and add some data into it: |
| 145 | + |
| 146 | +~~~jsx {2-6} title="data.ts" |
| 147 | +export function getData() { |
| 148 | + const value = ` |
| 149 | + <h2>RichText 2.0</h2> |
| 150 | + <p>Repository at <a href="https://git.webix.io/xbs/richtext">https://git.webix.io/xbs/richtext</a></p> |
| 151 | + <p><img src="https://placecats.com/500/300" style="width:500px;height:300px;"></p>`; |
| 152 | + return { value }; |
| 153 | +} |
| 154 | +~~~ |
| 155 | + |
| 156 | +Then open the ***richtext.component.ts*** file. Import the file with data and specify the corresponding data properties to the configuration object of RichText within the `ngOnInit()` method, as shown below: |
| 157 | + |
| 158 | +~~~jsx {2,23,25-27} title="Richtext.component.ts" |
| 159 | +import { Richtext} from '@dhx/trial-richtext'; |
| 160 | +import { getData } from "./data"; // import data |
| 161 | +import { Component, ElementRef, OnInit, ViewChild, OnDestroy, ViewEncapsulation} from '@angular/core'; |
| 162 | + |
| 163 | +@Component({ |
| 164 | + encapsulation: ViewEncapsulation.None, |
| 165 | + selector: "richtext", |
| 166 | + styleUrls: ["./richtext.component.css"], |
| 167 | + template: `<div class = "component_container"> |
| 168 | + <div #richtext_container class = "widget"></div> |
| 169 | + </div>` |
| 170 | +}) |
| 171 | + |
| 172 | +export class RichTextComponent implements OnInit, OnDestroy { |
| 173 | + @ViewChild("richtext_container", { static: true }) richtext_container!: ElementRef; |
| 174 | + |
| 175 | + private _editor!: RichText; |
| 176 | + |
| 177 | + ngOnInit() { |
| 178 | + const { value } = getData(); // initialize data property |
| 179 | + this._editor = new Richtext(this.richtext_container.nativeElement, { |
| 180 | + value |
| 181 | + // other configuration properties |
| 182 | + }); |
| 183 | + } |
| 184 | + |
| 185 | + ngOnDestroy(): void { |
| 186 | + this._editor.destructor(); |
| 187 | + } |
| 188 | +} |
| 189 | +~~~ |
| 190 | + |
| 191 | +You can also use the [`setValue()`](api/methods/set-value.md) method inside the `ngOnInit()` method of Angular to load data into RichText. |
| 192 | + |
| 193 | +~~~jsx {2,23,37-42} title="Richtext.component.ts" |
| 194 | +import { Richtext} from '@dhx/trial-richtext'; |
| 195 | +import { getData } from "./data"; // import data |
| 196 | +import { Component, ElementRef, OnInit, ViewChild, OnDestroy, ViewEncapsulation} from '@angular/core'; |
| 197 | + |
| 198 | +@Component({ |
| 199 | + encapsulation: ViewEncapsulation.None, |
| 200 | + selector: "richtext", |
| 201 | + styleUrls: ["./richtext.component.css"], |
| 202 | + template: `<div class = "component_container"> |
| 203 | + <div #richtext_container class = "widget"></div> |
| 204 | + </div>` |
| 205 | +}) |
| 206 | + |
| 207 | +export class RichTextComponent implements OnInit, OnDestroy { |
| 208 | + @ViewChild("richtext_container", { static: true }) richtext_container!: ElementRef; |
| 209 | + |
| 210 | + private _editor!: RichText; |
| 211 | + |
| 212 | + ngOnInit() { |
| 213 | + const { value } = getData(); // initialize data property |
| 214 | + this._editor = new Richtext(this.richtext_container.nativeElement, { |
| 215 | + // other configuration properties |
| 216 | + }); |
| 217 | + |
| 218 | + // apply the data via the setValue() method |
| 219 | + this._editor.setValue({ value }); |
| 220 | + } |
| 221 | + |
| 222 | + ngOnDestroy(): void { |
| 223 | + this._editor.destructor(); |
| 224 | + } |
| 225 | +} |
| 226 | +~~~ |
| 227 | + |
| 228 | +Now the RichText component is ready to use. When the element will be added to the page, it will initialize the RichText with data. You can provide necessary configuration settings as well. Visit our [RichText API docs](/category/api/) to check the full list of available properties. |
| 229 | + |
| 230 | +#### Handling events |
| 231 | + |
| 232 | +When a user makes some action in the RichText, it invokes an event. You can use these events to detect the action and run the desired code for it. See the [full list of events](/category/richtext-events/). |
| 233 | + |
| 234 | +Open the **richtext.component.ts** file and complete the `ngOnInit()` method in the following way: |
| 235 | + |
| 236 | +~~~jsx {5-7} title="Richtext.component.ts" |
| 237 | +// ... |
| 238 | +ngOnInit() { |
| 239 | + this._editor = new Richtext(this.richtext_container.nativeElement, {}); |
| 240 | + |
| 241 | + this._editor.api.on("print", () => { |
| 242 | + console.log("The document is printing"); |
| 243 | + }); |
| 244 | +} |
| 245 | + |
| 246 | +ngOnDestroy(): void { |
| 247 | + this._editor.destructor(); |
| 248 | +} |
| 249 | +~~~ |
| 250 | + |
| 251 | +### Step 3. Adding RichText into the app |
| 252 | + |
| 253 | +To add the ***RichTextComponent*** component into your app, open the ***src/app/app.component.ts*** file and replace the default code with the following one: |
| 254 | + |
| 255 | +~~~jsx {5} title="app.component.ts" |
| 256 | +import { Component } from "@angular/core"; |
| 257 | + |
| 258 | +@Component({ |
| 259 | + selector: "app-root", |
| 260 | + template: `<richtext/>` |
| 261 | +}) |
| 262 | +export class AppComponent { |
| 263 | + name = ""; |
| 264 | +} |
| 265 | +~~~ |
| 266 | + |
| 267 | +Then create the ***app.module.ts*** file in the ***src/app/*** directory and specify the *RichTextComponent* as shown below: |
| 268 | + |
| 269 | +~~~jsx {4-5,8} title="app.module.ts" |
| 270 | +import { NgModule } from "@angular/core"; |
| 271 | +import { BrowserModule } from "@angular/platform-browser"; |
| 272 | + |
| 273 | +import { AppComponent } from "./app.component"; |
| 274 | +import { RichTextComponent } from "./richtext/richtext.component"; |
| 275 | + |
| 276 | +@NgModule({ |
| 277 | + declarations: [AppComponent, RichTextComponent], |
| 278 | + imports: [BrowserModule], |
| 279 | + bootstrap: [AppComponent] |
| 280 | +}) |
| 281 | +export class AppModule {} |
| 282 | +~~~ |
| 283 | + |
| 284 | +The last step is to open the ***src/main.ts*** file and replace the existing code with the following one: |
| 285 | + |
| 286 | +~~~jsx title="main.ts" |
| 287 | +import { platformBrowserDynamic } from "@angular/platform-browser-dynamic"; |
| 288 | +import { AppModule } from "./app/app.module"; |
| 289 | +platformBrowserDynamic() |
| 290 | + .bootstrapModule(AppModule) |
| 291 | + .catch((err) => console.error(err)); |
| 292 | +~~~ |
| 293 | + |
| 294 | +After that, you can start the app to see RichText loaded with data on a page. |
| 295 | + |
| 296 | +[TODO] |
| 297 | + |
| 298 | +Now you know how to integrate DHTMLX RichText with Angular. You can customize the code according to your specific requirements. The final advanced example you can find on [**GitHub**](https://github.com/DHTMLX/angular-richtext-demo). |
0 commit comments