Skip to content

Commit 7702e3e

Browse files
committed
feat: text box has a default size, can be worked with properly: issues still remain like it cannot be resized, dragged,also the editor on right is a bad idea
1 parent a8e6bf4 commit 7702e3e

File tree

3 files changed

+71
-60
lines changed

3 files changed

+71
-60
lines changed

lib/features/models/canvas_models/objects/text_box_object.dart

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
// import 'dart:nativewrappers/_internal/vm/lib/math_patch.dart';
1+
// lib/features/models/canvas_models/objects/text_box_object.dart
22

33
import 'dart:math';
4-
54
import 'package:cookethflow/features/models/canvas_models/canvas_object.dart';
6-
import 'package:flutter/cupertino.dart';
75
import 'package:flutter/material.dart';
86
import 'package:uuid/uuid.dart';
97

@@ -21,22 +19,29 @@ class TextBoxObject extends CanvasObject {
2119
super.textDelta,
2220
});
2321

24-
TextBoxObject.fromJson(Map<String, dynamic> json)
25-
: topLeft = Offset(json['top_left']['x'], json['top_left']['y']),
26-
bottomRight = Offset(
22+
factory TextBoxObject.fromJson(Map<String, dynamic> json) {
23+
return TextBoxObject(
24+
id: json['id'],
25+
color: Color(json['color']),
26+
topLeft: Offset(json['top_left']['x'], json['top_left']['y']),
27+
bottomRight: Offset(
2728
json['bottom_right']['x'],
2829
json['bottom_right']['y'],
2930
),
30-
super(
31-
id: json['id'],
32-
color: json['color'],
33-
textDelta: json['text_delta'],
34-
);
31+
textDelta: json['text_delta'],
32+
);
33+
}
3534

36-
TextBoxObject.createNew(Offset defaultTopLeft, Offset defaultBottomRight)
37-
: topLeft = defaultTopLeft,
38-
bottomRight = defaultBottomRight,
39-
super(id: Uuid().v4(), color: Colors.transparent, textDelta: null);
35+
factory TextBoxObject.createNew(
36+
Offset defaultTopLeft, Offset defaultBottomRight) {
37+
return TextBoxObject(
38+
id: const Uuid().v4(),
39+
color: Colors.transparent,
40+
topLeft: defaultTopLeft,
41+
bottomRight: defaultBottomRight,
42+
textDelta: null,
43+
);
44+
}
4045

4146
@override
4247
Map<String, dynamic> toJson() {
@@ -61,7 +66,8 @@ class TextBoxObject extends CanvasObject {
6166
id: id,
6267
color: color ?? this.color,
6368
topLeft: topLeft ?? this.topLeft,
64-
bottomRight: bottomRight ?? this.topLeft,
69+
// FIX: Corrected a bug where it referenced topLeft instead of bottomRight
70+
bottomRight: bottomRight ?? this.bottomRight,
6571
textDelta: textDelta ?? this.textDelta,
6672
);
6773
}
@@ -88,10 +94,11 @@ class TextBoxObject extends CanvasObject {
8894
min(newTopLeft.dx, newBottomRight.dx),
8995
min(newTopLeft.dy, newBottomRight.dy),
9096
);
91-
final corrextedBottomRight = Offset(
97+
final correctedBottomRight = Offset(
9298
max(newTopLeft.dx, newBottomRight.dx),
9399
max(newTopLeft.dy, newBottomRight.dy),
94100
);
95-
return copyWith(topLeft: correctedTopLeft, bottomRight: corrextedBottomRight);
101+
return copyWith(
102+
topLeft: correctedTopLeft, bottomRight: correctedBottomRight);
96103
}
97-
}
104+
}

lib/features/workspace/providers/workspace_provider.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,16 +491,17 @@ class WorkspaceProvider extends StateHandler {
491491
defaultBottomRight,
492492
);
493493
break;
494-
case DrawMode.textBox: // NEW: Handle TextBox creation
494+
// CHANGE: Updated text box creation logic
495+
case DrawMode.textBox:
495496
final textBoxTopLeft = details.globalPosition;
497+
// Create a box with a default size instead of a single point
496498
final textBoxBottomRight = Offset(
497499
details.globalPosition.dx + _defaultTextBoxWidth,
498500
details.globalPosition.dy + _defaultTextBoxHeight,
499501
);
500502
newObject = TextBoxObject.createNew(textBoxTopLeft, textBoxBottomRight);
501-
// NEW: Automatically set some default text for new text boxes
502-
_tempQuillController.document =
503-
Document()..insert(0, 'Double-click to edit text');
503+
// Set initial dummy text
504+
_tempQuillController.document = Document()..insert(0, 'Click to edit');
504505
newObject = (newObject as TextBoxObject).copyWith(
505506
textDelta: jsonEncode(
506507
_tempQuillController.document.toDelta().toJson(),
@@ -739,4 +740,4 @@ class WorkspaceProvider extends StateHandler {
739740
return a == b;
740741
}
741742
}
742-
}
743+
}

