@formily-design/shared 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +20 -0
- package/README.md +1 -0
- package/dist/designable.shared.umd.production.js +1 -0
- package/dist/designable.shared.umd.production.min.js +2051 -0
- package/esm/animation.d.ts +2 -0
- package/esm/animation.js +33 -0
- package/esm/array.d.ts +37 -0
- package/esm/array.js +111 -0
- package/esm/clone.d.ts +4 -0
- package/esm/clone.js +97 -0
- package/esm/compose.d.ts +1 -0
- package/esm/compose.js +7 -0
- package/esm/coordinate.d.ts +106 -0
- package/esm/coordinate.js +475 -0
- package/esm/element.d.ts +6 -0
- package/esm/element.js +137 -0
- package/esm/event.d.ts +96 -0
- package/esm/event.js +213 -0
- package/esm/globalThisPolyfill.d.ts +1 -0
- package/esm/globalThisPolyfill.js +22 -0
- package/esm/index.d.ts +17 -0
- package/esm/index.js +17 -0
- package/esm/instanceof.d.ts +1 -0
- package/esm/instanceof.js +11 -0
- package/esm/keycode.d.ts +147 -0
- package/esm/keycode.js +150 -0
- package/esm/lru.d.ts +73 -0
- package/esm/lru.js +293 -0
- package/esm/observer.d.ts +9 -0
- package/esm/observer.js +29 -0
- package/esm/request-idle.d.ts +10 -0
- package/esm/request-idle.js +8 -0
- package/esm/scroller.d.ts +10 -0
- package/esm/scroller.js +82 -0
- package/esm/subscribable.d.ts +14 -0
- package/esm/subscribable.js +48 -0
- package/esm/types.d.ts +13 -0
- package/esm/types.js +21 -0
- package/esm/uid.d.ts +1 -0
- package/esm/uid.js +9 -0
- package/lib/animation.d.ts +2 -0
- package/lib/animation.js +38 -0
- package/lib/array.d.ts +37 -0
- package/lib/array.js +125 -0
- package/lib/clone.d.ts +4 -0
- package/lib/clone.js +102 -0
- package/lib/compose.d.ts +1 -0
- package/lib/compose.js +11 -0
- package/lib/coordinate.d.ts +106 -0
- package/lib/coordinate.js +504 -0
- package/lib/element.d.ts +6 -0
- package/lib/element.js +145 -0
- package/lib/event.d.ts +96 -0
- package/lib/event.js +218 -0
- package/lib/globalThisPolyfill.d.ts +1 -0
- package/lib/globalThisPolyfill.js +25 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.js +33 -0
- package/lib/instanceof.d.ts +1 -0
- package/lib/instanceof.js +15 -0
- package/lib/keycode.d.ts +147 -0
- package/lib/keycode.js +154 -0
- package/lib/lru.d.ts +73 -0
- package/lib/lru.js +297 -0
- package/lib/observer.d.ts +9 -0
- package/lib/observer.js +33 -0
- package/lib/request-idle.d.ts +10 -0
- package/lib/request-idle.js +13 -0
- package/lib/scroller.d.ts +10 -0
- package/lib/scroller.js +88 -0
- package/lib/subscribable.d.ts +14 -0
- package/lib/subscribable.js +52 -0
- package/lib/types.d.ts +13 -0
- package/lib/types.js +29 -0
- package/lib/uid.d.ts +1 -0
- package/lib/uid.js +12 -0
- package/package.json +32 -0
- package/rollup.config.js +3 -0
- package/src/animation.ts +38 -0
- package/src/array.ts +301 -0
- package/src/clone.ts +96 -0
- package/src/compose.ts +7 -0
- package/src/coordinate.ts +633 -0
- package/src/element.ts +144 -0
- package/src/event.ts +379 -0
- package/src/globalThisPolyfill.ts +19 -0
- package/src/index.ts +17 -0
- package/src/instanceof.ts +10 -0
- package/src/keycode.ts +150 -0
- package/src/lru.ts +322 -0
- package/src/observer.ts +38 -0
- package/src/request-idle.ts +22 -0
- package/src/scroller.ts +113 -0
- package/src/subscribable.ts +60 -0
- package/src/types.ts +27 -0
- package/src/uid.ts +10 -0
- package/tsconfig.build.json +10 -0
- package/tsconfig.json +5 -0
package/esm/lru.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A doubly linked list-based Least Recently Used (LRU) cache. Will keep most
|
|
3
|
+
* recently used items while discarding least recently used items when its limit
|
|
4
|
+
* is reached.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under MIT. Copyright (c) 2010 Rasmus Andersson <http://hunch.se/>
|
|
7
|
+
* See README.md for details.
|
|
8
|
+
*
|
|
9
|
+
* Illustration of the design:
|
|
10
|
+
*
|
|
11
|
+
* entry entry entry entry
|
|
12
|
+
* ______ ______ ______ ______
|
|
13
|
+
* | head |.newer => | |.newer => | |.newer => | tail |
|
|
14
|
+
* | A | | B | | C | | D |
|
|
15
|
+
* |______| <= older.|______| <= older.|______| <= older.|______|
|
|
16
|
+
*
|
|
17
|
+
* removed <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- added
|
|
18
|
+
*/
|
|
19
|
+
export declare class LRUMap<K, V> {
|
|
20
|
+
size: number;
|
|
21
|
+
limit: number;
|
|
22
|
+
oldest: any;
|
|
23
|
+
newest: any;
|
|
24
|
+
_keymap: Map<K, {
|
|
25
|
+
key: K;
|
|
26
|
+
value: V;
|
|
27
|
+
}>;
|
|
28
|
+
constructor(limit: number, entries?: any);
|
|
29
|
+
private _markEntryAsUsed;
|
|
30
|
+
assign(entries: any): void;
|
|
31
|
+
get(key: K): V;
|
|
32
|
+
set(key: K, value: V): this;
|
|
33
|
+
shift(): any[];
|
|
34
|
+
find(key: K): V;
|
|
35
|
+
has(key: K): boolean;
|
|
36
|
+
delete(key: any): V;
|
|
37
|
+
clear(): void;
|
|
38
|
+
keys(): KeyIterator;
|
|
39
|
+
values(): ValueIterator;
|
|
40
|
+
entries(): void;
|
|
41
|
+
forEach(fun: (value: any, key: any, ctx: object) => void, thisObj: any): void;
|
|
42
|
+
toJSON(): any[];
|
|
43
|
+
toString(): string;
|
|
44
|
+
[Symbol.iterator](): EntryIterator;
|
|
45
|
+
}
|
|
46
|
+
declare class EntryIterator {
|
|
47
|
+
entry: any;
|
|
48
|
+
constructor(oldestEntry: any);
|
|
49
|
+
[Symbol.iterator](): this;
|
|
50
|
+
next(): {
|
|
51
|
+
done: boolean;
|
|
52
|
+
value: any[];
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
declare class KeyIterator {
|
|
56
|
+
entry: any;
|
|
57
|
+
constructor(oldestEntry: any);
|
|
58
|
+
[Symbol.iterator](): this;
|
|
59
|
+
next(): {
|
|
60
|
+
done: boolean;
|
|
61
|
+
value: any;
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
declare class ValueIterator {
|
|
65
|
+
entry: any;
|
|
66
|
+
constructor(oldestEntry: any);
|
|
67
|
+
[Symbol.iterator](): this;
|
|
68
|
+
next(): {
|
|
69
|
+
done: boolean;
|
|
70
|
+
value: any;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
export {};
|
package/esm/lru.js
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A doubly linked list-based Least Recently Used (LRU) cache. Will keep most
|
|
3
|
+
* recently used items while discarding least recently used items when its limit
|
|
4
|
+
* is reached.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under MIT. Copyright (c) 2010 Rasmus Andersson <http://hunch.se/>
|
|
7
|
+
* See README.md for details.
|
|
8
|
+
*
|
|
9
|
+
* Illustration of the design:
|
|
10
|
+
*
|
|
11
|
+
* entry entry entry entry
|
|
12
|
+
* ______ ______ ______ ______
|
|
13
|
+
* | head |.newer => | |.newer => | |.newer => | tail |
|
|
14
|
+
* | A | | B | | C | | D |
|
|
15
|
+
* |______| <= older.|______| <= older.|______| <= older.|______|
|
|
16
|
+
*
|
|
17
|
+
* removed <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- added
|
|
18
|
+
*/
|
|
19
|
+
/* eslint-disable */
|
|
20
|
+
const NEWER = Symbol('newer');
|
|
21
|
+
const OLDER = Symbol('older');
|
|
22
|
+
function Entry(key, value) {
|
|
23
|
+
this.key = key;
|
|
24
|
+
this.value = value;
|
|
25
|
+
this[NEWER] = undefined;
|
|
26
|
+
this[OLDER] = undefined;
|
|
27
|
+
}
|
|
28
|
+
export class LRUMap {
|
|
29
|
+
constructor(limit, entries) {
|
|
30
|
+
if (typeof limit !== 'number') {
|
|
31
|
+
// called as (entries)
|
|
32
|
+
entries = limit;
|
|
33
|
+
limit = 0;
|
|
34
|
+
}
|
|
35
|
+
this.size = 0;
|
|
36
|
+
this.limit = limit;
|
|
37
|
+
this.oldest = this.newest = undefined;
|
|
38
|
+
this._keymap = new Map();
|
|
39
|
+
if (entries) {
|
|
40
|
+
this.assign(entries);
|
|
41
|
+
if (limit < 1) {
|
|
42
|
+
this.limit = this.size;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
_markEntryAsUsed(entry) {
|
|
47
|
+
if (entry === this.newest) {
|
|
48
|
+
// Already the most recenlty used entry, so no need to update the list
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
// HEAD--------------TAIL
|
|
52
|
+
// <.older .newer>
|
|
53
|
+
// <--- add direction --
|
|
54
|
+
// A B C <D> E
|
|
55
|
+
if (entry[NEWER]) {
|
|
56
|
+
if (entry === this.oldest) {
|
|
57
|
+
this.oldest = entry[NEWER];
|
|
58
|
+
}
|
|
59
|
+
entry[NEWER][OLDER] = entry[OLDER]; // C <-- E.
|
|
60
|
+
}
|
|
61
|
+
if (entry[OLDER]) {
|
|
62
|
+
entry[OLDER][NEWER] = entry[NEWER]; // C. --> E
|
|
63
|
+
}
|
|
64
|
+
entry[NEWER] = undefined; // D --x
|
|
65
|
+
entry[OLDER] = this.newest; // D. --> E
|
|
66
|
+
if (this.newest) {
|
|
67
|
+
this.newest[NEWER] = entry; // E. <-- D
|
|
68
|
+
}
|
|
69
|
+
this.newest = entry;
|
|
70
|
+
}
|
|
71
|
+
assign(entries) {
|
|
72
|
+
let entry;
|
|
73
|
+
let limit = this.limit || Number.MAX_VALUE;
|
|
74
|
+
this._keymap.clear();
|
|
75
|
+
const it = entries[Symbol.iterator]();
|
|
76
|
+
for (let itv = it.next(); !itv.done; itv = it.next()) {
|
|
77
|
+
const e = new Entry(itv.value[0], itv.value[1]);
|
|
78
|
+
this._keymap.set(e.key, e);
|
|
79
|
+
if (!entry) {
|
|
80
|
+
this.oldest = e;
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
entry[NEWER] = e;
|
|
84
|
+
e[OLDER] = entry;
|
|
85
|
+
}
|
|
86
|
+
entry = e;
|
|
87
|
+
if (limit-- === 0) {
|
|
88
|
+
throw new Error('overflow');
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
this.newest = entry;
|
|
92
|
+
this.size = this._keymap.size;
|
|
93
|
+
}
|
|
94
|
+
get(key) {
|
|
95
|
+
// First, find our cache entry
|
|
96
|
+
const entry = this._keymap.get(key);
|
|
97
|
+
if (!entry) {
|
|
98
|
+
return;
|
|
99
|
+
} // Not cached. Sorry.
|
|
100
|
+
// As <key> was found in the cache, register it as being requested recently
|
|
101
|
+
this._markEntryAsUsed(entry);
|
|
102
|
+
return entry.value;
|
|
103
|
+
}
|
|
104
|
+
set(key, value) {
|
|
105
|
+
let entry = this._keymap.get(key);
|
|
106
|
+
if (entry) {
|
|
107
|
+
// update existing
|
|
108
|
+
entry.value = value;
|
|
109
|
+
this._markEntryAsUsed(entry);
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
// new entry
|
|
113
|
+
this._keymap.set(key, (entry = new Entry(key, value)));
|
|
114
|
+
if (this.newest) {
|
|
115
|
+
// link previous tail to the new tail (entry)
|
|
116
|
+
this.newest[NEWER] = entry;
|
|
117
|
+
entry[OLDER] = this.newest;
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
// we're first in -- yay
|
|
121
|
+
this.oldest = entry;
|
|
122
|
+
}
|
|
123
|
+
// add new entry to the end of the linked list -- it's now the freshest entry.
|
|
124
|
+
this.newest = entry;
|
|
125
|
+
++this.size;
|
|
126
|
+
if (this.size > this.limit) {
|
|
127
|
+
// we hit the limit -- remove the head
|
|
128
|
+
this.shift();
|
|
129
|
+
}
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
shift() {
|
|
133
|
+
// todo: handle special case when limit == 1
|
|
134
|
+
const entry = this.oldest;
|
|
135
|
+
if (entry) {
|
|
136
|
+
if (this.oldest[NEWER]) {
|
|
137
|
+
// advance the list
|
|
138
|
+
this.oldest = this.oldest[NEWER];
|
|
139
|
+
this.oldest[OLDER] = undefined;
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
// the cache is exhausted
|
|
143
|
+
this.oldest = undefined;
|
|
144
|
+
this.newest = undefined;
|
|
145
|
+
}
|
|
146
|
+
// Remove last strong reference to <entry> and remove links from the purged
|
|
147
|
+
// entry being returned:
|
|
148
|
+
entry[NEWER] = entry[OLDER] = undefined;
|
|
149
|
+
this._keymap.delete(entry.key);
|
|
150
|
+
--this.size;
|
|
151
|
+
return [entry.key, entry.value];
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
find(key) {
|
|
155
|
+
const e = this._keymap.get(key);
|
|
156
|
+
return e ? e.value : undefined;
|
|
157
|
+
}
|
|
158
|
+
has(key) {
|
|
159
|
+
return this._keymap.has(key);
|
|
160
|
+
}
|
|
161
|
+
delete(key) {
|
|
162
|
+
const entry = this._keymap.get(key);
|
|
163
|
+
if (!entry) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
this._keymap.delete(entry.key);
|
|
167
|
+
if (entry[NEWER] && entry[OLDER]) {
|
|
168
|
+
// relink the older entry with the newer entry
|
|
169
|
+
entry[OLDER][NEWER] = entry[NEWER];
|
|
170
|
+
entry[NEWER][OLDER] = entry[OLDER];
|
|
171
|
+
}
|
|
172
|
+
else if (entry[NEWER]) {
|
|
173
|
+
// remove the link to us
|
|
174
|
+
entry[NEWER][OLDER] = undefined;
|
|
175
|
+
// link the newer entry to head
|
|
176
|
+
this.oldest = entry[NEWER];
|
|
177
|
+
}
|
|
178
|
+
else if (entry[OLDER]) {
|
|
179
|
+
// remove the link to us
|
|
180
|
+
entry[OLDER][NEWER] = undefined;
|
|
181
|
+
// link the newer entry to head
|
|
182
|
+
this.newest = entry[OLDER];
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
// if(entry[OLDER] === undefined && entry.newer === undefined) {
|
|
186
|
+
this.oldest = this.newest = undefined;
|
|
187
|
+
}
|
|
188
|
+
this.size--;
|
|
189
|
+
return entry.value;
|
|
190
|
+
}
|
|
191
|
+
clear() {
|
|
192
|
+
// Not clearing links should be safe, as we don't expose live links to user
|
|
193
|
+
this.oldest = this.newest = undefined;
|
|
194
|
+
this.size = 0;
|
|
195
|
+
this._keymap.clear();
|
|
196
|
+
}
|
|
197
|
+
keys() {
|
|
198
|
+
return new KeyIterator(this.oldest);
|
|
199
|
+
}
|
|
200
|
+
values() {
|
|
201
|
+
return new ValueIterator(this.oldest);
|
|
202
|
+
}
|
|
203
|
+
entries() { }
|
|
204
|
+
forEach(fun, thisObj) {
|
|
205
|
+
if (typeof thisObj !== 'object') {
|
|
206
|
+
thisObj = this;
|
|
207
|
+
}
|
|
208
|
+
let entry = this.oldest;
|
|
209
|
+
while (entry) {
|
|
210
|
+
fun.call(thisObj, entry.value, entry.key, this);
|
|
211
|
+
entry = entry[NEWER];
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
toJSON() {
|
|
215
|
+
const s = new Array(this.size);
|
|
216
|
+
let i = 0;
|
|
217
|
+
let entry = this.oldest;
|
|
218
|
+
while (entry) {
|
|
219
|
+
s[i++] = { key: entry.key, value: entry.value };
|
|
220
|
+
entry = entry[NEWER];
|
|
221
|
+
}
|
|
222
|
+
return s;
|
|
223
|
+
}
|
|
224
|
+
toString() {
|
|
225
|
+
let s = '';
|
|
226
|
+
let entry = this.oldest;
|
|
227
|
+
while (entry) {
|
|
228
|
+
s += String(entry.key) + ':' + entry.value;
|
|
229
|
+
entry = entry[NEWER];
|
|
230
|
+
if (entry) {
|
|
231
|
+
s += ' < ';
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return s;
|
|
235
|
+
}
|
|
236
|
+
[Symbol.iterator]() {
|
|
237
|
+
return new EntryIterator(this.oldest);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
class EntryIterator {
|
|
241
|
+
constructor(oldestEntry) {
|
|
242
|
+
this.entry = oldestEntry;
|
|
243
|
+
}
|
|
244
|
+
[Symbol.iterator]() {
|
|
245
|
+
return this;
|
|
246
|
+
}
|
|
247
|
+
next() {
|
|
248
|
+
const ent = this.entry;
|
|
249
|
+
if (ent) {
|
|
250
|
+
this.entry = ent[NEWER];
|
|
251
|
+
return { done: false, value: [ent.key, ent.value] };
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
return { done: true, value: undefined };
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
class KeyIterator {
|
|
259
|
+
constructor(oldestEntry) {
|
|
260
|
+
this.entry = oldestEntry;
|
|
261
|
+
}
|
|
262
|
+
[Symbol.iterator]() {
|
|
263
|
+
return this;
|
|
264
|
+
}
|
|
265
|
+
next() {
|
|
266
|
+
const ent = this.entry;
|
|
267
|
+
if (ent) {
|
|
268
|
+
this.entry = ent[NEWER];
|
|
269
|
+
return { done: false, value: ent.key };
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
return { done: true, value: undefined };
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
class ValueIterator {
|
|
277
|
+
constructor(oldestEntry) {
|
|
278
|
+
this.entry = oldestEntry;
|
|
279
|
+
}
|
|
280
|
+
[Symbol.iterator]() {
|
|
281
|
+
return this;
|
|
282
|
+
}
|
|
283
|
+
next() {
|
|
284
|
+
const ent = this.entry;
|
|
285
|
+
if (ent) {
|
|
286
|
+
this.entry = ent[NEWER];
|
|
287
|
+
return { done: false, value: ent.value };
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
return { done: true, value: undefined };
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
package/esm/observer.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export class LayoutObserver {
|
|
2
|
+
constructor(observer = () => { }) {
|
|
3
|
+
this.connected = false;
|
|
4
|
+
this.observe = (target) => {
|
|
5
|
+
this.resizeObserver.observe(target);
|
|
6
|
+
this.performanceObserver.observe({
|
|
7
|
+
entryTypes: ['paint', 'element', 'layout-shift', 'event'],
|
|
8
|
+
});
|
|
9
|
+
this.mutationObserver.observe(target, {
|
|
10
|
+
attributeFilter: ['style'],
|
|
11
|
+
attributes: true,
|
|
12
|
+
});
|
|
13
|
+
this.connected = true;
|
|
14
|
+
};
|
|
15
|
+
this.disconnect = () => {
|
|
16
|
+
if (this.connected) {
|
|
17
|
+
this.resizeObserver.disconnect();
|
|
18
|
+
this.performanceObserver.disconnect();
|
|
19
|
+
this.mutationObserver.disconnect();
|
|
20
|
+
}
|
|
21
|
+
this.connected = false;
|
|
22
|
+
};
|
|
23
|
+
this.resizeObserver = new ResizeObserver(() => observer());
|
|
24
|
+
this.performanceObserver = new PerformanceObserver(() => {
|
|
25
|
+
observer();
|
|
26
|
+
});
|
|
27
|
+
this.mutationObserver = new MutationObserver(() => observer());
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import 'requestidlecallback';
|
|
2
|
+
export interface IIdleDeadline {
|
|
3
|
+
didTimeout: boolean;
|
|
4
|
+
timeRemaining: () => DOMHighResTimeStamp;
|
|
5
|
+
}
|
|
6
|
+
export interface IdleCallbackOptions {
|
|
7
|
+
timeout?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare const requestIdle: (callback: (params: IIdleDeadline) => void, options?: IdleCallbackOptions) => number;
|
|
10
|
+
export declare const cancelIdle: (id: number) => void;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import 'requestidlecallback';
|
|
2
|
+
import { globalThisPolyfill } from './globalThisPolyfill';
|
|
3
|
+
export const requestIdle = (callback, options) => {
|
|
4
|
+
return globalThisPolyfill['requestIdleCallback'](callback, options);
|
|
5
|
+
};
|
|
6
|
+
export const cancelIdle = (id) => {
|
|
7
|
+
globalThisPolyfill['cancelIdleCallback'](id);
|
|
8
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { IPoint } from './coordinate';
|
|
2
|
+
export type ScrollDirection = 'begin' | 'end';
|
|
3
|
+
export interface IAutoScrollBasicInfo {
|
|
4
|
+
direction: ScrollDirection;
|
|
5
|
+
speedFactor: number;
|
|
6
|
+
speed: number;
|
|
7
|
+
}
|
|
8
|
+
export declare const calcAutoScrollBasicInfo: (point: IPoint, axis: "x" | "y", viewport: DOMRect, maxSpeed?: number) => IAutoScrollBasicInfo | null;
|
|
9
|
+
export declare const updateScrollValue: (element: HTMLElement | Window, axis: "x" | "y", value: number, callback?: (scrollValue: number) => void) => void;
|
|
10
|
+
export declare const scrollAnimate: (element: HTMLElement | Window, axis: "x" | "y", direction: "begin" | "end", speed: number, callback?: (scrollValue: number) => void) => () => void;
|
package/esm/scroller.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { createUniformSpeedAnimation, calcSpeedFactor } from './animation';
|
|
2
|
+
import { isFn, isWindow } from './types';
|
|
3
|
+
const MAX_SPEED = 80; // px/s
|
|
4
|
+
export const calcAutoScrollBasicInfo = (point, axis, viewport, maxSpeed = MAX_SPEED) => {
|
|
5
|
+
const { left, right, top, bottom } = viewport;
|
|
6
|
+
const { x, y } = point;
|
|
7
|
+
let begin;
|
|
8
|
+
let end;
|
|
9
|
+
let pos;
|
|
10
|
+
let speedFactor;
|
|
11
|
+
if (axis === 'x') {
|
|
12
|
+
begin = left;
|
|
13
|
+
end = right;
|
|
14
|
+
pos = x;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
begin = top;
|
|
18
|
+
end = bottom;
|
|
19
|
+
pos = y;
|
|
20
|
+
}
|
|
21
|
+
const scrollerSize = end - begin;
|
|
22
|
+
const moveDistance = scrollerSize > 400 ? 100 : scrollerSize / 3;
|
|
23
|
+
if (end - pos < moveDistance) {
|
|
24
|
+
return {
|
|
25
|
+
direction: 'end',
|
|
26
|
+
speedFactor,
|
|
27
|
+
speed: maxSpeed * calcSpeedFactor(end - pos, moveDistance),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
else if (pos - begin < moveDistance) {
|
|
31
|
+
return {
|
|
32
|
+
direction: 'begin',
|
|
33
|
+
speedFactor,
|
|
34
|
+
speed: maxSpeed * calcSpeedFactor(pos - begin, moveDistance),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
};
|
|
39
|
+
export const updateScrollValue = (element, axis, value, callback) => {
|
|
40
|
+
if (element) {
|
|
41
|
+
if (!isWindow(element)) {
|
|
42
|
+
if (axis === 'x') {
|
|
43
|
+
if (element.scrollLeft + value > element.scrollWidth)
|
|
44
|
+
return;
|
|
45
|
+
element.scrollLeft += value;
|
|
46
|
+
if (isFn(callback)) {
|
|
47
|
+
callback(element.scrollLeft);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
if (element.scrollTop + value > element.scrollHeight)
|
|
52
|
+
return;
|
|
53
|
+
element.scrollTop += value;
|
|
54
|
+
if (isFn(callback)) {
|
|
55
|
+
callback(element.scrollTop);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
if (axis === 'x') {
|
|
61
|
+
element.scrollBy({
|
|
62
|
+
left: value,
|
|
63
|
+
behavior: 'smooth',
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
element.scrollBy({
|
|
68
|
+
top: value,
|
|
69
|
+
behavior: 'smooth',
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
if (isFn(callback)) {
|
|
73
|
+
callback(value);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
export const scrollAnimate = (element, axis, direction, speed, callback) => {
|
|
79
|
+
return createUniformSpeedAnimation(speed, (delta) => {
|
|
80
|
+
updateScrollValue(element, axis, direction === 'begin' ? 0 - delta : delta, callback);
|
|
81
|
+
});
|
|
82
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
declare const UNSUBSCRIBE_ID_SYMBOL: unique symbol;
|
|
2
|
+
export interface ISubscriber<Payload = any> {
|
|
3
|
+
(payload: Payload): void | boolean;
|
|
4
|
+
}
|
|
5
|
+
export declare class Subscribable<ExtendsType = any> {
|
|
6
|
+
private subscribers;
|
|
7
|
+
dispatch<T extends ExtendsType = any>(event: T, context?: any): boolean;
|
|
8
|
+
subscribe(subscriber: ISubscriber): {
|
|
9
|
+
(): void;
|
|
10
|
+
[UNSUBSCRIBE_ID_SYMBOL]: number;
|
|
11
|
+
};
|
|
12
|
+
unsubscribe: (id?: number | string | (() => void)) => void;
|
|
13
|
+
}
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { isFn } from './types';
|
|
2
|
+
const UNSUBSCRIBE_ID_SYMBOL = Symbol('UNSUBSCRIBE_ID_SYMBOL');
|
|
3
|
+
export class Subscribable {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.subscribers = {
|
|
6
|
+
index: 0,
|
|
7
|
+
};
|
|
8
|
+
this.unsubscribe = (id) => {
|
|
9
|
+
if (id === undefined || id === null) {
|
|
10
|
+
for (const key in this.subscribers) {
|
|
11
|
+
this.unsubscribe(key);
|
|
12
|
+
}
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (!isFn(id)) {
|
|
16
|
+
delete this.subscribers[id];
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
delete this.subscribers[id[UNSUBSCRIBE_ID_SYMBOL]];
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
dispatch(event, context) {
|
|
24
|
+
let interrupted = false;
|
|
25
|
+
for (const key in this.subscribers) {
|
|
26
|
+
if (isFn(this.subscribers[key])) {
|
|
27
|
+
event['context'] = context;
|
|
28
|
+
if (this.subscribers[key](event) === false) {
|
|
29
|
+
interrupted = true;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return interrupted ? false : true;
|
|
34
|
+
}
|
|
35
|
+
subscribe(subscriber) {
|
|
36
|
+
let id;
|
|
37
|
+
if (isFn(subscriber)) {
|
|
38
|
+
id = this.subscribers.index + 1;
|
|
39
|
+
this.subscribers[id] = subscriber;
|
|
40
|
+
this.subscribers.index++;
|
|
41
|
+
}
|
|
42
|
+
const unsubscribe = () => {
|
|
43
|
+
this.unsubscribe(id);
|
|
44
|
+
};
|
|
45
|
+
unsubscribe[UNSUBSCRIBE_ID_SYMBOL] = id;
|
|
46
|
+
return unsubscribe;
|
|
47
|
+
}
|
|
48
|
+
}
|
package/esm/types.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const getType: (obj: any) => any;
|
|
2
|
+
export declare const isFn: (obj: unknown) => obj is (...args: any[]) => any;
|
|
3
|
+
export declare const isWindow: (obj: unknown) => obj is Window;
|
|
4
|
+
export declare const isHTMLElement: (obj: any) => obj is HTMLElement;
|
|
5
|
+
export declare const isArr: (arg: any) => arg is any[];
|
|
6
|
+
export declare const isPlainObj: (obj: unknown) => obj is object;
|
|
7
|
+
export declare const isStr: (obj: unknown) => obj is string;
|
|
8
|
+
export declare const isBool: (obj: unknown) => obj is boolean;
|
|
9
|
+
export declare const isNum: (obj: unknown) => obj is number;
|
|
10
|
+
export declare const isObj: (val: unknown) => val is object;
|
|
11
|
+
export declare const isRegExp: (obj: unknown) => obj is RegExp;
|
|
12
|
+
export declare const isValid: (val: any) => boolean;
|
|
13
|
+
export declare const isValidNumber: (val: any) => val is number;
|
package/esm/types.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const isType = (type) => (obj) => obj != null &&
|
|
2
|
+
(Array.isArray(type) ? type : [type]).some((t) => getType(obj) === `[object ${t}]`);
|
|
3
|
+
export const getType = (obj) => Object.prototype.toString.call(obj);
|
|
4
|
+
export const isFn = isType([
|
|
5
|
+
'Function',
|
|
6
|
+
'AsyncFunction',
|
|
7
|
+
'GeneratorFunction',
|
|
8
|
+
]);
|
|
9
|
+
export const isWindow = isType('Window');
|
|
10
|
+
export const isHTMLElement = (obj) => {
|
|
11
|
+
return (obj === null || obj === void 0 ? void 0 : obj['nodeName']) || (obj === null || obj === void 0 ? void 0 : obj['tagName']);
|
|
12
|
+
};
|
|
13
|
+
export const isArr = Array.isArray;
|
|
14
|
+
export const isPlainObj = isType('Object');
|
|
15
|
+
export const isStr = isType('String');
|
|
16
|
+
export const isBool = isType('Boolean');
|
|
17
|
+
export const isNum = isType('Number');
|
|
18
|
+
export const isObj = (val) => typeof val === 'object';
|
|
19
|
+
export const isRegExp = isType('RegExp');
|
|
20
|
+
export const isValid = (val) => val !== null && val !== undefined;
|
|
21
|
+
export const isValidNumber = (val) => !isNaN(val) && isNum(val);
|
package/esm/uid.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function uid(len?: number): string;
|
package/esm/uid.js
ADDED
package/lib/animation.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calcSpeedFactor = exports.createUniformSpeedAnimation = void 0;
|
|
4
|
+
const createUniformSpeedAnimation = (speed = 10, callback) => {
|
|
5
|
+
let request = null;
|
|
6
|
+
let startTime = null;
|
|
7
|
+
const start = () => {
|
|
8
|
+
if (request)
|
|
9
|
+
return;
|
|
10
|
+
request = requestAnimationFrame((timestamp) => {
|
|
11
|
+
if (startTime === null) {
|
|
12
|
+
startTime = timestamp;
|
|
13
|
+
}
|
|
14
|
+
const deltaTime = timestamp - startTime;
|
|
15
|
+
const delta = (deltaTime / 1000) * speed;
|
|
16
|
+
callback(delta);
|
|
17
|
+
request = null;
|
|
18
|
+
start();
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
start();
|
|
22
|
+
return () => {
|
|
23
|
+
if (request) {
|
|
24
|
+
cancelAnimationFrame(request);
|
|
25
|
+
request = null;
|
|
26
|
+
}
|
|
27
|
+
startTime = null;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
exports.createUniformSpeedAnimation = createUniformSpeedAnimation;
|
|
31
|
+
//越接近阈值,速度越小,越远离阈值,速度越大
|
|
32
|
+
const calcSpeedFactor = (delta = 0, threshold = Infinity) => {
|
|
33
|
+
if (threshold >= delta) {
|
|
34
|
+
return (threshold - delta) / threshold;
|
|
35
|
+
}
|
|
36
|
+
return 0;
|
|
37
|
+
};
|
|
38
|
+
exports.calcSpeedFactor = calcSpeedFactor;
|