|
1 | | -import { promisify } from './event'; |
2 | | - |
3 | | -export function durationOf( |
4 | | - type: 'transition' | 'animation', |
5 | | - element: HTMLElement |
6 | | -) { |
7 | | - const { transitionDuration, animationDuration } = getComputedStyle(element); |
8 | | - |
9 | | - const duration = |
10 | | - type === 'animation' ? animationDuration : transitionDuration; |
11 | | - |
12 | | - return parseFloat(duration) * (duration.slice(-2) === 'ms' ? 1 : 1000); |
13 | | -} |
14 | | - |
15 | | -export function watchMotion<T extends Event>( |
16 | | - type: 'transition' | 'animation', |
17 | | - element: HTMLElement |
18 | | -) { |
19 | | - return Promise.race([ |
20 | | - promisify<T>(type, element).catch(event => Promise.resolve(event)), |
21 | | - new Promise(resolve => setTimeout(resolve, durationOf(type, element))) |
22 | | - ]); |
23 | | -} |
24 | | - |
25 | | -function fadeIn<T extends Event>( |
26 | | - type: 'transition' | 'animation', |
27 | | - element: HTMLElement, |
28 | | - className: string, |
29 | | - display: string |
30 | | -) { |
31 | | - element.style.display = display; |
32 | | - |
33 | | - const end = watchMotion<T>(type, element); |
34 | | - |
35 | | - return new Promise<T>(resolve => |
36 | | - self.requestAnimationFrame(() => { |
37 | | - element.classList.add(className); |
38 | | - |
39 | | - end.then(resolve); |
40 | | - }) |
41 | | - ); |
42 | | -} |
43 | | - |
44 | | -async function fadeOut<T extends Event>( |
45 | | - type: 'transition' | 'animation', |
46 | | - element: HTMLElement, |
47 | | - className: string, |
48 | | - remove?: boolean |
49 | | -) { |
50 | | - const end = watchMotion<T>(type, element); |
51 | | - |
52 | | - element.classList.remove(className); |
53 | | - |
54 | | - await end; |
55 | | - |
56 | | - if (remove) element.remove(); |
57 | | - else element.style.display = 'none'; |
58 | | -} |
59 | | - |
60 | | -export function transitIn( |
61 | | - element: HTMLElement, |
62 | | - className: string, |
63 | | - display = 'block' |
64 | | -) { |
65 | | - return fadeIn<TransitionEvent>('transition', element, className, display); |
66 | | -} |
67 | | - |
68 | | -export function animateIn( |
69 | | - element: HTMLElement, |
70 | | - className: string, |
71 | | - display = 'block' |
72 | | -) { |
73 | | - return fadeIn<AnimationEvent>('animation', element, className, display); |
74 | | -} |
75 | | - |
76 | | -export function transitOut( |
77 | | - element: HTMLElement, |
78 | | - className: string, |
79 | | - remove?: boolean |
80 | | -) { |
81 | | - return fadeOut<TransitionEvent>('transition', element, className, remove); |
82 | | -} |
83 | | - |
84 | | -export function animateOut( |
85 | | - element: HTMLElement, |
86 | | - className: string, |
87 | | - remove?: boolean |
88 | | -) { |
89 | | - return fadeOut<AnimationEvent>('animation', element, className, remove); |
90 | | -} |
| 1 | +import { promisify } from './event'; |
| 2 | + |
| 3 | +export interface CartesianCoordinate { |
| 4 | + x: number; |
| 5 | + y: number; |
| 6 | + z?: number; |
| 7 | +} |
| 8 | + |
| 9 | +export class PageVector { |
| 10 | + from: CartesianCoordinate; |
| 11 | + to: CartesianCoordinate; |
| 12 | + |
| 13 | + constructor(from: CartesianCoordinate, to: CartesianCoordinate) { |
| 14 | + this.from = from; |
| 15 | + this.to = to; |
| 16 | + } |
| 17 | + |
| 18 | + get length() { |
| 19 | + const { from, to } = this; |
| 20 | + |
| 21 | + return Math.sqrt( |
| 22 | + Math.pow(to.x - from.x, 2) + |
| 23 | + Math.pow(to.y - from.y, 2) + |
| 24 | + (to.z != null ? Math.pow(to.z - from.z, 2) : 0) |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + get direction() { |
| 29 | + const { from, to } = this; |
| 30 | + const XD = to.x - from.x, |
| 31 | + YD = to.y - from.y, |
| 32 | + ZD = to.z - from.z; |
| 33 | + const XL = Math.abs(XD), |
| 34 | + YL = Math.abs(YD), |
| 35 | + ZL = Math.abs(ZD); |
| 36 | + |
| 37 | + switch (isNaN(ZL) ? Math.max(XL, YL) : Math.max(XL, YL, ZL)) { |
| 38 | + case XL: |
| 39 | + return XD > 0 ? 'right' : 'left'; |
| 40 | + case YL: |
| 41 | + return YD > 0 ? 'forward' : 'backward'; |
| 42 | + case ZL: |
| 43 | + return ZD > 0 ? 'up' : 'down'; |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export function getSwipeVector( |
| 49 | + from: CartesianCoordinate, |
| 50 | + to: CartesianCoordinate, |
| 51 | + threshold = parseInt(self.getComputedStyle(document.body).fontSize) * 6 |
| 52 | +) { |
| 53 | + const vector = new PageVector(from, to); |
| 54 | + |
| 55 | + if (vector.length >= threshold && !self.getSelection()?.toString().trim()) |
| 56 | + return vector; |
| 57 | +} |
| 58 | + |
| 59 | +export function durationOf( |
| 60 | + type: 'transition' | 'animation', |
| 61 | + element: HTMLElement |
| 62 | +) { |
| 63 | + const { transitionDuration, animationDuration } = getComputedStyle(element); |
| 64 | + |
| 65 | + const duration = |
| 66 | + type === 'animation' ? animationDuration : transitionDuration; |
| 67 | + |
| 68 | + return parseFloat(duration) * (duration.slice(-2) === 'ms' ? 1 : 1000); |
| 69 | +} |
| 70 | + |
| 71 | +export function watchMotion<T extends Event>( |
| 72 | + type: 'transition' | 'animation', |
| 73 | + element: HTMLElement |
| 74 | +) { |
| 75 | + return Promise.race([ |
| 76 | + promisify<T>(type, element).catch(event => Promise.resolve(event)), |
| 77 | + new Promise(resolve => setTimeout(resolve, durationOf(type, element))) |
| 78 | + ]); |
| 79 | +} |
| 80 | + |
| 81 | +function fadeIn<T extends Event>( |
| 82 | + type: 'transition' | 'animation', |
| 83 | + element: HTMLElement, |
| 84 | + className: string, |
| 85 | + display: string |
| 86 | +) { |
| 87 | + element.style.display = display; |
| 88 | + |
| 89 | + const end = watchMotion<T>(type, element); |
| 90 | + |
| 91 | + return new Promise<T>(resolve => |
| 92 | + self.requestAnimationFrame(() => { |
| 93 | + element.classList.add(className); |
| 94 | + |
| 95 | + end.then(resolve); |
| 96 | + }) |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +async function fadeOut<T extends Event>( |
| 101 | + type: 'transition' | 'animation', |
| 102 | + element: HTMLElement, |
| 103 | + className: string, |
| 104 | + remove?: boolean |
| 105 | +) { |
| 106 | + const end = watchMotion<T>(type, element); |
| 107 | + |
| 108 | + element.classList.remove(className); |
| 109 | + |
| 110 | + await end; |
| 111 | + |
| 112 | + if (remove) element.remove(); |
| 113 | + else element.style.display = 'none'; |
| 114 | +} |
| 115 | + |
| 116 | +export function transitIn( |
| 117 | + element: HTMLElement, |
| 118 | + className: string, |
| 119 | + display = 'block' |
| 120 | +) { |
| 121 | + return fadeIn<TransitionEvent>('transition', element, className, display); |
| 122 | +} |
| 123 | + |
| 124 | +export function animateIn( |
| 125 | + element: HTMLElement, |
| 126 | + className: string, |
| 127 | + display = 'block' |
| 128 | +) { |
| 129 | + return fadeIn<AnimationEvent>('animation', element, className, display); |
| 130 | +} |
| 131 | + |
| 132 | +export function transitOut( |
| 133 | + element: HTMLElement, |
| 134 | + className: string, |
| 135 | + remove?: boolean |
| 136 | +) { |
| 137 | + return fadeOut<TransitionEvent>('transition', element, className, remove); |
| 138 | +} |
| 139 | + |
| 140 | +export function animateOut( |
| 141 | + element: HTMLElement, |
| 142 | + className: string, |
| 143 | + remove?: boolean |
| 144 | +) { |
| 145 | + return fadeOut<AnimationEvent>('animation', element, className, remove); |
| 146 | +} |
0 commit comments