mirror of
https://github.com/open-webui/open-webui.git
synced 2026-12-05 11:07:55 -05:00
34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
import { autoUpdate, computePosition, flip, offset, shift } from '@floating-ui/dom';
|
|
|
|
// Range geometry follows scrolling; Floating UI also accounts for the on-screen keyboard.
|
|
export function positionSelectionToolbar(node: HTMLElement, range: Range | null) {
|
|
let cleanup = () => {};
|
|
const update = (range: Range | null) => {
|
|
cleanup();
|
|
cleanup = () => {};
|
|
if (!range) return;
|
|
let active = true;
|
|
const reference = {
|
|
getBoundingClientRect: () => range.getBoundingClientRect(),
|
|
contextElement: range.commonAncestorContainer.parentElement ?? undefined
|
|
};
|
|
const stop = autoUpdate(reference, node, () => {
|
|
void computePosition(reference, node, {
|
|
strategy: 'fixed',
|
|
placement: 'bottom-start',
|
|
middleware: [offset(12), flip(), shift({ padding: 8 })]
|
|
}).then(({ x, y }) => {
|
|
if (!active) return;
|
|
node.style.left = `${x}px`;
|
|
node.style.top = `${y}px`;
|
|
});
|
|
});
|
|
cleanup = () => {
|
|
active = false;
|
|
stop();
|
|
};
|
|
};
|
|
update(range);
|
|
return { update, destroy: () => cleanup() };
|
|
}
|