-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
ActiveSelectionLayoutManager.ts
54 lines (52 loc) · 2.07 KB
/
ActiveSelectionLayoutManager.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
import { LayoutManager } from './LayoutManager';
import type { RegistrationContext, StrictLayoutContext } from './types';
import type { Group } from '../shapes/Group';
/**
* Today the LayoutManager class also takes care of subscribing event handlers
* to update the group layout when the group is interactive and a transform is applied
* to a child object.
* The ActiveSelection is never interactive, but it could contain objects from
* groups that are.
* The standard LayoutManager would subscribe the children of the activeSelection to
* perform layout changes to the active selection itself, what we need instead is that
* the transformation applied to the active selection will trigger changes to the
* original group of the children ( the one referenced under the parent property )
* This subclass of the LayoutManager has a single duty to fill the gap of this difference.`
*/
export class ActiveSelectionLayoutManager extends LayoutManager {
subscribeTargets(
context: RegistrationContext & Partial<StrictLayoutContext>
): void {
const activeSelection = context.target;
const parents = context.targets.reduce((parents, target) => {
target.parent && parents.add(target.parent);
return parents;
}, new Set<Group>());
parents.forEach((parent) => {
parent.layoutManager.subscribeTargets({
target: parent,
targets: [activeSelection],
});
});
}
/**
* unsubscribe from parent only if all its children were deselected
*/
unsubscribeTargets(
context: RegistrationContext & Partial<StrictLayoutContext>
): void {
const activeSelection = context.target;
const selectedObjects = activeSelection.getObjects();
const parents = context.targets.reduce((parents, target) => {
target.parent && parents.add(target.parent);
return parents;
}, new Set<Group>());
parents.forEach((parent) => {
!selectedObjects.some((object) => object.parent === parent) &&
parent.layoutManager.unsubscribeTargets({
target: parent,
targets: [activeSelection],
});
});
}
}