From baf9f759828222a23d119693678fb8b0b85c4685 Mon Sep 17 00:00:00 2001 From: Alexis Deveria Date: Tue, 25 Aug 2009 16:35:57 +0000 Subject: [PATCH] Possibly fixed issue 99 and issue 108 by setting up an external handler function. Moved Opera references to seperate file and reprocessed thinker.li's patch to also use a use separate file git-svn-id: http://svg-edit.googlecode.com/svn/trunk@468 eee81c28-f429-11dd-99c0-75d572ba1ddd --- editor/svg-editor.html | 8 +-- editor/svg-editor.js | 45 ++++---------- editor/svgcanvas.js | 35 +---------- firefox-extension/build.sh | 8 +++ firefox-extension/chrome.manifest | 4 +- firefox-extension/content/SVG-edit-overlay.js | 3 +- firefox-extension/handlers.js | 55 ++++++++++++++++ firefox-extension/mk_xpi.sh | 4 ++ opera-widget/handlers.js | 62 +++++++++++++++++++ 9 files changed, 150 insertions(+), 74 deletions(-) create mode 100644 firefox-extension/build.sh create mode 100644 firefox-extension/handlers.js create mode 100644 firefox-extension/mk_xpi.sh create mode 100644 opera-widget/handlers.js diff --git a/editor/svg-editor.html b/editor/svg-editor.html index 36373cf7..14773da1 100644 --- a/editor/svg-editor.html +++ b/editor/svg-editor.html @@ -20,6 +20,8 @@ + + SVG-edit demo @@ -42,7 +44,7 @@
| Clear - + Save Source
@@ -286,9 +288,5 @@ - - diff --git a/editor/svg-editor.js b/editor/svg-editor.js index 408ecc85..bcf2c0ed 100644 --- a/editor/svg-editor.js +++ b/editor/svg-editor.js @@ -36,35 +36,7 @@ function svg_edit_setup() { // with a gradient will appear black in Firefox, etc. See bug 308590 // https://bugzilla.mozilla.org/show_bug.cgi?id=308590 var saveHandler = function(window,svg) { - if(window.opera && window.opera.io && window.opera.io.filesystem) - { - try { - window.opera.io.filesystem.browseForSave( - new Date().getTime(), /* mountpoint name */ - "", /* default location */ - function(file) { - try { - if (file) { - var fstream = file.open(file, "w"); - fstream.write(svg, "UTF-8"); - fstream.close(); - } - } - catch(e) { - console.log("Write to file failed."); - } - }, - false /* not persistent */ - ); - } - catch(e) { - console.log("Save file failed."); - } - } - else - { - window.open("data:image/svg+xml;base64," + Utils.encode64(svg)); - } + window.open("data:image/svg+xml;base64," + Utils.encode64(svg)); }; // called when we've selected a different element @@ -924,13 +896,20 @@ function svg_edit_setup() { $('#stroke_width').SpinButton({ min: 1, max: 99, step: 1, callback: changeStrokeWidth }); $('#angle').SpinButton({ min: -180, max: 180, step: 5, callback: changeRotationAngle }); - // if Opera and in widget form, enable the Open button - if (window.opera) { - opera.postError('opera.io=' + opera.io); - if(opera && opera.io && opera.io.filesystem) { + svgCanvas.setCustomHandlers = function(opts) { + if(opts.open) { $('#tool_open').show(); + svgCanvas.bind("opened", opts.open); + } + if(opts.save) { + svgCanvas.bind("saved", opts.save); } } return svgCanvas; }; + +// This happens when the page is loaded +$(function() { + svgCanvas = svg_edit_setup(); +}); diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index b0301940..4379f558 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -1985,38 +1985,9 @@ function SvgCanvas(c) // public functions - this.open = function() { - if(window.opera && window.opera.io && window.opera.io.filesystem) - { - try { - window.opera.io.filesystem.browseForFile( - new Date().getTime(), /* mountpoint name */ - "", /* default location */ - function(file) { - try { - if (file) { - fstream = file.open(file, "r"); - var output = ""; - while (!fstream.eof) { - output += fstream.readLine("UTF-16"); - } - - canvas.setSvgString(output); /* 'this' is bound to the filestream object here */ - } - } - catch(e) { - console.log("Reading file failed."); - } - }, - false, /* not persistent */ - false, /* no multiple selections */ - "*.svg" /* file extension filter */ - ); - } - catch(e) { - console.log("Open file failed."); - } - } + this.open = function(str) { + // Nothing by default, handled by optional widget/extention + call("opened", str); }; this.save = function() { diff --git a/firefox-extension/build.sh b/firefox-extension/build.sh new file mode 100644 index 00000000..b4b5c48e --- /dev/null +++ b/firefox-extension/build.sh @@ -0,0 +1,8 @@ +#!/bin/sh +DST="content/editor" +if [ -e "${DST}" ]; then + rm -rf "${DST}" +fi +cp -R ../editor content/ +SVNS=`find content/editor -name '.svn'` +rm -rf ${SVNS} diff --git a/firefox-extension/chrome.manifest b/firefox-extension/chrome.manifest index 1c2d6c6b..820514b2 100644 --- a/firefox-extension/chrome.manifest +++ b/firefox-extension/chrome.manifest @@ -1,2 +1,2 @@ -content SVG-edit content/ -overlay chrome://browser/content/browser.xul chrome://SVG-edit/content/SVG-edit-overlay.xul +content SVG-edit content/ +overlay chrome://browser/content/browser.xul chrome://SVG-edit/content/SVG-edit-overlay.xul diff --git a/firefox-extension/content/SVG-edit-overlay.js b/firefox-extension/content/SVG-edit-overlay.js index 6b6470a8..683aefe9 100644 --- a/firefox-extension/content/SVG-edit-overlay.js +++ b/firefox-extension/content/SVG-edit-overlay.js @@ -1,7 +1,6 @@ function start_svg_edit() { var url = "chrome://SVG-edit/content/editor/svg-editor.html"; - var browser = document.getElementById("content"); window.openDialog(url, "SVG Editor", - "width=1024,height=700,menubar=no,toolbar=no"); + "width=1024,height=700,menubar=no,toolbar=no"); } diff --git a/firefox-extension/handlers.js b/firefox-extension/handlers.js new file mode 100644 index 00000000..e0c29fdc --- /dev/null +++ b/firefox-extension/handlers.js @@ -0,0 +1,55 @@ +// Note: This JavaScript file must be included as the last script on the main HTML editor page to override the open/close handlers +$(function() { + if(!window.Components) return; + + function moz_file_picker(readflag) { + var fp = window.Components.classes["@mozilla.org/filepicker;1"]. + createInstance(Components.interfaces.nsIFilePicker); + if(readflag) + fp.init(window, "Pick a SVG file", fp.modeOpen); + else + fp.init(window, "Pick a SVG file", fp.modeSave); + fp.defaultExtension = "*.svg"; + fp.show(); + return fp.file; + } + + svgCanvas.setCustomHandlers({ + 'open':function() { + try { + netscape.security.PrivilegeManager. + enablePrivilege("UniversalXPConnect"); + var file = moz_file_picker(true); + if(!file) + return(null); + + var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream); + inputStream.init(file, 0x01, 00004, null); + var sInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream); + sInputStream.init(inputStream); + canvas.setSvgString(sInputStream. + read(sInputStream.available())); + } catch(e) { + console.log("Exception while attempting to load" + e); + } + }, + 'save':function(svg) { + try { + var file = moz_file_picker(false); + if(!file) + return; + + if (!file.exists()) + file.create(0, 0664); + + var out = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream); + out.init(file, 0x20 | 0x02, 00004,null); + out.write(str, str.length); + out.flush(); + out.close(); + } catch(e) { + alert(e); + } + } + }); +}); \ No newline at end of file diff --git a/firefox-extension/mk_xpi.sh b/firefox-extension/mk_xpi.sh new file mode 100644 index 00000000..2da358e6 --- /dev/null +++ b/firefox-extension/mk_xpi.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +./build.sh +zip -r ../svg-edit.xpi * diff --git a/opera-widget/handlers.js b/opera-widget/handlers.js new file mode 100644 index 00000000..d4364a87 --- /dev/null +++ b/opera-widget/handlers.js @@ -0,0 +1,62 @@ +// Note: This JavaScript file must be included as the last script on the main HTML editor page to override the open/close handlers +$(function() { + if(window.opera && window.opera.io && window.opera.io.filesystem) { + svgCanvas.setCustomHandlers({ + 'open':function() { + try { + window.opera.io.filesystem.browseForFile( + new Date().getTime(), /* mountpoint name */ + "", /* default location */ + function(file) { + try { + if (file) { + fstream = file.open(file, "r"); + var output = ""; + while (!fstream.eof) { + output += fstream.readLine("UTF-16"); + } + + svgCanvas.setSvgString(output); /* 'this' is bound to the filestream object here */ + } + } + catch(e) { + console.log("Reading file failed."); + } + }, + false, /* not persistent */ + false, /* no multiple selections */ + "*.svg" /* file extension filter */ + ); + } + catch(e) { + console.log("Open file failed."); + } + + }, + 'save':function(svg) { + try { + window.opera.io.filesystem.browseForSave( + new Date().getTime(), /* mountpoint name */ + "", /* default location */ + function(file) { + try { + if (file) { + var fstream = file.open(file, "w"); + fstream.write(svg, "UTF-8"); + fstream.close(); + } + } + catch(e) { + console.log("Write to file failed."); + } + }, + false /* not persistent */ + ); + } + catch(e) { + console.log("Save file failed."); + } + } + }); + } +}); \ No newline at end of file