Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: toolbar position at most top #214

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/src/editor/toolbar/desktop/floating_toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,18 @@ class _FloatingToolbarState extends State<FloatingToolbar>
final right = (rect.right - editorCenter.dx).abs();
final width = editorSize.width;
final threshold = width / 3.0;
final top = rect.top < floatingToolbarHeight
? rect.bottom + floatingToolbarHeight
: rect.top;
if (rect.left >= threshold && rect.right <= threshold * 2.0) {
// show in center
return (rect.top, threshold, null);
return (top, threshold, null);
} else if (left >= right) {
// show in left
return (rect.top, rect.left, null);
return (top, rect.left, null);
} else {
// show in right
return (rect.top, null, editorRect.right - rect.right);
return (top, null, editorRect.right - rect.right);
}
}
}
26 changes: 25 additions & 1 deletion test/new/infra/testable_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,40 @@ class TestableEditor {
bool autoFocus = false,
bool editable = true,
bool shrinkWrap = false,
bool withFloatingToolbar = false,
ScrollController? scrollController,
Widget Function(Widget child)? wrapper,
}) async {
final editor = AppFlowyEditor.standard(
await AppFlowyEditorLocalizations.load(locale);

if (withFloatingToolbar) {
scrollController ??= ScrollController();
}
Widget editor = AppFlowyEditor.standard(
editorState: editorState,
editable: editable,
autoFocus: autoFocus,
shrinkWrap: shrinkWrap,
scrollController: scrollController,
);
if (withFloatingToolbar) {
editor = FloatingToolbar(
items: [
paragraphItem,
...headingItems,
...markdownFormatItems,
quoteItem,
bulletedListItem,
numberedListItem,
linkItem,
textColorItem,
highlightColorItem
],
editorState: editorState,
scrollController: scrollController!,
child: editor,
);
}
await tester.pumpWidget(
MaterialApp(
localizationsDelegates: const [
Expand Down
36 changes: 36 additions & 0 deletions test/new/toolbar/desktop/floating_toolbar_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

import '../../util/util.dart';
import '../../infra/testable_editor.dart';

void main() async {
group('floating toolbar', () {
const text = 'Welcome to AppFlowy Editor 🔥!';

testWidgets(
'select the first line of the document, the toolbar should not be blocked',
(tester) async {
final editor = tester.editor..addParagraphs(3, initialText: text);
await editor.startTesting(
withFloatingToolbar: true,
);

final selection = Selection.single(
path: [0],
startOffset: 0,
endOffset: text.length,
);
await editor.updateSelection(selection);
await tester.pumpAndSettle();

final floatingToolbar = find.byType(FloatingToolbarWidget);
expect(floatingToolbar, findsOneWidget);
expect(tester.getTopLeft(floatingToolbar).dy >= 0, true);
await editor.dispose();
});
});
}