Move Editor Heuristics to be self contained

This way it can easily be used in other editors
This commit is contained in:
Vishesh Handa
2021-02-03 11:02:19 +01:00
parent 350fbb1ccf
commit e80fff575c
4 changed files with 54 additions and 12 deletions

View File

@ -32,6 +32,18 @@ class TextEditorState {
int cursorPos;
TextEditorState(this.text, this.cursorPos);
TextEditorState.fromValue(TextEditingValue val) {
text = val.text;
cursorPos = val.selection.baseOffset;
}
TextEditingValue toValue() {
return TextEditingValue(
text: text,
selection: TextSelection.collapsed(offset: cursorPos),
);
}
}
class EditorAppBar extends StatelessWidget implements PreferredSizeWidget {

View File

@ -69,3 +69,18 @@ TextEditorState autoAddBulletList(
return TextEditorState(text, newCursorPos);
}
class EditorHeuristics {
EditorHeuristics({String text = ''}) {
_lastState = TextEditorState(text, 0);
}
TextEditorState _lastState;
TextEditorState textChanged(TextEditorState es) {
var lastState = _lastState;
_lastState = es;
return autoAddBulletList(lastState.text, es.text, es.cursorPos);
}
}

View File

@ -63,20 +63,20 @@ class MarkdownEditorState extends State<MarkdownEditor>
TextEditingController _textController = TextEditingController();
TextEditingController _titleTextController = TextEditingController();
String _oldText;
EditorHeuristics _heuristics;
bool _noteModified;
MarkdownEditorState(this.note) {
_textController = TextEditingController(text: note.body);
_titleTextController = TextEditingController(text: note.title);
_oldText = note.body;
}
@override
void initState() {
super.initState();
_noteModified = widget.noteModified;
_heuristics = EditorHeuristics(text: note.body);
}
@override
@ -192,21 +192,18 @@ class MarkdownEditorState extends State<MarkdownEditor>
void _applyHeuristics() {
var selection = _textController.selection;
var editState = TextEditorState.fromValue(_textController.value);
// vHanda: Why does this happen?
if (selection.baseOffset != selection.extentOffset) {
_oldText = _textController.text;
_heuristics.textChanged(editState);
return;
}
var r =
autoAddBulletList(_oldText, _textController.text, selection.baseOffset);
_oldText = _textController.text;
if (r == null) {
return;
var r = _heuristics.textChanged(editState);
if (r != null) {
_textController.value = r.toValue();
}
_textController.text = r.text;
_textController.selection = TextSelection.collapsed(offset: r.cursorPos);
}
@override

View File

@ -22,6 +22,7 @@ import 'package:gitjournal/error_reporting.dart';
import 'package:gitjournal/features.dart';
import 'package:gitjournal/settings.dart';
import 'package:gitjournal/settings_migrations.dart';
import 'package:gitjournal/setup/clone.dart';
import 'package:gitjournal/utils/logger.dart';
enum SyncStatus {
@ -89,6 +90,23 @@ class Repository with ChangeNotifier {
var repo = await GitRepository.load(repoPath);
var remoteConfigured = repo.config.remotes.isNotEmpty;
if (remoteConfigured) {
// Code path for 'branch is null' exception
var branches = await repo.branches();
if (branches.isEmpty) {
var remoteConfig = repo.config.remotes[0];
await cloneRemote(
repoPath: repoPath,
remoteName: remoteConfig.name,
cloneUrl: remoteConfig.url,
authorName: settings.gitAuthor,
authorEmail: settings.gitAuthorEmail,
sshPublicKey: settings.sshPublicKey,
sshPrivateKey: settings.sshPrivateKey,
sshPassword: settings.sshPassword,
);
}
}
return Repository._internal(
repoPath: repoPath,