-
Notifications
You must be signed in to change notification settings - Fork 166
/
divider_block_component.dart
216 lines (184 loc) · 5.14 KB
/
divider_block_component.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class DividerBlockKeys {
const DividerBlockKeys._();
static const String type = 'divider';
}
// creating a new callout node
Node dividerNode() {
return Node(
type: DividerBlockKeys.type,
);
}
typedef DividerBlockWrapper = Widget Function(
BuildContext context,
Node node,
Widget child,
);
class DividerBlockComponentBuilder extends BlockComponentBuilder {
DividerBlockComponentBuilder({
super.configuration,
this.lineColor = Colors.grey,
this.height = 10,
this.wrapper,
});
final Color lineColor;
final double height;
final DividerBlockWrapper? wrapper;
@override
BlockComponentWidget build(BlockComponentContext blockComponentContext) {
final node = blockComponentContext.node;
return DividerBlockComponentWidget(
key: node.key,
node: node,
configuration: configuration,
lineColor: lineColor,
height: height,
wrapper: wrapper,
showActions: showActions(node),
actionBuilder: (context, state) => actionBuilder(
blockComponentContext,
state,
),
);
}
@override
bool validate(Node node) => node.children.isEmpty;
}
class DividerBlockComponentWidget extends BlockComponentStatefulWidget {
const DividerBlockComponentWidget({
super.key,
required super.node,
super.showActions,
super.actionBuilder,
super.configuration = const BlockComponentConfiguration(),
this.lineColor = Colors.grey,
this.height = 10,
this.wrapper,
});
final Color lineColor;
final double height;
final DividerBlockWrapper? wrapper;
@override
State<DividerBlockComponentWidget> createState() =>
_DividerBlockComponentWidgetState();
}
class _DividerBlockComponentWidgetState
extends State<DividerBlockComponentWidget>
with SelectableMixin, BlockComponentConfigurable {
@override
BlockComponentConfiguration get configuration => widget.configuration;
@override
Node get node => widget.node;
final dividerKey = GlobalKey();
RenderBox? get _renderBox => context.findRenderObject() as RenderBox?;
@override
Widget build(BuildContext context) {
Widget child = Container(
height: widget.height,
alignment: Alignment.center,
child: Divider(
color: widget.lineColor,
thickness: 1,
),
);
child = Padding(
key: dividerKey,
padding: padding,
child: child,
);
final editorState = context.read<EditorState>();
child = BlockSelectionContainer(
node: node,
delegate: this,
listenable: editorState.selectionNotifier,
remoteSelection: editorState.remoteSelections,
blockColor: editorState.editorStyle.selectionColor,
cursorColor: editorState.editorStyle.cursorColor,
selectionColor: editorState.editorStyle.selectionColor,
supportTypes: const [
BlockSelectionType.block,
BlockSelectionType.cursor,
BlockSelectionType.selection,
],
child: child,
);
if (widget.showActions && widget.actionBuilder != null) {
child = BlockComponentActionWrapper(
node: node,
actionBuilder: widget.actionBuilder!,
child: child,
);
}
if (widget.wrapper != null) {
child = widget.wrapper!(context, node, child);
}
return child;
}
@override
Position start() => Position(path: widget.node.path, offset: 0);
@override
Position end() => Position(path: widget.node.path, offset: 1);
@override
Position getPositionInOffset(Offset start) => end();
@override
bool get shouldCursorBlink => false;
@override
CursorStyle get cursorStyle => CursorStyle.cover;
@override
Rect getBlockRect({
bool shiftWithBaseOffset = false,
}) {
return getRectsInSelection(Selection.invalid()).first;
}
@override
Rect? getCursorRectInPosition(
Position position, {
bool shiftWithBaseOffset = false,
}) {
if (_renderBox == null) {
return null;
}
return getRectsInSelection(
Selection.collapsed(position),
shiftWithBaseOffset: shiftWithBaseOffset,
).firstOrNull;
}
@override
List<Rect> getRectsInSelection(
Selection selection, {
bool shiftWithBaseOffset = false,
}) {
if (_renderBox == null) {
return [];
}
final parentBox = context.findRenderObject();
final dividerBox = dividerKey.currentContext?.findRenderObject();
if (parentBox is RenderBox && dividerBox is RenderBox) {
return [
(shiftWithBaseOffset
? dividerBox.localToGlobal(Offset.zero, ancestor: parentBox)
: Offset.zero) &
dividerBox.size,
];
}
return [Offset.zero & _renderBox!.size];
}
@override
Selection getSelectionInRange(Offset start, Offset end) => Selection.single(
path: widget.node.path,
startOffset: 0,
endOffset: 1,
);
@override
Offset localToGlobal(
Offset offset, {
bool shiftWithBaseOffset = false,
}) =>
_renderBox!.localToGlobal(offset);
@override
TextDirection textDirection() {
return TextDirection.ltr;
}
}