Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(web): keep AI assistant fully visible

tianma-if07a3da44d75c
diff --git a/apps/web/src/components/dialogs/AiAssistantDialog.tsx b/apps/web/src/components/dialogs/AiAssistantDialog.tsx
index c9a7d186..00c60a30 100644
--- a/apps/web/src/components/dialogs/AiAssistantDialog.tsx
+++ b/apps/web/src/components/dialogs/AiAssistantDialog.tsx
@@ -56,7 +56,11 @@ import {
type TargetLanguage,
} from "@/lib/ai-assistant";
import { copyTextToClipboard } from "@/lib/clipboard";
-import { clampFloatingPanelPosition, type FloatingPanelPosition } from "@/lib/floating-panel";
+import {
+ clampFloatingPanelPosition,
+ resolveAnchoredFloatingPanelLayout,
+ type FloatingPanelPosition,
+} from "@/lib/floating-panel";
import { cn } from "@/lib/utils";
const FREEFORM_VALUE = "custom";
@@ -542,17 +546,12 @@ export const AiAssistantDialog = ({
};
}
const panelWidth = Math.min(576, Math.max(0, viewportWidth - AI_ASSISTANT_VIEWPORT_GAP * 2));
- const left = Math.max(
+ return resolveAnchoredFloatingPanelLayout(
+ anchor,
+ panelWidth,
+ { height: viewportHeight, width: viewportWidth },
AI_ASSISTANT_VIEWPORT_GAP,
- Math.min(anchor.left, viewportWidth - panelWidth - AI_ASSISTANT_VIEWPORT_GAP),
);
- const availableHeight = anchor.placement === "above"
- ? anchor.top - 12
- : viewportHeight - anchor.top - 12;
- const maxHeight = Math.max(180, Math.min(viewportHeight * 0.7, availableHeight));
- return anchor.placement === "above"
- ? { bottom: Math.max(12, viewportHeight - anchor.top), left, maxHeight }
- : { left, maxHeight, top: Math.max(12, anchor.top) };
}, [anchor, draggedPosition, viewportSize]);
return (
diff --git a/apps/web/src/lib/floating-panel.test.mjs b/apps/web/src/lib/floating-panel.test.mjs
index 37453983..e9229b09 100644
--- a/apps/web/src/lib/floating-panel.test.mjs
+++ b/apps/web/src/lib/floating-panel.test.mjs
@@ -1,5 +1,46 @@
import { describe, expect, test } from "bun:test";
-import { clampFloatingPanelPosition } from "./floating-panel.ts";
+import {
+ clampFloatingPanelPosition,
+ resolveAnchoredFloatingPanelLayout,
+} from "./floating-panel.ts";
+
+describe("resolveAnchoredFloatingPanelLayout", () => {
+ test("shifts a below-anchor panel upward instead of shortening it", () => {
+ expect(resolveAnchoredFloatingPanelLayout(
+ { left: 160, placement: "below", top: 360 },
+ 576,
+ { width: 1440, height: 900 },
+ )).toEqual({
+ left: 160,
+ maxHeight: 630,
+ top: 258,
+ });
+ });
+
+ test("keeps an above-anchor panel aligned when it has enough room", () => {
+ expect(resolveAnchoredFloatingPanelLayout(
+ { left: 160, placement: "above", top: 720 },
+ 576,
+ { width: 1440, height: 900 },
+ )).toEqual({
+ bottom: 180,
+ left: 160,
+ maxHeight: 630,
+ });
+ });
+
+ test("keeps the preferred height inside short and narrow viewports", () => {
+ expect(resolveAnchoredFloatingPanelLayout(
+ { left: 900, placement: "below", top: 300 },
+ 576,
+ { width: 800, height: 500 },
+ )).toEqual({
+ left: 212,
+ maxHeight: 350,
+ top: 138,
+ });
+ });
+});
describe("clampFloatingPanelPosition", () => {
test("keeps an in-bounds position unchanged", () => {
diff --git a/apps/web/src/lib/floating-panel.ts b/apps/web/src/lib/floating-panel.ts
index 9fcea959..dd2ffacc 100644
--- a/apps/web/src/lib/floating-panel.ts
+++ b/apps/web/src/lib/floating-panel.ts
@@ -3,6 +3,49 @@ export type FloatingPanelPosition = {
top: number;
};
+export type FloatingPanelAnchor = FloatingPanelPosition & {
+ placement: "above" | "below";
+};
+
+export type AnchoredFloatingPanelLayout = {
+ bottom?: number;
+ left: number;
+ maxHeight: number;
+ top?: number;
+};
+
+export const resolveAnchoredFloatingPanelLayout = (
+ anchor: FloatingPanelAnchor,
+ panelWidth: number,
+ viewportSize: { height: number; width: number },
+ edgeGap = 12,
+ maxHeightRatio = 0.7,
+): AnchoredFloatingPanelLayout => {
+ const availableHeight = Math.max(0, viewportSize.height - edgeGap * 2);
+ const maxHeight = Math.min(viewportSize.height * maxHeightRatio, availableHeight);
+ const left = Math.max(
+ edgeGap,
+ Math.min(anchor.left, viewportSize.width - panelWidth - edgeGap),
+ );
+
+ if (anchor.placement === "above") {
+ const desiredBottom = viewportSize.height - anchor.top;
+ const largestBottom = Math.max(edgeGap, viewportSize.height - maxHeight - edgeGap);
+ return {
+ bottom: Math.max(edgeGap, Math.min(desiredBottom, largestBottom)),
+ left,
+ maxHeight,
+ };
+ }
+
+ const largestTop = Math.max(edgeGap, viewportSize.height - maxHeight - edgeGap);
+ return {
+ left,
+ maxHeight,
+ top: Math.max(edgeGap, Math.min(anchor.top, largestTop)),
+ };
+};
+
export const clampFloatingPanelPosition = (
position: FloatingPanelPosition,
panelSize: { height: number; width: number },