|
1 | 1 | --- |
2 | | -title: Angular |
| 2 | +title: Angular Integration |
| 3 | +sidebarTitle: Angular |
3 | 4 | keywords: "angular docx editor, angular word component, superdoc angular, angular document editor, angular integration" |
4 | 5 | --- |
5 | 6 |
|
6 | | -SuperDoc works with Angular through direct DOM manipulation. |
| 7 | +SuperDoc works with Angular through direct DOM manipulation. No wrapper needed. |
7 | 8 |
|
8 | | -## Installation |
| 9 | +## Install |
9 | 10 |
|
10 | 11 | ```bash |
11 | 12 | npm install superdoc |
12 | 13 | ``` |
13 | 14 |
|
14 | | -## Basic component |
| 15 | +## Basic setup |
15 | 16 |
|
16 | 17 | ```typescript |
17 | | -import { Component, ElementRef, ViewChild, OnInit, OnDestroy } from '@angular/core'; |
| 18 | +import { Component, ElementRef, ViewChild, OnDestroy } from '@angular/core'; |
18 | 19 | import { SuperDoc } from 'superdoc'; |
| 20 | +import 'superdoc/style.css'; |
19 | 21 |
|
20 | 22 | @Component({ |
21 | | - selector: 'app-document-editor', |
22 | | - template: ` |
23 | | - <div #editor class="editor-container"></div> |
24 | | - `, |
25 | | - styles: [` |
26 | | - .editor-container { |
27 | | - height: 700px; |
28 | | - } |
29 | | - `] |
| 23 | + selector: 'app-editor', |
| 24 | + template: `<div #editor style="height: 700px"></div>`, |
30 | 25 | }) |
31 | | -export class DocumentEditorComponent implements OnInit, OnDestroy { |
32 | | - @ViewChild('editor', { static: true }) editorElement!: ElementRef; |
| 26 | +export class EditorComponent implements OnDestroy { |
| 27 | + @ViewChild('editor', { static: true }) editorRef!: ElementRef; |
33 | 28 | private superdoc: SuperDoc | null = null; |
34 | 29 |
|
35 | 30 | ngOnInit() { |
36 | 31 | this.superdoc = new SuperDoc({ |
37 | | - selector: this.editorElement.nativeElement, |
38 | | - document: 'contract.docx' |
| 32 | + selector: this.editorRef.nativeElement, |
| 33 | + document: 'contract.docx', |
| 34 | + documentMode: 'editing', |
39 | 35 | }); |
40 | 36 | } |
41 | 37 |
|
42 | 38 | ngOnDestroy() { |
43 | | - this.superdoc = null; |
44 | | - } |
45 | | - |
46 | | - async exportDocument() { |
47 | | - if (this.superdoc) { |
48 | | - await this.superdoc.export(); |
49 | | - } |
| 39 | + this.superdoc?.destroy(); |
50 | 40 | } |
51 | 41 | } |
52 | 42 | ``` |
53 | 43 |
|
54 | | -## With file input |
| 44 | +## Full component |
| 45 | + |
| 46 | +A reusable editor component with mode switching and export: |
55 | 47 |
|
56 | 48 | ```typescript |
| 49 | +import { Component, ElementRef, ViewChild, Input, OnDestroy } from '@angular/core'; |
| 50 | +import { SuperDoc } from 'superdoc'; |
| 51 | +import 'superdoc/style.css'; |
| 52 | + |
57 | 53 | @Component({ |
58 | | - selector: 'app-editor', |
| 54 | + selector: 'app-doc-editor', |
59 | 55 | template: ` |
60 | | - <input type="file" accept=".docx" (change)="onFileSelected($event)"> |
61 | | - <div #editor></div> |
62 | | - ` |
| 56 | + <div class="controls"> |
| 57 | + <button (click)="setMode('editing')">Edit</button> |
| 58 | + <button (click)="setMode('suggesting')">Review</button> |
| 59 | + <button (click)="setMode('viewing')">View</button> |
| 60 | + <button (click)="exportDoc()">Export</button> |
| 61 | + </div> |
| 62 | + <div #editor style="height: 700px"></div> |
| 63 | + `, |
63 | 64 | }) |
64 | | -export class EditorComponent { |
65 | | - @ViewChild('editor', { static: false }) editorElement!: ElementRef; |
| 65 | +export class DocEditorComponent implements OnDestroy { |
| 66 | + @ViewChild('editor', { static: true }) editorRef!: ElementRef; |
| 67 | + @Input() document!: File | string; |
| 68 | + @Input() user?: { name: string; email: string }; |
| 69 | + |
66 | 70 | private superdoc: SuperDoc | null = null; |
67 | 71 |
|
68 | | - onFileSelected(event: Event) { |
69 | | - const file = (event.target as HTMLInputElement).files?.[0]; |
70 | | - if (file && this.editorElement) { |
71 | | - this.superdoc = new SuperDoc({ |
72 | | - selector: this.editorElement.nativeElement, |
73 | | - document: file |
74 | | - }); |
75 | | - } |
| 72 | + ngOnInit() { |
| 73 | + this.superdoc = new SuperDoc({ |
| 74 | + selector: this.editorRef.nativeElement, |
| 75 | + document: this.document, |
| 76 | + documentMode: 'editing', |
| 77 | + user: this.user, |
| 78 | + onReady: () => console.log('Editor ready'), |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + ngOnDestroy() { |
| 83 | + this.superdoc?.destroy(); |
| 84 | + } |
| 85 | + |
| 86 | + setMode(mode: 'editing' | 'viewing' | 'suggesting') { |
| 87 | + this.superdoc?.setDocumentMode(mode); |
| 88 | + } |
| 89 | + |
| 90 | + async exportDoc() { |
| 91 | + await this.superdoc?.export({ isFinalDoc: true }); |
76 | 92 | } |
77 | 93 | } |
78 | 94 | ``` |
79 | 95 |
|
80 | | -## Service pattern |
| 96 | +## Handle file uploads |
81 | 97 |
|
82 | 98 | ```typescript |
83 | | -@Injectable({ providedIn: 'root' }) |
84 | | -export class DocumentService { |
| 99 | +@Component({ |
| 100 | + selector: 'app-upload-editor', |
| 101 | + template: ` |
| 102 | + <input type="file" accept=".docx" (change)="onFileChange($event)" /> |
| 103 | + <div #editor style="height: 700px"></div> |
| 104 | + `, |
| 105 | +}) |
| 106 | +export class UploadEditorComponent implements OnDestroy { |
| 107 | + @ViewChild('editor', { static: true }) editorRef!: ElementRef; |
85 | 108 | private superdoc: SuperDoc | null = null; |
86 | 109 |
|
87 | | - initialize(element: HTMLElement, document: File | string) { |
| 110 | + onFileChange(event: Event) { |
| 111 | + const file = (event.target as HTMLInputElement).files?.[0]; |
| 112 | + if (!file) return; |
| 113 | + |
| 114 | + this.superdoc?.destroy(); |
88 | 115 | this.superdoc = new SuperDoc({ |
89 | | - selector: element, |
90 | | - document |
| 116 | + selector: this.editorRef.nativeElement, |
| 117 | + document: file, |
| 118 | + documentMode: 'editing', |
91 | 119 | }); |
92 | | - return this.superdoc; |
93 | 120 | } |
94 | 121 |
|
95 | | - destroy() { |
96 | | - this.superdoc = null; |
| 122 | + ngOnDestroy() { |
| 123 | + this.superdoc?.destroy(); |
97 | 124 | } |
98 | 125 | } |
99 | | -``` |
| 126 | +``` |
| 127 | + |
| 128 | +## Next steps |
| 129 | + |
| 130 | +- [Configuration](/core/superdoc/configuration) - Full configuration options |
| 131 | +- [Methods](/core/superdoc/methods) - All available methods |
| 132 | +- [Angular Example](https://github.com/superdoc-dev/superdoc/tree/main/examples/getting-started/angular) - Working example on GitHub |
0 commit comments