Implement fileDialog.save for HTML5

This commit is contained in:
Joshua Granick
2017-11-01 11:04:51 -07:00
parent b4a26ca6f7
commit 1287b2e30d
2 changed files with 53 additions and 0 deletions

View File

@@ -57,6 +57,7 @@
<dependency name="extension-api" path="dependencies/extension-api" if="android" />
<dependency path="dependencies/howler.min.js" if="html5 howlerjs" />
<dependency path="dependencies/pako.min.js" if="html5" />
<dependency path="dependencies/FileSaver.min.js" if="html5" />
<dependency path="dependencies/webgl-debug.js" if="html5 webgl-debug" />
<dependency path="dependencies/stats.min.js" if="html5 stats" />
<dependency path="dependencies/angle/d3dcompiler_47.dll" if="windows angle" unless="static_link" />

View File

@@ -1,21 +1,30 @@
package lime.ui;
import haxe.io.Bytes;
import haxe.io.Path;
import lime._backend.native.NativeCFFI;
import lime.app.Event;
import lime.graphics.Image;
import lime.system.BackgroundWorker;
import lime.utils.ArrayBuffer;
import lime.utils.Resource;
#if sys
import sys.io.File;
#end
#if (js && html5)
import js.html.Blob;
#end
#if !lime_debug
@:fileXml('tags="haxe,release"')
@:noDebug
#end
@:access(lime._backend.native.NativeCFFI)
@:access(lime.graphics.Image)
class FileDialog {
@@ -190,6 +199,13 @@ class FileDialog {
public function save (data:Resource, filter:String = null, defaultPath:String = null, title:String = null):Bool {
if (data == null) {
onCancel.dispatch ();
return false;
}
#if desktop
var worker = new BackgroundWorker ();
@@ -226,6 +242,42 @@ class FileDialog {
return true;
#elseif (js && html5)
// TODO: Cleaner API for mimeType detection
var type = "application/octet-stream";
var defaultExtension = "";
if (Image.__isPNG (data)) {
type = "image/png";
defaultExtension = ".png";
} else if (Image.__isJPG (data)) {
type = "image/jpeg";
defaultExtension = ".jpg";
} else if (Image.__isGIF (data)) {
type = "image/gif";
defaultExtension = ".gif";
} else if (Image.__isWebP (data)) {
type = "image/webp";
defaultExtension = ".webp";
}
var path = defaultPath != null ? Path.withoutDirectory (defaultPath) : "download" + defaultExtension;
var buffer = (data:Bytes).getData ();
untyped window.saveAs (new Blob ([ buffer ], { type: type }), path, true);
onSave.dispatch (path);
return true;
#else
onCancel.dispatch ();