This repository has been archived by the owner on Jan 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinteraction.service.ts
397 lines (346 loc) · 14.7 KB
/
interaction.service.ts
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable max-lines */
import { ElementPropertiesUpdate } from '../../store/actions/element-properties.action';
import { RendererService } from 'src/app/services/renderer/renderer.service';
import { buildPropertyUpdatePayload, rectsIntersect } from 'cd-common/utils';
import { ISelectionState } from '../../store/reducers/selection.reducer';
import { IConfigPayload } from '../../interfaces/action.interface';
import { Observable, BehaviorSubject, Subscription } from 'rxjs';
import { distinctUntilChanged, map } from 'rxjs/operators';
import { Injectable, OnDestroy } from '@angular/core';
import { IProjectState } from '../../store/reducers';
import { areObjectsEqual } from 'cd-utils/object';
import { getRootIds } from 'cd-common/models';
import { Store, select } from '@ngrx/store';
import { CanvasService } from '../canvas/canvas.service';
import { areArraysEqual } from 'cd-utils/array';
import * as selectors from '../../store/selectors';
import * as actions from '../../store/actions';
import * as utils from './interaction.utils';
import * as snapUtils from './snap.utils';
import * as cd from 'cd-interfaces';
import { ProjectContentService } from 'src/app/database/changes/project-content.service';
import { PropertiesService } from '../properties/properties.service';
@Injectable({ providedIn: 'root' })
export class InteractionService implements OnDestroy {
private _elementProperties: cd.ElementPropertiesMap = {};
private _snappedPos = new snapUtils.SnapPosition();
private _selectionState?: ISelectionState;
private _subscriptions = new Subscription();
private _outletFrames: ReadonlyArray<utils.RootFrame> = [];
public visibleBoards$ = new BehaviorSubject<ReadonlyArray<string>>([]);
public outletFrameOrder$ = new BehaviorSubject<ReadonlyArray<string>>([]);
public outletFrameRects$ = new BehaviorSubject<cd.RenderRectMap>(new Map());
public elementProperties$: Observable<cd.ElementPropertiesMap>;
public elementsByOutletFrame$ = new BehaviorSubject<cd.RenderElementMap>(new Map());
public highlight$ = new BehaviorSubject<cd.RenderElementMap>(new Map());
public interacting$ = new BehaviorSubject<boolean>(false);
public propsPanelInteraction = new utils.InteractionQueue();
// public marqueeRect$ = new BehaviorSubject<cd.IRect>(generateFrame());
public renderRects$ = new BehaviorSubject<cd.RenderRectMap>(new Map());
public selection$ = new BehaviorSubject<cd.RenderElementMap>(new Map());
public linePoints$ = new BehaviorSubject<cd.ILine[]>([]);
public boardMoving$ = new BehaviorSubject<boolean>(false);
public selectionState$?: Observable<ISelectionState>;
constructor(
private _rendererService: RendererService,
private _canvasService: CanvasService,
private _projectStore: Store<IProjectState>,
private _projectContentService: ProjectContentService,
private _propsService: PropertiesService
) {
const outletFrames$ = this._propsService.currentOutletFrames$.pipe(
map((item) => item.map(({ id, elementType, frame }) => ({ id, elementType, frame }))),
distinctUntilChanged((x, y) => areObjectsEqual(x, y))
);
this.selectionState$ = this._projectStore.pipe(select(selectors.getSelectionState));
this.elementProperties$ = this._projectContentService.elementProperties$;
this._subscriptions.add(outletFrames$.subscribe(this.onOutletFramesSubscription));
this._subscriptions.add(this.elementProperties$.subscribe(this.onElementProperties));
this._subscriptions.add(this.selectionState$.subscribe(this.onSelectionChange));
this._subscriptions.add(
this._rendererService.renderResultsByBoard$.subscribe(this.onRenderResults)
);
this._subscriptions.add(
this._canvasService.canvas$
.pipe(
map((canvas) => utils.filterBoardsVisibleInViewport(canvas, this._outletFrames)),
distinctUntilChanged(areArraysEqual)
)
.subscribe(this.onVisibleBoards)
);
}
ngOnDestroy(): void {
this._subscriptions.unsubscribe();
}
private onVisibleBoards = (boards: ReadonlyArray<string>) => {
this.visibleBoards$.next(boards);
};
private updateFrameOrder(outletFrames: utils.RootFrame[]) {
const outletIds = outletFrames.map((item) => item.id);
// Removes outletFrameIds from order which have been removed from IOutletFrameProperties[]
const order = [...this.outletFrameOrder$.getValue()].filter((id) => outletIds.includes(id));
// Add new outletFrames to the top of the stack
for (const outletId of outletIds) {
if (!order.includes(outletId)) order.push(outletId);
}
this.outletFrameOrder = order;
}
propsPanelInteracting() {
this.propsPanelInteraction.didInteract();
}
get propsInteracting$(): BehaviorSubject<boolean> {
return this.propsPanelInteraction.interacting$;
}
get renderRects() {
return this.renderRects$.getValue();
}
get mergedRenderRects(): cd.RenderRectMap {
const { renderRects, outletFrameRects } = this;
return new Map([...renderRects, ...outletFrameRects]);
}
set outletFrameOrder(order: ReadonlyArray<string>) {
const prevOrder = [...this.outletFrameOrder$.getValue()];
if (!utils.didOrderChange(prevOrder, order)) return;
this.outletFrameOrder$.next(order);
}
private onElementProperties = (props: cd.ElementPropertiesMap) => {
this._elementProperties = props;
};
private onOutletFramesSubscription = (outletFrames: utils.RootFrame[]) => {
const outletRectMap = new Map();
for (const outlet of outletFrames) {
const { id: rootId, frame, elementType: type } = outlet;
const payload = { frame, rootId, type, id: rootId };
outletRectMap.set(rootId, payload);
}
this.updateFrameOrder(outletFrames);
this._outletFrames = outletFrames;
this.outletFrameRects$.next(outletRectMap);
};
private onRenderResults = (results: cd.IStringMap<cd.RenderResults>) => {
const renderRects = new Map();
const elementsByOutletFrame = new Map();
const validatedResults = utils.validateRenderResults(results, this._elementProperties);
for (const [outletFrameId, renderResults] of validatedResults) {
elementsByOutletFrame.set(outletFrameId, Object.keys(renderResults));
const children = Object.entries(renderResults);
for (const [id, renderData] of children) {
renderRects.set(id, renderData);
}
}
this.elementsByOutletFrame$.next(elementsByOutletFrame);
this.renderRects$.next(renderRects);
};
get interacting(): boolean {
return this.interacting$.getValue();
}
set interacting(value: boolean) {
if (this.interacting === value) return;
this.interacting$.next(value);
}
highlightElement(id: string) {
if (!id) this.resetHighlight();
const element = this._elementProperties[id];
if (!element) return;
this.highlight(element.rootId, id);
}
highlight(outletFrameId: string, id: string) {
if (this.interacting === true) return;
if (!outletFrameId || !id) return;
const highlight = new Map();
highlight.set(outletFrameId, [id]);
this.highlight$.next(highlight);
}
isHighlighted(outletFrameId: string, id: string): boolean {
const highlights = this.highlight$.getValue().get(outletFrameId) || [];
return highlights.includes(id);
}
resetHighlight() {
this.highlight$.next(new Map());
}
removeHighlight(outletFrameId: string, id: string): void {
if (this.interacting === true) return;
const highlights = new Map([...this.highlight$.getValue()]);
const highlighted = highlights.get(outletFrameId) || [];
const idx = highlighted.indexOf(id);
if (idx !== -1) {
this.resetHighlight();
}
}
private onSelectionChange = (state?: ISelectionState) => {
this._selectionState = state;
if (!state || state.ids.size === 0) {
this.selection$.next(new Map());
return;
}
const { _elementProperties } = this;
const outlets = getRootIds(state.ids, _elementProperties);
const selection = utils.buildSelectionOutletMap(outlets, state, _elementProperties);
this.bringSelectedOutletFrameToFront(outlets);
this.selection$.next(selection);
};
/** Used during drag & drop to bring an outletframe forward */
public bringOutletFrameToFront(outletFrameId: string) {
const outletFrames = this.outletFrameOrder$.getValue();
const lastOutletFrameId = outletFrames[outletFrames.length - 1];
const hasOutletFrame = outletFrames.includes(outletFrameId);
if (hasOutletFrame === false || outletFrameId === lastOutletFrameId) return;
const outlets = new Set([outletFrameId]);
this.bringSelectedOutletFrameToFront(outlets);
}
private bringSelectedOutletFrameToFront(outlets: Set<string>) {
const order = [...this.outletFrameOrder$.getValue()];
this.outletFrameOrder = utils.bringOutletToFront(order, outlets);
}
checkOutletFrameSelection(rect: cd.IRect, canvasPosition: cd.ICanvasPosition) {
const selRect = utils.selectionRectFromCanvas(rect, canvasPosition);
const { outletFrameRects } = this;
const selected = Array.from(outletFrameRects.entries()).reduce<string[]>((acc, curr) => {
const [key, value] = curr;
const frame = value.frame;
if (rectsIntersect(frame, selRect)) {
acc.push(key);
}
return acc;
}, []);
const selection = this.selection$.getValue();
const selectedLength = selected.length;
if (selectedLength) {
const same =
selection.size === selectedLength && selected.every((item) => selection.has(item));
if (same === false) this.selectOutletFrames(selected);
} else {
if (selection.size >= 1) this.deselectAll();
}
}
deselectAll() {
if (this.selection$.getValue().size === 0) return; // already deselected
this.dispatch(new actions.SelectionDeselectAll());
}
isSelected(id: string): boolean {
const { _selectionState } = this;
if (!_selectionState) return false;
return _selectionState.ids.has(id);
}
dispatch(action: any) {
if (!this._projectStore) return;
this._projectStore.dispatch(action);
}
toggleElements(ids: string[], append = false, allowDeselect = false) {
this.dispatch(new actions.SelectionToggleElements(ids, append, allowDeselect));
}
selectOutletFrames(outletFrameIds: string[]) {
this.dispatch(new actions.SelectionSet(new Set(outletFrameIds), true));
}
startInteracting() {
this.interacting = true;
this.resetHighlight();
}
stopInteracting(isOutletFrame: boolean = false) {
if (!isOutletFrame) this.interacting = false;
}
get outletFrameRects() {
return this.outletFrameRects$.getValue();
}
get selectedOutletFrames(): ReadonlyArray<cd.IRenderResult> {
const { outletFrameRects } = this;
const selected = Array.from(this.selection$.getValue().keys());
return selected.reduce<cd.IRenderResult[]>((acc, value) => {
const item = outletFrameRects.get(value);
if (item) acc.push(item);
return acc;
}, []);
}
// prettier-ignore
moveOutletFrame(dx: number, dy: number, clientX: number, clientY: number, z: number) {
const { selectedOutletFrames, outletFrameRects, _snappedPos } = this;
const selectedIRect = snapUtils.rectFromSelectedOutlets(selectedOutletFrames);
const lines = snapUtils.generateSnapLines(selectedOutletFrames, outletFrameRects, selectedIRect);
const snapPoint = snapUtils.generateSnapPointFromDelta(selectedIRect, dx, dy, lines);
const newPosAndDelta = snapUtils.generateClientPosAndDelta(_snappedPos, snapPoint, clientX, clientY, dx, dy, z);
const [newClientPos] = newPosAndDelta;
const newRenderMap = snapUtils.snapSelectedOutlets(selectedOutletFrames, outletFrameRects, selectedIRect, snapPoint, newPosAndDelta);
// Pass client position to be recorded as new snap point
this._snappedPos.update(...newClientPos);
this.linePoints$.next(lines);
this.outletFrameRects$.next(newRenderMap);
}
updateOutletFrames(payload: cd.IPropertiesUpdatePayload[]) {
this.dispatch(new ElementPropertiesUpdate(payload));
}
publishSelectedOutletFrameRects() {
const update = this.selectedOutletFrames.map((item) => {
const rounded = utils.roundOutletFrameToGrid(item.frame);
const frame = utils.clampOutletFrameDimensions(rounded);
return buildPropertyUpdatePayload(item.rootId, { frame });
});
this.updateOutletFrames(update);
}
endOutletFrameMove(changed: boolean) {
if (changed) this.publishSelectedOutletFrameRects();
this.boardMoving$.next(false);
this.interacting = false;
this.linePoints$.next([]);
}
updateElementRect(id: string, frame: cd.IRect, publish = false) {
const outletFrameRects = new Map([...this.outletFrameRects]);
const item = outletFrameRects.get(id);
if (item) {
const clone = { ...item, frame: { ...frame } };
outletFrameRects.set(id, clone);
this.outletFrameRects$.next(outletFrameRects);
}
if (publish) {
this.publishOutletFrameRects(id);
}
}
publishOutletFrameRects(id: string) {
const outletFrame = this.outletFrameRects.get(id)?.frame;
if (!outletFrame) return;
const rounded = utils.roundOutletFrameToGrid(outletFrame);
const frame = utils.clampOutletFrameDimensions(rounded);
const update = buildPropertyUpdatePayload(id, { frame });
this.updateOutletFrames([update]);
}
renderRectsForIds = (mergeWithOutlets: boolean, ...ids: string[]): cd.RenderRectMap => {
const rects = mergeWithOutlets ? this.mergedRenderRects : this.renderRects;
return ids.reduce((acc, curr) => {
const found = rects.get(curr);
if (found) acc.set(curr, found);
return acc;
}, new Map());
};
renderRectForId = (id: string): cd.IRenderResult | undefined => {
return this.renderRects.get(id);
};
isolateSymbol = (symbolInstance: cd.ISymbolInstanceProperties) => {
const payload: IConfigPayload = { propertyModels: [symbolInstance] };
this.dispatch(new actions.PanelIsolateSymbol(null, payload));
};
snapToBoardWithId = (id?: string | null) => {
if (!id) return;
const board = this._elementProperties[id];
if (!board) return;
this.dispatch(new actions.CanvasSnapToBoard(null, { propertyModels: [board] }));
};
snapToBoardAndSelect = (id?: string | null) => {
if (!id) return;
this.snapToBoardWithId(id);
this.selectOutletFrames([id]);
};
}