Skip to content

Commit 0a01034

Browse files
committed
Move TextCursor to text_cursor.dart.
1 parent 7588a76 commit 0a01034

File tree

5 files changed

+76
-69
lines changed

5 files changed

+76
-69
lines changed

patterns/state/manipulator_state/states/_/paint_style.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
33
import '../../shapes/circle_shape.dart';
44
import '../../shapes/shape.dart';
55
import '../../shapes/text_shape.dart';
6-
import '../selections/text_change_state.dart';
6+
import '../selections/text/text_cursor.dart';
77

88
class PaintStyle {
99
PaintStyle(this.color)
@@ -70,7 +70,10 @@ class PaintStyle {
7070
}
7171

7272
void paintTextCursor(
73-
TextCursor cursor, TextShape selectedShape, Canvas canvas) {
73+
TextCursor cursor,
74+
TextShape selectedShape,
75+
Canvas canvas,
76+
) {
7477
canvas.drawRect(
7578
Rect.fromLTWH(
7679
cursor.xCoordinate,
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import 'dart:ui';
2+
3+
import '../../../shapes/text_shape.dart';
4+
5+
class TextCursor {
6+
TextCursor(this._shape);
7+
8+
double get xCoordinate => _xPosition;
9+
10+
void changePosition(double x, double y) {
11+
x = x - _shape.x;
12+
y = y - _shape.y;
13+
14+
final pos = _shape.paragraph.getPositionForOffset(Offset(x, y));
15+
_charIndex = pos.offset;
16+
17+
final range = _shape.paragraph.getBoxesForRange(
18+
pos.offset - 1,
19+
pos.offset,
20+
);
21+
22+
_xPosition = _shape.x;
23+
24+
if (range.isNotEmpty) {
25+
_xPosition += range.first.right;
26+
}
27+
}
28+
29+
String inputText(String char) {
30+
final start = _shape.text.substring(0, _charIndex);
31+
final end = _shape.text.substring(_charIndex);
32+
_shape.text = '$start$char$end';
33+
final range = _shape.paragraph.getBoxesForRange(_charIndex, ++_charIndex);
34+
_xPosition = _shape.x;
35+
36+
if (range.isNotEmpty) {
37+
_xPosition += range.first.right;
38+
}
39+
40+
return '$start$char$end';
41+
}
42+
43+
void backspace() {
44+
if (_charIndex <= 0) {
45+
return;
46+
}
47+
48+
final start = _shape.text.substring(0, _charIndex - 1);
49+
final end = _shape.text.length > start.length
50+
? _shape.text.substring(_charIndex)
51+
: '';
52+
53+
_shape.text = '$start$end';
54+
55+
final range = _shape.paragraph.getBoxesForRange(_charIndex - 1, _charIndex);
56+
_xPosition = _shape.x;
57+
58+
if (range.isNotEmpty) {
59+
_xPosition += range.first.left;
60+
} else {
61+
_xPosition += _shape.width;
62+
}
63+
_charIndex--;
64+
}
65+
66+
final TextShape _shape;
67+
int _charIndex = 0;
68+
double _xPosition = 0;
69+
}

patterns/state/manipulator_state/states/selections/text_change_state.dart

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:flutter/services.dart';
33

44
import '../../shapes/text_shape.dart';
55
import 'selection_state.dart';
6+
import 'text/text_cursor.dart';
67

78
class TextChangeState extends SelectionState<TextShape> {
89
TextChangeState({
@@ -71,69 +72,3 @@ class TextChangeState extends SelectionState<TextShape> {
7172
return '${super.toString()} + Text Change State';
7273
}
7374
}
74-
75-
class TextCursor {
76-
TextCursor(this._shape);
77-
78-
double get xCoordinate => _xPosition;
79-
80-
void changePosition(double x, double y) {
81-
x = x - _shape.x;
82-
y = y - _shape.y;
83-
84-
final pos = _shape.paragraph.getPositionForOffset(Offset(x, y));
85-
_charIndex = pos.offset;
86-
87-
final range = _shape.paragraph.getBoxesForRange(
88-
pos.offset - 1,
89-
pos.offset,
90-
);
91-
92-
_xPosition = _shape.x;
93-
94-
if (range.isNotEmpty) {
95-
_xPosition += range.first.right;
96-
}
97-
}
98-
99-
String inputText(String char) {
100-
final start = _shape.text.substring(0, _charIndex);
101-
final end = _shape.text.substring(_charIndex);
102-
_shape.text = '$start$char$end';
103-
final range = _shape.paragraph.getBoxesForRange(_charIndex, ++_charIndex);
104-
_xPosition = _shape.x;
105-
106-
if (range.isNotEmpty) {
107-
_xPosition += range.first.right;
108-
}
109-
110-
return '$start$char$end';
111-
}
112-
113-
void backspace() {
114-
if (_charIndex <= 0) {
115-
return;
116-
}
117-
118-
final start = _shape.text.substring(0, _charIndex - 1);
119-
final end = _shape.text.length > start.length
120-
? _shape.text.substring(_charIndex)
121-
: '';
122-
123-
_shape.text = '$start$end';
124-
125-
final range = _shape.paragraph.getBoxesForRange(_charIndex - 1, _charIndex);
126-
_xPosition = _shape.x;
127-
128-
if (range.isNotEmpty) {
129-
_xPosition += range.first.left;
130-
} else {
131-
_xPosition += _shape.width;
132-
}
133-
_charIndex--;
134-
}
135-
136-
final TextShape _shape;
137-
int _charIndex = 0;
138-
double _xPosition = 0;
139-
}

patterns/state/manipulator_state/states/selections/text_resize_state.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'dart:ui';
33
import '../../shapes/text_shape.dart';
44
import '../_/sub_states/parent_state.dart';
55
import 'text_change_state.dart';
6-
import 'text_resize_marker/text_resize_marker_state.dart';
6+
import 'text/text_resize_marker_state.dart';
77

88
class TextResizeState extends ParentState<TextShape> {
99
TextResizeState({required super.selectedShape}) {

0 commit comments

Comments
 (0)