muzzle-velocity/lib/note_editor.dart

195 lines
6.5 KiB
Dart
Raw Normal View History

2025-02-20 22:40:41 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_code_editor/flutter_code_editor.dart';
2025-02-21 18:16:17 +02:00
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:markdown/markdown.dart' as md;
import 'package:flutter_markdown_latex/flutter_markdown_latex.dart';
2025-02-21 17:46:41 +02:00
import 'themes/day.dart';
import 'themes/night.dart';
import 'themes/markdown.dart';
2025-02-20 22:40:41 +02:00
import 'dart:io';
2025-02-21 22:02:06 +02:00
import 'package:path/path.dart' as path;
2025-02-20 22:40:41 +02:00
class NoteEditorScreen extends StatefulWidget {
final File note;
2025-02-21 22:02:06 +02:00
final String? notesDirectoryPath; // ✅ Add this
2025-02-21 17:46:41 +02:00
bool isDarkMode;
2025-02-20 22:40:41 +02:00
2025-02-21 22:02:06 +02:00
NoteEditorScreen({required this.note, required this.isDarkMode, required this.notesDirectoryPath});
2025-02-20 22:40:41 +02:00
@override
_NoteEditorScreenState createState() => _NoteEditorScreenState();
}
class _NoteEditorScreenState extends State<NoteEditorScreen> {
2025-02-21 22:02:06 +02:00
bool isLoading = true;
2025-02-21 18:16:17 +02:00
bool _isPreviewMode = false; // Default: Editing mode
2025-02-20 22:40:41 +02:00
late CodeController _controller;
2025-02-21 17:46:41 +02:00
double _fontSize = 16.0; // Default font size
void _updateFontSize(double newSize) {
setState(() {
_fontSize = newSize;
});
}
2025-02-20 22:40:41 +02:00
@override
void initState() {
super.initState();
_loadNoteContent();
}
Future<void> _loadNoteContent() async {
2025-02-21 22:02:06 +02:00
String content = "";
try {
content = await widget.note.readAsString();
} catch (e) {
print("❌ Failed to read file: ${widget.note.path}");
}
2025-02-20 22:40:41 +02:00
setState(() {
_controller = CodeController(
text: content,
language: markdown,
);
_controller.addListener(_saveNote);
_controller.popupController.enabled = false;
2025-02-21 22:02:06 +02:00
isLoading = false;
2025-02-20 22:40:41 +02:00
});
}
2025-02-21 22:02:06 +02:00
2025-02-20 22:40:41 +02:00
Future<void> _saveNote() async {
2025-02-21 22:02:06 +02:00
try {
final notesDirectory = widget.notesDirectoryPath ?? widget.note.parent.path; // ✅ Use correct directory
final newFilePath = path.join(notesDirectory, widget.note.uri.pathSegments.last);
print("📝 Attempting to save note at: $newFilePath");
final newFile = File(newFilePath);
await newFile.writeAsString(_controller.text);
print("✅ Successfully saved note at: $newFilePath");
} catch (e, stackTrace) {
print("❌ Error saving note: ${widget.note.path}");
print("⚠️ Exception: $e");
print("🛠 Stack trace: $stackTrace");
}
2025-02-20 22:40:41 +02:00
}
@override
void dispose() {
_controller.removeListener(_saveNote);
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.note.uri.pathSegments.last.replaceAll('.md', '')),
2025-02-21 17:46:41 +02:00
actions: [
2025-02-21 18:16:17 +02:00
Padding(
padding: EdgeInsets.symmetric(horizontal: 15),
child: PopupMenuButton<String>(
icon: Icon(
Icons.radio_button_checked,
color: Colors.orange
),
onSelected: (String value) {
if (value == 'Toggle Preview') {
setState(() {
_isPreviewMode = !_isPreviewMode;
});
}
},
itemBuilder: (BuildContext context) => [
PopupMenuItem<String>(
value: 'Toggle Preview',
child: ListTile(
leading: Icon(_isPreviewMode ? Icons.edit : Icons.preview),
title: Text(_isPreviewMode ? 'Edit Mode' : 'Preview Mode'),
2025-02-21 17:46:41 +02:00
),
),
2025-02-21 18:16:17 +02:00
PopupMenuItem<String>(
enabled: false,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Font Size'),
StatefulBuilder(
builder: (context, setState) {
return Slider(
value: _fontSize,
min: 12.0,
max: 24.0,
divisions: 6,
label: _fontSize.toString(),
onChanged: (newSize) {
setState(() {
_fontSize = newSize;
});
_updateFontSize(newSize);
},
);
},
),
],
),
2025-02-21 17:46:41 +02:00
),
2025-02-21 18:16:17 +02:00
],
),
2025-02-21 17:46:41 +02:00
),
],
2025-02-20 22:40:41 +02:00
),
2025-02-21 22:02:06 +02:00
body: isLoading
? Center(child: CircularProgressIndicator()) // Show loader while initializing
: _controller == null
? Center(child: CircularProgressIndicator())
: _isPreviewMode
? Padding(
2025-02-21 18:16:17 +02:00
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: MarkdownBody(
builders: {
'latex': LatexElementBuilder(
textStyle: TextStyle(
color: widget.isDarkMode ? Colors.white : Colors.black
)
),
},
extensionSet: md.ExtensionSet(
[LatexBlockSyntax()],
[LatexInlineSyntax()],
),
data: _controller.text, // ✅ Plain Markdown without syntax highlighting
selectable: true, // ✅ Allows text selection
styleSheet: MarkdownStyleSheet(
p: TextStyle(fontSize: _fontSize, color: widget.isDarkMode ? Colors.white : Colors.black),
h1: TextStyle(fontSize: _fontSize + 8, color: widget.isDarkMode ? Colors.white : Colors.black, fontWeight: FontWeight.bold),
h2: TextStyle(fontSize: _fontSize + 6, color: widget.isDarkMode ? Colors.white : Colors.black, fontWeight: FontWeight.bold),
h3: TextStyle(fontSize: _fontSize + 4, color: widget.isDarkMode ? Colors.white : Colors.black, fontWeight: FontWeight.bold),
code: TextStyle(fontSize: _fontSize, backgroundColor: Colors.transparent), // ✅ Disables syntax highlighting
codeblockDecoration: BoxDecoration(color: Colors.transparent), // ✅ Removes background color from code blocks
),
),
),
)
2025-02-20 22:40:41 +02:00
: Padding(
padding: const EdgeInsets.all(16.0),
child: CodeTheme(
2025-02-21 17:46:41 +02:00
data: CodeThemeData(styles: widget.isDarkMode ? nightTheme : dayTheme),
2025-02-20 22:40:41 +02:00
child: CodeField(
gutterStyle: GutterStyle.none,
controller: _controller,
expands: true,
textStyle: TextStyle(
2025-02-21 17:46:41 +02:00
fontSize: _fontSize,
2025-02-20 22:40:41 +02:00
),
),
),
),
);
}
}