feat(plugins): add settings shortcut entry and drag edge avoidance
This commit is contained in:
1 parent
fd6b7bd502
commit
e444c15e0c
32 files changed
+483
-34
No files matched your search
@@ -0,0 +1,67 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { createApp, h, nextTick, reactive, ref } from "vue";
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { useCursorFloatingPosition } from "../useCursorFloatingPosition";
|
||||
|
||||
let app: ReturnType<typeof createApp>;
|
||||
let host: HTMLDivElement;
|
||||
afterEach(() => {
|
||||
app?.unmount();
|
||||
host?.remove();
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("remeasures content and window changes, limits long previews, and cleans up observers", async () => {
|
||||
vi.stubGlobal("innerWidth", 300);
|
||||
vi.stubGlobal("innerHeight", 200);
|
||||
let resized!: () => void;
|
||||
const disconnect = vi.fn();
|
||||
vi.stubGlobal(
|
||||
"ResizeObserver",
|
||||
class {
|
||||
constructor(callback: () => void) {
|
||||
resized = callback;
|
||||
}
|
||||
observe() {}
|
||||
disconnect = disconnect;
|
||||
},
|
||||
);
|
||||
const removeListener = vi.spyOn(window, "removeEventListener");
|
||||
let contentWidth = 180;
|
||||
vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockImplementation(function (this: HTMLElement) {
|
||||
const width = Math.min(contentWidth, parseFloat(this.style.maxWidth.split(",")[1]));
|
||||
return { width, height: 32 } as DOMRect;
|
||||
});
|
||||
const cursor = reactive({ x: 290, y: 190 });
|
||||
const visible = ref(true);
|
||||
app = createApp({
|
||||
setup() {
|
||||
const preview = ref<HTMLElement | null>(null);
|
||||
const style = useCursorFloatingPosition(preview, cursor);
|
||||
return () => (visible.value ? h("div", { ref: preview, style: style.value }) : null);
|
||||
},
|
||||
});
|
||||
host = document.createElement("div");
|
||||
document.body.append(host);
|
||||
app.mount(host);
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
const preview = host.firstElementChild as HTMLElement;
|
||||
expect(preview.style.left).toBe("98px");
|
||||
expect(preview.style.top).toBe("146px");
|
||||
contentWidth = 1000;
|
||||
resized();
|
||||
await nextTick();
|
||||
expect(preview.style.left).toBe("8px");
|
||||
expect(preview.style.maxWidth).toBe("min(20rem, 284px)");
|
||||
vi.stubGlobal("innerWidth", 160);
|
||||
window.dispatchEvent(new Event("resize"));
|
||||
await nextTick();
|
||||
expect(preview.style.maxWidth).toBe("min(20rem, 144px)");
|
||||
expect(preview.style.left).toBe("8px");
|
||||
visible.value = false;
|
||||
await nextTick();
|
||||
expect(disconnect).toHaveBeenCalledOnce();
|
||||
expect(removeListener).toHaveBeenCalledWith("resize", expect.any(Function));
|
||||
});
|
||||
@@ -76,6 +76,16 @@ async function setup() {
|
||||
};
|
||||
}
|
||||
describe("shortcut sidebar height", () => {
|
||||
it("reserves the title row as well as 120px for the connection tree", async () => {
|
||||
const { sizing, savedHeight, setTotal } = await setup();
|
||||
const header = document.createElement("div");
|
||||
header.setAttribute("data-shortcut-header", "");
|
||||
Object.defineProperty(header, "offsetHeight", { get: () => 32 });
|
||||
root.querySelector("nav")!.append(header);
|
||||
savedHeight.value = 500;
|
||||
setTotal(300);
|
||||
expect(sizing.height.value).toBe(142);
|
||||
});
|
||||
it("does not attach a resize observer after disposal during a pending measurement", async () => {
|
||||
const observe = vi.fn();
|
||||
vi.stubGlobal(
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { ref, watch, type CSSProperties, type Ref } from "vue";
|
||||
import { floatingCursorPosition } from "@/lib/common/floatingViewportPosition";
|
||||
|
||||
/** Measure after mounting and whenever the preview or viewport changes. Coordinates are CSS pixels. */
|
||||
export function useCursorFloatingPosition(element: Ref<HTMLElement | null>, cursor: { x: number; y: number }, preferredSide: () => "left" | "right" = () => "right") {
|
||||
const style = ref<CSSProperties>({ visibility: "hidden" });
|
||||
function update() {
|
||||
const preview = element.value;
|
||||
if (!preview) return;
|
||||
const width = window.innerWidth;
|
||||
const height = window.innerHeight;
|
||||
// Apply constraints before measuring so truncation and narrow windows use the actual rendered size.
|
||||
preview.style.maxWidth = `min(20rem, ${Math.max(0, width - 16)}px)`;
|
||||
preview.style.maxHeight = `${Math.max(0, height - 16)}px`;
|
||||
const position = floatingCursorPosition(cursor, preview.getBoundingClientRect(), { width, height }, preferredSide());
|
||||
style.value = { left: `${position.x}px`, top: `${position.y}px`, maxWidth: preview.style.maxWidth, maxHeight: preview.style.maxHeight };
|
||||
}
|
||||
watch([element, () => cursor.x, () => cursor.y, preferredSide], update, { flush: "post" });
|
||||
watch(
|
||||
element,
|
||||
(preview, _, onCleanup) => {
|
||||
if (!preview) {
|
||||
style.value = { visibility: "hidden" };
|
||||
return;
|
||||
}
|
||||
const observer = new ResizeObserver(update);
|
||||
observer.observe(preview);
|
||||
window.addEventListener("resize", update);
|
||||
onCleanup(() => {
|
||||
observer.disconnect();
|
||||
window.removeEventListener("resize", update);
|
||||
});
|
||||
},
|
||||
{ flush: "post" },
|
||||
);
|
||||
return style;
|
||||
}
|
||||
@@ -24,8 +24,9 @@ export function usePluginShortcutHeight(options: { section: Ref<HTMLElement | nu
|
||||
if (!section || !scroll || !options.sidebarList.value) return;
|
||||
const tree = section.previousElementSibling as HTMLElement | null;
|
||||
const divider = section.querySelector<HTMLElement>("[data-shortcut-resize]");
|
||||
available.value = (tree?.offsetHeight ?? 0) + section.offsetHeight - (divider?.offsetHeight ?? 0);
|
||||
const item = scroll.querySelector<HTMLElement>("[data-shortcut-id]");
|
||||
const header = section.querySelector<HTMLElement>("[data-shortcut-header]");
|
||||
available.value = (tree?.offsetHeight ?? 0) + section.offsetHeight - (divider?.offsetHeight ?? 0) - (header?.offsetHeight ?? 0);
|
||||
const item = scroll.querySelector<HTMLElement>("[data-shortcut-id], [data-plugin-shortcut-settings]");
|
||||
const grid = scroll.firstElementChild;
|
||||
itemSize.value = item?.offsetHeight || 28;
|
||||
gap.value = grid ? parseFloat(getComputedStyle(grid).gap) || 0 : 0;
|
||||
|
||||
Reference in new issue
Block a user