Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(client): dock the attribute popup above the status bar on the old layout
Elian Doran5c36f9f0dca3
diff --git a/apps/client/src/widgets/attribute_widgets/AttributeEditor.tsx b/apps/client/src/widgets/attribute_widgets/AttributeEditor.tsxindex 1042a147f7..a640fc3988 100644--- a/apps/client/src/widgets/attribute_widgets/AttributeEditor.tsx+++ b/apps/client/src/widgets/attribute_widgets/AttributeEditor.tsx@@ -189,7 +189,6 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxIattribute: attrs[attrs.length - 1],isOwned: true,x: rect ? (rect.left + rect.right) / 2 : 0,- y: rect?.bottom ?? 0,focus: "name",parent: wrapperRef.current ?? undefined});@@ -305,7 +304,6 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxIattribute: matchedAttr,isOwned: true,x: e.pageX,- y: e.pageY,parent: wrapperRef.current ?? undefined});} else {diff --git a/apps/client/src/widgets/attribute_widgets/InheritedAttributes.tsx b/apps/client/src/widgets/attribute_widgets/InheritedAttributes.tsxindex c5cdc6a3d6..d87fb50a4f 100644--- a/apps/client/src/widgets/attribute_widgets/InheritedAttributes.tsx+++ b/apps/client/src/widgets/attribute_widgets/InheritedAttributes.tsx@@ -78,7 +78,6 @@ export default function InheritedAttributes({ note, componentId }: InheritedAttr},isOwned: false,x: e.pageX,- y: e.pageY,parent: containerRef.current ?? undefined});}}diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.tsxindex 8182851f98..6cd7fef422 100644--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.tsx+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.tsx@@ -12,14 +12,6 @@ vi.mock("../../services/i18n", async (importOriginal) => ({t: (key: string) => key}));-// The popup docks differently under the new layout, and the flag behind that is read once when the-// module loads — so the two are exercised by loading it twice.-const newLayout = vi.hoisted(() => ({ enabled: false }));-vi.mock("../../services/experimental_features", async (importOriginal) => ({- ...(await importOriginal<typeof import("../../services/experimental_features")>()),- isExperimentalFeatureEnabled: () => newLayout.enabled-}));-// The form fetches the names a definition can complete against and the notes already carrying the// attribute. Neither is what these tests are about, and an unanswered request rejects into the run.vi.mock("../../services/server", () => ({@@ -57,7 +49,7 @@ describe("attribute detail popup positioning", () => {const warn = vi.spyOn(console, "warn").mockImplementation(() => {});const popup = sized(document.createElement("div"), 0, 0);- (await load()).positionPopup(popup, opts({ x: 100, y: 100 }), { top: 0, left: 0 });+ (await import("./attribute_detail")).positionPopup(popup, opts({ x: 100 }));expect(popup.style.left).toBe("");expect(warn).toHaveBeenCalledWith(expect.stringContaining("position popup"));@@ -65,49 +57,29 @@ describe("attribute detail popup positioning", () => {});it("puts an anchored popup on whichever side of its anchor has the room for it", async () => {- const { positionPopup } = await load();+ const { positionPopup } = await import("./attribute_detail");const popup = sized(document.createElement("div"), 300, 400);// Room to the left of the anchor: 300 wide plus the gap and the margin fit before it.- positionPopup(popup, opts({ anchor: anchoredAt({ left: 900, right: 1100, top: 120 }) }), NO_OFFSET);+ positionPopup(popup, opts({ anchor: anchoredAt({ left: 900, right: 1100, top: 120 }) }));expect(popup.style.left).toBe("592px"); // 900 - 300 - 8expect(popup.style.top).toBe("120px");expect(popup.style.right).toBe("");expect(popup.style.maxHeight).toBe("780px"); // the viewport less a margin above and below// No room to the left, so it takes the right — and slides up to stay wholly on screen.- positionPopup(popup, opts({ anchor: anchoredAt({ left: 40, right: 200, top: 700 }) }), NO_OFFSET);+ positionPopup(popup, opts({ anchor: anchoredAt({ left: 40, right: 200, top: 700 }) }));expect(popup.style.left).toBe("208px"); // 200 + 8expect(popup.style.top).toBe("390px"); // 800 - 400 - 10// Narrower than the popup on either side: it keeps to the viewport's left margin.sizeViewport(305, VIEWPORT.height);- positionPopup(popup, opts({ anchor: anchoredAt({ left: 10, right: 300, top: 40 }) }), NO_OFFSET);- expect(popup.style.left).toBe("10px");- });-- it("places an unanchored popup below the click, against whichever edge it falls nearest", async () => {- const { positionPopup } = await load();- const popup = sized(document.createElement("div"), 300, 200);-- // A press far enough left that the popup would run off the edge is held against it.- positionPopup(popup, opts({ x: 100, y: 100 }), { top: 30, left: 0 });+ positionPopup(popup, opts({ anchor: anchoredAt({ left: 10, right: 300, top: 40 }) }));expect(popup.style.left).toBe("10px");- expect(popup.style.right).toBe("");- expect(popup.style.top).toBe("140px"); // 100 - 30 + 70- // Room enough below the click for the whole popup, so it is left uncapped.- expect(popup.style.maxHeight).toBe("10000px");-- // Anywhere else the popup pins to the right edge, and a press low down caps its height.- positionPopup(popup, opts({ x: 600, y: 700 }), NO_OFFSET);- expect(popup.style.left).toBe("");- expect(popup.style.right).toBe("10px");- expect(popup.style.maxHeight).toBe("50px"); // 800 - 700 - 50});- it("sits above the attributes pane under the new layout, and above the status bar without one", async () => {- newLayout.enabled = true;- const { positionPopup } = await load();+ it("sits above the attributes pane, and above the status bar without one", async () => {+ const { positionPopup } = await import("./attribute_detail");const popup = sized(document.createElement("div"), 300, 200);sizeBody(VIEWPORT.height);@@ -116,7 +88,7 @@ describe("attribute detail popup positioning", () => {pane.getBoundingClientRect = (() => ({ top: 600 })) as Element["getBoundingClientRect"];document.body.append(pane);- positionPopup(popup, opts({ x: 500, y: 100 }), NO_OFFSET);+ positionPopup(popup, opts({ x: 500 }));expect(popup.style.top).toBe("unset");expect(popup.style.bottom).toBe("200px"); // the body's height down to the paneexpect(popup.style.maxHeight).toBe("600px");@@ -129,19 +101,11 @@ describe("attribute detail popup positioning", () => {document.body.append(statusBar);// A press near the left edge is held a margin away from it rather than centred.- positionPopup(popup, opts({ x: 100, y: 100 }), NO_OFFSET);+ positionPopup(popup, opts({ x: 100 }));expect(popup.style.bottom).toBe("40px");expect(popup.style.maxHeight).toBe("760px");expect(popup.style.left).toBe("10px");-- newLayout.enabled = false;});-- /** Re-imports the module so the layout flag above is read afresh. */- async function load() {- vi.resetModules();- return await import("./attribute_detail");- }});describe("attribute detail popup naming", () => {@@ -220,7 +184,7 @@ describe("attribute detail popup naming", () => {// The same row pressed again: a fresh opts object around an equal attribute is the same show,// whether or not the attribute object itself survived.- expect(isSameShow(shown, { ...shown, x: 40, y: 90 })).toBe(true);+ expect(isSameShow(shown, { ...shown, x: 40 })).toBe(true);expect(isSameShow(shown, opts({ attribute: { type: "label", name: "author", value: "Tolkien" } }))).toBe(true);// Anything the form would show differently is another show.@@ -454,14 +418,12 @@ describe("dismissing the attribute detail popup", () => {});});-const NO_OFFSET = { top: 0, left: 0 };function opts(overrides: Partial<AttributeDetailOpts>): AttributeDetailOpts {return {attribute: { type: "label", name: "author", value: "" },isOwned: true,x: 0,- y: 0,...overrides};}diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsxindex ecffa2bb1e..e26e92cde5 100644--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx@@ -4,21 +4,19 @@ import "./attribute_kind.css";import { type DefinitionObject, type LabelType, promotedAttributeDefinitionParser } from "@triliumnext/commons";import clsx from "clsx";import { ComponentChildren, ComponentProps } from "preact";-import { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from "preact/hooks";+import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "preact/hooks";import appContext from "../../components/app_context.js";import { DEFINITION_PREFIXES, isDefinitionName } from "../../entities/fattribute.js";import contextMenu, { MenuItem } from "../../menus/context_menu.js";import type { Attribute } from "../../services/attribute_parser.js";import { getBuiltinLabelSelectOptions, getBuiltinLabelValueType, isBuiltinAttribute } from "../../services/attributes.js";-import { isExperimentalFeatureEnabled } from "../../services/experimental_features.js";import { focusSavedElement, saveFocusedElement } from "../../services/focus.js";import froca from "../../services/froca.js";import { t } from "../../services/i18n.js";import server from "../../services/server.js";import { isIMEComposing } from "../../services/shortcuts.js";import utils from "../../services/utils.js";-import BasicWidget from "../basic_widget.js";import NoteContextAwareWidget from "../note_context_aware_widget.js";import { Badge, BadgeWithDropdown } from "../react/Badge.jsx";import Button from "../react/Button.jsx";@@ -31,7 +29,7 @@ import Icon from "../react/Icon.jsx";import { suspendModalFocusTraps } from "../react/modal_focustrap.js";import NoteAutocomplete, { HighlightedText } from "../react/NoteAutocomplete.jsx";import NoteLink, { NewNoteLink } from "../react/NoteLink.jsx";-import { disposeReactWidget, ParentComponent, renderReactWidgetAtElement } from "../react/react_utils.jsx";+import { disposeReactWidget, renderReactWidgetAtElement } from "../react/react_utils.jsx";import OptionsRow, { OptionsRowWithToggle } from "../type_widgets/options/components/OptionsRow.jsx";import { ATTR_HELP, AttrHelpEntry } from "./attr_help.js";import { attributeKindIcon, DEFINITION_TYPE_ICONS, RELATION_DEFINITION_TYPE } from "./attribute_types.js";@@ -43,7 +41,6 @@ export interface AttributeDetailOpts {attribute: Attribute;isOwned: boolean;x: number;- y: number;focus?: "name";/*** The element the popup was spawned from. Mouse presses inside it do not dismiss the@@ -180,7 +177,6 @@ export interface AttributeDetailProps extends AttributeFormCallbacks {*/export function AttributeDetail({ opts, currentNoteId, onDismiss, onCancel, ...formCallbacks }: AttributeDetailProps) {const popupRef = useRef<HTMLDivElement>(null);- const parentComponent = useContext(ParentComponent);const shown = !!opts;const { onSaveAndClose } = formCallbacks;@@ -222,11 +218,7 @@ export function AttributeDetail({ opts, currentNoteId, onDismiss, onCancel, ...freturn;}- // Classic-layout coordinates are relative to the hosting widget, mirroring how the- // legacy widget resolved its own parent in the component tree.- const hostWidget = parentComponent instanceof BasicWidget ? parentComponent : null;- const reposition = () =>- positionPopup(popup, opts, hostWidget?.$widget?.offset() ?? { top: 0, left: 0 });+ const reposition = () => positionPopup(popup, opts);reposition();// The placement holds only for the size it measured, and editing changes that size — a@@ -236,7 +228,7 @@ export function AttributeDetail({ opts, currentNoteId, onDismiss, onCancel, ...fconst resizeObserver = new ResizeObserver(reposition);resizeObserver.observe(popup);return () => resizeObserver.disconnect();- }, [ opts, parentComponent ]);+ }, [ opts ]);// Dismiss on click outside the popup, except in floating UI logically belonging// to it (autocomplete dropdowns and context menus are appended to the body).@@ -1104,9 +1096,7 @@ const ATTR_TITLES: Record<string, string> = {"relation-definition": t("attribute_detail.definition")};-const isNewLayout = isExperimentalFeatureEnabled("new-layout");--export function positionPopup(popup: HTMLElement, { x, y, anchor }: AttributeDetailOpts, parentOffset: { top: number; left: number }) {+export function positionPopup(popup: HTMLElement, { x, anchor }: AttributeDetailOpts) {const outerWidth = popup.offsetWidth;const outerHeight = popup.offsetHeight;const windowHeight = document.documentElement.clientHeight;@@ -1118,38 +1108,26 @@ export function positionPopup(popup: HTMLElement, { x, y, anchor }: AttributeDetif (anchor) {positionPopupBesideAnchor(popup, anchor);- } else if (isNewLayout) {- // The popup always sits above the note attributes pane so it never covers it;- // when the pane is closed (e.g. opened from the collection column editor),- // it docks to the status bar instead.- const attrPane = document.querySelector(".bottom-panel.attribute-list");- const paneShown = attrPane instanceof HTMLElement && !attrPane.classList.contains("hidden-ext");- const anchorTop = paneShown- ? attrPane.getBoundingClientRect().top- : document.body.clientHeight - (document.querySelector<HTMLElement>(".component.status-bar")?.offsetHeight ?? 0);-- // Centered on the click, clamped to the viewport. Deliberately not using- // getDetailPosition(): its legacy right-pin quirk reads as a plain 0 here,- // which kept the popup flush left regardless of the click position.- const windowWidth = document.documentElement.clientWidth;- const left = Math.max(Math.min(x - outerWidth / 2, windowWidth - outerWidth - 10), 10);-- popup.style.left = `${left}px`;- popup.style.right = "";- popup.style.top = "unset";- popup.style.bottom = `${document.body.clientHeight - anchorTop}px`;- popup.style.maxHeight = `${anchorTop}px`;- } else {- const detPosition = getDetailPosition(x, parentOffset.left, outerWidth);-- popup.style.left = toCssPos(detPosition.left);- popup.style.right = toCssPos(detPosition.right);- popup.style.top = `${y - parentOffset.top + 70}px`;- popup.style.bottom = "";- // `>=` so that re-placing a popup already at the cap keeps the cap: with `>` it would- // un-cap, grow, and be capped again by the resize-driven re-placement, ping-ponging forever.- popup.style.maxHeight = outerHeight + y >= windowHeight - 50 ? `${windowHeight - y - 50}px` : "10000px";+ return;}++ // The popup sits above the note attributes pane so it never covers it; when the pane is closed+ // (e.g. opened from the collection column editor), it docks to the status bar instead.+ const attrPane = document.querySelector(".bottom-panel.attribute-list");+ const paneShown = attrPane instanceof HTMLElement && !attrPane.classList.contains("hidden-ext");+ const anchorTop = paneShown+ ? attrPane.getBoundingClientRect().top+ : document.body.clientHeight - (document.querySelector<HTMLElement>(".component.status-bar")?.offsetHeight ?? 0);++ // Centered on the click, clamped to the viewport.+ const windowWidth = document.documentElement.clientWidth;+ const left = Math.max(Math.min(x - outerWidth / 2, windowWidth - outerWidth - 10), 10);++ popup.style.left = `${left}px`;+ popup.style.right = "";+ popup.style.top = "unset";+ popup.style.bottom = `${document.body.clientHeight - anchorTop}px`;+ popup.style.maxHeight = `${anchorTop}px`;}/** How far the popup stays from its anchor, and from the edges of the viewport. */@@ -1181,32 +1159,6 @@ function positionPopupBesideAnchor(popup: HTMLElement, anchor: HTMLElement) {popup.style.bottom = "";}-function getDetailPosition(x: number, offsetLeft: number, outerWidth: number) {- let left: number | string = x - offsetLeft - outerWidth / 2;- let right: number | string = "";-- if (left < 0) {- left = 10;- } else {- const rightEdge = left + outerWidth;-- // Kept bug-for-bug from the legacy widget: this compares against the popup's own- // width instead of the viewport's, so it holds whenever left >= 0 and the popup- // effectively pins to the right edge except for far-left clicks.- if (rightEdge > outerWidth - 10) {- left = "";- right = 10;- }- }-- return { left, right };-}--/** An empty string clears the property, matching the jQuery `.css()` behavior. */-function toCssPos(value: number | string) {- return typeof value === "number" ? `${value}px` : value;-}-export type AttrType = "label" | "label-definition" | "relation" | "relation-definition" | undefined;export function getAttrType(attribute: Attribute): AttrType {diff --git a/apps/client/src/widgets/collections/board/group_by.tsx b/apps/client/src/widgets/collections/board/group_by.tsxindex c20eeda1b0..076a28c7dc 100644--- a/apps/client/src/widgets/collections/board/group_by.tsx+++ b/apps/client/src/widgets/collections/board/group_by.tsx@@ -62,7 +62,6 @@ export default function BoardGroupBy({ note, options, current, onSelect }: {allAttributes: [ definition ],isOwned: true,x: event.pageX,- y: event.pageY,focus: "name",// The board answers for all four: a grouping is a promoted, inheritable select whose// options are the columns the board makes, and a card stands in one column at a time.diff --git a/apps/client/src/widgets/collections/table/col_editing.ts b/apps/client/src/widgets/collections/table/col_editing.tsindex 301167459a..dbbd106894 100644--- a/apps/client/src/widgets/collections/table/col_editing.ts+++ b/apps/client/src/widgets/collections/table/col_editing.ts@@ -55,7 +55,6 @@ export default function useColTableEditing(api: RefObject<Tabulator | null>, attallAttributes: [ attr ],isOwned: true,x: 0,- y: 150,focus: "name",hideMultiplicity: true});diff --git a/apps/client/src/widgets/react/PromotedAttributesCard.tsx b/apps/client/src/widgets/react/PromotedAttributesCard.tsxindex 3ea55a412a..531e33460e 100644--- a/apps/client/src/widgets/react/PromotedAttributesCard.tsx+++ b/apps/client/src/widgets/react/PromotedAttributesCard.tsx@@ -153,7 +153,6 @@ export default function PromotedAttributesCard({isOwned: true,// Where the press was, or below the top of the page for one made from the keyboard.x: event?.pageX ?? 0,- y: event?.pageY ?? 150,focus: attribute ? undefined : "name",hideMultiplicity: true,// Every attribute listed here is inheritable and promoted already, so the two togglesdiff --git a/apps/client/src/widgets/sidebar/AttributeList.tsx b/apps/client/src/widgets/sidebar/AttributeList.tsxindex 7b147a1912..ab0ae3cc46 100644--- a/apps/client/src/widgets/sidebar/AttributeList.tsx+++ b/apps/client/src/widgets/sidebar/AttributeList.tsx@@ -149,7 +149,6 @@ export default function AttributeList() {attribute,isOwned,x: e.pageX,- y: e.pageY,anchor: anchor ?? undefined,// Presses on another row swap the shown attribute instead of dismissing the popup first.parent: spawningArea()@@ -191,7 +190,6 @@ export default function AttributeList() {attribute,isOwned: true,x: e.pageX,- y: e.pageY,focus: "name",// There is no row to anchor to yet: the attribute only joins the list once it is saved.anchor: containerRef.current ?? undefined,