From 12e7d395feabee6b718bf483c2e855563dc4c9e0 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Morin Date: Tue, 3 Dec 2024 21:20:19 -0500 Subject: [PATCH 1/7] Add ability to load files through wasm in a browser Signed-off-by: Jean-Christophe Morin --- app.cpp | 11 +++++++++++ shell_minimal.html | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/app.cpp b/app.cpp index af0ca6b..649a7aa 100644 --- a/app.cpp +++ b/app.cpp @@ -15,6 +15,8 @@ #ifndef EMSCRIPTEN #include "nfd.h" +#else +#include #endif #include "mz.h" @@ -759,6 +761,13 @@ void SaveTheme() { std::string OpenFileDialog() { #ifdef EMSCRIPTEN + // https://stackoverflow.com/a/69935189 + EM_ASM( + var file_selector = document.createElement('input'); + file_selector.setAttribute('type', 'file'); + file_selector.setAttribute('onchange','Module.LoadStringFromEvent(event)'); + file_selector.click(); + ); return ""; #else nfdchar_t* outPath = NULL; @@ -803,11 +812,13 @@ void DrawMenu() { if (path != "") LoadFile(path); } +#ifndef EMSCRIPTEN if (ImGui::MenuItem("Save As...")) { auto path = SaveFileDialog(); if (path != "") SaveFile(path); } +#endif if (ImGui::MenuItem("Revert")) { LoadFile(appState.file_path); } diff --git a/shell_minimal.html b/shell_minimal.html index d45b812..77c1ac3 100644 --- a/shell_minimal.html +++ b/shell_minimal.html @@ -4,6 +4,7 @@ Raven +