exupero / saveSvgAsPng

Save SVGs as PNGs from the browser.
MIT License
1.09k stars 362 forks source link

Fix download for iOS devices. (#163) #218

Closed pawelangelow closed 5 years ago

pawelangelow commented 5 years ago

Implement popup for "workaround" download in order to support iOS devices. Code split download method and introduce helpers.

exupero commented 5 years ago

Thanks for the fix. I added ba5c562d23cc740d10c00cd26c809befcf88251c which should do the same thing but with less indirection. Let me know if it doesn't work for you.

pawelangelow commented 5 years ago

@exupero, the actual problem is that you can't create popup ba5c562 there, because, as it is mentioned here, safari is blocking any call to window.open() which is made inside an async call. With few words: it is too late.

One possible solution is:

Pseudocode:

<button onClick=() => {
    const popup = window.open();
    processing.then((result) => {
        popup.updateContent(result);
    }
)}>

In the current codebase, popup initialisation has to be here, before return, and somehow pass the reference to the popup for using it here .

out$.download = (name, uri) => {
   ......
       // THIS WONT WORK, CHECK SCREENSHOT
        const popup = window.open();
       // Instead, use the reference created in the out$.saveSvg
        popup.document.title = name;
        popup.location.replace(uri);
      }
    }
  };
  out$.saveSvg = (el, name, options) => {
    const popup = window.open();
    return ....
  };
Screenshot 2019-05-09 at 11 35 09

If you create the popup in the event handler, everything works as expected.

Screenshot 2019-05-09 at 11 49 01
exupero commented 5 years ago

Thanks for the clarifications. Please check whether f0df944 works for you.

pawelangelow commented 5 years ago

Yup, it is working now. Thanks 😃

exupero commented 5 years ago

Published version 1.4.14 to NPM with these changes.