Skip to content

Commit 49574d3

Browse files
committed
fix: [Bug] Text formatting toolbar disappear under layout #1542
1 parent c64b83c commit 49574d3

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

frontend/app_flowy/packages/appflowy_editor/lib/src/render/toolbar/toolbar_widget.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ class ToolbarWidget extends StatefulWidget {
1616
required this.layerLink,
1717
required this.offset,
1818
required this.items,
19+
this.aligment = Alignment.topLeft,
1920
}) : super(key: key);
2021

2122
final EditorState editorState;
2223
final LayerLink layerLink;
2324
final Offset offset;
2425
final List<ToolbarItem> items;
26+
final Alignment aligment;
2527

2628
@override
2729
State<ToolbarWidget> createState() => _ToolbarWidgetState();
@@ -39,6 +41,7 @@ class _ToolbarWidgetState extends State<ToolbarWidget> with ToolbarMixin {
3941
link: widget.layerLink,
4042
showWhenUnlinked: true,
4143
offset: widget.offset,
44+
followerAnchor: widget.aligment,
4245
child: _buildToolbar(context),
4346
),
4447
);

frontend/app_flowy/packages/appflowy_editor/lib/src/service/selection_service.dart

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,11 @@ class _AppFlowySelectionState extends State<AppFlowySelection>
388388

389389
// TODO: need to be refactored.
390390
Offset? toolbarOffset;
391+
Alignment? alignment;
391392
LayerLink? layerLink;
392393
final editorOffset =
393394
editorState.renderBox?.localToGlobal(Offset.zero) ?? Offset.zero;
395+
final editorSize = editorState.renderBox?.size ?? Size.zero;
394396

395397
final backwardNodes =
396398
selection.isBackward ? nodes : nodes.reversed.toList(growable: false);
@@ -438,10 +440,33 @@ class _AppFlowySelectionState extends State<AppFlowySelection>
438440
// TODO: Need to compute more precise location.
439441
if ((selectionRect.topLeft.dy - editorOffset.dy) <=
440442
baseToolbarOffset.dy) {
441-
toolbarOffset ??= rect.bottomLeft;
443+
if (selectionRect.topLeft.dx <=
444+
editorSize.width / 3.0 + editorOffset.dx) {
445+
toolbarOffset ??= rect.bottomLeft;
446+
alignment ??= Alignment.topLeft;
447+
} else if (selectionRect.topRight.dx >=
448+
editorSize.width * 2.0 / 3.0 + editorOffset.dx) {
449+
toolbarOffset ??= rect.bottomRight;
450+
alignment ??= Alignment.topRight;
451+
} else {
452+
toolbarOffset ??= rect.bottomCenter;
453+
alignment ??= Alignment.topCenter;
454+
}
442455
} else {
443-
toolbarOffset ??= rect.topLeft - baseToolbarOffset;
456+
if (selectionRect.topLeft.dx <=
457+
editorSize.width / 3.0 + editorOffset.dx) {
458+
toolbarOffset ??= rect.topLeft - baseToolbarOffset;
459+
alignment ??= Alignment.topLeft;
460+
} else if (selectionRect.topRight.dx >=
461+
editorSize.width * 2.0 / 3.0 + editorOffset.dx) {
462+
toolbarOffset ??= rect.topRight - baseToolbarOffset;
463+
alignment ??= Alignment.topRight;
464+
} else {
465+
toolbarOffset ??= rect.topCenter - baseToolbarOffset;
466+
alignment ??= Alignment.topCenter;
467+
}
444468
}
469+
445470
layerLink ??= node.layerLink;
446471

447472
final overlay = OverlayEntry(
@@ -460,6 +485,7 @@ class _AppFlowySelectionState extends State<AppFlowySelection>
460485
if (toolbarOffset != null && layerLink != null) {
461486
editorState.service.toolbarService?.showInOffset(
462487
toolbarOffset,
488+
alignment!,
463489
layerLink,
464490
);
465491
}

frontend/app_flowy/packages/appflowy_editor/lib/src/service/toolbar_service.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:appflowy_editor/src/extensions/object_extensions.dart';
88

99
abstract class AppFlowyToolbarService {
1010
/// Show the toolbar widget beside the offset.
11-
void showInOffset(Offset offset, LayerLink layerLink);
11+
void showInOffset(Offset offset, Alignment alignment, LayerLink layerLink);
1212

1313
/// Hide the toolbar widget.
1414
void hide();
@@ -37,7 +37,7 @@ class _FlowyToolbarState extends State<FlowyToolbar>
3737
final _toolbarWidgetKey = GlobalKey(debugLabel: '_toolbar_widget');
3838

3939
@override
40-
void showInOffset(Offset offset, LayerLink layerLink) {
40+
void showInOffset(Offset offset, Alignment alignment, LayerLink layerLink) {
4141
hide();
4242
final items = _filterItems(defaultToolbarItems);
4343
if (items.isEmpty) {
@@ -50,6 +50,7 @@ class _FlowyToolbarState extends State<FlowyToolbar>
5050
layerLink: layerLink,
5151
offset: offset,
5252
items: items,
53+
aligment: alignment,
5354
),
5455
);
5556
Overlay.of(context)?.insert(_toolbarOverlay!);

0 commit comments

Comments
 (0)