Skip to content

Commit

Permalink
Fix web problems
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Jan 31, 2024
1 parent 9cfe6cc commit 4716732
Show file tree
Hide file tree
Showing 9 changed files with 57,263 additions and 42 deletions.
29 changes: 5 additions & 24 deletions app/lib/api/save.dart
Original file line number Diff line number Diff line change
@@ -1,36 +1,17 @@
import 'dart:io';

import 'package:file_selector/file_selector.dart' as fs;
import 'package:flutter/foundation.dart';
import 'package:butterfly/api/save_stub.dart'
if (dart.library.io) 'package:butterfly/api/save_io.dart'
if (dart.library.js) 'package:butterfly/api/save_html.dart' as save;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:share_plus/share_plus.dart';

Future<void> exportFile(
BuildContext context,
List<int> bytes,
String fileExtension,
String mimeType,
) async {
final file = fs.XFile.fromData(Uint8List.fromList(bytes),
mimeType: mimeType, name: 'output.$fileExtension');
if (kIsWeb || Platform.isAndroid || Platform.isIOS) {
Share.shareXFiles([file]);
return;
}
final result = await fs.getSaveLocation(
acceptedTypeGroups: [
fs.XTypeGroup(
label: AppLocalizations.of(context).export,
extensions: [fileExtension],
mimeTypes: [mimeType],
),
],
);
if (result == null) return;
await file.saveTo(result.path);
}
) =>
save.exportFile(context, bytes, fileExtension, mimeType);

Future<void> exportSvg(BuildContext context, String data) =>
exportFile(context, data.codeUnits, 'svg', 'image/svg');
Expand Down
20 changes: 20 additions & 0 deletions app/lib/api/save_html.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ignore_for_file: avoid_web_libraries_in_flutter

import 'dart:html';

import 'package:flutter/material.dart';

Future<void> exportFile(
BuildContext context,
List<int> bytes,
String fileExtension,
String mimeType,
) async {
final a = document.createElement('a') as AnchorElement;
// Create data URL
final blob = Blob([bytes], 'text/plain');
final url = Url.createObjectUrl(blob);
a.href = url;
a.download = 'output.$fileExtension';
a.click();
}
32 changes: 32 additions & 0 deletions app/lib/api/save_io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:file_selector/file_selector.dart' as fs;
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:share_plus/share_plus.dart';

Future<void> exportFile(
BuildContext context,
List<int> bytes,
String fileExtension,
String mimeType,
) async {
final file = fs.XFile.fromData(Uint8List.fromList(bytes),
mimeType: mimeType, name: 'output.$fileExtension');
if (Platform.isAndroid || Platform.isIOS) {
Share.shareXFiles([file]);
return;
}
final result = await fs.getSaveLocation(
acceptedTypeGroups: [
fs.XTypeGroup(
label: AppLocalizations.of(context).export,
extensions: [fileExtension],
mimeTypes: [mimeType],
),
],
);
if (result == null) return;
await file.saveTo(result.path);
}
8 changes: 8 additions & 0 deletions app/lib/api/save_stub.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:flutter/material.dart';

Future<void> exportFile(
BuildContext context,
List<int> bytes,
String fileExtension,
String mimeType,
) async {}
2 changes: 1 addition & 1 deletion app/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<link rel="manifest" href="manifest.json">


<script src="pdf.js/pdf.min.js"></script>
<script src="pdf.js/pdf.min.js" type="module"></script>
<script type="text/javascript">
pdfjsLib.GlobalWorkerOptions.workerSrc = "pdf.js/pdf.worker.min.js";
</script>
Expand Down
28 changes: 21 additions & 7 deletions app/web/pdf.js/pdf.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 4716732

Please sign in to comment.