mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2026-02-23 16:45:27 +01:00
opt.: editor lang parse
This commit is contained in:
@@ -15,7 +15,6 @@ import 'package:server_box/data/model/container/ps.dart';
|
||||
import 'package:server_box/data/model/server/server_private_info.dart';
|
||||
import 'package:server_box/data/provider/container.dart';
|
||||
import 'package:server_box/view/page/ssh/page.dart';
|
||||
import 'package:server_box/view/widget/two_line_text.dart';
|
||||
|
||||
class ContainerPage extends StatefulWidget {
|
||||
final SpiRequiredArgs args;
|
||||
|
||||
@@ -1,261 +0,0 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:computer/computer.dart';
|
||||
import 'package:fl_lib/fl_lib.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_highlight/theme_map.dart';
|
||||
import 'package:flutter_highlight/themes/a11y-light.dart';
|
||||
import 'package:flutter_highlight/themes/monokai.dart';
|
||||
import 'package:server_box/core/extension/context/locale.dart';
|
||||
import 'package:server_box/data/res/highlight.dart';
|
||||
import 'package:server_box/data/res/store.dart';
|
||||
import 'package:server_box/data/store/setting.dart';
|
||||
|
||||
import 'package:server_box/view/widget/two_line_text.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
import 'package:server_box/view/widget/code_find_panel_view.dart';
|
||||
|
||||
enum EditorPageRetType { path, text }
|
||||
|
||||
final class EditorPageRet {
|
||||
final EditorPageRetType typ;
|
||||
final String val;
|
||||
|
||||
const EditorPageRet(this.typ, this.val);
|
||||
}
|
||||
|
||||
final class EditorPageArgs {
|
||||
/// If path is not null, then it's a file editor
|
||||
/// If path is null, then it's a text editor
|
||||
final String? path;
|
||||
|
||||
/// Only used when path is null
|
||||
final String? text;
|
||||
|
||||
/// Code of language, eg: dart, go, etc.
|
||||
/// Higher priority than [path]
|
||||
final String? langCode;
|
||||
|
||||
final String? title;
|
||||
|
||||
final void Function(BuildContext, EditorPageRet) onSave;
|
||||
|
||||
const EditorPageArgs({
|
||||
this.path,
|
||||
this.text,
|
||||
this.langCode,
|
||||
this.title,
|
||||
required this.onSave,
|
||||
});
|
||||
}
|
||||
|
||||
class EditorPage extends StatefulWidget {
|
||||
final EditorPageArgs? args;
|
||||
|
||||
const EditorPage({super.key, this.args});
|
||||
|
||||
static const route = AppRoute<void, EditorPageArgs>(
|
||||
page: EditorPage.new,
|
||||
path: '/editor',
|
||||
);
|
||||
|
||||
@override
|
||||
State<EditorPage> createState() => _EditorPageState();
|
||||
}
|
||||
|
||||
class _EditorPageState extends State<EditorPage> {
|
||||
final _focusNode = FocusNode();
|
||||
|
||||
late CodeLineEditingController _controller;
|
||||
late CodeFindController _findController;
|
||||
late Map<String, TextStyle> _codeTheme;
|
||||
|
||||
String? _langCode;
|
||||
var _saved = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
_findController.dispose();
|
||||
_focusNode.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller = CodeLineEditingController();
|
||||
_findController = CodeFindController(_controller);
|
||||
_init();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
|
||||
if (context.isDark) {
|
||||
_codeTheme = themeMap[Stores.setting.editorDarkTheme.fetch()] ?? monokaiTheme;
|
||||
} else {
|
||||
_codeTheme = themeMap[Stores.setting.editorTheme.fetch()] ?? a11yLightTheme;
|
||||
}
|
||||
_focusNode.requestFocus();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return PopScope(
|
||||
canPop: false,
|
||||
onPopInvokedWithResult: (didPop, result) {
|
||||
_pop();
|
||||
},
|
||||
child: Scaffold(
|
||||
backgroundColor: _codeTheme['root']?.backgroundColor,
|
||||
appBar: _buildAppBar(),
|
||||
body: _buildBody(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
PreferredSizeWidget _buildAppBar() {
|
||||
return CustomAppBar(
|
||||
centerTitle: true,
|
||||
title: TwoLineText(
|
||||
up: widget.args?.title ?? widget.args?.path?.getFileName() ?? l10n.unknown,
|
||||
down: l10n.editor,
|
||||
),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.search),
|
||||
tooltip: libL10n.search,
|
||||
onPressed: () => _findController.findMode(),
|
||||
),
|
||||
PopupMenuButton<String>(
|
||||
icon: const Icon(Icons.language),
|
||||
tooltip: libL10n.language,
|
||||
onSelected: (value) {
|
||||
_langCode = value;
|
||||
},
|
||||
initialValue: _langCode,
|
||||
itemBuilder: (BuildContext context) {
|
||||
return Highlights.all.keys.map((e) {
|
||||
return PopupMenuItem(
|
||||
value: e,
|
||||
child: Text(e),
|
||||
);
|
||||
}).toList();
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.save),
|
||||
tooltip: l10n.save,
|
||||
onPressed: _onSave,
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBody() {
|
||||
return Container(
|
||||
color: _EditorReTheme.getBackground(_codeTheme),
|
||||
child: CodeEditor(
|
||||
controller: _controller,
|
||||
findController: _findController,
|
||||
wordWrap: Stores.setting.editorSoftWrap.fetch(),
|
||||
focusNode: _focusNode,
|
||||
indicatorBuilder: (context, editingController, chunkController, notifier) {
|
||||
return Row(
|
||||
children: [
|
||||
DefaultCodeLineNumber(
|
||||
controller: editingController,
|
||||
notifier: notifier,
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
findBuilder: (context, controller, readOnly) =>
|
||||
CodeFindPanelView(controller: controller, readOnly: readOnly),
|
||||
// toolbarController: const ContextMenuControllerImpl(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
extension on _EditorPageState {
|
||||
Future<void> _init() async {
|
||||
/// Higher priority than [path]
|
||||
if (Stores.setting.editorHighlight.fetch()) {
|
||||
_langCode = widget.args?.langCode ?? Highlights.getCode(widget.args?.path);
|
||||
}
|
||||
if (_langCode == null) {
|
||||
_setupCtrl();
|
||||
} else {
|
||||
Future.delayed(const Duration(milliseconds: 377)).then(
|
||||
(value) async => await _setupCtrl(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _setupCtrl() async {
|
||||
final path = widget.args?.path;
|
||||
final text = widget.args?.text;
|
||||
if (path != null) {
|
||||
final code = await Computer.shared.startNoParam(
|
||||
() => File(path).readAsString(),
|
||||
);
|
||||
_controller.text = code;
|
||||
} else if (text != null) {
|
||||
_controller.text = text;
|
||||
}
|
||||
}
|
||||
|
||||
void _onSave() async {
|
||||
// If path is not null, then it's a file editor
|
||||
final path = widget.args?.path;
|
||||
if (path != null) {
|
||||
final (res, _) = await context.showLoadingDialog(
|
||||
fn: () => File(path).writeAsString(_controller.text),
|
||||
);
|
||||
if (res == null) {
|
||||
context.showSnackBar(libL10n.fail);
|
||||
return;
|
||||
}
|
||||
final ret = EditorPageRet(EditorPageRetType.path, path);
|
||||
widget.args?.onSave(context, ret);
|
||||
_saved = true;
|
||||
|
||||
final pop_ = SettingStore.instance.closeAfterSave.fetch();
|
||||
if (pop_) _pop();
|
||||
return;
|
||||
}
|
||||
// it's a text editor
|
||||
final ret = EditorPageRet(EditorPageRetType.text, _controller.text);
|
||||
widget.args?.onSave(context, ret);
|
||||
_saved = true;
|
||||
|
||||
final pop_ = SettingStore.instance.closeAfterSave.fetch();
|
||||
if (pop_) _pop();
|
||||
}
|
||||
|
||||
void _pop() async {
|
||||
if (!_saved) {
|
||||
final ret = await context.showRoundDialog(
|
||||
title: libL10n.attention,
|
||||
child: Text(libL10n.askContinue(libL10n.exit)),
|
||||
actions: Btnx.cancelOk,
|
||||
);
|
||||
if (ret != true) return;
|
||||
}
|
||||
context.pop();
|
||||
}
|
||||
}
|
||||
|
||||
class _EditorReTheme {
|
||||
static Color? getBackground(Map<String, TextStyle> codeTheme) {
|
||||
return codeTheme['root']?.backgroundColor;
|
||||
}
|
||||
|
||||
// static TextStyle? getTextStyle(Map<String, TextStyle> codeTheme) {
|
||||
// return codeTheme['root'];
|
||||
// }
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import 'package:server_box/data/res/store.dart';
|
||||
import 'package:server_box/data/model/app/shell_func.dart';
|
||||
import 'package:server_box/data/model/server/proc.dart';
|
||||
import 'package:server_box/data/model/server/server_private_info.dart';
|
||||
import 'package:server_box/view/widget/two_line_text.dart';
|
||||
|
||||
class ProcessPage extends StatefulWidget {
|
||||
final SpiRequiredArgs args;
|
||||
|
||||
@@ -8,7 +8,6 @@ import 'package:server_box/data/model/server/server_private_info.dart';
|
||||
import 'package:server_box/data/provider/pve.dart';
|
||||
import 'package:server_box/data/res/store.dart';
|
||||
import 'package:server_box/view/widget/percent_circle.dart';
|
||||
import 'package:server_box/view/widget/two_line_text.dart';
|
||||
|
||||
final class PvePageArgs {
|
||||
final Spi spi;
|
||||
|
||||
@@ -209,7 +209,7 @@ extension _Server on _AppSettingsPageState {
|
||||
final map = Stores.setting.getAllMap(includeInternalKeys: true);
|
||||
final keys = map.keys;
|
||||
|
||||
void onSave(BuildContext context, EditorPageRet ret) {
|
||||
void onSave(EditorPageRet ret) {
|
||||
if (ret.typ != EditorPageRetType.text) {
|
||||
context.showRoundDialog(
|
||||
title: libL10n.fail,
|
||||
@@ -240,9 +240,12 @@ extension _Server on _AppSettingsPageState {
|
||||
context,
|
||||
args: EditorPageArgs(
|
||||
text: text,
|
||||
langCode: 'json',
|
||||
lang: ProgLang.json,
|
||||
title: libL10n.setting,
|
||||
onSave: onSave,
|
||||
closeAfterSave: SettingStore.instance.closeAfterSave.fetch(),
|
||||
softWrap: SettingStore.instance.editorSoftWrap.fetch(),
|
||||
enableHighlight: SettingStore.instance.editorHighlight.fetch(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ import 'package:server_box/data/res/url.dart';
|
||||
import 'package:server_box/data/model/app/net_view.dart';
|
||||
import 'package:server_box/data/res/build_data.dart';
|
||||
import 'package:server_box/view/page/backup.dart';
|
||||
import 'package:server_box/view/page/editor.dart';
|
||||
import 'package:server_box/view/page/private_key/list.dart';
|
||||
import 'package:server_box/view/page/setting/platform/android.dart';
|
||||
import 'package:server_box/view/page/setting/platform/ios.dart';
|
||||
|
||||
@@ -10,7 +10,7 @@ import 'package:server_box/data/provider/sftp.dart';
|
||||
import 'package:server_box/data/res/misc.dart';
|
||||
|
||||
import 'package:server_box/data/model/app/path_with_prefix.dart';
|
||||
import 'package:server_box/view/page/editor.dart';
|
||||
import 'package:server_box/data/store/setting.dart';
|
||||
import 'package:server_box/view/page/storage/sftp.dart';
|
||||
import 'package:server_box/view/page/storage/sftp_mission.dart';
|
||||
|
||||
@@ -103,16 +103,14 @@ class _LocalFilePageState extends State<LocalFilePage> with AutomaticKeepAliveCl
|
||||
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 13),
|
||||
itemBuilder: (context, index) {
|
||||
if (index == 0 && _path.canBack) {
|
||||
return CardX(
|
||||
child: ListTile(
|
||||
leading: const Icon(Icons.arrow_back),
|
||||
title: const Text('..'),
|
||||
onTap: () {
|
||||
_path.update('..');
|
||||
setState(() {});
|
||||
},
|
||||
),
|
||||
);
|
||||
return ListTile(
|
||||
leading: const Icon(Icons.arrow_back),
|
||||
title: const Text('..'),
|
||||
onTap: () {
|
||||
_path.update('..');
|
||||
setState(() {});
|
||||
},
|
||||
).cardx;
|
||||
}
|
||||
|
||||
if (_path.canBack) index--;
|
||||
@@ -218,10 +216,13 @@ class _LocalFilePageState extends State<LocalFilePage> with AutomaticKeepAliveCl
|
||||
context,
|
||||
args: EditorPageArgs(
|
||||
path: file.absolute.path,
|
||||
onSave: (context, _) {
|
||||
onSave: (_) {
|
||||
context.showSnackBar(l10n.saved);
|
||||
setState(() {});
|
||||
},
|
||||
closeAfterSave: SettingStore.instance.closeAfterSave.fetch(),
|
||||
softWrap: SettingStore.instance.editorSoftWrap.fetch(),
|
||||
enableHighlight: SettingStore.instance.editorHighlight.fetch(),
|
||||
),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -13,17 +13,15 @@ import 'package:server_box/data/model/sftp/worker.dart';
|
||||
import 'package:server_box/data/provider/sftp.dart';
|
||||
import 'package:server_box/data/res/misc.dart';
|
||||
import 'package:server_box/data/res/store.dart';
|
||||
import 'package:server_box/view/page/editor.dart';
|
||||
import 'package:server_box/data/store/setting.dart';
|
||||
import 'package:server_box/view/page/ssh/page.dart';
|
||||
import 'package:server_box/view/page/storage/local.dart';
|
||||
import 'package:server_box/view/page/storage/sftp_mission.dart';
|
||||
import 'package:server_box/view/widget/omit_start_text.dart';
|
||||
import 'package:server_box/view/widget/two_line_text.dart';
|
||||
import 'package:server_box/view/widget/unix_perm.dart';
|
||||
|
||||
import 'package:icons_plus/icons_plus.dart';
|
||||
|
||||
|
||||
final class SftpPageArgs {
|
||||
final Spi spi;
|
||||
final bool isSelect;
|
||||
@@ -108,15 +106,15 @@ extension _UI on _SftpPageState {
|
||||
(_SortType.size, l10n.size),
|
||||
(_SortType.time, l10n.time),
|
||||
];
|
||||
return _sortOption.listenVal((value) {
|
||||
return _sortOption.listenVal(
|
||||
(value) {
|
||||
return PopupMenuButton<_SortType>(
|
||||
icon: const Icon(Icons.sort),
|
||||
itemBuilder: (context) {
|
||||
return options.map((r) {
|
||||
final (type, name) = r;
|
||||
final selected = type == value.sortBy;
|
||||
final title =
|
||||
selected ? "$name (${value.reversed ? '-' : '+'})" : name;
|
||||
final title = selected ? "$name (${value.reversed ? '-' : '+'})" : name;
|
||||
return PopupMenuItem(
|
||||
value: type,
|
||||
child: Text(
|
||||
@@ -353,7 +351,7 @@ extension _Actions on _SftpPageState {
|
||||
context,
|
||||
args: EditorPageArgs(
|
||||
path: localPath,
|
||||
onSave: (context, _) {
|
||||
onSave: (_) {
|
||||
SftpProvider.add(SftpReq(
|
||||
req.spi,
|
||||
remotePath,
|
||||
@@ -362,6 +360,9 @@ extension _Actions on _SftpPageState {
|
||||
));
|
||||
context.showSnackBar(l10n.added2List);
|
||||
},
|
||||
closeAfterSave: SettingStore.instance.closeAfterSave.fetch(),
|
||||
softWrap: SettingStore.instance.editorSoftWrap.fetch(),
|
||||
enableHighlight: SettingStore.instance.editorHighlight.fetch(),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -653,9 +654,7 @@ extension _Actions on _SftpPageState {
|
||||
fs.removeAt(0);
|
||||
}
|
||||
|
||||
if (fs.isNotEmpty &&
|
||||
fs.firstOrNull?.filename == '..' &&
|
||||
_status.path.path == '/') {
|
||||
if (fs.isNotEmpty && fs.firstOrNull?.filename == '..' && _status.path.path == '/') {
|
||||
fs.removeAt(0);
|
||||
}
|
||||
if (mounted) {
|
||||
@@ -909,9 +908,7 @@ const _extCmdMap = {
|
||||
|
||||
/// Return fmt: 2021-01-01 00:00:00
|
||||
String _getTime(int? unixMill) {
|
||||
return DateTime.fromMillisecondsSinceEpoch((unixMill ?? 0) * 1000)
|
||||
.toString()
|
||||
.replaceFirst('.000', '');
|
||||
return DateTime.fromMillisecondsSinceEpoch((unixMill ?? 0) * 1000).toString().replaceFirst('.000', '');
|
||||
}
|
||||
|
||||
enum _SortType {
|
||||
@@ -930,8 +927,7 @@ enum _SortType {
|
||||
files.sort(
|
||||
comparator
|
||||
.thenWithComparator(
|
||||
(a, b) => Comparators.compareStringCaseInsensitive()(
|
||||
a.filename, b.filename),
|
||||
(a, b) => Comparators.compareStringCaseInsensitive()(a.filename, b.filename),
|
||||
reversed: reversed,
|
||||
)
|
||||
.compare,
|
||||
|
||||
Reference in New Issue
Block a user