2025-02-21 22:51:56 +02:00
|
|
|
import 'dart:io';
|
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:flutter_markdown_latex/flutter_markdown_latex.dart';
|
2025-02-22 19:11:10 +02:00
|
|
|
import 'package:flutter_svg/svg.dart';
|
2025-02-21 22:51:56 +02:00
|
|
|
import 'package:markdown/markdown.dart' as md;
|
2025-02-22 19:11:10 +02:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2025-02-21 17:46:41 +02:00
|
|
|
import 'themes/day.dart';
|
|
|
|
|
import 'themes/night.dart';
|
|
|
|
|
import 'themes/markdown.dart';
|
2025-02-22 17:18:47 +02:00
|
|
|
import 'codefield.dart';
|
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 22:51:56 +02:00
|
|
|
final bool isDarkMode;
|
2025-02-20 22:40:41 +02:00
|
|
|
|
2025-02-21 22:51:56 +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> {
|
|
|
|
|
late CodeController _controller;
|
2025-02-22 19:11:10 +02:00
|
|
|
late String _initialContent;
|
|
|
|
|
bool _isPreviewMode = false;
|
|
|
|
|
bool _isLoading = true;
|
|
|
|
|
bool _autoSaveEnabled = false; // Controlled by user toggle
|
|
|
|
|
double _fontSize = 16.0;
|
2025-02-21 17:46:41 +02:00
|
|
|
|
2025-02-20 22:40:41 +02:00
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
2025-02-22 19:11:10 +02:00
|
|
|
_loadPreferences();
|
2025-02-20 22:40:41 +02:00
|
|
|
_loadNoteContent();
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-22 19:11:10 +02:00
|
|
|
Future<void> _loadPreferences() async {
|
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
|
setState(() {
|
|
|
|
|
_autoSaveEnabled = prefs.getBool('auto_save_enabled') ?? false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _setAutoSaveEnabled(bool value) async {
|
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
|
await prefs.setBool('auto_save_enabled', value);
|
|
|
|
|
setState(() {
|
|
|
|
|
_autoSaveEnabled = value;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-20 22:40:41 +02:00
|
|
|
Future<void> _loadNoteContent() async {
|
2025-02-21 22:02:06 +02:00
|
|
|
try {
|
2025-02-22 19:11:10 +02:00
|
|
|
_initialContent = await widget.note.readAsString();
|
2025-02-21 22:02:06 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
print("❌ Failed to read file: ${widget.note.path}");
|
2025-02-22 19:11:10 +02:00
|
|
|
_initialContent = "";
|
2025-02-21 22:02:06 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-20 22:40:41 +02:00
|
|
|
setState(() {
|
|
|
|
|
_controller = CodeController(
|
2025-02-22 19:11:10 +02:00
|
|
|
text: _initialContent,
|
2025-02-20 22:40:41 +02:00
|
|
|
language: markdown,
|
|
|
|
|
);
|
2025-02-22 19:11:10 +02:00
|
|
|
_controller.addListener(_onTextChanged);
|
|
|
|
|
_isLoading = false;
|
2025-02-20 22:40:41 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-22 19:11:10 +02:00
|
|
|
void _onTextChanged() {
|
|
|
|
|
if (_autoSaveEnabled && _controller.text != _initialContent) {
|
|
|
|
|
_saveNote();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-20 22:40:41 +02:00
|
|
|
Future<void> _saveNote() async {
|
2025-02-21 22:02:06 +02:00
|
|
|
try {
|
2025-02-22 19:11:10 +02:00
|
|
|
if (_controller.text == _initialContent) return; // No change, skip saving
|
2025-02-21 22:02:06 +02:00
|
|
|
|
2025-02-22 19:11:10 +02:00
|
|
|
await widget.note.writeAsString(_controller.text);
|
|
|
|
|
_initialContent = _controller.text; // Update initial state
|
2025-02-21 22:02:06 +02:00
|
|
|
|
2025-02-22 19:11:10 +02:00
|
|
|
print("✅ Note saved successfully");
|
|
|
|
|
} catch (e) {
|
2025-02-21 22:02:06 +02:00
|
|
|
print("❌ Error saving note: ${widget.note.path}");
|
|
|
|
|
}
|
2025-02-20 22:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-22 19:11:10 +02:00
|
|
|
Future<bool> _onWillPop() async {
|
|
|
|
|
if (_controller.text == _initialContent) return true; // No changes, exit
|
|
|
|
|
|
|
|
|
|
return await showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
|
title: Text("Save Changes?"),
|
|
|
|
|
content: Text("You have unsaved changes. Save before exiting?"),
|
|
|
|
|
actions: [
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: () => Navigator.of(context).pop(true), // Discard and exit
|
|
|
|
|
child: Text("Discard"),
|
|
|
|
|
),
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: () async {
|
|
|
|
|
await _saveNote();
|
|
|
|
|
Navigator.of(context).pop(true); // Save and exit
|
|
|
|
|
},
|
|
|
|
|
child: Text("Save"),
|
|
|
|
|
),
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: () => Navigator.of(context).pop(false), // Stay on page
|
|
|
|
|
child: Text("Cancel"),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
) ??
|
|
|
|
|
false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 22:51:56 +02:00
|
|
|
void _updateFontSize(double newSize) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_fontSize = newSize;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-20 22:40:41 +02:00
|
|
|
@override
|
|
|
|
|
void dispose() {
|
2025-02-22 19:11:10 +02:00
|
|
|
_controller.removeListener(_onTextChanged);
|
2025-02-20 22:40:41 +02:00
|
|
|
_controller.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-02-22 19:11:10 +02:00
|
|
|
return WillPopScope(
|
|
|
|
|
onWillPop: _onWillPop,
|
|
|
|
|
child: Scaffold(
|
|
|
|
|
appBar: AppBar(
|
|
|
|
|
title: Text(widget.note.uri.pathSegments.last.replaceAll('.md', '')),
|
|
|
|
|
leading: IconButton(
|
|
|
|
|
icon: Icon(Icons.arrow_back),
|
|
|
|
|
onPressed: () async {
|
|
|
|
|
if (await _onWillPop()) {
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
actions: [
|
|
|
|
|
Padding(
|
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 15),
|
|
|
|
|
child: PopupMenuButton<String>(
|
|
|
|
|
icon: SvgPicture.asset(
|
|
|
|
|
"assets/blaster.svg",
|
|
|
|
|
width: 30,
|
|
|
|
|
color: Colors.orange,
|
2025-02-21 17:46:41 +02:00
|
|
|
),
|
2025-02-22 19:11:10 +02:00
|
|
|
onSelected: (String value) {
|
|
|
|
|
switch (value) {
|
|
|
|
|
case 'Save':
|
|
|
|
|
_saveNote();
|
|
|
|
|
break;
|
|
|
|
|
case 'Toggle Autosave':
|
|
|
|
|
_setAutoSaveEnabled(!_autoSaveEnabled);
|
|
|
|
|
break;
|
|
|
|
|
case 'Toggle Preview':
|
|
|
|
|
setState(() {
|
|
|
|
|
_isPreviewMode = !_isPreviewMode;
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
itemBuilder: (BuildContext context) => [
|
|
|
|
|
PopupMenuItem<String>(
|
|
|
|
|
value: 'Save',
|
|
|
|
|
child: ListTile(
|
2025-02-22 20:59:42 +02:00
|
|
|
leading: Icon(Icons.save_outlined),
|
2025-02-22 19:11:10 +02:00
|
|
|
title: Text("Save"),
|
|
|
|
|
),
|
2025-02-21 18:16:17 +02:00
|
|
|
),
|
2025-02-22 19:11:10 +02:00
|
|
|
PopupMenuItem<String>(
|
|
|
|
|
value: 'Toggle Autosave',
|
|
|
|
|
child: ListTile(
|
|
|
|
|
leading: Icon(_autoSaveEnabled
|
2025-02-22 20:59:42 +02:00
|
|
|
? Icons.alarm_off
|
|
|
|
|
: Icons.alarm_on),
|
2025-02-22 19:11:10 +02:00
|
|
|
title: Text(_autoSaveEnabled
|
|
|
|
|
? "Disable Autosave"
|
|
|
|
|
: "Enable Autosave"),
|
2025-02-21 22:51:56 +02:00
|
|
|
),
|
|
|
|
|
),
|
2025-02-22 20:59:42 +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-22 19:11:10 +02:00
|
|
|
PopupMenuItem<String>(
|
|
|
|
|
value: 'Toggle Preview',
|
|
|
|
|
child: ListTile(
|
|
|
|
|
leading:
|
2025-02-22 20:59:42 +02:00
|
|
|
Icon(_isPreviewMode ? Icons.edit_note_outlined : Icons.visibility_outlined),
|
2025-02-22 19:11:10 +02:00
|
|
|
title:
|
|
|
|
|
Text(_isPreviewMode ? "Edit Mode" : "Preview Mode"),
|
2025-02-21 22:51:56 +02:00
|
|
|
),
|
|
|
|
|
),
|
2025-02-22 19:11:10 +02:00
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
body: _isLoading
|
|
|
|
|
? 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)),
|
|
|
|
|
},
|
|
|
|
|
extensionSet: md.ExtensionSet(
|
|
|
|
|
[LatexBlockSyntax()],
|
|
|
|
|
[LatexInlineSyntax()],
|
|
|
|
|
),
|
|
|
|
|
data: _controller.text,
|
|
|
|
|
selectable: true,
|
|
|
|
|
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),
|
|
|
|
|
codeblockDecoration: BoxDecoration(
|
|
|
|
|
color: Colors.transparent),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
: Padding(
|
|
|
|
|
padding: const EdgeInsets.all(16.0),
|
|
|
|
|
child: CodeTheme(
|
|
|
|
|
data: CodeThemeData(
|
|
|
|
|
styles: widget.isDarkMode ? nightTheme : dayTheme),
|
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
|
child: WrappedCodeField(
|
|
|
|
|
wrap: true,
|
|
|
|
|
controller: _controller,
|
|
|
|
|
textStyle: TextStyle(
|
|
|
|
|
fontSize: _fontSize,
|
2025-02-21 22:51:56 +02:00
|
|
|
),
|
2025-02-22 19:11:10 +02:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
2025-02-20 22:40:41 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|