lib/features/workspace/widgets/object_text_editor.dart

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ class _ObjectTextEditorState extends State<ObjectTextEditor> {
3030
return Consumer2<WorkspaceProvider, CanvasProvider>(
3131
builder: (context, workspaceProvider, canvasProvider, child) {
3232
final selectedObjectId = workspaceProvider.currentlySelectedObjectId;
33-
final selectedObject =
34-
selectedObjectId != null
35-
? workspaceProvider.canvasObjects[selectedObjectId]
36-
: null;
33+
final selectedObject = selectedObjectId != null
34+
? workspaceProvider.canvasObjects[selectedObjectId]
35+
: null;
3736

3837
if (workspaceProvider.interactionMode != InteractionMode.editingText ||
3938
selectedObject == null) {
@@ -47,14 +46,11 @@ class _ObjectTextEditorState extends State<ObjectTextEditor> {
4746
});
4847

4948
final Rect objectBounds = selectedObject.getBounds();
50-
final Matrix4 transform = canvasProvider.transformationController.value;
49+
final Matrix4 transform =
50+
canvasProvider.transformationController.value;
5151
final vc.Vector3 transformedTopLeft = transform.transform3(
5252
vc.Vector3(objectBounds.topLeft.dx, objectBounds.topLeft.dy, 0),
5353
);
54-
final Offset screenTopLeft = Offset(
55-
transformedTopLeft.x,
56-
transformedTopLeft.y,
57-
);
5854
final vc.Vector3 transformedBottomRight = transform.transform3(
5955
vc.Vector3(
6056
objectBounds.bottomRight.dx,
@@ -63,23 +59,24 @@ class _ObjectTextEditorState extends State<ObjectTextEditor> {
6359
),
6460
);
6561
final visibleRect = Rect.fromPoints(
66-
screenTopLeft,
62+
Offset(transformedTopLeft.x, transformedTopLeft.y),
6763
Offset(transformedBottomRight.x, transformedBottomRight.y),
6864
);
6965

70-
final quillController = workspaceProvider.selectedObjectQuillController;
66+
final quillController =
67+
workspaceProvider.selectedObjectQuillController;
7168

72-
return Positioned(
73-
left: visibleRect.left,
74-
// Position the entire widget (toolbar + editor) at the object's location
75-
top: visibleRect.top,
76-
child: Material(
77-
color: Colors.transparent,
78-
child: Column(
79-
crossAxisAlignment: CrossAxisAlignment.start,
80-
children: [
81-
Container(
82-
width: visibleRect.width < 350.w ? 350.w : visibleRect.width,
69+
// RESTRUCTURE: Use a Stack to position the toolbar and editor independently.
70+
return Stack(
71+
children: [
72+
// 1. The Quill Toolbar, positioned to the right of the text object.
73+
Positioned(
74+
left: visibleRect.right + 10.w, // Place it right of the object
75+
top: visibleRect.top,
76+
child: Material(
77+
color: Colors.transparent,
78+
child: Container(
79+
// The toolbar will size itself, but you can add constraints if needed
8380
decoration: BoxDecoration(
8481
color: Colors.white,
8582
borderRadius: BorderRadius.circular(8.r),
@@ -111,18 +108,24 @@ class _ObjectTextEditorState extends State<ObjectTextEditor> {
111108
),
112109
),
113110
),
114-
SizedBox(height: 4.h), // Spacing between toolbar and editor
115-
// FIX: Add a visible container for the editor
116-
Container(
117-
width: visibleRect.width,
118-
height: visibleRect.height,
111+
),
112+
),
113+
114+
// 2. The Quill Editor, positioned directly over the text object.
115+
Positioned(
116+
left: visibleRect.left,
117+
top: visibleRect.top,
118+
width: visibleRect.width,
119+
height: visibleRect.height,
120+
child: Material(
121+
color: Colors.transparent,
122+
child: Container(
119123
decoration: BoxDecoration(
120-
color: Colors.white, // Make the editor background white
124+
color: Colors.white,
121125
border: Border.all(
122-
color: Colors.blue.shade300,
123-
width: 1.5,
124-
), // Add a border
125-
borderRadius: BorderRadius.circular(4.r),
126+
color: Colors.blue.shade400, // Brighter border
127+
width: 2.0, // Thicker border to indicate editing
128+
),
126129
),
127130
child: QuillEditor.basic(
128131
controller: quillController,
@@ -139,17 +142,17 @@ class _ObjectTextEditorState extends State<ObjectTextEditor> {
139142
null,
140143
),
141144
),
142-
placeholder: 'Type here...',
145+
placeholder: 'Type something...',
143146
),
144147
focusNode: _focusNode,
145148
scrollController: ScrollController(),
146149
),
147150
),
148-
],
151+
),
149152
),
150-
),
153+
],
151154
);
152155
},
153156
);
154157
}
155-
}
158+
}

0 commit comments

Comments
 (0)