-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support customizing error block (#524)
* feat: support customzing error block * feat: support slash menu in toggle list * fix: enter in heading block
- Loading branch information
Showing
6 changed files
with
177 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() async { | ||
await AppFlowyEditorLocalizations.load( | ||
const Locale.fromSubtags(languageCode: 'en'), | ||
); | ||
|
||
testWidgets('custom error block', (tester) async { | ||
final editorState = EditorState.blank(withInitialText: false); | ||
editorState.document.insert( | ||
[0], | ||
[ | ||
Node( | ||
type: 'not_exist', | ||
attributes: { | ||
'text': 'line 1', | ||
}, | ||
), | ||
Node( | ||
type: 'heading', | ||
attributes: {}, | ||
), | ||
], | ||
); | ||
final widget = ErrorEditor( | ||
editorState: editorState, | ||
); | ||
await tester.pumpWidget(widget); | ||
await tester.pumpAndSettle(); | ||
|
||
expect(find.byType(ErrorBlockComponentWidget), findsNWidgets(2)); | ||
}); | ||
} | ||
|
||
class ErrorEditor extends StatelessWidget { | ||
const ErrorEditor({ | ||
super.key, | ||
required this.editorState, | ||
}); | ||
|
||
final EditorState editorState; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
home: Scaffold( | ||
body: SafeArea( | ||
child: Column( | ||
crossAxisAlignment: CrossAxisAlignment.center, | ||
children: [ | ||
Expanded( | ||
child: Container( | ||
width: 1000, | ||
decoration: BoxDecoration( | ||
border: Border.all(color: Colors.blue), | ||
), | ||
child: AppFlowyEditor( | ||
editorState: editorState, | ||
blockComponentBuilders: { | ||
...standardBlockComponentBuilderMap, | ||
errorBlockComponentBuilderKey: | ||
ErrorBlockComponentBuilder(), | ||
}, | ||
), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
class ErrorBlockComponentBuilder extends BlockComponentBuilder { | ||
ErrorBlockComponentBuilder({ | ||
super.configuration, | ||
}); | ||
|
||
@override | ||
BlockComponentWidget build(BlockComponentContext blockComponentContext) { | ||
final node = blockComponentContext.node; | ||
return ErrorBlockComponentWidget( | ||
key: node.key, | ||
node: node, | ||
configuration: configuration, | ||
showActions: showActions(node), | ||
actionBuilder: (context, state) => actionBuilder( | ||
blockComponentContext, | ||
state, | ||
), | ||
); | ||
} | ||
|
||
@override | ||
bool validate(Node node) => true; | ||
} | ||
|
||
class ErrorBlockComponentWidget extends BlockComponentStatefulWidget { | ||
const ErrorBlockComponentWidget({ | ||
super.key, | ||
required super.node, | ||
super.showActions, | ||
super.actionBuilder, | ||
super.configuration = const BlockComponentConfiguration(), | ||
}); | ||
|
||
@override | ||
State<ErrorBlockComponentWidget> createState() => | ||
_DividerBlockComponentWidgetState(); | ||
} | ||
|
||
class _DividerBlockComponentWidgetState extends State<ErrorBlockComponentWidget> | ||
with BlockComponentConfigurable { | ||
@override | ||
BlockComponentConfiguration get configuration => widget.configuration; | ||
|
||
@override | ||
Node get node => widget.node; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container( | ||
color: Colors.red, | ||
child: const Text('error'), | ||
); | ||
} | ||
} |