-
-
Notifications
You must be signed in to change notification settings - Fork 983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Guidelines for consuming this library in angular 2 #478
Comments
I'm React rather than Angular 2. If you get it working please let me know and I'll add something to the docs. I suggest asking StackOverflow. |
I need the same. Please help us. |
Maybe this will help you. First declare types in "typings.d.ts". Here is an example, please notice that I only added methods that I needed.
After that, you should create directive. Something like this.
Pay attention to tag and tag class, this is used as a selector in created directive.
I know that I am late, but maybe someone will find this useful. |
Does anyone have advice for consuming the contentWindow portion of it from angular2? |
@gausie I've not tried, but would expect it just to work. It's just a service running in the background on the page. If your using es6/TypeScript then I think you just need to import it. If it is more complex than that then when you get the solution please share here. |
Everything is working fine - had a little issue with tree-shaking but this works fine: import { iframeResizerContentWindow } from 'iframe-resizer';
// This needs to be referenced so it isn't tree-shaken.
iframeResizerContentWindow; // tslint:disable-line no-unused-expression |
That sounds like a bug with tree-shaking. Might be worth reporting over at WebPack. |
It would be great if @arminbaljic or someone else in the Angular community would publish an Angular wrapper to this project, like we have with https://github.com/davidjbradshaw/iframe-resizer-react |
Hi, @davidjbradshaw I have created a repository for angular wrapper: https://github.com/arminbaljic/ngx-iframe-resizer. This week basic version should be completed and later I could add some examples. Anyone who wants to join me is welcome. |
@arminbaljic It would be great to see this wrapper working. I'm trying to make your directive working but I'm not able... Thanks! |
+1 |
+2 |
Maybe this will help, more detailed explanation on how to use the library in Angular (2,4,5). First, you need install "typings" for the iframe-resizer library. In order to do that, run this command:
After that, you need to create a directive that will apply iframe-resizer to the iframe. Here is an example: import {AfterViewInit, Directive, ElementRef, OnDestroy} from '@angular/core';
import {IFrameComponent, iframeResizer} from 'iframe-resizer';
@Directive({
selector: '[appIframeResizer]'
})
export class IFrameResizerDirective implements AfterViewInit, OnDestroy {
component: IFrameComponent;
constructor(public element: ElementRef) {
}
ngAfterViewInit() {
const components = iframeResizer({
checkOrigin: false,
heightCalculationMethod: 'documentElementOffset',
log: false
}, this.element.nativeElement);
/* save component reference so we can close it later */
this.component = components && components.length > 0 ? components[0] : null;
}
ngOnDestroy(): void {
if (this.component && this.component.iFrameResizer) {
this.component.iFrameResizer.close();
}
}
} Add a directive to your iframe: <iframe appIframeResizer
class="thread-content"
[srcdoc]="processedDocument"
title="Thread Content"
scrolling="no" frameborder="0"></iframe> And the last thing you should do is to add a tag that will load "iframeResizer.contentWindow.js" in iframe. In order to do this, copy "iframeResizer.contentWindow.js" file to assets/js folder. There are different ways to append the script to iframe content. Personally, I used "DOMPurify" plugin to sanitize and convert a string to HTML. Then, create the script tag and append it to the content of iframe document: export class MessagePreviewComponent {
@Input() document: string;
constructor(private sanitizer: DomSanitizer) {
}
get processedDocument(): SafeHtml {
if (this.document) {
const sanitized = DOMPurify.sanitize(this.document, {FORBID_TAGS: ['script'], RETURN_DOM: true});
/* Add script tag */
const script = document.createElement('script');
script.src = 'assets/js/iframeResizer.contentWindow.js';
sanitized.appendChild(script);
/* Return result */
return this.sanitizer.bypassSecurityTrustHtml(sanitized.outerHTML);
}
return null;
}
} I hope this will help you :) |
There is a simpler version of "MessagePreviewComponent" that doesn't use DOMPurify plugin. export class MessagePreviewComponent {
@Input() document: string;
constructor(private sanitizer: DomSanitizer) {
}
get processedDocument(): SafeHtml {
if (this.document) {
/* Add script tag */
const script = document.createElement('script');
script.src = 'assets/js/iframeResizer.contentWindow.js';
/* Return result */
const content = this.document.concat(script.outerHTML);
return this.sanitizer.bypassSecurityTrustHtml(content);
}
return null;
}
} But please notice that this is a security risk. Use this version only if you are 100% sure that content of the iframe will not contain any harmful scripts. |
@arminbaljic thanks for the input on that. The only comment I have is this line.
If you have more than one iFrame on the page that will loose the reference all but the first iFrame. So I think it would be better to just do |
@davidjbradshaw In my case, only one iframe can be active on the page. The directive is directly used on the "iframe" element, so I am not sure how many components will Your suggestion is definitely something to look out for. |
If you only have one iFrame, it will return a one element array |
@davidjbradshaw if I'm not mistaken we will only want the first one [0] reference since it will be applied once for every iframe using this directive, it isn't it? I have a couple of iframes using the directive: <section class="admin-box">
<iframe [src]="boxOfficeUrl" frameborder="0" width="100%" height="750" iframe-resizer></iframe>
<iframe [src]="boxOfficeUrl" frameborder="0" width="100%" height="750" iframe-resizer></iframe>
</section> So the will resize independently: |
@alfupe of yes I see it is passing So basically, just ignore me :) |
How, when you use the directive in the iframe, is the To clarify, I have a component, on which i've placed 3 iframes in its html. I've got the directive setup and the attribute added to the iframe. The I'm not sure how i'm supposed to hook that |
@paqogomez : Simplified structure of my use case is this: <div class="thread-container">
<app-thread> ... </app-thread>
<app-thread> ... </app-thread>
<app-thread> ... </app-thread>
<app-thread>
<!-- Some elements -->
<!-- MessagePreviewComponent -->
<app-message-preview [document]="emailContent">
<!-- BEGIN: This is located in message-preview.component.html -->
<iframe appIframeResizer
class="thread-content"
[srcdoc]="processedDocument"
title="Thread Content"
scrolling="no"
frameborder="0">
</iframe>
<!-- END: This is located in message-preview.component.html -->
</app-message-preview>
</app-thread>
<app-thread> ... </app-thread>
<app-thread> ... </app-thread>
</div> As you can see, the iframe is inside of message preview component. The task of message preview component is to take "emailContent" passed as a document, do some processing with that and append tag that will fetch "iframeResizer.contentWindow.js". "iframeResizer.contentWindow.js" is needed inside iframe content in order for resizing to work (library requirements). From docs:
MessagePreviewComponent does not run in iframe because iframe content is executed in separated context than the main window so it has no idea about Angular. |
@arminbaljic - MessagePreviewComponent is given document of type string through input decorator. In my case I don't have document coming from same domain. Is there a way in Angular to convert a document into string by passing url? |
@chaitanya1248 Implement angular service that will get page content using HttpClient. If page content is loaded from another domain you will probably get The second option is to implement your own endpoint that will do the job for you. There are probably many tutorials on this topic. When you get response page, content in string form is available as Additionally, please notice that method |
@arminbaljic - Thank you for the response. Ill try the options available. |
@arminbaljic Is there any chance you've released your Angular wrapper on GitHub or through NPM at all? That is super useful and solved the problem that my team and I had at work. |
@arminbaljic , How can i use the resize function here . I have implemented this , but i have a problem . When i am resizing the window screen to the minimize , the height of the iframe increases, but when i try to maximize , the height of iframe doesn't get decreased . What can i do to achieve that ? |
@arminbaljic , i created a directive that will apply iframe-resizer, and a simpler version of "MessagePreviewComponent" but it shows me on page - null. |
FYI the NPM-types are broken in angular atm. see: #31020 |
@arminbaljic Sorry to bring this up long after your answer, but I'm unsure where to include your MessagePreviewComponent so that it injects the script on the iFrame. Will this work with a cross-domain iframe? |
Hi everyone. First you'll need a scriptLoadingService, to take teh js files and inject them dynamically in the code. Be sure to inject it on the app.module file asa provider, otherwise you'll be using a new instance of this service everytime, missing the loadedLibaries "cache" (to avoid loading a library again and again):
Then copy iframeResizer.js and iframeResizer.contentWindow.js files to your assets folder, lets say assets/js/ Finally, make an iframe wrapper component like this one: SCSS file:
Component file:
And the component template:
Also using a SafePipe, so HTML tags can be added without any problems:
Thinking seriously on making a wrapper component. |
@davidjbradshaw @arminbaljic I followed all your instruction but it's not working. Is this plugin working on Angular 9 ? |
@Elka74100 I have no idea, never used Angular, if you find that you need to do something new in version 9, can you please update this thread. |
@jsanta solution is the only one which worked fine for me. Though, when adding iframeResizer.js and iframeResizer.contentWindow.js don't forget to add iframeResizer.contentWindow.map and iframeResizer.map as well or it won't work properly. |
@arminbaljic What is the 'document' passed into the 'processedDocument' fucntion? |
I just wanted to mention this here in case anyone else is having problems getting the iframeResizerContentWindow working with an Angular application embedded as an iframe in a non-Angular web site. To solve this on my end, after the NPM install, I simply added the following to the polyfills.ts file on my Angular 12 project: import 'iframe-resizer/js/iframeResizer.contentWindow.js'; That was all I needed and everything started working. No other changes needed to my Angular project. |
@arminbaljic @clinicaloffice |
follow up to @clinicaloffice I use iframeResizerContentWindow just by adding line in angular.json file
... |
|
rmahmood19: You still need to have the iFrameResizer code in your parent page (the one with the iFrame in it), but for the Angular application that is being run inside the iFrame I only needed to import the library with NPM and add the import statement to my polyfills.ts file. |
First you should install types Below is a simple demo how responsive iframe responds to content. |
Hi, I'm trying to use it in version 4.3.9 for a mobile version and this fails, I know that it is not what I am using as the background, because it is in the web part, it works without problems, but in the mobile part it gives me some errors and it doesn't render correctly: i'm use two different animations for mobile and web. Thank you |
@jsanta, @spdi, @arminbaljic, @paqogomez and everyone else on this thread. I'm currently working on a large v5 update (#1212) to iframe-resizer and I would like to includes an The new version splits the NPM module into @iframe-resizer/parent and @iframe-resizer/child. I have just published an alpha build of version 5 on NPM and have created a draft documentation site This includes an upgrade guide and a guide for creating Framework components Would any of you be able to help create a small Angular wrapper component? |
Hi David First - thanks for all the work you put into this library! Much appreciated! Let me know if you are interested, you can hit me up via my Github profile. Cheers! André |
@monobasic thanks, I have just sent you an email. |
V5 guidelines now on website. |
Could you please provide guidelines for using this library in angular 2 project created with CLI/Webpack.
The text was updated successfully, but these errors were encountered: