grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

QUESTION: Adding inline scripts and updating old events

Open theSC0RP opened this issue 5 years ago • 8 comments

In that case, I suggest you access the document of the iframe and to append manually the inline script (to get the document use editor.Canvas.getDocument())

Originally posted by @artf in https://github.com/artf/grapesjs/issues/2012#issuecomment-496304498

Hey, @artf, I was going through the issue, #2012 and I have a doubt. If a change is made to the script e.g. change in alert msg on click. Then multiple events are added. How to remove the old events and add new ones (without saving and reloading). One way is to refresh the iframe but that leads to the loss of all the events (even the predefined ones). How would you suggest doing this?

theSC0RP avatar Apr 26 '21 08:04 theSC0RP

I tried to update the inline script using the following steps:

  • Get the document of the iframe: iframeDoc = editor.Canvas.getDocument()
  • Get the innerHTML of the body of the document: iframeDocBodyHtml = iframeDoc.body.innerHTML
  • Replace the innerHTML of the body: iframeDoc.body.innerHTML = iframeDocBodyHtml
  • Reattach the inline script

Doing this leads to a loss of the inbuilt grapesjs events like hover or component select (this is obvious as all the events are lost). Is there a way to reattach these grapesjs events? I also tried to do editor.Canvas.toggleFramesEvents(1) but it did not help.

theSC0RP avatar Apr 27 '21 05:04 theSC0RP

I tried to add the select and hover events by running component-select command by writing editor.trigger('core:component-select') but that didn't help either and doing editor.runCommand('core:component-select') gives an error. Am I missing something?

theSC0RP avatar Apr 28 '21 05:04 theSC0RP

Well, I think it won't be bad to add an easy way to force the rerender of the iframe (not a common case but for sure might be useful in some cases). Unfortunately, right now, you should try to use private methods and check if everything works as expected, then if you confirm, I'll be happy to add a listener for something like frame.trigger('rerender'). For now I'd ask you to try something like this

// Take the actual view of the frame
const frameView = editor.Canvas.getFrame().view;
// Once we remove the frameView, we have to rerender the frame 
// wrapper view (which contains different elements like toolbars and the frame itself).
const frameWrapView = frameView.config.frameWrapView;
// Now, remove the frameView and rerender the wrapper
frameView.remove();
frameWrapView.render();

artf avatar May 04 '21 12:05 artf

Hey, @artf. Thanks for the reply. This does not seem to work. I got the following errors after the above code was run.

Error

It works on the demo page though. If I'm not wrong, the demo uses the dev branch so my guess is that it's working there because the dev branch has a lot of new code in src/canvas.

theSC0RP avatar May 04 '21 13:05 theSC0RP

Yeah, you have to use the latest version.

artf avatar May 06 '21 22:05 artf

@artf, Is that version stable? I had doubts regarding the latest version because it has a warning icon along with the text "POSSIBLY BREAKING CHANGE".

Latest Version

theSC0RP avatar May 07 '21 16:05 theSC0RP

Hi @theSC0RP you can find here a paragraph about possible breaking changes.

artf avatar May 18 '21 11:05 artf

@artf. Thank you so much.

theSC0RP avatar Jun 24 '21 07:06 theSC0RP