Skip to content

Commit

Permalink
feat: import extensions locally (#237)
Browse files Browse the repository at this point in the history
* feat add local import

fix url import erro
add local import

* fix: from copy to create new file

* Add maxWidth property to showPlatformDialog

* Refactor extension installation methods

* Refactor extension import UI

---------

Co-authored-by: MiaoMint <[email protected]>
  • Loading branch information
appdevelpo and MiaoMint authored Mar 24, 2024
1 parent 5cd64ef commit 2d6d50b
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 37 deletions.
3 changes: 2 additions & 1 deletion assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
"url-label": "Extension URL",
"tips": "You can import extensions through a link, or click on the extension directory below and place the extension file there.",
"extension-dir": "Extension Directory",
"import-by-url": "Import by URL"
"import-by-url": "Import by URL",
"import-by-local": "Import Locally"
},
"error-dialog": "Error Message",
"installed": "Installed",
Expand Down
36 changes: 33 additions & 3 deletions lib/utils/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ExtensionUtils {
break;
case FileSystemEvent.create:
case FileSystemEvent.modify:
await _installByPath(event.path);
await installByPath(event.path);
break;
}
_reloadPage();
Expand All @@ -57,7 +57,7 @@ class ExtensionUtils {
final extensionsList = Directory(extensionsDir).listSync();
// 遍历扩展列表
for (final extension in extensionsList) {
await _installByPath(extension.path);
await installByPath(extension.path);
}

_reloadPage();
Expand All @@ -80,6 +80,8 @@ class ExtensionUtils {
final savePath = path.join(extensionsDir, '${ext.package}.js');
// 保存文件
File(savePath).writeAsStringSync(res.data!);
//reload
_loadExtensions();
} catch (e) {
if (context.mounted) {
showPlatformDialog(
Expand All @@ -100,7 +102,35 @@ class ExtensionUtils {
}
}

static _installByPath(String p) async {
static installByScript(String script, BuildContext context) async {
try {
final ext = ExtensionUtils.parseExtension(script);
final savePath = path.join(extensionsDir, '${ext.package}.js');
// 保存文件
File(savePath).writeAsStringSync(script);
runtimes[ext.package] = await ExtensionService().initRuntime(ext);
_reloadPage();
} catch (e) {
if (context.mounted) {
showPlatformDialog(
context: context,
title: 'extension-install-error'.i18n,
content: Text(e.toString()),
actions: [
PlatformButton(
child: Text('common.close'.i18n),
onPressed: () {
RouterUtils.pop();
},
)
],
);
}
rethrow;
}
}

static installByPath(String p) async {
if (path.extension(p) == '.js') {
try {
final file = File(p);
Expand Down
67 changes: 42 additions & 25 deletions lib/views/pages/extension/extension_page.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:fluent_ui/fluent_ui.dart' as fluent;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:miru_app/controllers/extension/extension_controller.dart';
import 'package:miru_app/views/widgets/extension/extension_tile.dart';
Expand Down Expand Up @@ -48,6 +48,7 @@ class _ExtensionPageState extends State<ExtensionPage> {
showPlatformDialog(
context: context,
title: 'extension.import.title'.i18n,
maxWidth: 500,
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
Expand All @@ -62,11 +63,30 @@ class _ExtensionPageState extends State<ExtensionPage> {
url = value;
},
),
desktopWidget: fluent.TextBox(
placeholder: 'extension.import.url-label'.i18n,
onChanged: (value) {
url = value;
},
desktopWidget: Row(
children: [
Expanded(
child: fluent.TextBox(
placeholder: 'extension.import.url-label'.i18n,
onChanged: (value) {
url = value;
},
)),
const SizedBox(width: 8),
fluent.Tooltip(
message: 'extension.import.extension-dir'.i18n,
child: fluent.IconButton(
icon: const Icon(fluent.FluentIcons.fabric_folder),
onPressed: () async {
RouterUtils.pop();
// 定位目录
final dir = ExtensionUtils.extensionsDir;
final uri = Uri.directory(dir);
await launchUrl(uri);
},
),
)
],
),
),
const SizedBox(height: 16),
Expand Down Expand Up @@ -94,31 +114,28 @@ class _ExtensionPageState extends State<ExtensionPage> {
PlatformFilledButton(
onPressed: () async {
RouterUtils.pop();
// 定位目录
final dir = ExtensionUtils.extensionsDir;
if (Platform.isAndroid) {
Clipboard.setData(ClipboardData(text: dir));
if (!mounted) {
return;
}
showPlatformSnackbar(
context: context,
title: 'extension.import.extension-dir'.i18n,
content: 'common.copied'.i18n,
);
return;
}
final uri = Uri.directory(dir);
await launchUrl(uri);
await ExtensionUtils.install(url, context);
},
child: Text('extension.import.extension-dir'.i18n),
child: Text('extension.import.import-by-url'.i18n),
),
PlatformFilledButton(
child: Text('extension.import.import-by-local'.i18n),
onPressed: () async {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['js'],
);
if (result == null || !mounted) {
return;
}
final path = result.files.single.path;
if (path == null) {
return;
}
final script = File(path).readAsStringSync();
await ExtensionUtils.installByScript(script, context);
RouterUtils.pop();
await ExtensionUtils.install(url, context);
},
child: Text('extension.import.import-by-url'.i18n),
),
],
);
Expand Down
2 changes: 2 additions & 0 deletions lib/views/widgets/messenger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ showPlatformDialog({
required String title,
required Widget? content,
required List<Widget>? actions,
double? maxWidth,
}) {
if (Platform.isAndroid) {
return material.showDialog(
Expand All @@ -51,6 +52,7 @@ showPlatformDialog({
return fluent.showDialog(
context: context,
builder: (context) => fluent.ContentDialog(
constraints: BoxConstraints(maxWidth: maxWidth ?? 368),
title: Text(title),
content: content,
actions: actions,
Expand Down
48 changes: 40 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.7.1"
leak_tracker:
dependency: transitive
description:
name: leak_tracker
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
url: "https://pub.dev"
source: hosted
version: "10.0.0"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
url: "https://pub.dev"
source: hosted
version: "2.0.1"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
url: "https://pub.dev"
source: hosted
version: "2.0.1"
linked_scroll_controller:
dependency: transitive
description:
Expand Down Expand Up @@ -778,18 +802,18 @@ packages:
dependency: transitive
description:
name: matcher
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb
url: "https://pub.dev"
source: hosted
version: "0.12.16"
version: "0.12.16+1"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "0.8.0"
math_expressions:
dependency: transitive
description:
Expand Down Expand Up @@ -874,10 +898,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
url: "https://pub.dev"
source: hosted
version: "1.10.0"
version: "1.11.0"
mime:
dependency: transitive
description:
Expand Down Expand Up @@ -930,10 +954,10 @@ packages:
dependency: "direct main"
description:
name: path
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
url: "https://pub.dev"
source: hosted
version: "1.8.3"
version: "1.9.0"
path_drawing:
dependency: transitive
description:
Expand Down Expand Up @@ -1451,6 +1475,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
vm_service:
dependency: transitive
description:
name: vm_service
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
url: "https://pub.dev"
source: hosted
version: "13.0.0"
volume_controller:
dependency: "direct main"
description:
Expand Down

0 comments on commit 2d6d50b

Please sign in to comment.