Skip to content

feat: handle drop files from web #317

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Binary file added .DS_Store
Binary file not shown.
6 changes: 3 additions & 3 deletions packages/desktop_drop/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ packages:
dependency: transitive
description:
name: web
sha256: "14f1f70c51119012600c5f1f60ca68efda5a9b6077748163c6af2893ec5df8fc"
sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
url: "https://pub.dev"
source: hosted
version: "0.2.1-beta"
version: "0.3.0"
sdks:
dart: ">=3.2.0-157.0.dev <4.0.0"
dart: ">=3.2.0-194.0.dev <4.0.0"
flutter: ">=1.20.0"
36 changes: 32 additions & 4 deletions packages/desktop_drop/lib/desktop_drop_web.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'dart:async';
import 'dart:convert';
import 'dart:html' as html;
import 'dart:js_util' as js_util;

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:mime/mime.dart';

import 'src/web_drop_item.dart';

Expand Down Expand Up @@ -47,7 +49,8 @@ class DesktopDropWeb {
final children = await Future.wait(
entries.map((e) => _entryToWebDropItem(e)),
)
..removeWhere((element) => element.name == '.DS_Store' && element.type == '');
..removeWhere(
(element) => element.name == '.DS_Store' && element.type == '');
return WebDropItem(
uri: html.Url.createObjectUrlFromBlob(html.Blob([], 'directory')),
name: entry.name ?? '',
Expand Down Expand Up @@ -80,19 +83,44 @@ class DesktopDropWeb {
);
}

String _getMimeType(String text) {
final pattern = RegExp(r'^data:([^;]+);');
return pattern.firstMatch(text)?.group(1) ?? 'text/plain';
}

void _registerEvents() {
html.window.onDrop.listen((event) {
event.preventDefault();

final items = event.dataTransfer.items;
Future.wait(List.generate(items?.length ?? 0, (index) {
final item = items![index];
final entry = item.getAsEntry();
return _entryToWebDropItem(entry);
if (item.kind == 'file') {
final entry = item.getAsEntry();
return _entryToWebDropItem(entry);
}
if (item.kind == 'string' && item.type == 'text/uri-list') {
final data = event.dataTransfer.getData(item.type!);
final mime = _getMimeType(data);
return Future.value(
WebDropItem(
uri: data,
name: 'file.${extensionFromMime(mime)}',
type: mime,
data: base64Decode(data.split(';base64,')[1]),
size: 0,
relativePath: '',
lastModified: DateTime.now(),
children: [],
),
);
}
// other types such as text/html
return Future.value(null);
})).then((webItems) {
channel.invokeMethod(
"performOperation_web",
webItems.map((e) => e.toJson()).toList(),
webItems.whereType<WebDropItem>().map((e) => e.toJson()).toList(),
);
}).catchError((e, s) {
debugPrint('desktop_drop_web: $e $s');
Expand Down
4 changes: 3 additions & 1 deletion packages/desktop_drop/lib/src/web_drop_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class WebDropItem {
.cast<Map>()
.map((e) => WebDropItem.fromJson(e.cast<String, dynamic>()))
.toList(),
data: json['data'] != null ? Uint8List.fromList(json['data']) : null,
data: json['data'] != null
? Uint8List.fromList((json['data'] as List).cast<int>())
: null,
name: json['name'],
type: json['type'],
size: json['size'],
Expand Down
3 changes: 2 additions & 1 deletion packages/desktop_drop/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
flutter_web_plugins:
sdk: flutter
cross_file: ^0.3.3+4
mime: ^1.0.5

dev_dependencies:
flutter_test:
Expand All @@ -33,4 +34,4 @@ flutter:
pluginClass: DesktopDropPlugin
web:
pluginClass: DesktopDropWeb
fileName: desktop_drop_web.dart
fileName: desktop_drop_web.dart