mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-27 17:29:50 +08:00
Move Editor Heuristics to be self contained
This way it can easily be used in other editors
This commit is contained in:
@ -32,6 +32,18 @@ class TextEditorState {
|
|||||||
int cursorPos;
|
int cursorPos;
|
||||||
|
|
||||||
TextEditorState(this.text, this.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 {
|
class EditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||||
|
@ -69,3 +69,18 @@ TextEditorState autoAddBulletList(
|
|||||||
|
|
||||||
return TextEditorState(text, newCursorPos);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -63,20 +63,20 @@ class MarkdownEditorState extends State<MarkdownEditor>
|
|||||||
TextEditingController _textController = TextEditingController();
|
TextEditingController _textController = TextEditingController();
|
||||||
TextEditingController _titleTextController = TextEditingController();
|
TextEditingController _titleTextController = TextEditingController();
|
||||||
|
|
||||||
String _oldText;
|
EditorHeuristics _heuristics;
|
||||||
|
|
||||||
bool _noteModified;
|
bool _noteModified;
|
||||||
|
|
||||||
MarkdownEditorState(this.note) {
|
MarkdownEditorState(this.note) {
|
||||||
_textController = TextEditingController(text: note.body);
|
_textController = TextEditingController(text: note.body);
|
||||||
_titleTextController = TextEditingController(text: note.title);
|
_titleTextController = TextEditingController(text: note.title);
|
||||||
_oldText = note.body;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_noteModified = widget.noteModified;
|
_noteModified = widget.noteModified;
|
||||||
|
_heuristics = EditorHeuristics(text: note.body);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -192,21 +192,18 @@ class MarkdownEditorState extends State<MarkdownEditor>
|
|||||||
|
|
||||||
void _applyHeuristics() {
|
void _applyHeuristics() {
|
||||||
var selection = _textController.selection;
|
var selection = _textController.selection;
|
||||||
|
var editState = TextEditorState.fromValue(_textController.value);
|
||||||
|
|
||||||
|
// vHanda: Why does this happen?
|
||||||
if (selection.baseOffset != selection.extentOffset) {
|
if (selection.baseOffset != selection.extentOffset) {
|
||||||
_oldText = _textController.text;
|
_heuristics.textChanged(editState);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var r =
|
var r = _heuristics.textChanged(editState);
|
||||||
autoAddBulletList(_oldText, _textController.text, selection.baseOffset);
|
if (r != null) {
|
||||||
_oldText = _textController.text;
|
_textController.value = r.toValue();
|
||||||
|
|
||||||
if (r == null) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_textController.text = r.text;
|
|
||||||
_textController.selection = TextSelection.collapsed(offset: r.cursorPos);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -22,6 +22,7 @@ import 'package:gitjournal/error_reporting.dart';
|
|||||||
import 'package:gitjournal/features.dart';
|
import 'package:gitjournal/features.dart';
|
||||||
import 'package:gitjournal/settings.dart';
|
import 'package:gitjournal/settings.dart';
|
||||||
import 'package:gitjournal/settings_migrations.dart';
|
import 'package:gitjournal/settings_migrations.dart';
|
||||||
|
import 'package:gitjournal/setup/clone.dart';
|
||||||
import 'package:gitjournal/utils/logger.dart';
|
import 'package:gitjournal/utils/logger.dart';
|
||||||
|
|
||||||
enum SyncStatus {
|
enum SyncStatus {
|
||||||
@ -89,6 +90,23 @@ class Repository with ChangeNotifier {
|
|||||||
|
|
||||||
var repo = await GitRepository.load(repoPath);
|
var repo = await GitRepository.load(repoPath);
|
||||||
var remoteConfigured = repo.config.remotes.isNotEmpty;
|
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(
|
return Repository._internal(
|
||||||
repoPath: repoPath,
|
repoPath: repoPath,
|
||||||
|
Reference in New Issue
Block a user