-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathContentProjection.component.ts
More file actions
62 lines (55 loc) · 1.87 KB
/
ContentProjection.component.ts
File metadata and controls
62 lines (55 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* eslint-disable max-classes-per-file */
import { Component, TemplateRef, Input } from '@angular/core';
import { apiKey } from '../Settings';
/*
Normally when projecting content you would use ng-content but this is not something that works
with TinyMCE due to its usage of an iframe (when in iframe mode). The reason for this is that
the lifecycle of ng-content is controlled by its parent view and not where it's consumed. In
other words, the projected content can be initialized and destroyed when not actually being in
the DOM. Iframes will re-render whenever they are detached/attached to the DOM and thus this
breaks TinyMCE.
A workaround is to use a template outlet instead of content projection. The result is what you
would expect from content projection, but with the lifecycle being in sync to where the content
is being consumed.
*/
@Component({
selector: 'container',
styles: [ `
.container {
border: 1px solid blue;
display: block;
padding: 15px;
}
` ],
template: `
<ng-template #placeHolder>
<p>I am a placeholder.</p>
</ng-template>
<div [ngClass]="'container'">
<button [innerText]="show ? 'Hide' : 'Show'" (click)="handleToggle()"></button>
<ng-container *ngTemplateOutlet="show ? editorTemplate : placeHolder"></ng-container>
<div>
`,
standalone: false
})
export class ContainerComponent {
@Input() public editorTemplate!: TemplateRef<any>;
public show = true;
public handleToggle() {
this.show = !this.show;
}
}
@Component({
selector: 'content-projection',
template: `
<ng-template #editorTemplate>
<editor [apiKey]="apiKey" [(ngModel)]="editorValue"></editor>
</ng-template>
<container [editorTemplate]="editorTemplate"></container>
`,
standalone: false
})
export class ContentProjectionComponent {
public apiKey = apiKey;
public editorValue = '';
}