Add Editor Heuristics to Journal Editor

I frequently make lists in the Journal Editor and it's annoying that
these features aren't available.
This commit is contained in:
Vishesh Handa
2021-02-03 19:24:19 +01:00
parent b301d70991
commit 8eb8905d6d
2 changed files with 24 additions and 11 deletions

View File

@ -5,7 +5,10 @@ import 'package:flutter/material.dart';
import 'package:gitjournal/core/note.dart'; import 'package:gitjournal/core/note.dart';
import 'package:gitjournal/editors/common.dart'; import 'package:gitjournal/editors/common.dart';
import 'package:gitjournal/editors/disposable_change_notifier.dart'; import 'package:gitjournal/editors/disposable_change_notifier.dart';
import 'package:gitjournal/editors/heuristics.dart';
import 'package:gitjournal/editors/note_body_editor.dart'; import 'package:gitjournal/editors/note_body_editor.dart';
import 'package:gitjournal/error_reporting.dart';
import 'package:gitjournal/utils/logger.dart';
import 'package:gitjournal/widgets/editor_scroll_view.dart'; import 'package:gitjournal/widgets/editor_scroll_view.dart';
import 'package:gitjournal/widgets/journal_editor_header.dart'; import 'package:gitjournal/widgets/journal_editor_header.dart';
@ -57,6 +60,8 @@ class JournalEditorState extends State<JournalEditor>
TextEditingController _textController = TextEditingController(); TextEditingController _textController = TextEditingController();
bool _noteModified; bool _noteModified;
EditorHeuristics _heuristics;
JournalEditorState(this.note) { JournalEditorState(this.note) {
_textController = TextEditingController(text: note.body); _textController = TextEditingController(text: note.body);
} }
@ -65,6 +70,7 @@ class JournalEditorState extends State<JournalEditor>
void initState() { void initState() {
super.initState(); super.initState();
_noteModified = widget.noteModified; _noteModified = widget.noteModified;
_heuristics = EditorHeuristics(text: note.body);
} }
@override @override
@ -121,6 +127,13 @@ class JournalEditorState extends State<JournalEditor>
} }
void _noteTextChanged() { void _noteTextChanged() {
try {
_applyHeuristics();
} catch (e, stackTrace) {
Log.e("EditorHeuristics: $e");
logExceptionWarning(e, stackTrace);
}
if (_noteModified && !widget.editMode) { if (_noteModified && !widget.editMode) {
notifyListeners(); notifyListeners();
return; return;
@ -136,6 +149,14 @@ class JournalEditorState extends State<JournalEditor>
notifyListeners(); notifyListeners();
} }
void _applyHeuristics() {
var editState = TextEditorState.fromValue(_textController.value);
var es = _heuristics.textChanged(editState);
if (es != null) {
_textController.value = es.toValue();
}
}
@override @override
Future<void> addImage(File file) async { Future<void> addImage(File file) async {
await getNote().addImage(file); await getNote().addImage(file);

View File

@ -188,18 +188,10 @@ class MarkdownEditorState extends State<MarkdownEditor>
} }
void _applyHeuristics() { void _applyHeuristics() {
var selection = _textController.selection;
var editState = TextEditorState.fromValue(_textController.value); var editState = TextEditorState.fromValue(_textController.value);
var es = _heuristics.textChanged(editState);
// vHanda: Why does this happen? if (es != null) {
if (selection.baseOffset != selection.extentOffset) { _textController.value = es.toValue();
_heuristics.textChanged(editState);
return;
}
var r = _heuristics.textChanged(editState);
if (r != null) {
_textController.value = r.toValue();
} }
} }