@douyinfe/semi-foundation 2.20.0-beta.0 → 2.20.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.
Files changed (57) hide show
  1. package/calendar/foundation.ts +1 -2
  2. package/datePicker/foundation.ts +6 -5
  3. package/datePicker/monthsGridFoundation.ts +6 -6
  4. package/datePicker/yearAndMonthFoundation.ts +2 -2
  5. package/form/form.scss +14 -0
  6. package/image/image.scss +3 -0
  7. package/image/{imageFoundation.tsx → imageFoundation.ts} +0 -0
  8. package/image/{previewFooterFoundation.tsx → previewFooterFoundation.ts} +0 -0
  9. package/image/{previewFoundation.tsx → previewFoundation.ts} +0 -0
  10. package/image/{previewImageFoundation.tsx → previewImageFoundation.ts} +5 -5
  11. package/image/{previewInnerFoundation.tsx → previewInnerFoundation.ts} +5 -1
  12. package/image/utils.ts +5 -3
  13. package/lib/cjs/calendar/foundation.d.ts +2 -1
  14. package/lib/cjs/datePicker/foundation.d.ts +4 -4
  15. package/lib/cjs/datePicker/monthsGridFoundation.d.ts +2 -3
  16. package/lib/cjs/datePicker/yearAndMonthFoundation.d.ts +2 -3
  17. package/lib/cjs/form/form.css +8 -0
  18. package/lib/cjs/form/form.scss +14 -0
  19. package/lib/cjs/image/image.css +3 -0
  20. package/lib/cjs/image/image.scss +3 -0
  21. package/lib/cjs/image/imageFoundation.d.ts +11 -0
  22. package/lib/cjs/image/imageFoundation.js +89 -0
  23. package/lib/cjs/image/previewFooterFoundation.d.ts +10 -0
  24. package/lib/cjs/image/previewFooterFoundation.js +69 -0
  25. package/lib/cjs/image/previewFoundation.d.ts +5 -0
  26. package/lib/cjs/image/previewFoundation.js +49 -0
  27. package/lib/cjs/image/previewImageFoundation.d.ts +58 -0
  28. package/lib/cjs/image/previewImageFoundation.js +367 -0
  29. package/lib/cjs/image/previewInnerFoundation.d.ts +44 -0
  30. package/lib/cjs/image/previewInnerFoundation.js +341 -0
  31. package/lib/cjs/image/utils.js +5 -5
  32. package/lib/cjs/navigation/foundation.d.ts +0 -1
  33. package/lib/cjs/navigation/foundation.js +1 -11
  34. package/lib/es/calendar/foundation.d.ts +2 -1
  35. package/lib/es/datePicker/foundation.d.ts +4 -4
  36. package/lib/es/datePicker/monthsGridFoundation.d.ts +2 -3
  37. package/lib/es/datePicker/yearAndMonthFoundation.d.ts +2 -3
  38. package/lib/es/form/form.css +8 -0
  39. package/lib/es/form/form.scss +14 -0
  40. package/lib/es/image/image.css +3 -0
  41. package/lib/es/image/image.scss +3 -0
  42. package/lib/es/image/imageFoundation.d.ts +11 -0
  43. package/lib/es/image/imageFoundation.js +76 -0
  44. package/lib/es/image/previewFooterFoundation.d.ts +10 -0
  45. package/lib/es/image/previewFooterFoundation.js +57 -0
  46. package/lib/es/image/previewFoundation.d.ts +5 -0
  47. package/lib/es/image/previewFoundation.js +37 -0
  48. package/lib/es/image/previewImageFoundation.d.ts +58 -0
  49. package/lib/es/image/previewImageFoundation.js +352 -0
  50. package/lib/es/image/previewInnerFoundation.d.ts +44 -0
  51. package/lib/es/image/previewInnerFoundation.js +326 -0
  52. package/lib/es/image/utils.js +5 -5
  53. package/lib/es/navigation/foundation.d.ts +0 -1
  54. package/lib/es/navigation/foundation.js +1 -11
  55. package/navigation/foundation.ts +2 -9
  56. package/package.json +2 -2
  57. package/tree/treeUtil.ts +1 -1
