|
1 | 1 | import 'package:flutter/material.dart';
|
| 2 | +import 'package:flutter/services.dart'; |
2 | 3 | import 'package:flutter_markdown/flutter_markdown.dart';
|
| 4 | +import 'package:markdown_editor_plus/src/toolbar.dart'; |
| 5 | +import '../src/constants.dart'; |
3 | 6 | import '../src/emoji_input_formatter.dart';
|
4 | 7 | import 'markdown_toolbar.dart';
|
5 | 8 |
|
@@ -202,87 +205,121 @@ class _SplittedMarkdownFormFieldState extends State<SplittedMarkdownFormField> {
|
202 | 205 | // Internal parameter
|
203 | 206 | late TextEditingController _internalController;
|
204 | 207 | final FocusNode _focusNode = FocusNode();
|
| 208 | + late Toolbar _toolbar; |
205 | 209 |
|
206 | 210 | @override
|
207 | 211 | void initState() {
|
208 | 212 | _internalController = widget.controller ?? TextEditingController();
|
209 | 213 |
|
| 214 | + _toolbar = Toolbar( |
| 215 | + controller: _internalController, |
| 216 | + bringEditorToFocus: () { |
| 217 | + _focusNode.requestFocus(); |
| 218 | + }, |
| 219 | + ); |
| 220 | + |
210 | 221 | super.initState();
|
211 | 222 | }
|
212 | 223 |
|
213 | 224 | @override
|
214 | 225 | Widget build(BuildContext context) {
|
215 |
| - return LayoutBuilder( |
216 |
| - builder: (context, constraints) { |
217 |
| - return Column( |
218 |
| - mainAxisAlignment: MainAxisAlignment.start, |
219 |
| - crossAxisAlignment: CrossAxisAlignment.center, |
220 |
| - mainAxisSize: MainAxisSize.min, |
221 |
| - children: [ |
222 |
| - Row( |
223 |
| - children: [ |
224 |
| - Expanded( |
225 |
| - child: TextFormField( |
226 |
| - readOnly: widget.readOnly, |
227 |
| - controller: _internalController, |
228 |
| - cursorColor: widget.cursorColor, |
229 |
| - focusNode: _focusNode, |
230 |
| - inputFormatters: [ |
231 |
| - if (widget.emojiConvert) EmojiInputFormatter(), |
232 |
| - ], |
233 |
| - onChanged: (value) { |
234 |
| - setState(() {}); |
235 |
| - widget.onChanged?.call(value); |
236 |
| - }, |
237 |
| - onTap: widget.onTap, |
238 |
| - scrollController: widget.scrollController, |
239 |
| - style: widget.style, |
240 |
| - textCapitalization: widget.textCapitalization, |
241 |
| - maxLines: widget.maxLines, |
242 |
| - minLines: widget.minLines, |
243 |
| - expands: widget.expands, |
244 |
| - decoration: widget.decoration, |
245 |
| - validator: widget.validator, |
246 |
| - autovalidateMode: widget.autovalidateMode, |
247 |
| - onSaved: widget.onSaved, |
| 226 | + return FocusableActionDetector( |
| 227 | + shortcuts: { |
| 228 | + LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyB): |
| 229 | + BoldTextIntent(), |
| 230 | + LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyI): |
| 231 | + ItalicTextIntent(), |
| 232 | + }, |
| 233 | + actions: { |
| 234 | + BoldTextIntent: CallbackAction<BoldTextIntent>( |
| 235 | + onInvoke: (intent) { |
| 236 | + _toolbar.action("**", "**"); |
| 237 | + |
| 238 | + // onActionCompleted |
| 239 | + setState(() {}); |
| 240 | + return null; |
| 241 | + }, |
| 242 | + ), |
| 243 | + ItalicTextIntent: CallbackAction<ItalicTextIntent>( |
| 244 | + onInvoke: (intent) { |
| 245 | + _toolbar.action("_", "_"); |
| 246 | + |
| 247 | + // onActionCompleted |
| 248 | + setState(() {}); |
| 249 | + return null; |
| 250 | + }, |
| 251 | + ), |
| 252 | + }, |
| 253 | + child: LayoutBuilder( |
| 254 | + builder: (context, constraints) { |
| 255 | + return Column( |
| 256 | + mainAxisAlignment: MainAxisAlignment.start, |
| 257 | + crossAxisAlignment: CrossAxisAlignment.center, |
| 258 | + mainAxisSize: MainAxisSize.min, |
| 259 | + children: [ |
| 260 | + Row( |
| 261 | + children: [ |
| 262 | + Expanded( |
| 263 | + child: TextFormField( |
| 264 | + readOnly: widget.readOnly, |
| 265 | + controller: _internalController, |
| 266 | + cursorColor: widget.cursorColor, |
| 267 | + focusNode: _focusNode, |
| 268 | + inputFormatters: [ |
| 269 | + if (widget.emojiConvert) EmojiInputFormatter(), |
| 270 | + ], |
| 271 | + onChanged: (value) { |
| 272 | + setState(() {}); |
| 273 | + widget.onChanged?.call(value); |
| 274 | + }, |
| 275 | + onTap: widget.onTap, |
| 276 | + scrollController: widget.scrollController, |
| 277 | + style: widget.style, |
| 278 | + textCapitalization: widget.textCapitalization, |
| 279 | + maxLines: widget.maxLines, |
| 280 | + minLines: widget.minLines, |
| 281 | + expands: widget.expands, |
| 282 | + decoration: widget.decoration, |
| 283 | + validator: widget.validator, |
| 284 | + autovalidateMode: widget.autovalidateMode, |
| 285 | + onSaved: widget.onSaved, |
| 286 | + ), |
248 | 287 | ),
|
249 |
| - ), |
250 |
| - // Some padding |
251 |
| - const SizedBox(width: 8.0), |
252 |
| - Expanded( |
253 |
| - child: MarkdownBody( |
254 |
| - // key: const ValueKey<String>("zmarkdown-parse-body"), |
255 |
| - data: _internalController.text == "" |
256 |
| - ? "_Markdown text_" |
257 |
| - : _internalController.text, |
258 |
| - selectable: true, |
| 288 | + // Some padding |
| 289 | + const SizedBox(width: 8.0), |
| 290 | + Expanded( |
| 291 | + child: MarkdownBody( |
| 292 | + // key: const ValueKey<String>("zmarkdown-parse-body"), |
| 293 | + data: _internalController.text == "" |
| 294 | + ? "_Markdown text_" |
| 295 | + : _internalController.text, |
| 296 | + selectable: true, |
| 297 | + ), |
259 | 298 | ),
|
260 |
| - ), |
261 |
| - ], |
262 |
| - ), |
263 |
| - |
264 |
| - // show toolbar |
265 |
| - if (widget.enableToolBar) |
266 |
| - MarkdownToolbar( |
267 |
| - markdownSyntax: widget.markdownSyntax, |
268 |
| - showPreviewButton: false, |
269 |
| - // key: const ValueKey<String>("zmarkdowntoolbar"), |
270 |
| - controller: _internalController, |
271 |
| - autoCloseAfterSelectEmoji: widget.autoCloseAfterSelectEmoji, |
272 |
| - emojiConvert: widget.emojiConvert, |
273 |
| - toolbarBackground: widget.toolbarBackground, |
274 |
| - expandableBackground: widget.expandableBackground, |
275 |
| - bringEditorToFocus: () { |
276 |
| - _focusNode.requestFocus(); |
277 |
| - }, |
278 |
| - onActionCompleted: () { |
279 |
| - setState(() {}); |
280 |
| - }, |
281 |
| - showEmojiSelection: widget.showEmojiSelection, |
282 |
| - ) |
283 |
| - ], |
284 |
| - ); |
285 |
| - }, |
| 299 | + ], |
| 300 | + ), |
| 301 | + |
| 302 | + // show toolbar |
| 303 | + if (widget.enableToolBar) |
| 304 | + MarkdownToolbar( |
| 305 | + markdownSyntax: widget.markdownSyntax, |
| 306 | + showPreviewButton: false, |
| 307 | + // key: const ValueKey<String>("zmarkdowntoolbar"), |
| 308 | + controller: _internalController, |
| 309 | + autoCloseAfterSelectEmoji: widget.autoCloseAfterSelectEmoji, |
| 310 | + emojiConvert: widget.emojiConvert, |
| 311 | + toolbarBackground: widget.toolbarBackground, |
| 312 | + expandableBackground: widget.expandableBackground, |
| 313 | + toolbar: _toolbar, |
| 314 | + onActionCompleted: () { |
| 315 | + setState(() {}); |
| 316 | + }, |
| 317 | + showEmojiSelection: widget.showEmojiSelection, |
| 318 | + ) |
| 319 | + ], |
| 320 | + ); |
| 321 | + }, |
| 322 | + ), |
286 | 323 | );
|
287 | 324 | }
|
288 | 325 | }
|
0 commit comments