Skip to content

Commit

Permalink
Specify extensions to file picker
Browse files Browse the repository at this point in the history
  • Loading branch information
MRmlik12 committed Jun 8, 2024
1 parent 607f0d5 commit 2854ee8
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/NoteSHR.Browser/AppBundle/fileUtils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export function openFilePicker() {
export function openFilePicker(fileTypes) {
return new Promise((resolve, reject) => {
const input = document.createElement('input');
input.type = 'file';
input.accept = fileTypes.join(',');

input.addEventListener('change', () => {
if (input.files.length > 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/NoteSHR.Browser/BrowserFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public BrowserFileService()
Task.Run(async () => await JSHost.ImportAsync("FileUtils", "./fileUtils.js"));
}

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

return url;
}
Expand All @@ -27,7 +27,7 @@ public async Task SaveFile(string fileName, byte[] content)
internal partial class FileUtilsEmbed
{
[JSImport("openFilePicker", "FileUtils")]
public static partial Task<string?> OpenFilePicker();
public static partial Task<string?> OpenFilePicker(string[] fileTypes);

[JSImport("saveFile", "FileUtils")]
public static partial Task SaveFile(string fileName, byte[] contents);
Expand Down
11 changes: 11 additions & 0 deletions src/NoteSHR.Core/FileTypeFilters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Avalonia.Platform.Storage;

namespace NoteSHR.Core;

public class FileTypeFilters
{
public static FilePickerFileType Board = new ("NoteSHR Board File")
{
Patterns = new[] { "*.shr" }
};
}
2 changes: 1 addition & 1 deletion src/NoteSHR.Core/Services/IFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace NoteSHR.Core.Services;

public interface IFileService
{
Task<string?> GetFileUrl();
Task<string?> GetFileUrl(string[] fileTypes);
Task SaveFile(string fileName, byte[] content);
}
3 changes: 2 additions & 1 deletion src/NoteSHR/Components/Image/ImageComponentViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reactive;
using Avalonia.Controls;
using Avalonia.Interactivity;
Expand All @@ -24,7 +25,7 @@ public ImageComponentViewModel()
{
if (OperatingSystem.IsBrowser())
{
var url = await App.FileService.GetFileUrl();
var url = await App.FileService.GetFileUrl(FilePickerFileTypes.ImageAll.Patterns!.Select(x => x.Remove(0, 1)).ToArray());
if (string.IsNullOrEmpty(url)) return;

Image = await HttpUtils.GetBitmapFromUrl(url);
Expand Down
11 changes: 5 additions & 6 deletions src/NoteSHR/ViewModels/BoardViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using NoteSHR.Components.List;
using NoteSHR.Components.NoteNode.EventArgs;
using NoteSHR.Components.Text;
using NoteSHR.Core;
using NoteSHR.Core.EventArgs;
using NoteSHR.Core.Helpers;
using NoteSHR.Core.Models;
Expand All @@ -31,7 +32,6 @@ public BoardViewModel()
{
CreateNoteCommand = ReactiveCommand.Create((BoardPointerEventArgs e) =>
{
LastMousePosition = e.Args.GetPosition(e.Source);
if (e.Args.Source is not Canvas)
{
if (e.Args.Source is not Grid header)
Expand Down Expand Up @@ -159,18 +159,19 @@ public BoardViewModel()

ImportBoardCommand = ReactiveCommand.CreateFromTask(async (RoutedEventArgs args) =>
{
string path;
if (OperatingSystem.IsBrowser())
string path; if (OperatingSystem.IsBrowser())
{
path = await App.FileService.GetFileUrl();
path = await App.FileService.GetFileUrl(FileTypeFilters.Board.Patterns!.Select(x => x.Remove(0, 1)).ToArray());
if (string.IsNullOrEmpty(path)) return;
}
else
{
var topLevel = TopLevel.GetTopLevel(args.Source as Visual);

var openFilePickerOptions = new FilePickerOpenOptions()
{
Title = Translations.SelectFile,
FileTypeFilter = new [] { FileTypeFilters.Board },
AllowMultiple = false
};

Expand All @@ -192,8 +193,6 @@ public BoardViewModel()
[Reactive] public bool DeleteMode { get; set; }
[Reactive] public bool EditMode { get; set; }

private Point LastMousePosition { get; set; }

public ReactiveCommand<BoardPointerEventArgs, Unit> CreateNoteCommand { get; set; }
public ReactiveCommand<Guid, Unit> RemoveNote { get; set; }
public ReactiveCommand<BoardPointerEventArgs, Unit> UpdateNoteLocation { get; set; }
Expand Down

0 comments on commit 2854ee8

Please sign in to comment.