forked from frappe/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrackTarget.ts
More file actions
47 lines (41 loc) · 1.34 KB
/
trackTarget.ts
File metadata and controls
47 lines (41 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { useElementBounding, useMutationObserver } from "@vueuse/core";
import { nextTick, reactive, watch, watchEffect } from "vue";
import { addPxToNumber } from "./helpers";
declare global {
interface Window {
observer: any;
}
}
window.observer = null;
const updateList: (() => void)[] = [];
function trackTarget(target: HTMLElement | SVGElement, host: HTMLElement, canvasProps: CanvasProps) {
const targetBounds = reactive(useElementBounding(target));
const container = target.closest(".canvas-container");
// TODO: too much? find a better way to track changes
updateList.push(targetBounds.update);
watch(canvasProps, () => nextTick(targetBounds.update), { deep: true });
if (!window.observer) {
let callback = () => {
nextTick(() => {
updateList.forEach((fn) => {
fn();
});
});
};
window.observer = useMutationObserver(container as HTMLElement, callback, {
attributes: true,
childList: true,
subtree: true,
attributeFilter: ["style", "class"],
characterData: true,
});
}
watchEffect(() => {
host.style.width = addPxToNumber(targetBounds.width, false);
host.style.height = addPxToNumber(targetBounds.height, false);
host.style.top = addPxToNumber(targetBounds.top, false);
host.style.left = addPxToNumber(targetBounds.left, false);
});
return targetBounds.update;
}
export default trackTarget;