From c269aa3c1df59bd9a86d676ad64150f677c3d1bc Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Wed, 27 Sep 2023 11:31:25 +0300
Subject: [PATCH] fix

---
 .../OverlayService/OverlayComponent.js              |  4 ++--
 .../src/AiService/components/AskAIOverlay.js        | 13 +++++++++----
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/wax-prosemirror-core/src/config/defaultServices/OverlayService/OverlayComponent.js b/wax-prosemirror-core/src/config/defaultServices/OverlayService/OverlayComponent.js
index d32429730..5a5f1cf0b 100644
--- a/wax-prosemirror-core/src/config/defaultServices/OverlayService/OverlayComponent.js
+++ b/wax-prosemirror-core/src/config/defaultServices/OverlayService/OverlayComponent.js
@@ -7,6 +7,7 @@ import usePosition from './usePosition';
 export default (Component, markType) => props => {
   const context = useContext(WaxContext);
   const [position, setPosition, mark] = usePosition(markType);
+
   const component = useMemo(
     () => (
       <Component
@@ -18,8 +19,7 @@ export default (Component, markType) => props => {
     ),
     [JSON.stringify(mark), position, context.activeViewId],
   );
-  const visible = !!position.left;
-
+  const visible = position.left !== null;
   return (
     <Overlay position={position}>
       {props.activeViewId === context.activeViewId && visible && component}
diff --git a/wax-prosemirror-services/src/AiService/components/AskAIOverlay.js b/wax-prosemirror-services/src/AiService/components/AskAIOverlay.js
index 1d17d47a9..e33fa0a72 100644
--- a/wax-prosemirror-services/src/AiService/components/AskAIOverlay.js
+++ b/wax-prosemirror-services/src/AiService/components/AskAIOverlay.js
@@ -111,17 +111,22 @@ const AskAIOverlay = ({ setPosition, position, config }) => {
   useLayoutEffect(() => {
     const WaxSurface = activeView.dom.getBoundingClientRect();
     const { selection } = activeView.state;
-    const { to } = selection;
+    const { from, to } = selection;
+    const start = activeView.coordsAtPos(from);
     const end = activeView.coordsAtPos(to - 1);
     const overLayComponent = document.getElementById('ai-overlay');
 
     const overLayComponentCoords = overLayComponent.getBoundingClientRect();
     const top = end.top - WaxSurface.top + 20;
-    let left = end.left - WaxSurface.left;
+    let left = end.left - WaxSurface.left - overLayComponentCoords.width / 2;
+
+    if (end.left - overLayComponentCoords.width / 2 < WaxSurface.left) {
+      left += WaxSurface.left - (end.left - overLayComponentCoords.width / 2);
+    }
 
     // Don't get out of right boundary of the surface
-    if (end.left + overLayComponentCoords.width > WaxSurface.right) {
-      left -= end.left + overLayComponentCoords.width - WaxSurface.right;
+    if (end.left + overLayComponentCoords.width / 2 > WaxSurface.right) {
+      left -= end.left + overLayComponentCoords.width / 2 - WaxSurface.right;
     }
 
     setPosition({ ...position, left, top });
-- 
GitLab