|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/widgets.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | +import 'package:flutter_test_runners/flutter_test_runners.dart'; |
| 5 | +import 'package:super_editor/super_editor.dart'; |
| 6 | +import 'package:super_editor/super_editor_test.dart'; |
| 7 | + |
| 8 | +import 'supereditor_test_tools.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + group("SuperEditor > ", () { |
| 12 | + testWidgetsOnAllPlatforms("inserts placeholder at the end of the attributed text", (tester) async { |
| 13 | + final context = await tester // |
| 14 | + .createDocument() |
| 15 | + .withCustomContent(MutableDocument(nodes: [ |
| 16 | + ParagraphNode( |
| 17 | + id: "1", |
| 18 | + text: AttributedText( |
| 19 | + "Hello", |
| 20 | + AttributedSpans( |
| 21 | + attributions: const [ |
| 22 | + SpanMarker( |
| 23 | + attribution: NamedAttribution("bold"), |
| 24 | + offset: 0, |
| 25 | + markerType: SpanMarkerType.start, |
| 26 | + ), |
| 27 | + SpanMarker( |
| 28 | + attribution: NamedAttribution("bold"), |
| 29 | + offset: 4, |
| 30 | + markerType: SpanMarkerType.end, |
| 31 | + ), |
| 32 | + ], |
| 33 | + ), |
| 34 | + )) |
| 35 | + ])) |
| 36 | + .withInputSource(TextInputSource.ime) |
| 37 | + .useStylesheet(defaultStylesheet.copyWith( |
| 38 | + inlineWidgetBuilders: [inlineWidgetBuilder], |
| 39 | + )) |
| 40 | + .pump(); |
| 41 | + |
| 42 | + Document doc = SuperEditorInspector.findDocument()!; |
| 43 | + final Editor editor = context.editor; |
| 44 | + |
| 45 | + // Place the caret at "Hello|". |
| 46 | + await tester.placeCaretInParagraph(doc.first.id, 5); |
| 47 | + |
| 48 | + await tester.pump(); |
| 49 | + |
| 50 | + // Insert the placeholder at the current caret offset. |
| 51 | + editor.execute([const InsertInlinePlaceholderAtCaretRequest(_ExamplePlaceholder())]); |
| 52 | + |
| 53 | + await tester.pump(); |
| 54 | + |
| 55 | + // Ensure the placeholder is inserted at expected offset while maintaining the text attributions. |
| 56 | + expect( |
| 57 | + (doc.getNodeById("1") as ParagraphNode).text, |
| 58 | + AttributedText( |
| 59 | + "Hello", |
| 60 | + AttributedSpans( |
| 61 | + attributions: const [ |
| 62 | + SpanMarker( |
| 63 | + attribution: NamedAttribution("bold"), |
| 64 | + offset: 0, |
| 65 | + markerType: SpanMarkerType.start, |
| 66 | + ), |
| 67 | + SpanMarker( |
| 68 | + attribution: NamedAttribution("bold"), |
| 69 | + offset: 4, |
| 70 | + markerType: SpanMarkerType.end, |
| 71 | + ), |
| 72 | + ], |
| 73 | + ), |
| 74 | + { |
| 75 | + 5: const _ExamplePlaceholder(), |
| 76 | + }), |
| 77 | + ); |
| 78 | + }); |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +Widget? inlineWidgetBuilder(BuildContext context, TextStyle textStyle, Object placeholder) { |
| 83 | + if (placeholder is! _ExamplePlaceholder) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + return LineHeight( |
| 88 | + style: textStyle, |
| 89 | + child: Container( |
| 90 | + color: Colors.red, |
| 91 | + padding: const EdgeInsets.symmetric(horizontal: 8), |
| 92 | + child: Text( |
| 93 | + 'placeholder', |
| 94 | + style: textStyle, |
| 95 | + ))); |
| 96 | +} |
| 97 | + |
| 98 | +class _ExamplePlaceholder { |
| 99 | + const _ExamplePlaceholder(); |
| 100 | +} |
0 commit comments