forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocation.ts
More file actions
184 lines (156 loc) · 4.24 KB
/
location.ts
File metadata and controls
184 lines (156 loc) · 4.24 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { DocumentModel, Node as ComponentNode, ParentalNode } from '../document';
import { LocateEvent } from './dragon';
export interface LocationData {
target: ParentalNode; // shadowNode | ConditionFlow | ElementNode | RootNode
detail: LocationDetail;
source: string;
event: LocateEvent;
}
export enum LocationDetailType {
Children = 'Children',
Prop = 'Prop',
}
export interface LocationChildrenDetail {
type: LocationDetailType.Children;
index?: number | null;
/**
* 是否有效位置
*/
valid?: boolean;
edge?: DOMRect;
near?: {
node: ComponentNode;
pos: 'before' | 'after' | 'replace';
rect?: Rect;
align?: 'V' | 'H';
};
focus?: { type: 'slots' } | { type: 'node'; node: ParentalNode };
}
export interface LocationPropDetail {
// cover 形态,高亮 domNode,如果 domNode 为空,取 container 的值
type: LocationDetailType.Prop;
name: string;
domNode?: HTMLElement;
}
export type LocationDetail = LocationChildrenDetail | LocationPropDetail | { type: string; [key: string]: any };
export interface Point {
clientX: number;
clientY: number;
}
export interface CanvasPoint {
canvasX: number;
canvasY: number;
}
export type Rects = DOMRect[] & {
elements: Array<Element | Text>;
};
export type Rect = DOMRect & {
elements: Array<Element | Text>;
computed?: boolean;
};
export function isLocationData(obj: any): obj is LocationData {
return obj && obj.target && obj.detail;
}
export function isLocationChildrenDetail(obj: any): obj is LocationChildrenDetail {
return obj && obj.type === LocationDetailType.Children;
}
export function isRowContainer(container: Element | Text, win?: Window) {
if (isText(container)) {
return true;
}
const style = (win || getWindow(container)).getComputedStyle(container);
const display = style.getPropertyValue('display');
if (/flex$/.test(display)) {
const direction = style.getPropertyValue('flex-direction') || 'row';
if (direction === 'row' || direction === 'row-reverse') {
return true;
}
}
if (/grid$/.test(display)) {
return true;
}
return false;
}
export function isChildInline(child: Element | Text, win?: Window) {
if (isText(child)) {
return true;
}
const style = (win || getWindow(child)).getComputedStyle(child);
return /^inline/.test(style.getPropertyValue('display')) || /^(left|right)$/.test(style.getPropertyValue('float'));
}
export function getRectTarget(rect: Rect | null) {
if (!rect || rect.computed) {
return null;
}
const els = rect.elements;
return els && els.length > 0 ? els[0]! : null;
}
export function isVerticalContainer(rect: Rect | null) {
const el = getRectTarget(rect);
if (!el) {
return false;
}
return isRowContainer(el);
}
export function isVertical(rect: Rect | null) {
const el = getRectTarget(rect);
if (!el) {
return false;
}
return isChildInline(el) || (el.parentElement ? isRowContainer(el.parentElement) : false);
}
function isText(elem: any): elem is Text {
return elem.nodeType === Node.TEXT_NODE;
}
function isDocument(elem: any): elem is Document {
return elem.nodeType === Node.DOCUMENT_NODE;
}
export function getWindow(elem: Element | Document): Window {
return (isDocument(elem) ? elem : elem.ownerDocument!).defaultView!;
}
export class DropLocation {
readonly target: ParentalNode;
readonly detail: LocationDetail;
readonly event: LocateEvent;
readonly source: string;
get document(): DocumentModel {
return this.target.document;
}
constructor({ target, detail, source, event }: LocationData) {
this.target = target;
this.detail = detail;
this.source = source;
this.event = event;
}
clone(event: LocateEvent): DropLocation {
return new DropLocation({
target: this.target,
detail: this.detail,
source: this.source,
event,
});
}
/**
* @deprecated
* 兼容 vision
*/
getContainer() {
return this.target;
}
/**
* @deprecated
* 兼容 vision
*/
getInsertion() {
if (!this.detail) {
return null;
}
if (this.detail.type === 'Children') {
if (this.detail.index <= 0) {
return null;
}
return this.target.children.get(this.detail.index - 1);
}
return (this.detail as any)?.near?.node;
}
}