-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathparse.ts
154 lines (133 loc) · 3.94 KB
/
parse.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
import { decimalsLength, getValueInRange } from './helpers';
import { extractValues } from './directives';
import { getPrefix } from './prefix';
import { FilterValue } from './directives/tg-filter';
import { EdgeOptions } from './directives/tg-edge';
import { TgElement } from './type';
import { easePercentage as ease } from './ease';
/**
* This function will be called in observe stage,
* caching those values into an object for ease of use in scroll event.
*/
export function parseAttributes(element: HTMLElement): TgElement {
const prefix = getPrefix()
const follow: HTMLElement = extractValues(element, `${prefix}follow`);
const actualElement = follow || element;
const style = getComputedStyle(actualElement);
const top = +style.getPropertyValue(`--${prefix}top`);
const height = +style.getPropertyValue(`--${prefix}height`);
const name: string = extractValues(element, `${prefix}name`);
const from: number = extractValues(actualElement, `${prefix}from`);
const to: number = extractValues(actualElement, `${prefix}to`);
const steps: number = extractValues(actualElement, `${prefix}steps`);
const step: number = extractValues(actualElement, `${prefix}step`);
const bezier: string | Array<number> = extractValues(
actualElement,
`${prefix}bezier`
);
const filter: FilterValue = extractValues(element, `${prefix}filter`);
const edge: EdgeOptions = extractValues(actualElement, `${prefix}edge`);
const range = Math.abs(to - from);
const increment = step === 0 ? range / steps : step;
const segments = steps ? steps : range / increment;
const decimals = decimalsLength(increment);
const multiplier = from > to ? -1 : 1;
const mapping: Record<string, string> = extractValues(
element,
`${prefix}map`,
{
increment,
decimals,
}
);
return {
el: element,
top,
height,
name,
from,
to,
steps,
step,
mapping,
filter,
edge,
range,
increment,
segments,
decimals,
multiplier,
lastValue: null,
bezier,
};
}
/**
* Calculation happens here,
* this function is called when scroll event happens.
* So keep this as light as possible.
*/
export function parseValues(elements: TgElement[]) {
const { scrollTop: scrolled, clientHeight } = document.documentElement;
elements.forEach((element) => {
const {
el,
top,
height,
increment,
segments,
decimals,
multiplier,
name,
from,
// currently unused
// to,
mapping,
filter,
edge,
lastValue,
bezier,
} = element;
// If the name is equal to '_' (--_), skip
if (name === '--_') {
return;
}
// edge is 'cover' by default
let percentage = edge === 'cover' ?
getValueInRange((scrolled + clientHeight - top) / (clientHeight + height), 0, 1)
: getValueInRange((scrolled - top) / (height - clientHeight), 0, 1);
// Calculation result value of bezier
percentage = bezier ? ease(bezier, percentage) : percentage;
let value: string | number;
const mappingValue = (
from +
Math.floor((segments + 1) * percentage) * increment * multiplier
).toFixed(decimals);
value = +mappingValue;
if (filter.values.length > 0 && !filter.values.includes(value)) {
// If the mode is 'exact', remove the CSS property
// Setting the lastValue to null to ensure correct comparison below
if (filter.mode === 'exact') {
element.lastValue = null;
el.style.removeProperty(name);
}
return;
}
if (typeof mapping[value] !== 'undefined') {
value = mapping[value];
}
if (lastValue != value) {
el.style.setProperty(name, `${value}`);
el.dispatchEvent(
new CustomEvent('tg', {
// @ts-ignore
target: el,
detail: {
value,
},
})
);
element.lastValue = value;
console.log('value', element, value);
}
});
}