|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:super_editor/src/core/document.dart'; |
| 3 | +import 'package:super_editor/src/default_editor/layout_single_column/layout_single_column.dart'; |
| 4 | +import 'package:super_editor/src/default_editor/tasks.dart'; |
| 5 | +import 'package:super_editor/src/default_editor/text_tools.dart'; |
| 6 | + |
| 7 | +/// Builds [TaskComponentViewModel]s and [TaskComponent]s for every |
| 8 | +/// [TaskNode] in a document. |
| 9 | +/// |
| 10 | +/// A [TaskComponent] built by this builder is read-only, meaning that |
| 11 | +/// the user cannot toggle it. |
| 12 | +class ReadOnlyTaskComponentBuilder implements ComponentBuilder { |
| 13 | + const ReadOnlyTaskComponentBuilder(); |
| 14 | + |
| 15 | + @override |
| 16 | + TaskComponentViewModel? createViewModel(Document document, DocumentNode node) { |
| 17 | + if (node is! TaskNode) { |
| 18 | + return null; |
| 19 | + } |
| 20 | + |
| 21 | + final textDirection = getParagraphDirection(node.text.toPlainText()); |
| 22 | + |
| 23 | + return TaskComponentViewModel( |
| 24 | + nodeId: node.id, |
| 25 | + padding: EdgeInsets.zero, |
| 26 | + indent: node.indent, |
| 27 | + isComplete: node.isComplete, |
| 28 | + setComplete: null, |
| 29 | + text: node.text, |
| 30 | + textDirection: textDirection, |
| 31 | + textAlignment: textDirection == TextDirection.ltr ? TextAlign.left : TextAlign.right, |
| 32 | + textStyleBuilder: noStyleBuilder, |
| 33 | + selectionColor: const Color(0x00000000), |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + @override |
| 38 | + Widget? createComponent( |
| 39 | + SingleColumnDocumentComponentContext componentContext, SingleColumnLayoutComponentViewModel componentViewModel) { |
| 40 | + if (componentViewModel is! TaskComponentViewModel) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + return TaskComponent( |
| 45 | + key: componentContext.componentKey, |
| 46 | + viewModel: componentViewModel, |
| 47 | + ); |
| 48 | + } |
| 49 | +} |
0 commit comments