Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 33 additions & 29 deletions apps/docs/getting-started/frameworks/angular.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@ npm install superdoc
## Basic setup

```typescript
import { Component, ElementRef, ViewChild, OnInit, OnDestroy } from '@angular/core';
import { Component, ElementRef, viewChild, AfterViewInit, inject, DestroyRef } from '@angular/core';
import { SuperDoc } from 'superdoc';
import 'superdoc/style.css';

@Component({
selector: 'app-editor',
template: `<div #editor style="height: 700px"></div>`,
})
export class EditorComponent implements OnInit, OnDestroy {
@ViewChild('editor', { static: true }) editorRef!: ElementRef;
export class EditorComponent implements AfterViewInit {
private readonly editorRef = viewChild.required<ElementRef>('editor');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nit (applies to all three snippets): typing ElementRef<HTMLDivElement> gives nativeElement a real type instead of any. since the template is a <div>, it's a free win.

Suggested change
private readonly editorRef = viewChild.required<ElementRef>('editor');
private readonly editorRef = viewChild.required<ElementRef<HTMLDivElement>>('editor');

private superdoc: SuperDoc | null = null;

ngOnInit() {
constructor() {
inject(DestroyRef).onDestroy(() => this.superdoc?.destroy());
}

ngAfterViewInit() {
this.superdoc = new SuperDoc({
selector: this.editorRef.nativeElement,
selector: this.editorRef().nativeElement,
document: 'contract.docx',
documentMode: 'editing',
});
}

ngOnDestroy() {
this.superdoc?.destroy();
}
}
```

Expand All @@ -46,7 +46,7 @@ export class EditorComponent implements OnInit, OnDestroy {
A reusable editor component with mode switching and export:

```typescript
import { Component, ElementRef, ViewChild, Input, OnInit, OnDestroy } from '@angular/core';
import { Component, ElementRef, viewChild, input, AfterViewInit, inject, DestroyRef } from '@angular/core';
import { SuperDoc } from 'superdoc';
import 'superdoc/style.css';

Expand All @@ -62,27 +62,27 @@ import 'superdoc/style.css';
<div #editor style="height: 700px"></div>
`,
})
export class DocEditorComponent implements OnInit, OnDestroy {
@ViewChild('editor', { static: true }) editorRef!: ElementRef;
@Input() document!: File | string;
@Input() user?: { name: string; email: string };
export class DocEditorComponent implements AfterViewInit {
private readonly editorRef = viewChild.required<ElementRef>('editor');
readonly document = input.required<File | string>();
readonly user = input<{ name: string; email: string }>();

private superdoc: SuperDoc | null = null;

ngOnInit() {
constructor() {
inject(DestroyRef).onDestroy(() => this.superdoc?.destroy());
}

ngAfterViewInit() {
this.superdoc = new SuperDoc({
selector: this.editorRef.nativeElement,
document: this.document,
selector: this.editorRef().nativeElement,
document: this.document(),
documentMode: 'editing',
user: this.user,
user: this.user(),
onReady: () => console.log('Editor ready'),
});
}

ngOnDestroy() {
this.superdoc?.destroy();
}

setMode(mode: 'editing' | 'viewing' | 'suggesting') {
this.superdoc?.setDocumentMode(mode);
}
Expand All @@ -96,32 +96,36 @@ export class DocEditorComponent implements OnInit, OnDestroy {
## Handle file uploads

```typescript
import { Component, ElementRef, viewChild, inject, DestroyRef } from '@angular/core';
import { SuperDoc } from 'superdoc';
import 'superdoc/style.css';

@Component({
selector: 'app-upload-editor',
template: `
<input type="file" accept=".docx" (change)="onFileChange($event)" />
<div #editor style="height: 700px"></div>
`,
})
export class UploadEditorComponent implements OnDestroy {
@ViewChild('editor', { static: true }) editorRef!: ElementRef;
export class UploadEditorComponent {
private readonly editorRef = viewChild.required<ElementRef>('editor');
private superdoc: SuperDoc | null = null;

constructor() {
inject(DestroyRef).onDestroy(() => this.superdoc?.destroy());
}

onFileChange(event: Event) {
const file = (event.target as HTMLInputElement).files?.[0];
if (!file) return;

this.superdoc?.destroy();
this.superdoc = new SuperDoc({
selector: this.editorRef.nativeElement,
selector: this.editorRef().nativeElement,
document: file,
documentMode: 'editing',
});
}

ngOnDestroy() {
this.superdoc?.destroy();
}
}
```

Expand Down
Loading