From adf28a4e78dba234820c3d7fde2a9e99f67f143f Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 7 Oct 2019 20:20:48 +0200 Subject: [PATCH] Add a RawEditing option This way one can directly modify the YAML to add whatever tags they want. --- lib/note.dart | 56 +++++++++++-------- lib/screens/note_editor.dart | 103 ++++++++++++++++++++++++++--------- pubspec.yaml | 2 +- 3 files changed, 109 insertions(+), 52 deletions(-) diff --git a/lib/note.dart b/lib/note.dart index 52baa8d5..d36e159c 100644 --- a/lib/note.dart +++ b/lib/note.dart @@ -13,7 +13,7 @@ enum NoteLoadState { class Note implements Comparable { String filePath = ""; DateTime created; - NoteData data = NoteData(); + NoteData _data = NoteData(); DateTime _fileLastModified; @@ -32,6 +32,37 @@ class Note implements Comparable { data.body = newBody; } + NoteData get data { + return _data; + } + + set data(NoteData data) { + _data = data; + + if (data.props.containsKey("created")) { + var createdStr = data.props['created'].toString(); + try { + created = DateTime.parse(data.props['created']).toLocal(); + } catch (ex) { + // Ignore it + } + + if (created == null) { + var regex = RegExp( + r"(\d{4})-(\d{2})-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})\+(\d{2})\:(\d{2})"); + if (regex.hasMatch(createdStr)) { + // FIXME: Handle the timezone! + createdStr = createdStr.substring(0, 19); + created = DateTime.parse(createdStr); + } + } + } + + if (created == null) { + created = DateTime(0, 0, 0, 0, 0, 0, 0, 0); + } + } + bool hasValidDate() { // Arbitrary number, when we set the year = 0, it becomes 1, somehow return created.year > 10; @@ -58,29 +89,6 @@ class Note implements Comparable { final string = await file.readAsString(); data = _serializer.decode(string); - if (data.props.containsKey("created")) { - var createdStr = data.props['created'].toString(); - try { - created = DateTime.parse(data.props['created']).toLocal(); - } catch (ex) { - // Ignore it - } - - if (created == null) { - var regex = RegExp( - r"(\d{4})-(\d{2})-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})\+(\d{2})\:(\d{2})"); - if (regex.hasMatch(createdStr)) { - // FIXME: Handle the timezone! - createdStr = createdStr.substring(0, 19); - created = DateTime.parse(createdStr); - } - } - } - - if (created == null) { - created = DateTime(0, 0, 0, 0, 0, 0, 0, 0); - } - _fileLastModified = file.lastModifiedSync(); _loadState = NoteLoadState.Loaded; diff --git a/lib/screens/note_editor.dart b/lib/screens/note_editor.dart index 501d9c3c..bee4bb53 100644 --- a/lib/screens/note_editor.dart +++ b/lib/screens/note_editor.dart @@ -2,6 +2,9 @@ import 'package:flutter/material.dart'; import 'package:gitjournal/note.dart'; import 'package:gitjournal/state_container.dart'; import 'package:gitjournal/widgets/note_header.dart'; +import 'package:gitjournal/storage/serializers.dart'; + +enum NoteEditorDropDownChoices { Discard, RawEditor } class NoteEditor extends StatefulWidget { final Note note; @@ -23,6 +26,8 @@ class NoteEditorState extends State { Note note = Note(); final bool newNote; TextEditingController _textController = TextEditingController(); + bool rawEditor = false; + final serializer = MarkdownYAMLSerializer(); NoteEditorState() : newNote = true { note.created = DateTime.now(); @@ -34,19 +39,15 @@ class NoteEditorState extends State { @override Widget build(BuildContext context) { - var bodyWidget = Form( - child: TextFormField( - autofocus: true, - keyboardType: TextInputType.multiline, - maxLines: null, - decoration: InputDecoration( - hintText: 'Write here', - border: InputBorder.none, - ), - controller: _textController, - textCapitalization: TextCapitalization.sentences, - ), + Widget editor = Column( + children: [ + NoteHeader(note), + NoteMarkdownEditor(_textController), + ], ); + if (rawEditor) { + editor = NoteMarkdownEditor(_textController); + } var title = newNote ? "Journal Entry" : "Edit Journal Entry"; var newJournalScreen = Scaffold( @@ -57,7 +58,11 @@ class NoteEditorState extends State { icon: Icon(Icons.check), onPressed: () { final stateContainer = StateContainer.of(context); - note.body = _textController.text; + if (rawEditor == false) { + note.body = _textController.text; + } else { + note.data = serializer.decode(_textController.text); + } if (note.body.isNotEmpty) { newNote ? stateContainer.addNote(note) @@ -67,27 +72,44 @@ class NoteEditorState extends State { }, ), actions: [ - IconButton( - icon: Icon(Icons.close), - onPressed: () { - if (_noteModified()) { - showDialog(context: context, builder: _buildAlertDialog); - } else { - Navigator.pop(context); + PopupMenuButton( + onSelected: (NoteEditorDropDownChoices choice) { + switch (choice) { + case NoteEditorDropDownChoices.Discard: + if (_noteModified()) { + showDialog(context: context, builder: _buildAlertDialog); + } else { + Navigator.pop(context); + } + break; + case NoteEditorDropDownChoices.RawEditor: + setState(() { + rawEditor = true; + var noteData = + NoteData(_textController.text, note.data.props); + _textController.text = serializer.encode(noteData); + }); + break; } }, + itemBuilder: (BuildContext context) => + >[ + const PopupMenuItem( + value: NoteEditorDropDownChoices.Discard, + child: Text('Discard'), + ), + const PopupMenuItem( + value: NoteEditorDropDownChoices.RawEditor, + child: Text('RawEditor'), + ), + ], ), ], ), body: Padding( padding: const EdgeInsets.all(16.0), child: SingleChildScrollView( - child: Column( - children: [ - NoteHeader(note), - bodyWidget, - ], - ), + child: editor, ), ), ); @@ -127,9 +149,36 @@ class NoteEditorState extends State { return false; } if (note != null) { - return noteContent != note.body; + if (rawEditor) { + return serializer.encode(note.data) != noteContent; + } else { + return noteContent != note.body; + } } return false; } } + +class NoteMarkdownEditor extends StatelessWidget { + final TextEditingController textController; + + NoteMarkdownEditor(this.textController); + + @override + Widget build(BuildContext context) { + return Form( + child: TextFormField( + autofocus: true, + keyboardType: TextInputType.multiline, + maxLines: null, + decoration: InputDecoration( + hintText: 'Write here', + border: InputBorder.none, + ), + controller: textController, + textCapitalization: TextCapitalization.sentences, + ), + ); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 5e464924..78c1f99a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: gitjournal description: A Journaling App Built on top of Git -version: 1.1.14+10 +version: 1.1.15+10 dependencies: flutter: