Skip to content

Commit

Permalink
Fix export functionality in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
MRmlik12 committed May 29, 2024
1 parent 417f8b2 commit 0cde6b2
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ export function openFilePicker() {

input.click();
});
}

export function saveFile(fileName, content) {
return new Promise(() => {
const link = document.createElement('a');
link.download = fileName;
link.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
}
26 changes: 0 additions & 26 deletions src/NoteSHR.Browser/BrowserFilePicker.cs

This file was deleted.

34 changes: 34 additions & 0 deletions src/NoteSHR.Browser/BrowserFileService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Runtime.InteropServices.JavaScript;
using System.Threading.Tasks;
using NoteSHR.Core.Services;

namespace NoteSHR.Browser;

public class BrowserFileService : IFileService
{
public BrowserFileService()
{
Task.Run(async () => await JSHost.ImportAsync("FileUtils", "./fileUtils.js"));
}

public async Task<string?> GetFileUrl()
{
var url = await FileUtilsEmbed.OpenFilePicker();

return url;
}

public async Task SaveFile(string fileName, string content)
{
await FileUtilsEmbed.SaveFile(fileName, content);
}
}

internal partial class FileUtilsEmbed
{
[JSImport("openFilePicker", "FileUtils")]
public static partial Task<string?> OpenFilePicker();

[JSImport("saveFile", "FileUtils")]
public static partial Task SaveFile(string fileName, string contents);
}
2 changes: 1 addition & 1 deletion src/NoteSHR.Browser/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ private static Task Main(string[] args)
public static AppBuilder BuildAvaloniaApp()
{
return AppBuilder.Configure<App>()
.AfterSetup(a => { App.FilePicker = new BrowserFilePicker(); });
.AfterSetup(a => { App.FileService = new BrowserFileService(); });
}
}
7 changes: 7 additions & 0 deletions src/NoteSHR.Core/Services/IFileService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NoteSHR.Core.Services;

public interface IFileService
{
Task<string?> GetFileUrl();
Task SaveFile(string fileName, string content);
}
13 changes: 12 additions & 1 deletion src/NoteSHR.File/BoardExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System.Text;
using Newtonsoft.Json;
using NoteSHR.Core.Models;
using NoteSHR.Core.Services;
using NoteSHR.File.Utils;

namespace NoteSHR.File;

public static class BoardExporter
{
public static async Task<string> ExportToFile(Guid id, List<Note> notes, string name, string path)
public static async Task<string> ExportToFile(Guid id, List<Note> notes, string name, string path, IFileService fileService = null)
{
var tempFolder = PathUtils.GetTemporaryPath(id);
if (Directory.Exists(tempFolder))
Expand All @@ -24,6 +25,16 @@ public static async Task<string> ExportToFile(Guid id, List<Note> notes, string
await boardFile.WriteAsync(Encoding.UTF8.GetBytes(json));
boardFile.Close();

if (OperatingSystem.IsBrowser())
{
using var memoryStream = new MemoryStream();
ZipFile.CreateFromDirectory(tempFolder, memoryStream);
memoryStream.Position = 0;
await fileService.SaveFile($"{name}.zip", Encoding.ASCII.GetString(memoryStream.ToArray()));

return string.Empty;
}

var destinationPath = $"{path}/{name}.zip";
if (System.IO.File.Exists(destinationPath)) System.IO.File.Delete(destinationPath);

Expand Down
2 changes: 1 addition & 1 deletion src/NoteSHR/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace NoteSHR;

public class App : Application
{
public static IFilePicker FilePicker { get; set; }
public static IFileService FileService { get; set; }

public override void Initialize()
{
Expand Down
2 changes: 1 addition & 1 deletion src/NoteSHR/Components/Image/ImageComponentViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ImageComponentViewModel()
{
if (OperatingSystem.IsBrowser())
{
var url = await App.FilePicker.GetFileUrl();
var url = await App.FileService.GetFileUrl();
if (string.IsNullOrEmpty(url)) return;
Image = await HttpUtils.GetBitmapFromUrl(url);
Expand Down
8 changes: 0 additions & 8 deletions src/NoteSHR/Core/Helpers/IFilePicker.cs

This file was deleted.

8 changes: 7 additions & 1 deletion src/NoteSHR/ViewModels/BoardViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ public BoardViewModel()

ExportBoardCommand = ReactiveCommand.CreateFromTask(async (RoutedEventArgs args) =>
{
if (OperatingSystem.IsBrowser())
{
await BoardExporter.ExportToFile(Guid.NewGuid(), Notes.ToList(), Name, string.Empty, App.FileService);
return;
}
var topLevel = TopLevel.GetTopLevel(args.Source as Visual);
var saveFilePickerOptions = new FolderPickerOpenOptions
{
Expand All @@ -154,7 +160,7 @@ public BoardViewModel()
string path;
if (OperatingSystem.IsBrowser())
{
path = await App.FilePicker.GetFileUrl();
path = await App.FileService.GetFileUrl();
if (string.IsNullOrEmpty(path)) return;
}
else
Expand Down

0 comments on commit 0cde6b2

Please sign in to comment.