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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
import { action, observable, untracked } from 'mobx';
import { CollectionFreeFormView } from '.';
import { intersectRect } from '../../../../Utils';
import { Doc, Opt } from '../../../../fields/Doc';
import { NumCast, StrCast } from '../../../../fields/Types';
import { DocumentType } from '../../../documents/DocumentTypes';
import { DragManager } from '../../../util/DragManager';
import { dropActionType } from '../../../util/DropActionTypes';
import { StyleProp } from '../../StyleProp';
import { CollectionFreeFormDocumentView } from '../../nodes/CollectionFreeFormDocumentView';
import { DocumentView } from '../../nodes/DocumentView';
import { FieldViewProps } from '../../nodes/FieldView';
import './CollectionFreeFormView.scss';
export class CollectionFreeFormClusters {
private _view: CollectionFreeFormView;
private _clusterDistance: number = 75;
private _hitCluster: number = -1;
@observable _clusterSets: Doc[][] = [];
constructor(view: CollectionFreeFormView) {
this._view = view;
}
get Document() { return this._view.Document; } // prettier-ignore
get DocumentView() { return this._view.DocumentView?.(); } // prettier-ignore
get childDocs() { return this._view.childDocs; } // prettier-ignore
get childLayoutPairs() { return this._view.childLayoutPairs; } // prettier-ignore
get screenToContentsXf() { return this._view.screenToFreeformContentsXf; } // prettier-ignore
get viewStyleProvider() { return this._view._props.styleProvider; } // prettier-ignore
get viewMoveDocument() { return this._view._props.moveDocument; } // prettier-ignore
get selectDocuments() { return this._view.selectDocuments; } // prettier-ignore
static overlapping(doc1: Doc, doc2: Doc, clusterDistance: number) {
const x2 = NumCast(doc2.x) - clusterDistance;
const y2 = NumCast(doc2.y) - clusterDistance;
const w2 = NumCast(doc2._width) + clusterDistance;
const h2 = NumCast(doc2._height) + clusterDistance;
const x = NumCast(doc1.x) - clusterDistance;
const y = NumCast(doc1.y) - clusterDistance;
const w = NumCast(doc1._width) + clusterDistance;
const h = NumCast(doc1._height) + clusterDistance;
return doc1.z === doc2.z && intersectRect({ left: x, top: y, width: w, height: h }, { left: x2, top: y2, width: w2, height: h2 });
}
handlePointerDown(probe: number[]) {
this._hitCluster = this.childLayoutPairs
.map(pair => pair.layout)
.reduce((cluster, cd) => {
const grouping = this.Document._freeform_useClusters ? NumCast(cd.layout_cluster, -1) : NumCast(cd.group, -1);
if (grouping !== -1) {
const cx = NumCast(cd.x) - this._clusterDistance / 2;
const cy = NumCast(cd.y) - this._clusterDistance / 2;
const cw = NumCast(cd._width) + this._clusterDistance;
const ch = NumCast(cd._height) + this._clusterDistance;
return !cd.z && intersectRect({ left: cx, top: cy, width: cw, height: ch }, { left: probe[0], top: probe[1], width: 1, height: 1 }) ? grouping : cluster;
}
return cluster;
}, -1);
return this._hitCluster;
}
tryToDrag(e: PointerEvent) {
const cluster = this._hitCluster;
if (cluster !== -1) {
const ptsParent = e;
if (ptsParent) {
const eles = this.childLayoutPairs.map(pair => pair.layout).filter(cd => (this.Document._freeform_useClusters ? NumCast(cd.layout_cluster) : NumCast(cd.group, -1)) === cluster);
const clusterDocs = eles.map(ele => DocumentView.getDocumentView(ele, this.DocumentView)!);
const { left, top } = clusterDocs[0].getBounds || { left: 0, top: 0 };
const de = new DragManager.DocumentDragData(eles, e.ctrlKey || e.altKey ? dropActionType.embed : undefined);
de.moveDocument = this.viewMoveDocument;
de.offset = this.screenToContentsXf.transformDirection(ptsParent.clientX - left, ptsParent.clientY - top);
DragManager.StartDocumentDrag(
clusterDocs.map(v => v.ContentDiv!),
de,
ptsParent.clientX,
ptsParent.clientY,
{ hideSource: !de.dropAction }
);
return true;
}
}
return false;
}
initLayout() {
if (this.Document._freeform_useClusters && !this._clusterSets.length && this.childDocs.length) {
return this.updateClusters(true);
}
return false;
}
@action
updateClusters(useClusters: boolean) {
this.Document._freeform_useClusters = useClusters;
this._clusterSets.length = 0;
this.childLayoutPairs.map(pair => pair.layout).map(c => this.addDocument(c));
}
@action
addDocuments(docs: Doc[]) {
const childLayouts = this.childLayoutPairs.map(pair => pair.layout);
if (this.Document._freeform_useClusters) {
const docFirst = docs[0];
docs.forEach(doc => this._clusterSets.map(set => Doc.IndexOf(doc, set) !== -1 && set.splice(Doc.IndexOf(doc, set), 1)));
const preferredInd = NumCast(docFirst.layout_cluster);
docs.forEach(doc => {
doc.layout_cluster = -1;
});
docs.map(doc =>
this._clusterSets.map((set, i) =>
set.forEach(member => {
if (docFirst.layout_cluster === -1 && Doc.IndexOf(member, childLayouts) !== -1 && CollectionFreeFormClusters.overlapping(doc, member, this._clusterDistance)) {
docFirst.layout_cluster = i;
}
})
)
);
if (
docFirst.layout_cluster === -1 &&
preferredInd !== -1 &&
this._clusterSets.length > preferredInd &&
(!this._clusterSets[preferredInd] || !this._clusterSets[preferredInd].filter(member => Doc.IndexOf(member, childLayouts) !== -1).length)
) {
docFirst.layout_cluster = preferredInd;
}
this._clusterSets.forEach((set, i) => {
if (docFirst.layout_cluster === -1 && !set.filter(member => Doc.IndexOf(member, childLayouts) !== -1).length) {
docFirst.layout_cluster = i;
}
});
if (docFirst.layout_cluster === -1) {
docs.forEach(doc => {
doc.layout_cluster = this._clusterSets.length;
this._clusterSets.push([doc]);
});
} else if (this._clusterSets.length) {
for (let i = this._clusterSets.length; i <= NumCast(docFirst.layout_cluster); i++) !this._clusterSets[i] && this._clusterSets.push([]);
docs.forEach(doc => {
this._clusterSets[(doc.layout_cluster = NumCast(docFirst.layout_cluster))].push(doc);
});
}
childLayouts.map(child => !this._clusterSets.some((set, i) => Doc.IndexOf(child, set) !== -1 && child.layout_cluster === i) && this.addDocument(child));
}
}
@action
addDocument = (doc: Doc) => {
const childLayouts = this.childLayoutPairs.map(pair => pair.layout);
if (this.Document._freeform_useClusters) {
this._clusterSets.forEach(set => Doc.IndexOf(doc, set) !== -1 && set.splice(Doc.IndexOf(doc, set), 1));
const preferredInd = NumCast(doc.layout_cluster);
doc.layout_cluster = -1;
this._clusterSets.forEach((set, i) =>
set.forEach(member => {
if (doc.layout_cluster === -1 && Doc.IndexOf(member, childLayouts) !== -1 && CollectionFreeFormClusters.overlapping(doc, member, this._clusterDistance)) {
doc.layout_cluster = i;
}
})
);
if (doc.layout_cluster === -1 && preferredInd !== -1 && this._clusterSets.length > preferredInd && (!this._clusterSets[preferredInd] || !this._clusterSets[preferredInd].filter(member => Doc.IndexOf(member, childLayouts) !== -1).length)) {
doc.layout_cluster = preferredInd;
}
this._clusterSets.forEach((set, i) => {
if (doc.layout_cluster === -1 && !set.filter(member => Doc.IndexOf(member, childLayouts) !== -1).length) {
doc.layout_cluster = i;
}
});
if (doc.layout_cluster === -1) {
doc.layout_cluster = this._clusterSets.length;
this._clusterSets.push([doc]);
} else if (this._clusterSets.length) {
for (let i = this._clusterSets.length; i <= doc.layout_cluster; i++) !this._clusterSets[i] && this._clusterSets.push([]);
this._clusterSets[doc.layout_cluster ?? 0].push(doc);
}
}
};
styleProvider = (doc: Opt<Doc>, props: Opt<FieldViewProps>, property: string) => {
// without untracked, every inquired style property for any Doc will be invalidated if a change is made to the collection's childDocs.
// this prevents that by assuming that a Doc is generally always (or never) a member of childDocs - if it's removed or added, then all of its properties get updated anyway.
if (doc && untracked(() => this.childDocs)?.includes(doc))
switch (property.split(':')[0]) {
case StyleProp.BackgroundColor:
{
const cluster = NumCast(doc?.layout_cluster);
if (this.Document._freeform_useClusters && doc?.type !== DocumentType.IMG && !doc.layout_isSvg) {
if (this._clusterSets.length <= cluster) {
setTimeout(() => doc && this.addDocument(doc));
} else {
const palette = ['#da42429e', '#31ea318c', 'rgba(197, 87, 20, 0.55)', '#4a7ae2c4', 'rgba(216, 9, 255, 0.5)', '#ff7601', '#1dffff', 'yellow', 'rgba(27, 130, 49, 0.55)', 'rgba(0, 0, 0, 0.268)'];
// override palette cluster color with an explicitly set cluster doc color ONLY if doc color matches the current default text color
return this._clusterSets[cluster]?.reduce((b, s) => (s.backgroundColor !== Doc.UserDoc().textBackgroundColor ? StrCast(s.backgroundColor, b) : b), palette[cluster % palette.length]);
}
}
}
break;
case StyleProp.FillColor:
if (doc && this.Document._currentFrame !== undefined) {
return CollectionFreeFormDocumentView.getStringValues(doc, NumCast(this.Document._currentFrame))?.fillColor;
}
break;
default:
}
return this.viewStyleProvider?.(doc, props, property); // bcz: check 'props' used to be renderDepth + 1
};
tryToSelect = (addToSel: boolean) => {
if (addToSel && this._hitCluster !== -1) {
!addToSel && DocumentView.DeselectAll();
const eles = this.childLayoutPairs.map(pair => pair.layout).filter(cd => (this.Document._freeform_useClusters ? NumCast(cd.layout_cluster) : NumCast(cd.group, -1)) === this._hitCluster);
this.selectDocuments(eles);
return true;
}
return false;
};
}
|