From e7adc3bcbfdfa0ff0a6b0f8c717f8f0288e7dc51 Mon Sep 17 00:00:00 2001
From: "Lucas.Xu" <lucas.xu@appflowy.io>
Date: Wed, 26 Oct 2022 14:36:00 +0800
Subject: [PATCH] fix: selection menu show in wrong position

---
 .../selection_menu_service.dart               | 22 +++++++++++++------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/selection_menu/selection_menu_service.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/selection_menu/selection_menu_service.dart
index 1f47ad823b..76e12a5f62 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/selection_menu/selection_menu_service.dart
+++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/selection_menu/selection_menu_service.dart
@@ -61,19 +61,27 @@ class SelectionMenu implements SelectionMenuService {
     //  Just subtract the padding here as a result.
     const menuHeight = 200.0;
     const menuOffset = Offset(10, 10);
-    final baseOffset =
+    final editorOffset =
         editorState.renderBox?.localToGlobal(Offset.zero) ?? Offset.zero;
-    var offset = selectionRects.first.bottomRight + menuOffset;
-    if (offset.dy >=
-        baseOffset.dy + editorState.renderBox!.size.height - menuHeight) {
-      offset = selectionRects.first.topRight - menuOffset;
-      offset = offset.translate(0, -menuHeight);
+    final editorHeight = editorState.renderBox!.size.height;
+
+    // show below defualt
+    var showBelow = true;
+    final bottomRight = selectionRects.first.bottomRight;
+    final topRight = selectionRects.first.topRight;
+    var offset = bottomRight + menuOffset;
+    // overflow
+    if (offset.dy + menuHeight >= editorOffset.dy + editorHeight) {
+      // show above
+      offset = topRight - menuOffset;
+      showBelow = false;
     }
     _topLeft = offset;
 
     _selectionMenuEntry = OverlayEntry(builder: (context) {
       return Positioned(
-        top: offset.dy,
+        top: showBelow ? offset.dy : null,
+        bottom: showBelow ? null : editorHeight - offset.dy,
         left: offset.dx,
         child: SelectionMenuWidget(
           items: [