Initial RuneStone project
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { isImagePath } from "../workspace/media";
|
||||
|
||||
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 (!isImagePath(path)) 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;
|
||||
}
|
||||
Reference in New Issue
Block a user