From abc9cd6ffb6971998b65030db57462c2572cd9bd Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Thu, 22 May 2014 10:21:29 +0000 Subject: [PATCH] Move embedapi.html code which is not part of the API into embedapi-dom.js; demo export PDF in embedded editor; add new exportPDF method to canvas (and exposed to embedded editor) which can support JSON-able data URI string response, removing PDF exporting from rasterExport; JSLint; move dependency checking code for canvg and jsPDF to utilities; simplify Utils calls; allow for custom PDF export handler (but not yet implemented in server-based extensions); import PDF todo git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2860 eee81c28-f429-11dd-99c0-75d572ba1ddd --- editor/embedapi-dom.js | 76 ++++++++++++++++ editor/embedapi.html | 59 +----------- editor/embedapi.js | 2 +- editor/extensions/ext-server_moinsave.js | 8 +- editor/extensions/ext-server_opensave.js | 9 +- editor/svg-editor.js | 109 ++++++++--------------- editor/svgcanvas.js | 52 +++++++++-- editor/svgutils.js | 51 +++++++++-- 8 files changed, 216 insertions(+), 150 deletions(-) create mode 100644 editor/embedapi-dom.js diff --git a/editor/embedapi-dom.js b/editor/embedapi-dom.js new file mode 100644 index 00000000..f0c1f51e --- /dev/null +++ b/editor/embedapi-dom.js @@ -0,0 +1,76 @@ +/*globals $, EmbeddedSVGEdit*/ +/*jslint vars: true */ +var initEmbed; + +$(function () {'use strict'; + + var svgCanvas = null; + var frame; + + initEmbed = function () { + var doc, mainButton; + svgCanvas = new EmbeddedSVGEdit(frame); + // Hide main button, as we will be controlling new, load, save, etc. from the host document + doc = frame.contentDocument || frame.contentWindow.document; + mainButton = doc.getElementById('main_button'); + mainButton.style.display = 'none'; + }; + + 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() { + var svgexample = 'Layer 1'; + svgCanvas.setSvgString(svgexample); + } + + function saveSvg() { + svgCanvas.getSvgString()(handleSvgData); + } + + function exportPNG() { + var str = frame.contentWindow.svgEditor.uiStrings.notification.loadingImage; + var exportWindow = window.open( + 'data:text/html;charset=utf-8,' + encodeURIComponent('' + str + '

' + str + '

'), + 'svg-edit-exportWindow' + ); + svgCanvas.rasterExport('PNG', null, exportWindow.name); + } + + function exportPDF() { + var str = frame.contentWindow.svgEditor.uiStrings.notification.loadingImage; + + /** + // If you want to handle the PDF blob yourself, do as follows + svgCanvas.bind('exportedPDF', function (win, data) { + alert(data.dataurlstring); + }); + svgCanvas.exportPDF(); // Accepts two args: optionalWindowName supplied back to bound exportPDF handler and optionalOutputType (defaults to dataurlstring) + return; + */ + + var 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); + $('body').append( + $('' + ) + ); + frame = document.getElementById('svgedit'); +}); diff --git a/editor/embedapi.html b/editor/embedapi.html index d410f0d0..44ee3308 100644 --- a/editor/embedapi.html +++ b/editor/embedapi.html @@ -5,66 +5,13 @@ Embed API - + - + +
diff --git a/editor/embedapi.js b/editor/embedapi.js index ea4e45ee..b50a896b 100644 --- a/editor/embedapi.js +++ b/editor/embedapi.js @@ -120,7 +120,7 @@ function EmbeddedSVGEdit (frame, allowedOrigins) { // Run in svgedit itself var i, functions = [ - 'clearSvgContentElement', 'setIdPrefix', 'getCurrentDrawing', 'addSvgElementFromJson', 'getTransformList', 'matrixMultiply', 'hasMatrixTransform', 'transformListToTransform', 'convertToNum', 'findDefs', 'getUrlFromAttr', 'getHref', 'setHref', 'getBBox', 'getRotationAngle', 'getElem', 'getRefElem', 'assignAttributes', 'cleanupElement', 'remapElement', 'recalculateDimensions', 'sanitizeSvg', 'runExtensions', 'addExtension', 'round', 'getIntersectionList', 'getStrokedBBox', 'getVisibleElements', 'getVisibleElementsAndBBoxes', 'groupSvgElem', 'getId', 'getNextId', 'call', 'bind', 'prepareSvg', 'setRotationAngle', 'recalculateAllSelectedDimensions', 'clearSelection', 'addToSelection', 'selectOnly', 'removeFromSelection', 'selectAllInCurrentLayer', 'getMouseTarget', 'removeUnusedDefElems', 'svgCanvasToString', 'svgToString', 'embedImage', 'setGoodImage', 'open', 'save', 'rasterExport', 'getSvgString', 'randomizeIds', 'uniquifyElems', 'setUseData', 'convertGradients', 'convertToGroup', 'setSvgString', 'importSvgString', 'identifyLayers', 'createLayer', 'cloneLayer', 'deleteCurrentLayer', 'setCurrentLayer', 'renameCurrentLayer', 'setCurrentLayerPosition', 'setLayerVisibility', 'moveSelectedToLayer', 'mergeLayer', 'mergeAllLayers', 'leaveContext', 'setContext', 'clear', 'linkControlPoints', 'getContentElem', 'getRootElem', 'getSelectedElems', 'getResolution', 'getZoom', 'getVersion', 'setUiStrings', 'setConfig', 'getTitle', 'setGroupTitle', 'getDocumentTitle', 'setDocumentTitle', 'getEditorNS', 'setResolution', 'getOffset', 'setBBoxZoom', 'setZoom', 'getMode', 'setMode', 'getColor', 'setColor', 'setGradient', 'setPaint', 'setStrokePaint', 'setFillPaint', 'getStrokeWidth', 'setStrokeWidth', 'setStrokeAttr', 'getStyle', 'getOpacity', 'setOpacity', 'getFillOpacity', 'getStrokeOpacity', 'setPaintOpacity', 'getPaintOpacity', 'getBlur', 'setBlurNoUndo', 'setBlurOffsets', 'setBlur', 'getBold', 'setBold', 'getItalic', 'setItalic', 'getFontFamily', 'setFontFamily', 'setFontColor', 'getFontColor', 'getFontSize', 'setFontSize', 'getText', 'setTextContent', 'setImageURL', 'setLinkURL', 'setRectRadius', 'makeHyperlink', 'removeHyperlink', 'setSegType', 'convertToPath', 'changeSelectedAttribute', 'deleteSelectedElements', 'cutSelectedElements', 'copySelectedElements', 'pasteElements', 'groupSelectedElements', 'pushGroupProperties', 'ungroupSelectedElement', 'moveToTopSelectedElement', 'moveToBottomSelectedElement', 'moveUpDownSelected', 'moveSelectedElements', 'cloneSelectedElements', 'alignSelectedElements', 'updateCanvas', 'setBackground', 'cycleElement', 'getPrivateMethods', 'zoomChanged', 'ready' + 'clearSvgContentElement', 'setIdPrefix', 'getCurrentDrawing', 'addSvgElementFromJson', 'getTransformList', 'matrixMultiply', 'hasMatrixTransform', 'transformListToTransform', 'convertToNum', 'findDefs', 'getUrlFromAttr', 'getHref', 'setHref', 'getBBox', 'getRotationAngle', 'getElem', 'getRefElem', 'assignAttributes', 'cleanupElement', 'remapElement', 'recalculateDimensions', 'sanitizeSvg', 'runExtensions', 'addExtension', 'round', 'getIntersectionList', 'getStrokedBBox', 'getVisibleElements', 'getVisibleElementsAndBBoxes', 'groupSvgElem', 'getId', 'getNextId', 'call', 'bind', 'prepareSvg', 'setRotationAngle', 'recalculateAllSelectedDimensions', 'clearSelection', 'addToSelection', 'selectOnly', 'removeFromSelection', 'selectAllInCurrentLayer', 'getMouseTarget', 'removeUnusedDefElems', 'svgCanvasToString', 'svgToString', 'embedImage', 'setGoodImage', 'open', 'save', 'rasterExport', 'exportPDF', 'getSvgString', 'randomizeIds', 'uniquifyElems', 'setUseData', 'convertGradients', 'convertToGroup', 'setSvgString', 'importSvgString', 'identifyLayers', 'createLayer', 'cloneLayer', 'deleteCurrentLayer', 'setCurrentLayer', 'renameCurrentLayer', 'setCurrentLayerPosition', 'setLayerVisibility', 'moveSelectedToLayer', 'mergeLayer', 'mergeAllLayers', 'leaveContext', 'setContext', 'clear', 'linkControlPoints', 'getContentElem', 'getRootElem', 'getSelectedElems', 'getResolution', 'getZoom', 'getVersion', 'setUiStrings', 'setConfig', 'getTitle', 'setGroupTitle', 'getDocumentTitle', 'setDocumentTitle', 'getEditorNS', 'setResolution', 'getOffset', 'setBBoxZoom', 'setZoom', 'getMode', 'setMode', 'getColor', 'setColor', 'setGradient', 'setPaint', 'setStrokePaint', 'setFillPaint', 'getStrokeWidth', 'setStrokeWidth', 'setStrokeAttr', 'getStyle', 'getOpacity', 'setOpacity', 'getFillOpacity', 'getStrokeOpacity', 'setPaintOpacity', 'getPaintOpacity', 'getBlur', 'setBlurNoUndo', 'setBlurOffsets', 'setBlur', 'getBold', 'setBold', 'getItalic', 'setItalic', 'getFontFamily', 'setFontFamily', 'setFontColor', 'getFontColor', 'getFontSize', 'setFontSize', 'getText', 'setTextContent', 'setImageURL', 'setLinkURL', 'setRectRadius', 'makeHyperlink', 'removeHyperlink', 'setSegType', 'convertToPath', 'changeSelectedAttribute', 'deleteSelectedElements', 'cutSelectedElements', 'copySelectedElements', 'pasteElements', 'groupSelectedElements', 'pushGroupProperties', 'ungroupSelectedElement', 'moveToTopSelectedElement', 'moveToBottomSelectedElement', 'moveUpDownSelected', 'moveSelectedElements', 'cloneSelectedElements', 'alignSelectedElements', 'updateCanvas', 'setBackground', 'cycleElement', 'getPrivateMethods', 'zoomChanged', 'ready' ]; // TODO: rewrite the following, it's pretty scary. diff --git a/editor/extensions/ext-server_moinsave.js b/editor/extensions/ext-server_moinsave.js index c71efa65..f11b9210 100644 --- a/editor/extensions/ext-server_moinsave.js +++ b/editor/extensions/ext-server_moinsave.js @@ -14,7 +14,7 @@ svgEditor.addExtension("server_opensave", { callback: function() {'use strict'; - + var Utils = svgedit.utilities; var save_svg_action = '/+modify'; // Create upload target (hidden iframe) @@ -25,18 +25,18 @@ svgEditor.addExtension("server_opensave", { var svg = "\n" + data; var qstr = $.param.querystring(); var name = qstr.substr(9).split('/+get/')[1]; - var svg_data = svgedit.utilities.encode64(svg); + var svg_data = Utils.encode64(svg); if(!$('#export_canvas').length) { $('', {id: 'export_canvas'}).hide().appendTo('body'); } var c = $('#export_canvas')[0]; c.width = svgCanvas.contentW; c.height = svgCanvas.contentH; - svgEditor.buildCanvgCallback(function () { + Utils.buildCanvgCallback(function () { canvg(c, svg, {renderCallback: function() { var datauri = c.toDataURL('image/png'); // var uiStrings = svgEditor.uiStrings; - var png_data = svgedit.utilities.encode64(datauri); + var png_data = Utils.encode64(datauri); var form = $('
').attr({ method: 'post', action: save_svg_action + '/' + name, diff --git a/editor/extensions/ext-server_opensave.js b/editor/extensions/ext-server_opensave.js index b4ccf293..5ac21552 100644 --- a/editor/extensions/ext-server_opensave.js +++ b/editor/extensions/ext-server_opensave.js @@ -34,7 +34,8 @@ svgEditor.addExtension("server_opensave", { save_svg_action = svgEditor.curConfig.extPath + 'filesave.php', save_img_action = svgEditor.curConfig.extPath + 'filesave.php', // Create upload target (hidden iframe) - cancelled = false; + cancelled = false, + Utils = svgedit.utilities; $('