Skip to content

Commit

Permalink
Migrate from dart:html to web
Browse files Browse the repository at this point in the history
  • Loading branch information
Carapacik committed May 29, 2024
1 parent 4d44c12 commit 6b57a27
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 40 deletions.
1 change: 0 additions & 1 deletion swagger_parser_pages/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ migrate_working_dir/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
/build/
Expand Down
3 changes: 1 addition & 2 deletions swagger_parser_pages/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ class App extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
title: 'Swagger Parser',
restorationScopeId: 'app',
restorationScopeId: 'swagger parser',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorSchemeSeed: const Color(0xFFD0BCFF),
brightness: Brightness.dark,
useMaterial3: true,
),
themeMode: ThemeMode.dark,
home: const MainPage(),
Expand Down
22 changes: 13 additions & 9 deletions swagger_parser_pages/lib/utils/file_utils.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// ignore: avoid_web_libraries_in_flutter
import 'dart:html' as html;
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'dart:typed_data';

import 'package:archive/archive.dart';
import 'package:swagger_parser/swagger_parser.dart';
import 'package:web/web.dart' as web;

void generateArchive(List<GeneratedFile> files) {
final encoder = ZipEncoder();
Expand All @@ -16,20 +18,22 @@ void generateArchive(List<GeneratedFile> files) {
level: Deflate.BEST_COMPRESSION,
output: outputStream,
);
if (bytes == null || bytes is! Uint8List) {
throw Exception('Error with encode');
}

final blob = html.Blob(<List<int>?>[bytes]);
final url = html.Url.createObjectUrlFromBlob(blob);
final anchor = html.document.createElement('a') as html.AnchorElement
// ignore: unsafe_html
final blobWeb = web.Blob(<JSUint8Array>[bytes.toJS].toJS);
final url = web.URL.createObjectURL(blobWeb);
final anchor = web.document.createElement('a') as web.HTMLAnchorElement
..href = url
..style.display = 'none'
..download = 'generated.zip';
html.document.body!.children.add(anchor);
web.document.body!.children.add(anchor);

// download
anchor.click();

// cleanup
html.document.body!.children.remove(anchor);
html.Url.revokeObjectUrl(url);
web.document.body!.children.delete(anchor);
web.URL.revokeObjectURL(url);
}
15 changes: 8 additions & 7 deletions swagger_parser_pages/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
name: swagger_parser_pages
description: Web interface for swagger_parser
publish_to: none
version: 1.0.0+2
version: 1.0.0+1
environment:
sdk: ^3.3.0
sdk: '>=3.4.0 <4.0.0'

dependencies:
archive: ^3.4.10
file_picker: ^6.1.1
archive: ^3.6.0
file_picker: ^8.0.3
flutter:
sdk: flutter
flutter_web_plugins:
sdk: flutter
swagger_parser: ^1.16.1
url_launcher: ^6.2.5
swagger_parser: ^1.17.3
url_launcher: ^6.2.6
web: ^0.5.1

dev_dependencies:
carapacik_lints: ^1.7.0
carapacik_lints: ^1.8.1

flutter:
uses-material-design: true
22 changes: 1 addition & 21 deletions swagger_parser_pages/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,8 @@

<title>Swagger Parser</title>
<link rel="manifest" href="manifest.json">

<script>
// The value below is injected by flutter build, do not touch.
const serviceWorkerVersion = null;
</script>
<script src="flutter.js" defer></script>
</head>
<body>
<script>
window.addEventListener('load', function(ev) {
// Download main.dart.js
_flutter.loader.loadEntrypoint({
serviceWorker: {
serviceWorkerVersion: serviceWorkerVersion,
},
onEntrypointLoaded: function(engineInitializer) {
engineInitializer.initializeEngine().then(function(appRunner) {
appRunner.runApp();
});
}
});
});
</script>
<script src="flutter_bootstrap.js" async></script>
</body>
</html>

0 comments on commit 6b57a27

Please sign in to comment.