mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-26 08:36:50 +08:00
Rename EditorState to TextEditorState
Also remove the EditorHeuristicsResult class. This way all of these are being combined together.
This commit is contained in:
@ -4,6 +4,8 @@ import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:time/time.dart';
|
||||
|
||||
import 'package:gitjournal/editors/common.dart';
|
||||
|
||||
class AutoCompletionWidget extends StatefulWidget {
|
||||
final FocusNode textFieldFocusNode;
|
||||
final GlobalKey textFieldKey;
|
||||
@ -52,7 +54,7 @@ class _AutoCompletionWidgetState extends State<AutoCompletionWidget> {
|
||||
|
||||
var range = TextRange(0, 0);
|
||||
try {
|
||||
var es = EditorState(text, selection.baseOffset);
|
||||
var es = TextEditorState(text, selection.baseOffset);
|
||||
range = autoCompleter.textChanged(es);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
@ -183,13 +185,6 @@ bool enterPressed(String oldText, String newText, int cursorPos) {
|
||||
}
|
||||
*/
|
||||
|
||||
class EditorState {
|
||||
String text;
|
||||
int cursorPos;
|
||||
|
||||
EditorState(this.text, this.cursorPos);
|
||||
}
|
||||
|
||||
// https://levelup.gitconnected.com/flutter-medium-like-text-editor-b41157f50f0e
|
||||
// https://stackoverflow.com/questions/59243627/flutter-how-to-get-the-coordinates-of-the-cursor-in-a-textfield
|
||||
|
||||
@ -265,7 +260,7 @@ class TextRange {
|
||||
}
|
||||
|
||||
class TagsAutoCompleter {
|
||||
TextRange textChanged(EditorState es) {
|
||||
TextRange textChanged(TextEditorState es) {
|
||||
// print("${es.text} ${es.cursorPos}");
|
||||
var start = es.text.lastIndexOf(RegExp(r'^|[ .?!:;\n]'), es.cursorPos);
|
||||
if (start <= 0) {
|
||||
|
@ -27,6 +27,13 @@ abstract class EditorState with ChangeNotifier {
|
||||
bool get noteModified;
|
||||
}
|
||||
|
||||
class TextEditorState {
|
||||
String text;
|
||||
int cursorPos;
|
||||
|
||||
TextEditorState(this.text, this.cursorPos);
|
||||
}
|
||||
|
||||
class EditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
final Editor editor;
|
||||
final EditorState editorState;
|
||||
|
@ -1,11 +1,6 @@
|
||||
class EditorHeuristicResult {
|
||||
String text;
|
||||
int cursorPos;
|
||||
import 'package:gitjournal/editors/common.dart';
|
||||
|
||||
EditorHeuristicResult(this.text, this.cursorPos);
|
||||
}
|
||||
|
||||
EditorHeuristicResult autoAddBulletList(
|
||||
TextEditorState autoAddBulletList(
|
||||
String oldText, String curText, final int cursorPos) {
|
||||
// We only want to do this on inserts
|
||||
if (curText.length <= oldText.length) {
|
||||
@ -64,7 +59,7 @@ EditorHeuristicResult autoAddBulletList(
|
||||
var newCursorPos = text.length;
|
||||
|
||||
text += remainingText;
|
||||
return EditorHeuristicResult(text, newCursorPos);
|
||||
return TextEditorState(text, newCursorPos);
|
||||
}
|
||||
|
||||
var extraText = indent + bulletType + spacesBeforeContent;
|
||||
@ -72,5 +67,5 @@ EditorHeuristicResult autoAddBulletList(
|
||||
var newCursorPos = text.length;
|
||||
text += remainingText;
|
||||
|
||||
return EditorHeuristicResult(text, newCursorPos);
|
||||
return TextEditorState(text, newCursorPos);
|
||||
}
|
||||
|
@ -1,42 +1,43 @@
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'package:gitjournal/editors/autocompletion_widget.dart';
|
||||
import 'package:gitjournal/editors/common.dart';
|
||||
|
||||
void main() {
|
||||
var c = TagsAutoCompleter();
|
||||
|
||||
test('Extract first word', () {
|
||||
var es = EditorState("#Hel", 3);
|
||||
var es = TextEditorState("#Hel", 3);
|
||||
var r = c.textChanged(es);
|
||||
expect(es.text.substring(r.start, r.end), "Hel");
|
||||
});
|
||||
|
||||
test('Extract second word', () {
|
||||
var es = EditorState("Hi #Hel", 7);
|
||||
var es = TextEditorState("Hi #Hel", 7);
|
||||
var r = c.textChanged(es);
|
||||
expect(es.text.substring(r.start, r.end), "Hel");
|
||||
});
|
||||
|
||||
test('Extract second word - cursor not at end', () {
|
||||
var es = EditorState("Hi #Hell", 7);
|
||||
var es = TextEditorState("Hi #Hell", 7);
|
||||
var r = c.textChanged(es);
|
||||
expect(es.text.substring(r.start, r.end), "Hell");
|
||||
});
|
||||
|
||||
test("Second word with dot", () {
|
||||
var es = EditorState("Hi.#Hel", 6);
|
||||
var es = TextEditorState("Hi.#Hel", 6);
|
||||
var r = c.textChanged(es);
|
||||
expect(es.text.substring(r.start, r.end), "Hel");
|
||||
});
|
||||
|
||||
test("Second word with newline", () {
|
||||
var es = EditorState("Hi\n#H", 5);
|
||||
var es = TextEditorState("Hi\n#H", 5);
|
||||
var r = c.textChanged(es);
|
||||
expect(es.text.substring(r.start, r.end), "H");
|
||||
});
|
||||
|
||||
test('Nothing to extract', () {
|
||||
var es = EditorState("#Hel hi ", 8);
|
||||
var es = TextEditorState("#Hel hi ", 8);
|
||||
var r = c.textChanged(es);
|
||||
expect(r.isEmpty, true);
|
||||
});
|
||||
|
Reference in New Issue
Block a user