/* globals jQuery */ /** * Attaches items to DOM for Embedded SVG support * @module EmbeddedSVGEditDOM */ import EmbeddedSVGEdit from './embedapi.js'; const $ = jQuery; let svgCanvas = null; function handleSvgData (data, error) { if (error) { alert('error ' + error); } else { alert('Congratulations. Your SVG string is back in the host page, do with it what you will\n\n' + data); } } function loadSvg () { const svgexample = 'Layer 1'; svgCanvas.setSvgString(svgexample); } function saveSvg () { svgCanvas.getSvgString()(handleSvgData); } function exportPNG () { svgCanvas.getUIStrings()(function (uiStrings) { const str = uiStrings.notification.loadingImage; const exportWindow = window.open( 'data:text/html;charset=utf-8,' + encodeURIComponent('' + str + '

' + str + '

'), 'svg-edit-exportWindow' ); svgCanvas.rasterExport('PNG', null, exportWindow.name); }); } function exportPDF () { svgCanvas.getUIStrings()(function (uiStrings) { const str = uiStrings.notification.loadingImage; /** // If you want to handle the PDF blob yourself, do as follows svgCanvas.bind('exportedPDF', function (win, data) { alert(data.output); }); svgCanvas.exportPDF(); // Accepts two args: optionalWindowName supplied back to bound exportPDF handler and optional outputType (defaults to dataurlstring) return; */ const exportWindow = window.open( 'data:text/html;charset=utf-8,' + encodeURIComponent('' + str + '

' + str + '

'), 'svg-edit-exportWindow' ); svgCanvas.exportPDF(exportWindow.name); }); } // Add event handlers $('#load').click(loadSvg); $('#save').click(saveSvg); $('#exportPNG').click(exportPNG); $('#exportPDF').click(exportPDF); const iframe = $('' ); iframe[0].addEventListener('load', function () { svgCanvas = new EmbeddedSVGEdit(frame); // Hide main button, as we will be controlling new, load, save, etc. from the host document let doc; try { doc = frame.contentDocument || frame.contentWindow.document; } catch (err) { console.log('Blocked from accessing document'); return; } const mainButton = doc.getElementById('main_button'); mainButton.style.display = 'none'; }); $('body').append(iframe); const frame = document.getElementById('svgedit');