import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:flutter_test/flutter_test.dart'; import '../util/util.dart'; void main() async { group('allSatisfyInSelection - node', () { const welcome = 'Welcome '; const toAppFlowy = 'to AppFlowy'; const editor = ' Editor 🔥!'; // Welcome |to AppFlowy Editor 🔥! test('the selection is collapsed', () async { final document = Document.blank().addParagraph( builder: (index) => Delta() ..insert(welcome) ..insert( toAppFlowy, attributes: { 'bold': true, }, ) ..insert(editor), ); final editorState = EditorState(document: document); // Welcome |to AppFlowy| Editor 🔥! final selection = Selection.collapsed( Position(path: [0], offset: welcome.length), ); editorState.selection = selection; final node = editorState.getNodeAtPath([0]); final result = node!.allSatisfyInSelection(selection, (delta) { final textInserts = delta.whereType(); return textInserts .every((element) => element.attributes?['bold'] == true); }); expect(result, false); }); // Welcome |to AppFlowy| Editor 🔥! test('the selection is single and not collapsed - 1', () async { final document = Document.blank().addParagraph( builder: (index) => Delta() ..insert(welcome) ..insert( toAppFlowy, attributes: { 'bold': true, }, ) ..insert(editor), ); final editorState = EditorState(document: document); // Welcome |to AppFlowy| Editor 🔥! final selection = Selection.single( path: [0], startOffset: welcome.length, endOffset: welcome.length + toAppFlowy.length, ); editorState.selection = selection; final node = editorState.getNodeAtPath([0]); final result = node!.allSatisfyInSelection(selection, (delta) { final textInserts = delta.whereType(); return textInserts .every((element) => element.attributes?['bold'] == true); }); expect(result, true); }); // |Welcome to AppFlowy| Editor 🔥! test('the selection is single and not collapsed - 2', () async { final document = Document.blank().addParagraph( builder: (index) => Delta() ..insert(welcome) ..insert( toAppFlowy, attributes: { 'bold': true, }, ) ..insert(editor), ); final editorState = EditorState(document: document); // Welcome |to AppFlowy| Editor 🔥! final selection = Selection.single( path: [0], startOffset: 0, endOffset: welcome.length + toAppFlowy.length, ); editorState.selection = selection; final node = editorState.getNodeAtPath([0]); final result = node!.allSatisfyInSelection(selection, (delta) { final textInserts = delta.whereType(); return textInserts .every((element) => element.attributes?['bold'] == true); }); expect(result, false); }); }); group('allSatisfyInSelection - nodes', () { const welcome = 'Welcome '; const toAppFlowy = 'to AppFlowy'; const editor = ' Editor 🔥!'; // Welcome |to AppFlowy Editor 🔥! // Welcome to AppFlowy| Editor 🔥! test('the selection is not collapsed and not single - 1', () async { final document = Document.blank().addParagraph( builder: (index) => Delta() ..insert(welcome) ..insert( toAppFlowy + editor, attributes: { 'bold': true, }, ), )..addParagraph( builder: (index) => Delta() ..insert( welcome + toAppFlowy, attributes: { 'bold': true, }, ) ..insert( editor, ), ); final editorState = EditorState(document: document); // Welcome |to AppFlowy Editor 🔥! // Welcome to AppFlowy| Editor 🔥! final selection = Selection( start: Position(path: [0], offset: welcome.length), end: Position(path: [1], offset: welcome.length + toAppFlowy.length), ); editorState.selection = selection; final nodes = editorState.getNodesInSelection(selection); final result = nodes.allSatisfyInSelection(selection, (delta) { final textInserts = delta.whereType(); return textInserts .every((element) => element.attributes?['bold'] == true); }); expect(result, true); }); // |Welcome to AppFlowy Editor 🔥! // Welcome to AppFlowy Editor 🔥!| test('the selection is not collapsed and not single - 2', () async { final document = Document.blank().addParagraph( builder: (index) => Delta() ..insert(welcome) ..insert( toAppFlowy + editor, attributes: { 'bold': true, }, ), )..addParagraph( builder: (index) => Delta() ..insert( welcome + toAppFlowy, attributes: { 'bold': true, }, ) ..insert( editor, ), ); final editorState = EditorState(document: document); // |Welcome to AppFlowy Editor 🔥! // Welcome to AppFlowy Editor 🔥!| final selection = Selection( start: Position(path: [0]), end: Position( path: [1], offset: welcome.length + toAppFlowy.length + editor.length, ), ); editorState.selection = selection; final nodes = editorState.getNodesInSelection(selection); final result = nodes.allSatisfyInSelection(selection, (delta) { final textInserts = delta.whereType(); return textInserts .every((element) => element.attributes?['bold'] == true); }); expect(result, false); }); test('2 non-empty nodes with 1 empty node', () { final document = Document.blank() ..addParagraph( builder: (index) => Delta() ..insert( 'Hello', attributes: {AppFlowyRichTextKeys.bold: true}, ), ) ..addParagraph( builder: (index) => Delta(), ) ..addParagraph( builder: (index) => Delta() ..insert( 'World', attributes: { AppFlowyRichTextKeys.bold: true, }, ), ); final editorState = EditorState(document: document); final selection = Selection( start: Position(path: [0]), end: Position(path: [2], offset: 5), ); final nodes = editorState.getNodesInSelection(selection); final isHighlight = nodes.allSatisfyInSelection( selection, (delta) => delta.isNotEmpty && delta.everyAttributes( (attr) => attr[AppFlowyRichTextKeys.bold] == true, ), ); expect(isHighlight, true); }); }); }