-
Notifications
You must be signed in to change notification settings - Fork 42
/
preview-renderer.component.ts
93 lines (85 loc) · 2.63 KB
/
preview-renderer.component.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
import { Component, ChangeDetectionStrategy } from "@angular/core";
import { SkyhookDndService, Offset } from "@angular-skyhook/core";
import { map } from "rxjs/operators";
// @ts-ignore
import { Observable } from 'rxjs';
/**
* This is internal, you probably won't ever need to use it directly.
*
* For understanding's sake, it helps to know that this component
* essentially just renders whatever is placed between its tags, but
* in a `position: fixed` container that is translated according to
* the drag in progress and how far it has travelled.
*
* It currently has a workaround for some Firefox versions where the
* whole thing wouldn't re-render unless you animated the border.
*/
@Component({
selector: "skyhook-preview-renderer",
template: `
<div class="firefox-bug" [ngStyle]="style$|async">
<ng-content></ng-content>
</div>
`,
styles: [
`
:host {
display: block;
position: fixed;
pointer-events: none;
z-index: 100;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
@keyframes animatedBorder {
from {
border-color: rgba(0, 0, 0, 0);
}
to {
border-color: rgba(0, 0, 0, 1);
}
}
.firefox-bug {
animation-name: animatedBorder;
animation-duration: 1s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
`
],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class SkyhookPreviewRendererComponent {
/** @ignore */
private layer = this.skyhook.dragLayer();
/** @ignore */
collect$ = this.layer.listen(monitor => ({
initialOffset: monitor.getInitialSourceClientOffset() as Offset,
currentOffset: monitor.getSourceClientOffset()
}));
/** @ignore */
style$ = this.collect$.pipe(
map(c => {
const { initialOffset, currentOffset } = c;
if (!initialOffset || !currentOffset) {
return {
display: "none"
};
}
let { x, y } = currentOffset;
const transform = `translate(${x}px, ${y}px)`;
return {
transform,
WebkitTransform: transform
};
})
);
/** @ignore */
constructor(private skyhook: SkyhookDndService) {}
/** @ignore */
ngOnDestroy() {
this.layer.unsubscribe();
}
}