@@ -0,0 +1,326 @@
1
+ import BaseFoundation from "../base/foundation";
2
+ import KeyCode from "../utils/keyCode";
3
+ import { getPreloadImagArr, downloadImage, isTargetEmit } from "./utils";
4
+ const NOT_CLOSE_TARGETS = ["icon", "footer"];
5
+ const STOP_CLOSE_TARGET = ["icon", "footer", "header"];
6
+ export default class PreviewInnerFoundation extends BaseFoundation {
7
+ constructor(adapter) {
8
+ super(Object.assign({}, adapter));
9
+
10
+ this.handleViewVisibleChange = () => {
11
+ const nowTime = new Date().getTime();
12
+
13
+ const mouseActiveTime = this._adapter.getMouseActiveTime();
14
+
15
+ const stopTiming = this._adapter.getStopTiming();
16
+
17
+ const {
18
+ viewerVisibleDelay
19
+ } = this.getProps();
20
+ const {
21
+ viewerVisible
22
+ } = this.getStates();
23
+
24
+ if (nowTime - mouseActiveTime > viewerVisibleDelay && !stopTiming) {
25
+ viewerVisible && this.setState({
26
+ viewerVisible: false
27
+ });
28
+ }
29
+ };
30
+
31
+ this.handleMouseMoveEvent = (e, event) => {
32
+ const isTarget = isTargetEmit(e.nativeEvent, STOP_CLOSE_TARGET);
33
+
34
+ if (isTarget && event === "over") {
35
+ this._adapter.setStopTiming(true);
36
+ } else if (isTarget && event === "out") {
37
+ this._adapter.setStopTiming(false);
38
+ }
39
+ };
40
+
41
+ this.handleMouseMove = e => {
42
+ this._adapter.setMouseActiveTime(new Date().getTime());
43
+
44
+ this.setState({
45
+ viewerVisible: true
46
+ });
47
+ };
48
+
49
+ this.handleMouseUp = e => {
50
+ const {
51
+ maskClosable
52
+ } = this.getProps();
53
+ let couldClose = !isTargetEmit(e.nativeEvent, NOT_CLOSE_TARGETS);
54
+ const {
55
+ clientX,
56
+ clientY
57
+ } = e;
58
+
59
+ const {
60
+ x,
61
+ y
62
+ } = this._adapter.getStartMouseDown(); // 对鼠标移动做容错处理,当 x 和 y 方向在 mouseUp 的时候移动距离都小于等于 5px 时候就可以关闭预览
63
+ // Error-tolerant processing of mouse movement, when the movement distance in the x and y directions is less than or equal to 5px in mouseUp, the preview can be closed
64
+ // 不做容错处理的话,直接用 clientX !== x || y !== clientY 做判断,鼠标在用户点击时候无意识的轻微移动无法关闭预览,不符合用户预期
65
+ // If you do not do fault-tolerant processing, but directly use clientX !== x || y !== clientY to make judgments, the slight movement of the mouse when the user clicks will not be able to close the preview, which does not meet the user's expectations.
66
+
67
+
68
+ if (Math.abs(clientX - x) > 5 || Math.abs(y - clientY) > 5) {
69
+ couldClose = false;
70
+ }
71
+
72
+ if (couldClose && maskClosable) {
73
+ this.handlePreviewClose();
74
+ }
75
+ };
76
+
77
+ this.handleMouseDown = e => {
78
+ const {
79
+ clientX,
80
+ clientY
81
+ } = e;
82
+
83
+ this._adapter.setStartMouseDown(clientX, clientY);
84
+ };
85
+
86
+ this.handleKeyDown = e => {
87
+ const {
88
+ closeOnEsc
89
+ } = this.getProps();
90
+
91
+ if (closeOnEsc && e.keyCode === KeyCode.ESC) {
92
+ e.stopPropagation();
93
+
94
+ this._adapter.notifyVisibleChange(false);
95
+
96
+ this._adapter.notifyClose();
97
+
98
+ return;
99
+ }
100
+ };
101
+
102
+ this.handleSwitchImage = direction => {
103
+ const step = direction === "prev" ? -1 : 1;
104
+ const {
105
+ imgSrc,
106
+ currentIndex: currentIndexInState
107
+ } = this.getStates();
108
+ const srcLength = imgSrc.length;
109
+ const newIndex = (currentIndexInState + step + srcLength) % srcLength;
110
+
111
+ if ("currentIndex" in this.getProps()) {
112
+ if (this._adapter.getIsInGroup()) {
113
+ const setCurrentIndex = this._adapter.getContext("setCurrentIndex");
114
+
115
+ setCurrentIndex(newIndex);
116
+ }
117
+ } else {
118
+ this.setState({
119
+ currentIndex: newIndex
120
+ });
121
+ }
122
+
123
+ this._adapter.notifyChange(newIndex);
124
+
125
+ this.setState({
126
+ direction,
127
+ rotation: 0
128
+ });
129
+
130
+ this._adapter.notifyRotateChange(0);
131
+ };
132
+
133
+ this.handleDownload = () => {
134
+ const {
135
+ currentIndex,
136
+ imgSrc
137
+ } = this.getStates();
138
+ const downloadSrc = imgSrc[currentIndex];
139
+ const downloadName = downloadSrc.slice(downloadSrc.lastIndexOf("/") + 1);
140
+ downloadImage(downloadSrc, downloadName);
141
+
142
+ this._adapter.notifyDownload(downloadSrc, currentIndex);
143
+ };
144
+
145
+ this.handlePreviewClose = () => {
146
+ this._adapter.notifyVisibleChange(false);
147
+
148
+ this._adapter.notifyClose();
149
+ };
150
+
151
+ this.handleAdjustRatio = type => {
152
+ this.setState({
153
+ ratio: type
154
+ });
155
+
156
+ this._adapter.notifyRatioChange(type);
157
+ };
158
+
159
+ this.handleRotateImage = direction => {
160
+ const {
161
+ rotation
162
+ } = this.getStates();
163
+ const newRotation = rotation + (direction === "left" ? 90 : -90);
164
+ this.setState({
165
+ rotation: newRotation
166
+ });
167
+
168
+ this._adapter.notifyRotateChange(newRotation);
169
+ };
170
+
171
+ this.handleZoomImage = newZoom => {
172
+ const {
173
+ zoom
174
+ } = this.getStates();
175
+
176
+ this._adapter.notifyZoom(newZoom, newZoom > zoom);
177
+
178
+ this.setState({
179
+ zoom: newZoom
180
+ });
181
+ }; // 当 visible 改变之后,预览组件完成首张图片加载后,启动预加载
182
+ // 如: 1,2,3,4,5,6,7,8张图片, 点击第 4 张图片,preLoadGap 为 2
183
+ // 当 visible 从 false 变为 true ,首先加载第 4 张图片,当第 4 张图片加载完成后,
184
+ // 再按照 5,3,6,2的顺序预先加载这几张图片
185
+ // When visible changes, the preview component finishes loading the first image and starts preloading
186
+ // Such as: 1, 2, 3, 4, 5, 6, 7, 8 pictures, click the 4th picture, preLoadGap is 2
187
+ // When visible changes from false to true , load the 4th image first, when the 4th image is loaded,
188
+ // Preload these pictures in the order of 5, 3, 6, 2
189
+
190
+
191
+ this.preloadGapImage = () => {
192
+ const {
193
+ preLoad,
194
+ preLoadGap,
195
+ infinite,
196
+ currentIndex
197
+ } = this.getProps();
198
+ const {
199
+ imgSrc
200
+ } = this.getStates();
201
+
202
+ if (!preLoad || typeof preLoadGap !== "number" || preLoadGap < 1) {
203
+ return;
204
+ }
205
+
206
+ const preloadImages = getPreloadImagArr(imgSrc, currentIndex, preLoadGap, infinite);
207
+ const Img = new Image();
208
+ let index = 0;
209
+
210
+ function callback(e) {
211
+ index++;
212
+
213
+ if (index < preloadImages.length) {
214
+ Img.src = preloadImages[index];
215
+ }
216
+ }
217
+
218
+ Img.onload = e => {
219
+ this.setLoadSuccessStatus(Img.src);
220
+ callback(e);
221
+ };
222
+
223
+ Img.onerror = callback;
224
+ Img.src = preloadImages[0];
225
+ }; // 在切换左右图片时,当被切换图片完成加载后,根据方向决定下一个预加载的图片
226
+ // 如: 1,2,3,4,5,6,7,8张图片
227
+ // 当 preLoadGap 为 2, 从第 5 张图片进行切换
228
+ // - 如果向 右 切换到第 6 张,则第 6 张图片加载动作结束后(无论加载成功 or 失败),会预先加载第 8 张;
229
+ // - 如果向 左 切换到第 4 张,则第 4 张图片加载动作结束后(无论加载成功 or 失败),会预先加载第 2 张;
230
+ // When switching the left and right pictures, when the switched picture is loaded, the next preloaded picture is determined according to the direction
231
+ // Such as: 1, 2, 3, 4, 5, 6, 7, 8 pictures
232
+ // When preLoadGap is 2, switch from the 5th picture
233
+ // - If you switch to the 6th image(direction is next), the 8th image will be preloaded after the 6th image is loaded (whether it succeeds or fails to load);
234
+ // - If you switch to the 4th image(direction is prev), the second image will be preloaded after the 4th image is loaded (whether it succeeds or fails to load);
235
+
236
+
237
+ this.preloadSingleImage = () => {
238
+ const {
239
+ preLoad,
240
+ preLoadGap,
241
+ infinite
242
+ } = this.getProps();
243
+ const {
244
+ imgSrc,
245
+ currentIndex,
246
+ direction,
247
+ imgLoadStatus
248
+ } = this.getStates();
249
+
250
+ if (!preLoad || typeof preLoadGap !== "number" || preLoadGap < 1) {
251
+ return;
252
+ } // 根据方向决定preload那个index
253
+ // Determine the index of preload according to the direction
254
+
255
+
256
+ let preloadIndex = currentIndex + (direction === "prev" ? -1 : 1) * preLoadGap;
257
+
258
+ if (preloadIndex < 0 || preloadIndex >= imgSrc.length) {
259
+ if (infinite) {
260
+ preloadIndex = (preloadIndex + imgSrc.length) % imgSrc.length;
261
+ } else {
262
+ return;
263
+ }
264
+ } // 如果图片没有加载成功过,则进行预加载
265
+ // If the image has not been loaded successfully, preload it
266
+
267
+
268
+ if (!imgLoadStatus[preloadIndex]) {
269
+ const Img = new Image();
270
+
271
+ Img.onload = e => {
272
+ this.setLoadSuccessStatus(imgSrc[preloadIndex]);
273
+ };
274
+
275
+ Img.src = imgSrc[preloadIndex];
276
+ }
277
+ };
278
+
279
+ this.setLoadSuccessStatus = src => {
280
+ const {
281
+ imgLoadStatus
282
+ } = this.getStates();
283
+ const status = Object.assign({}, imgLoadStatus);
284
+ status[src] = true;
285
+ this.setState({
286
+ imgLoadStatus: status
287
+ });
288
+ };
289
+
290
+ this.onImageLoad = src => {
291
+ const {
292
+ preloadAfterVisibleChange
293
+ } = this.getStates();
294
+ this.setLoadSuccessStatus(src); // 当 preview 中当前加载的图片加载完成后,
295
+ // 如果是在visible change之后的第一次加载,则启动加载该currentIndex左右preloadGap范围的图片
296
+ // 如果是非第一次加载,是在左右切换图片,则根据方向预先加载单张图片
297
+ // When the currently loaded image in Preview is loaded,
298
+ // - It is the first load after visible change, start loading the images in the preloadGap range around the currentIndex
299
+ // - It is not the first load, the image is switched left and right, and a single image is preloaded according to the direction
300
+
301
+ if (preloadAfterVisibleChange) {
302
+ this.preloadGapImage();
303
+ this.setState({
304
+ preloadAfterVisibleChange: false
305
+ });
306
+ } else {
307
+ this.preloadSingleImage();
308
+ }
309
+ };
310
+ }
311
+
312
+ beforeShow() {
313
+ this._adapter.registerKeyDownListener();
314
+ }
315
+
316
+ afterHide() {
317
+ this._adapter.unregisterKeyDownListener();
318
+ }
319
+
320
+ handleRatio(type) {
321
+ this.setState({
322
+ ratio: type
323
+ });
324
+ }
325
+
326
+ }
@@ -1,9 +1,9 @@
1
1
  export const isTargetEmit = (event, targetClasses) => {
2
- var _a; // e.path is the event-triggered bubbling path, which stores each node through which bubbling passes.
3
- // e.path.length-4 is to remove elements above the root node, such as body, html, document, window
4
-
5
-
6
- const isTarget = (_a = event === null || event === void 0 ? void 0 : event.path) === null || _a === void 0 ? void 0 : _a.slice(0, event.path.length - 4).some(node => {
2
+ // event.path usage is discouraged, use event.composedPath() as it's standard and is more future-proof
3
+ // path is the event-triggered bubbling path, which stores each node through which bubbling passes.
4
+ // path.length-4 is to remove elements above the root node, such as body, html, document, window
5
+ const path = event === null || event === void 0 ? void 0 : event.composedPath();
6
+ const isTarget = path === null || path === void 0 ? void 0 : path.slice(0, path.length - 4).some(node => {
7
7
  if (node.className && typeof node.className === "string") {
8
8
  return targetClasses.some(c => node.className.includes(c));
9
9
  }
@@ -74,7 +74,6 @@ export default class NavigationFoundation<P = Record<string, any>, S = Record<st
74
74
  * @param {*} itemKeysMap
75
75
  */
76
76
  getWillOpenKeys(itemKeysMap: ItemKey2ParentKeysMap): any[];
77
- getItemKey(item: string | number, keyPropName?: string): string | number;
78
77
  getShouldOpenKeys(itemKeysMap?: ItemKey2ParentKeysMap, selectedKeys?: string | number[]): unknown[];
79
78
  destroy(): void;
80
79
  selectLevelZeroParentKeys(itemKeysMap: ItemKey2ParentKeysMap, ...itemKeys: (string | number)[]): any[];
@@ -173,16 +173,6 @@ export default class NavigationFoundation extends BaseFoundation {
173
173
  return [...willOpenKeys];
174
174
  }
175
175
 
176
- getItemKey(item) {
177
- let keyPropName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'itemKey';
178
-
179
- if (item && typeof item === 'object') {
180
- return item[keyPropName];
181
- }
182
-
183
- return item;
184
- }
185
-
186
176
  getShouldOpenKeys() {
187
177
  let itemKeysMap = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
188
178
  let selectedKeys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
@@ -191,7 +181,7 @@ export default class NavigationFoundation extends BaseFoundation {
191
181
  if (Array.isArray(selectedKeys) && selectedKeys.length) {
192
182
  selectedKeys.forEach(item => {
193
183
  if (item) {
194
- const parentKeys = _get(itemKeysMap, this.getItemKey(item));
184
+ const parentKeys = _get(itemKeysMap, item);
195
185
 
196
186
  if (Array.isArray(parentKeys)) {
197
187
  parentKeys.forEach(k => willOpenKeySet.add(k));
@@ -181,20 +181,13 @@ export default class NavigationFoundation<P = Record<string, any>, S = Record<st
181
181
  return [...willOpenKeys];
182
182
  }
183
183
 
184
- getItemKey(item: string | number, keyPropName = 'itemKey') {
185
- if (item && typeof item === 'object') {
186
- return item[keyPropName];
187
- }
188
- return item;
189
- }
190
-
191
- getShouldOpenKeys(itemKeysMap: ItemKey2ParentKeysMap = {}, selectedKeys: string | number[] = []) {
184
+ getShouldOpenKeys(itemKeysMap: ItemKey2ParentKeysMap = {}, selectedKeys: string | number[]= []) {
192
185
  const willOpenKeySet = new Set();
193
186
 
194
187
  if (Array.isArray(selectedKeys) && selectedKeys.length) {
195
188
  selectedKeys.forEach(item => {
196
189
  if (item) {
197
- const parentKeys = get(itemKeysMap, this.getItemKey(item));
190
+ const parentKeys = get(itemKeysMap, item);
198
191
 
199
192
  if (Array.isArray(parentKeys)) {
200
193
  parentKeys.forEach(k => willOpenKeySet.add(k));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@douyinfe/semi-foundation",
3
- "version": "2.20.0-beta.0",
3
+ "version": "2.20.0",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build:lib": "node ./scripts/compileLib.js",
@@ -23,7 +23,7 @@
23
23
  "*.scss",
24
24
  "*.css"
25
25
  ],
26
- "gitHead": "9b2c94bd0f089928bff5ec1edc0e08d116324107",
26
+ "gitHead": "09833ac091bb7caf6d1ed2199e8fc528ba7510c2",
27
27
  "devDependencies": {
28
28
  "@babel/plugin-transform-runtime": "^7.15.8",
29
29
  "@babel/preset-env": "^7.15.8",
package/tree/treeUtil.ts CHANGED
@@ -314,7 +314,7 @@ export function calcCheckedKeys(values: any, keyEntities: KeyEntities) {
314
314
  let halfCheckedKeys = new Set([]);
315
315
  let visited: any[] = [];
316
316
 
317
- const levelMap = getSortedKeyList(keyList, keyEntities);
317
+ const levelMap:{[key: number]: string[]} = getSortedKeyList(keyList, keyEntities);
318
318
 
319
319
  const calcCurrLevel = (node: any) => {
320
320
  const { key, parent, level } = node;