forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmisc.ts
More file actions
56 lines (51 loc) · 1.6 KB
/
misc.ts
File metadata and controls
56 lines (51 loc) · 1.6 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
48
49
50
51
52
53
54
55
56
import Viewport from '../builtin-simulator/viewport';
import { ISimulatorHost } from '../simulator';
export function isElementNode(domNode: Element) {
return domNode.nodeType === Node.ELEMENT_NODE;
}
/**
* 判断节点是否在 viewport 内,判断依据:只要节点有一部分在 viewport 内,都算 true,其余情况 false
* @param domNode 待检测的节点
* @param viewport 画布 viewport
* @returns 是否在 viewport 内
*/
export function isDOMNodeVisible(domNode: Element, viewport: Viewport) {
const domNodeRect = domNode.getBoundingClientRect();
const { width, height } = viewport.contentBounds;
const { left, right, top, bottom, width: nodeWidth, height: nodeHeight } = domNodeRect;
return (
left >= -nodeWidth &&
top >= -nodeHeight &&
bottom <= height + nodeHeight &&
right <= width + nodeWidth
);
}
/**
* normalize triggers
* @param triggers
*/
export function normalizeTriggers(triggers: string[]) {
return triggers.map((trigger: string) => trigger?.toUpperCase());
}
/**
* make a handler that listen all sensors:document, avoid frame lost
*/
export function makeEventsHandler(
boostEvent: MouseEvent | DragEvent,
sensors: ISimulatorHost[],
): (fn: (sdoc: Document) => void) => void {
const topDoc = window.document;
const sourceDoc = boostEvent.view?.document || topDoc;
const docs = new Set<Document>();
docs.add(topDoc);
docs.add(sourceDoc);
sensors.forEach((sim) => {
const sdoc = sim.contentDocument;
if (sdoc) {
docs.add(sdoc);
}
});
return (handle: (sdoc: Document) => void) => {
docs.forEach((doc) => handle(doc));
};
}