-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathcode_component.dart
More file actions
238 lines (218 loc) · 7.27 KB
/
Copy pathcode_component.dart
File metadata and controls
238 lines (218 loc) · 7.27 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import 'package:flutter/material.dart';
import 'package:super_editor/super_editor.dart';
class FeatherCodeComponentBuilder implements ComponentBuilder {
const FeatherCodeComponentBuilder();
@override
SingleColumnLayoutComponentViewModel? createViewModel(
Document document,
DocumentNode node,
List<ComponentBuilder> componentBuilders,
) {
if (node is! ParagraphNode) {
return null;
}
if (node.getMetadataValue('blockType') != codeAttribution) {
return null;
}
final textDirection = getParagraphDirection(node.text.text);
TextAlign textAlign = (textDirection == TextDirection.ltr) ? TextAlign.left : TextAlign.right;
final textAlignName = node.getMetadataValue('textAlign');
switch (textAlignName) {
case 'left':
textAlign = TextAlign.left;
break;
case 'center':
textAlign = TextAlign.center;
break;
case 'right':
textAlign = TextAlign.right;
break;
case 'justify':
textAlign = TextAlign.justify;
break;
}
return CodeBlockComponentViewModel(
nodeId: node.id,
text: node.text,
textStyleBuilder: noStyleBuilder,
backgroundColor: const Color(0x00000000),
borderRadius: BorderRadius.zero,
textDirection: textDirection,
textAlignment: textAlign,
selectionColor: const Color(0x00000000),
);
}
@override
Widget? createComponent(
SingleColumnDocumentComponentContext componentContext, SingleColumnLayoutComponentViewModel componentViewModel) {
if (componentViewModel is! CodeBlockComponentViewModel) {
return null;
}
return CodeBlockComponent(
textKey: componentContext.componentKey,
text: componentViewModel.text,
textAlign: componentViewModel.textAlignment,
styleBuilder: componentViewModel.textStyleBuilder,
backgroundColor: componentViewModel.backgroundColor,
borderRadius: componentViewModel.borderRadius,
textSelection: componentViewModel.selection,
selectionColor: componentViewModel.selectionColor,
highlightWhenEmpty: componentViewModel.highlightWhenEmpty,
underlines: componentViewModel.createUnderlines(),
);
}
}
class CodeBlockComponentViewModel extends SingleColumnLayoutComponentViewModel with TextComponentViewModel {
CodeBlockComponentViewModel({
required super.nodeId,
super.maxWidth,
super.padding = EdgeInsets.zero,
required this.text,
required this.textStyleBuilder,
this.inlineWidgetBuilders = const [],
this.textDirection = TextDirection.ltr,
this.textAlignment = TextAlign.left,
required this.backgroundColor,
required this.borderRadius,
this.selection,
required this.selectionColor,
this.highlightWhenEmpty = false,
TextRange? composingRegion,
bool showComposingRegionUnderline = false,
UnderlineStyle spellingErrorUnderlineStyle = const SquiggleUnderlineStyle(color: Color(0xFFFF0000)),
List<TextRange> spellingErrors = const <TextRange>[],
}) {
this.composingRegion = composingRegion;
this.showComposingRegionUnderline = showComposingRegionUnderline;
this.spellingErrorUnderlineStyle = spellingErrorUnderlineStyle;
this.spellingErrors = spellingErrors;
}
@override
AttributedText text;
@override
AttributionStyleBuilder textStyleBuilder;
@override
InlineWidgetBuilderChain inlineWidgetBuilders;
@override
TextDirection textDirection;
@override
TextAlign textAlignment;
@override
TextSelection? selection;
@override
Color selectionColor;
@override
bool highlightWhenEmpty;
Color backgroundColor;
BorderRadius borderRadius;
@override
void applyStyles(Map<String, dynamic> styles) {
super.applyStyles(styles);
backgroundColor = styles[Styles.backgroundColor] ?? Colors.transparent;
borderRadius = styles[Styles.borderRadius] ?? BorderRadius.zero;
}
@override
CodeBlockComponentViewModel copy() {
return CodeBlockComponentViewModel(
nodeId: nodeId,
maxWidth: maxWidth,
padding: padding,
text: text,
textStyleBuilder: textStyleBuilder,
inlineWidgetBuilders: inlineWidgetBuilders,
textDirection: textDirection,
textAlignment: textAlignment,
backgroundColor: backgroundColor,
borderRadius: borderRadius,
selection: selection,
selectionColor: selectionColor,
highlightWhenEmpty: highlightWhenEmpty,
composingRegion: composingRegion,
showComposingRegionUnderline: showComposingRegionUnderline,
spellingErrorUnderlineStyle: spellingErrorUnderlineStyle,
spellingErrors: List.from(spellingErrors),
);
}
@override
bool operator ==(Object other) =>
identical(this, other) ||
super == other &&
other is BlockquoteComponentViewModel &&
runtimeType == other.runtimeType &&
nodeId == other.nodeId &&
text == other.text &&
textDirection == other.textDirection &&
textAlignment == other.textAlignment &&
backgroundColor == other.backgroundColor &&
borderRadius == other.borderRadius &&
selection == other.selection &&
selectionColor == other.selectionColor &&
highlightWhenEmpty == other.highlightWhenEmpty &&
composingRegion == other.composingRegion &&
showComposingRegionUnderline == other.showComposingRegionUnderline;
@override
int get hashCode =>
super.hashCode ^
nodeId.hashCode ^
text.hashCode ^
textDirection.hashCode ^
textAlignment.hashCode ^
backgroundColor.hashCode ^
borderRadius.hashCode ^
selection.hashCode ^
selectionColor.hashCode ^
highlightWhenEmpty.hashCode ^
composingRegion.hashCode ^
showComposingRegionUnderline.hashCode;
}
class CodeBlockComponent extends StatelessWidget {
const CodeBlockComponent({
super.key,
required this.textKey,
required this.text,
this.textAlign = TextAlign.left,
required this.styleBuilder,
this.textSelection,
this.selectionColor = Colors.lightBlueAccent,
required this.backgroundColor,
required this.borderRadius,
this.highlightWhenEmpty = false,
this.underlines = const [],
this.showDebugPaint = false,
});
final GlobalKey textKey;
final AttributedText text;
final TextAlign textAlign;
final AttributionStyleBuilder styleBuilder;
final TextSelection? textSelection;
final Color selectionColor;
final Color backgroundColor;
final BorderRadius borderRadius;
final bool highlightWhenEmpty;
final List<Underlines> underlines;
final bool showDebugPaint;
@override
Widget build(BuildContext context) {
return IgnorePointer(
child: Container(
margin: const EdgeInsets.symmetric(vertical: 16),
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
color: const Color(0xFF222222),
),
child: TextComponent(
key: textKey,
text: text,
textAlign: textAlign,
textStyleBuilder: styleBuilder,
textSelection: textSelection,
selectionColor: selectionColor,
highlightWhenEmpty: highlightWhenEmpty,
underlines: underlines,
showDebugPaint: showDebugPaint,
),
),
);
}
}