Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dialogs] Add web target support to openFile #129

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions plugins/dialogs/runtime/src/ceramic/Dialogs.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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<DialogsFileFilter>, done:(file:Null<String>)->Void) {

#if (cpp && (mac || windows || linux))
Expand Down Expand Up @@ -73,10 +80,29 @@ class Dialogs {
}
}
}
else {
log.warning('Dialogs not implemented on web without electron');
done(null);
}

#elseif web

final extensions:Array<String> = [
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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down