25 lines
1.1 KiB
TypeScript
25 lines
1.1 KiB
TypeScript
type Point = { x: number; y: number };
|
|
const targets = new Map<HTMLElement, (path: string, point: Point) => void>();
|
|
let highlighted: HTMLElement | undefined;
|
|
export function registerImageDropTarget(element: HTMLElement, drop: (path: string, point: Point) => void) {
|
|
targets.set(element, drop);
|
|
return () => { targets.delete(element); element.classList.remove("image-drop-target"); };
|
|
}
|
|
function targetAt(path: string, point: Point) {
|
|
if (!path || path.endsWith("/")) return;
|
|
const hit = document.elementFromPoint(point.x, point.y);
|
|
return [...targets].find(([element]) => element.isConnected && hit && element.contains(hit));
|
|
}
|
|
export function highlightImageDrop(path?: string, point?: Point): boolean {
|
|
highlighted?.classList.remove("image-drop-target");
|
|
highlighted = path && point ? targetAt(path, point)?.[0] : undefined;
|
|
highlighted?.classList.add("image-drop-target");
|
|
return !!highlighted;
|
|
}
|
|
export function dropWorkspaceImage(path: string, point: Point): boolean {
|
|
const target = targetAt(path, point);
|
|
highlightImageDrop();
|
|
target?.[1](path, point);
|
|
return !!target;
|
|
}
|