-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy patheditorjs_toolbar.dart
More file actions
240 lines (230 loc) · 7.96 KB
/
editorjs_toolbar.dart
File metadata and controls
240 lines (230 loc) · 7.96 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
239
240
import 'package:flutter/material.dart';
import '../../domain/entities/blocks/checklist_block.dart';
import '../../domain/entities/blocks/code_block.dart';
import '../../domain/entities/blocks/delimiter_block.dart';
import '../../domain/entities/blocks/header_block.dart';
import '../../domain/entities/blocks/image_block.dart';
import '../../domain/entities/blocks/list_block.dart';
import '../../domain/entities/blocks/paragraph_block.dart';
import '../../domain/entities/blocks/quote_block.dart';
import '../../domain/entities/blocks/table_block.dart';
import '../../domain/entities/blocks/warning_block.dart';
import '../controller/editor_controller.dart';
import '../registry/block_renderer_registry.dart';
/// Toolbar for [EditorJSEditor].
///
/// Communicates exclusively via [EditorController] — holds no reference to
/// any parent widget. Each action appends a new [BlockEntity] to the controller.
class EditorJSToolbar extends StatelessWidget {
final EditorController controller;
final BlockRendererRegistry rendererRegistry;
const EditorJSToolbar({
super.key,
required this.controller,
required this.rendererRegistry,
});
@override
Widget build(BuildContext context) {
return SafeArea(
child: Container(
decoration: BoxDecoration(
border: Border(top: BorderSide(color: Colors.grey.shade300)),
color: Theme.of(context).colorScheme.surface,
),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
// --- Text blocks ---
_ToolbarButton(
label: 'T',
tooltip: 'Paragraph',
onTap: () => controller.addBlock(const ParagraphBlock(html: '')),
),
for (int level = 1; level <= 6; level++)
_ToolbarButton(
label: 'H$level',
tooltip: 'Heading $level',
onTap: () =>
controller.addBlock(HeaderBlock(text: '', level: level)),
),
_ToolbarButton(
icon: Icons.format_quote,
tooltip: 'Quote',
onTap: () =>
controller.addBlock(const QuoteBlock(text: '')),
),
_ToolbarButton(
icon: Icons.code,
tooltip: 'Code',
onTap: () => controller.addBlock(const CodeBlock(code: '')),
),
// --- Lists ---
_ToolbarButton(
icon: Icons.format_list_bulleted,
tooltip: 'Unordered list',
onTap: () => controller.addBlock(
const ListBlock(
style: ListStyle.unordered,
items: [ListItem(content: '')],
),
),
),
_ToolbarButton(
icon: Icons.format_list_numbered,
tooltip: 'Ordered list',
onTap: () => controller.addBlock(
const ListBlock(
style: ListStyle.ordered,
items: [ListItem(content: '')],
),
),
),
_ToolbarButton(
icon: Icons.checklist,
tooltip: 'Checklist',
onTap: () => controller.addBlock(
const ChecklistBlock(
items: [ChecklistItem(text: '')],
),
),
),
// --- Structural ---
_ToolbarButton(
icon: Icons.table_chart_outlined,
tooltip: 'Table',
onTap: () => controller.addBlock(
const TableBlock(
content: [
['', ''],
['', ''],
],
),
),
),
_ToolbarButton(
icon: Icons.horizontal_rule,
tooltip: 'Delimiter',
onTap: () => controller.addBlock(const DelimiterBlock()),
),
_ToolbarButton(
icon: Icons.warning_amber_outlined,
tooltip: 'Warning',
onTap: () =>
controller.addBlock(const WarningBlock(title: '', message: '')),
),
// --- Media ---
_ToolbarButton(
icon: Icons.image_outlined,
tooltip: 'Image',
onTap: () => controller.addBlock(const ImageBlock(url: '')),
),
_ToolbarButton(
icon: Icons.link,
tooltip: 'Hyperlink',
onTap: () => _showHyperlinkDialog(context),
),
// --- Actions ---
_ToolbarButton(
icon: Icons.undo,
tooltip: 'Undo',
onTap: controller.canUndo ? controller.undo : null,
),
_ToolbarButton(
icon: Icons.redo,
tooltip: 'Redo',
onTap: controller.canRedo ? controller.redo : null,
),
_ToolbarButton(
icon: Icons.delete_outline,
tooltip: 'Remove last block',
onTap: controller.blockCount > 0
? () => controller.removeBlock(controller.blockCount - 1)
: null,
),
],
),
),
),
);
}
void _showHyperlinkDialog(BuildContext context) {
final titleController = TextEditingController();
final urlController = TextEditingController();
showDialog<void>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('Add hyperlink'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: titleController,
decoration: const InputDecoration(hintText: 'Link title'),
),
const SizedBox(height: 8),
TextField(
controller: urlController,
decoration: const InputDecoration(hintText: 'https://...'),
keyboardType: TextInputType.url,
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx),
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
final url = urlController.text.trim();
final title = titleController.text.trim();
if (url.isNotEmpty) {
controller.addBlock(ParagraphBlock(
html: '<a href="$url">${title.isNotEmpty ? title : url}</a>',
));
}
Navigator.pop(ctx);
},
child: const Text('Add'),
),
],
),
);
}
}
class _ToolbarButton extends StatelessWidget {
final String? label;
final IconData? icon;
final String tooltip;
final VoidCallback? onTap;
const _ToolbarButton({
this.label,
this.icon,
required this.tooltip,
required this.onTap,
}) : assert(label != null || icon != null);
@override
Widget build(BuildContext context) {
final child = icon != null
? Icon(icon, size: 20, color: Colors.black54)
: Text(
label!,
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.black54,
),
);
return Tooltip(
message: tooltip,
child: InkWell(
onTap: onTap,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
child: child,
),
),
);
}
}