mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-26 16:46:51 +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:time/time.dart';
|
||||||
|
|
||||||
|
import 'package:gitjournal/editors/common.dart';
|
||||||
|
|
||||||
class AutoCompletionWidget extends StatefulWidget {
|
class AutoCompletionWidget extends StatefulWidget {
|
||||||
final FocusNode textFieldFocusNode;
|
final FocusNode textFieldFocusNode;
|
||||||
final GlobalKey textFieldKey;
|
final GlobalKey textFieldKey;
|
||||||
@ -52,7 +54,7 @@ class _AutoCompletionWidgetState extends State<AutoCompletionWidget> {
|
|||||||
|
|
||||||
var range = TextRange(0, 0);
|
var range = TextRange(0, 0);
|
||||||
try {
|
try {
|
||||||
var es = EditorState(text, selection.baseOffset);
|
var es = TextEditorState(text, selection.baseOffset);
|
||||||
range = autoCompleter.textChanged(es);
|
range = autoCompleter.textChanged(es);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print(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://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
|
// 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 {
|
class TagsAutoCompleter {
|
||||||
TextRange textChanged(EditorState es) {
|
TextRange textChanged(TextEditorState es) {
|
||||||
// print("${es.text} ${es.cursorPos}");
|
// print("${es.text} ${es.cursorPos}");
|
||||||
var start = es.text.lastIndexOf(RegExp(r'^|[ .?!:;\n]'), es.cursorPos);
|
var start = es.text.lastIndexOf(RegExp(r'^|[ .?!:;\n]'), es.cursorPos);
|
||||||
if (start <= 0) {
|
if (start <= 0) {
|
||||||
|
@ -27,6 +27,13 @@ abstract class EditorState with ChangeNotifier {
|
|||||||
bool get noteModified;
|
bool get noteModified;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TextEditorState {
|
||||||
|
String text;
|
||||||
|
int cursorPos;
|
||||||
|
|
||||||
|
TextEditorState(this.text, this.cursorPos);
|
||||||
|
}
|
||||||
|
|
||||||
class EditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
class EditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||||
final Editor editor;
|
final Editor editor;
|
||||||
final EditorState editorState;
|
final EditorState editorState;
|
||||||
|
@ -1,11 +1,6 @@
|
|||||||
class EditorHeuristicResult {
|
import 'package:gitjournal/editors/common.dart';
|
||||||
String text;
|
|
||||||
int cursorPos;
|
|
||||||
|
|
||||||
EditorHeuristicResult(this.text, this.cursorPos);
|
TextEditorState autoAddBulletList(
|
||||||
}
|
|
||||||
|
|
||||||
EditorHeuristicResult autoAddBulletList(
|
|
||||||
String oldText, String curText, final int cursorPos) {
|
String oldText, String curText, final int cursorPos) {
|
||||||
// We only want to do this on inserts
|
// We only want to do this on inserts
|
||||||
if (curText.length <= oldText.length) {
|
if (curText.length <= oldText.length) {
|
||||||
@ -64,7 +59,7 @@ EditorHeuristicResult autoAddBulletList(
|
|||||||
var newCursorPos = text.length;
|
var newCursorPos = text.length;
|
||||||
|
|
||||||
text += remainingText;
|
text += remainingText;
|
||||||
return EditorHeuristicResult(text, newCursorPos);
|
return TextEditorState(text, newCursorPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
var extraText = indent + bulletType + spacesBeforeContent;
|
var extraText = indent + bulletType + spacesBeforeContent;
|
||||||
@ -72,5 +67,5 @@ EditorHeuristicResult autoAddBulletList(
|
|||||||
var newCursorPos = text.length;
|
var newCursorPos = text.length;
|
||||||
text += remainingText;
|
text += remainingText;
|
||||||
|
|
||||||
return EditorHeuristicResult(text, newCursorPos);
|
return TextEditorState(text, newCursorPos);
|
||||||
}
|
}
|
||||||
|
@ -1,42 +1,43 @@
|
|||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
import 'package:gitjournal/editors/autocompletion_widget.dart';
|
import 'package:gitjournal/editors/autocompletion_widget.dart';
|
||||||
|
import 'package:gitjournal/editors/common.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
var c = TagsAutoCompleter();
|
var c = TagsAutoCompleter();
|
||||||
|
|
||||||
test('Extract first word', () {
|
test('Extract first word', () {
|
||||||
var es = EditorState("#Hel", 3);
|
var es = TextEditorState("#Hel", 3);
|
||||||
var r = c.textChanged(es);
|
var r = c.textChanged(es);
|
||||||
expect(es.text.substring(r.start, r.end), "Hel");
|
expect(es.text.substring(r.start, r.end), "Hel");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Extract second word', () {
|
test('Extract second word', () {
|
||||||
var es = EditorState("Hi #Hel", 7);
|
var es = TextEditorState("Hi #Hel", 7);
|
||||||
var r = c.textChanged(es);
|
var r = c.textChanged(es);
|
||||||
expect(es.text.substring(r.start, r.end), "Hel");
|
expect(es.text.substring(r.start, r.end), "Hel");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Extract second word - cursor not at end', () {
|
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);
|
var r = c.textChanged(es);
|
||||||
expect(es.text.substring(r.start, r.end), "Hell");
|
expect(es.text.substring(r.start, r.end), "Hell");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("Second word with dot", () {
|
test("Second word with dot", () {
|
||||||
var es = EditorState("Hi.#Hel", 6);
|
var es = TextEditorState("Hi.#Hel", 6);
|
||||||
var r = c.textChanged(es);
|
var r = c.textChanged(es);
|
||||||
expect(es.text.substring(r.start, r.end), "Hel");
|
expect(es.text.substring(r.start, r.end), "Hel");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("Second word with newline", () {
|
test("Second word with newline", () {
|
||||||
var es = EditorState("Hi\n#H", 5);
|
var es = TextEditorState("Hi\n#H", 5);
|
||||||
var r = c.textChanged(es);
|
var r = c.textChanged(es);
|
||||||
expect(es.text.substring(r.start, r.end), "H");
|
expect(es.text.substring(r.start, r.end), "H");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Nothing to extract', () {
|
test('Nothing to extract', () {
|
||||||
var es = EditorState("#Hel hi ", 8);
|
var es = TextEditorState("#Hel hi ", 8);
|
||||||
var r = c.textChanged(es);
|
var r = c.textChanged(es);
|
||||||
expect(r.isEmpty, true);
|
expect(r.isEmpty, true);
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user