From 9905eb7fbb73f3f6a3b71dc6537aee49d4fc9352 Mon Sep 17 00:00:00 2001 From: logo4poop Date: Sun, 14 Jan 2024 17:40:11 -0500 Subject: [PATCH 1/2] [dialogs] Add web target support to openFile --- .../dialogs/runtime/src/ceramic/Dialogs.hx | 46 +++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/plugins/dialogs/runtime/src/ceramic/Dialogs.hx b/plugins/dialogs/runtime/src/ceramic/Dialogs.hx index f8bccf8c1..cbd909446 100644 --- a/plugins/dialogs/runtime/src/ceramic/Dialogs.hx +++ b/plugins/dialogs/runtime/src/ceramic/Dialogs.hx @@ -12,8 +12,15 @@ import ceramic.Shortcuts.*; using StringTools; +/** + * A way to create filesystem dialogs + */ class Dialogs { + /** + * Opens a file picker + * IMPORTANT: On non-electron web targets, the dialog will only open on click events + */ public static function openFile(title:String, ?filters:Array, done:(file:Null)->Void) { #if (cpp && (mac || windows || linux)) @@ -73,10 +80,29 @@ class Dialogs { } } } - else { - log.warning('Dialogs not implemented on web without electron'); - done(null); - } + + #elseif web + + final extensions:Array = [ + for (filter in filters) { + for (extension in filter.extensions) { + '.$extension'; + } + } + ]; + + var input: js.html.InputElement = cast js.Browser.document.createElement("input"); + input.type = "file"; + input.accept = extensions.join(","); + + input.onchange = () -> { + var reader = new js.html.FileReader(); + reader.readAsText(input.files[0]); + reader.onloadend = () -> { + done(reader.result); + }; + }; + input.click(); #else @@ -138,14 +164,10 @@ class Dialogs { done(null); } } - else { - log.warning('Dialogs not implemented on web without electron'); - done(null); - } #else - log.warning('Dialogs not implemented on this platform'); + log.warning('openDirectory is not implemented on this platform'); done(null); #end @@ -210,14 +232,10 @@ class Dialogs { } } } - else { - log.warning('Dialogs not implemented on web without electron'); - done(null); - } #else - log.warning('Dialogs not implemented on this platform'); + log.warning('saveFile is not implemented on this platform'); done(null); #end From 3524a4936406390e0e141cc1a02b9a8743952c62 Mon Sep 17 00:00:00 2001 From: logo4poop Date: Sun, 14 Jan 2024 17:42:18 -0500 Subject: [PATCH 2/2] [dialogs] Properly format comment --- plugins/dialogs/runtime/src/ceramic/Dialogs.hx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/dialogs/runtime/src/ceramic/Dialogs.hx b/plugins/dialogs/runtime/src/ceramic/Dialogs.hx index cbd909446..55edfa26c 100644 --- a/plugins/dialogs/runtime/src/ceramic/Dialogs.hx +++ b/plugins/dialogs/runtime/src/ceramic/Dialogs.hx @@ -17,10 +17,10 @@ using StringTools; */ class Dialogs { - /** - * Opens a file picker - * IMPORTANT: On non-electron web targets, the dialog will only open on click events - */ + /** + * Opens a file picker + * IMPORTANT: On non-electron web targets, the dialog will only open on click events + */ public static function openFile(title:String, ?filters:Array, done:(file:Null)->Void) { #if (cpp && (mac || windows || linux))