This commit is contained in:
randogoth 2025-02-21 22:51:56 +02:00
parent 7e82ddcbb7
commit 432048dc6a
5 changed files with 535 additions and 507 deletions

View file

@ -1,20 +1,23 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_code_editor/flutter_code_editor.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:markdown/markdown.dart' as md;
import 'package:flutter_markdown_latex/flutter_markdown_latex.dart';
import 'package:markdown/markdown.dart' as md;
import 'package:path/path.dart' as path;
import 'themes/day.dart';
import 'themes/night.dart';
import 'themes/markdown.dart';
import 'dart:io';
import 'package:path/path.dart' as path;
class NoteEditorScreen extends StatefulWidget {
final File note;
final String? notesDirectoryPath; // Add this
bool isDarkMode;
final bool isDarkMode;
NoteEditorScreen({required this.note, required this.isDarkMode, required this.notesDirectoryPath});
NoteEditorScreen(
{required this.note,
required this.isDarkMode,
required this.notesDirectoryPath});
@override
_NoteEditorScreenState createState() => _NoteEditorScreenState();
@ -26,13 +29,6 @@ class _NoteEditorScreenState extends State<NoteEditorScreen> {
late CodeController _controller;
double _fontSize = 16.0; // Default font size
void _updateFontSize(double newSize) {
setState(() {
_fontSize = newSize;
});
}
@override
void initState() {
super.initState();
@ -58,11 +54,12 @@ class _NoteEditorScreenState extends State<NoteEditorScreen> {
});
}
Future<void> _saveNote() async {
try {
final notesDirectory = widget.notesDirectoryPath ?? widget.note.parent.path; // Use correct directory
final newFilePath = path.join(notesDirectory, widget.note.uri.pathSegments.last);
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);
@ -76,6 +73,12 @@ class _NoteEditorScreenState extends State<NoteEditorScreen> {
}
}
void _updateFontSize(double newSize) {
setState(() {
_fontSize = newSize;
});
}
@override
void dispose() {
_controller.removeListener(_saveNote);
@ -92,10 +95,7 @@ class _NoteEditorScreenState extends State<NoteEditorScreen> {
Padding(
padding: EdgeInsets.symmetric(horizontal: 15),
child: PopupMenuButton<String>(
icon: Icon(
Icons.radio_button_checked,
color: Colors.orange
),
icon: Icon(Icons.radio_button_checked, color: Colors.orange),
onSelected: (String value) {
if (value == 'Toggle Preview') {
setState(() {
@ -143,52 +143,75 @@ class _NoteEditorScreenState extends State<NoteEditorScreen> {
],
),
body: isLoading
? Center(child: CircularProgressIndicator()) // Show loader while initializing
: _controller == null
? Center(child: CircularProgressIndicator())
: _isPreviewMode
? Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: MarkdownBody(
builders: {
'latex': LatexElementBuilder(
textStyle: TextStyle(
color: widget.isDarkMode ? Colors.white : Colors.black
? Center(
child:
CircularProgressIndicator()) // Show loader while initializing
: _isPreviewMode
? Padding(
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
),
),
),
)
),
},
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
),
),
),
)
: Padding(
padding: const EdgeInsets.all(16.0),
child: CodeTheme(
data: CodeThemeData(styles: widget.isDarkMode ? nightTheme : dayTheme),
child: CodeField(
gutterStyle: GutterStyle.none,
controller: _controller,
expands: true,
textStyle: TextStyle(
fontSize: _fontSize,
),
),
),
),
: Padding(
padding: const EdgeInsets.all(16.0),
child: CodeTheme(
data: CodeThemeData(
styles: widget.isDarkMode ? nightTheme : dayTheme),
child: CodeField(
gutterStyle: GutterStyle.none,
controller: _controller,
expands: true,
textStyle: TextStyle(
fontSize: _fontSize,
),
),
),
),
);
}
}