@douyinfe/semi-foundation 2.49.0-beta.0 → 2.49.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.
@@ -4,10 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _isUndefined2 = _interopRequireDefault(require("lodash/isUndefined"));
8
- var _throttle2 = _interopRequireDefault(require("lodash/throttle"));
9
7
  var _foundation = _interopRequireDefault(require("../base/foundation"));
10
- var _a11y = require("../utils/a11y");
11
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
9
  const DefaultDOMRect = {
13
10
  bottom: 0,
@@ -23,6 +20,12 @@ const DefaultDOMRect = {
23
20
  class PreviewImageFoundation extends _foundation.default {
24
21
  constructor(adapter) {
25
22
  super(Object.assign({}, adapter));
23
+ this.startMouseOffset = {
24
+ x: 0,
25
+ y: 0
26
+ };
27
+ this.originImageWidth = null;
28
+ this.originImageHeight = null;
26
29
  this._isImageVertical = () => this.getProp("rotation") % 180 !== 0;
27
30
  this._getImageBounds = () => {
28
31
  const imageDOM = this._adapter.getImage();
@@ -52,32 +55,18 @@ class PreviewImageFoundation extends _foundation.default {
52
55
  this._adapter.setLoading(loading);
53
56
  };
54
57
  this.handleWindowResize = () => {
55
- const {
56
- ratio,
57
- setRatio
58
- } = this.getProps();
59
- const {
60
- originImageWidth,
61
- originImageHeight
62
- } = this._adapter.getOriginImageSize();
63
- if (originImageWidth && originImageHeight) {
64
- if (ratio !== "adaptation") {
65
- setRatio("adaptation");
66
- } else {
67
- this.handleResizeImage();
68
- }
58
+ if (this.originImageWidth && this.originImageHeight) {
59
+ this.handleResizeImage();
69
60
  }
70
61
  };
71
62
  this.handleLoad = e => {
72
63
  if (e.target) {
73
64
  const {
74
- width: w,
75
- height: h
65
+ naturalWidth: w,
66
+ naturalHeight: h
76
67
  } = e.target;
77
- this._adapter.setOriginImageSize({
78
- originImageWidth: w,
79
- originImageHeight: h
80
- });
68
+ this.originImageHeight = h;
69
+ this.originImageWidth = w;
81
70
  this.setState({
82
71
  loading: false
83
72
  });
@@ -102,13 +91,14 @@ class PreviewImageFoundation extends _foundation.default {
102
91
  this.handleResizeImage = () => {
103
92
  const horizontal = !this._isImageVertical();
104
93
  const {
105
- originImageWidth,
106
- originImageHeight
107
- } = this._adapter.getOriginImageSize();
108
- const imgWidth = horizontal ? originImageWidth : originImageHeight;
109
- const imgHeight = horizontal ? originImageHeight : originImageWidth;
94
+ currZoom
95
+ } = this.getStates();
96
+ const imgWidth = horizontal ? this.originImageWidth : this.originImageHeight;
97
+ const imgHeight = horizontal ? this.originImageHeight : this.originImageWidth;
110
98
  const {
111
- onZoom
99
+ onZoom,
100
+ setRatio,
101
+ ratio
112
102
  } = this.getProps();
113
103
  const containerDOM = this._adapter.getContainer();
114
104
  if (containerDOM) {
@@ -118,8 +108,44 @@ class PreviewImageFoundation extends _foundation.default {
118
108
  } = this._getContainerBounds();
119
109
  const reservedWidth = containerWidth - 80;
120
110
  const reservedHeight = containerHeight - 80;
121
- const _zoom = Number(Math.min(reservedWidth / imgWidth, reservedHeight / imgHeight).toFixed(2));
122
- onZoom(_zoom);
111
+ let _zoom = 1;
112
+ if (imgWidth > reservedWidth || imgHeight > reservedHeight) {
113
+ _zoom = Number(Math.min(reservedWidth / imgWidth, reservedHeight / imgHeight).toFixed(2));
114
+ }
115
+ if (currZoom === _zoom) {
116
+ this.calculatePreviewImage(_zoom, null);
117
+ } else {
118
+ onZoom(_zoom);
119
+ }
120
+ }
121
+ };
122
+ this.handleRatioChange = () => {
123
+ if (this.originImageWidth && this.originImageHeight) {
124
+ const {
125
+ currZoom
126
+ } = this.getStates();
127
+ const {
128
+ ratio,
129
+ onZoom
130
+ } = this.getProps();
131
+ let _zoom;
132
+ if (ratio === 'adaptation') {
133
+ const horizontal = !this._isImageVertical();
134
+ const imgWidth = horizontal ? this.originImageWidth : this.originImageHeight;
135
+ const imgHeight = horizontal ? this.originImageHeight : this.originImageWidth;
136
+ const {
137
+ width: containerWidth,
138
+ height: containerHeight
139
+ } = this._getContainerBounds();
140
+ const reservedWidth = containerWidth - 80;
141
+ const reservedHeight = containerHeight - 80;
142
+ _zoom = Number(Math.min(reservedWidth / imgWidth, reservedHeight / imgHeight).toFixed(2));
143
+ } else {
144
+ _zoom = 1;
145
+ }
146
+ if (currZoom !== _zoom) {
147
+ onZoom(_zoom);
148
+ }
123
149
  }
124
150
  };
125
151
  this.handleRightClickImage = e => {
@@ -134,38 +160,6 @@ class PreviewImageFoundation extends _foundation.default {
134
160
  return true;
135
161
  }
136
162
  };
137
- // e: WheelEvent<HTMLImageElement>
138
- this.handleWheel = e => {
139
- this.onWheel(e);
140
- (0, _a11y.handlePrevent)(e);
141
- };
142
- // e: WheelEvent<HTMLImageElement>
143
- this.onWheel = (0, _throttle2.default)(e => {
144
- const {
145
- onZoom,
146
- zoomStep,
147
- maxZoom,
148
- minZoom
149
- } = this.getProps();
150
- const {
151
- currZoom
152
- } = this.getStates();
153
- let _zoom;
154
- if (e.deltaY < 0) {
155
- /* zoom in */
156
- if (currZoom + zoomStep <= maxZoom) {
157
- _zoom = Number((currZoom + zoomStep).toFixed(2));
158
- }
159
- } else if (e.deltaY > 0) {
160
- /* zoom out */
161
- if (currZoom - zoomStep >= minZoom) {
162
- _zoom = Number((currZoom - zoomStep).toFixed(2));
163
- }
164
- }
165
- if (!(0, _isUndefined2.default)(_zoom)) {
166
- onZoom(_zoom);
167
- }
168
- }, 50);
169
163
  this.calcCanDragDirection = () => {
170
164
  const {
171
165
  width,
@@ -189,12 +183,8 @@ class PreviewImageFoundation extends _foundation.default {
189
183
  canDragHorizontal
190
184
  };
191
185
  };
192
- this.handleZoomChange = (newZoom, e) => {
186
+ this.calculatePreviewImage = (newZoom, e) => {
193
187
  const imageDOM = this._adapter.getImage();
194
- const {
195
- originImageWidth,
196
- originImageHeight
197
- } = this._adapter.getOriginImageSize();
198
188
  const {
199
189
  canDragVertical,
200
190
  canDragHorizontal
@@ -204,8 +194,8 @@ class PreviewImageFoundation extends _foundation.default {
204
194
  width: containerWidth,
205
195
  height: containerHeight
206
196
  } = this._getContainerBounds();
207
- const newWidth = Math.floor(originImageWidth * newZoom);
208
- const newHeight = Math.floor(originImageHeight * newZoom);
197
+ const newWidth = Math.floor(this.originImageWidth * newZoom);
198
+ const newHeight = Math.floor(this.originImageHeight * newZoom);
209
199
  // debugger;
210
200
  let _offset;
211
201
  const horizontal = !this._isImageVertical();
@@ -264,13 +254,13 @@ class PreviewImageFoundation extends _foundation.default {
264
254
  width,
265
255
  height
266
256
  } = this.getStates();
267
- const startMouseMove = this._adapter.getMouseMove();
268
- const startMouseOffset = this._adapter.getMouseOffset();
269
257
  const {
270
258
  canDragVertical,
271
259
  canDragHorizontal
272
260
  } = this.calcCanDragDirection();
273
- if (startMouseMove && (canDragVertical || canDragHorizontal)) {
261
+ // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
262
+ const mouseLeftPress = e.buttons === 1;
263
+ if (mouseLeftPress && (canDragVertical || canDragHorizontal)) {
274
264
  const {
275
265
  clientX,
276
266
  clientY
@@ -283,8 +273,8 @@ class PreviewImageFoundation extends _foundation.default {
283
273
  left: extremeLeft,
284
274
  top: extremeTop
285
275
  } = this.calcExtremeBounds();
286
- let newX = canDragHorizontal ? clientX - containerLeft - startMouseOffset.x : offset.x;
287
- let newY = canDragVertical ? clientY - containerTop - startMouseOffset.y : offset.y;
276
+ let newX = canDragHorizontal ? clientX - containerLeft - this.startMouseOffset.x : offset.x;
277
+ let newY = canDragVertical ? clientY - containerTop - this.startMouseOffset.y : offset.y;
288
278
  if (canDragHorizontal) {
289
279
  newX = newX > 0 ? 0 : newX < extremeLeft ? extremeLeft : newX;
290
280
  }
@@ -303,11 +293,7 @@ class PreviewImageFoundation extends _foundation.default {
303
293
  }
304
294
  };
305
295
  this.handleImageMouseDown = e => {
306
- this._adapter.setStartMouseOffset(this._getOffset(e));
307
- this._adapter.setStartMouseMove(true);
308
- };
309
- this.handleImageMouseUp = () => {
310
- this._adapter.setStartMouseMove(false);
296
+ this.startMouseOffset = this._getOffset(e);
311
297
  };
312
298
  }
313
299
  }
@@ -1,3 +1,4 @@
1
+ /// <reference types="lodash" />
1
2
  import BaseFoundation, { DefaultAdapter } from "../base/foundation";
2
3
  export type RatioType = "adaptation" | "realSize";
3
4
  export interface PreviewInnerAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
@@ -11,26 +12,27 @@ export interface PreviewInnerAdapter<P = Record<string, any>, S = Record<string,
11
12
  notifyDownload: (src: string, index: number) => void;
12
13
  registerKeyDownListener: () => void;
13
14
  unregisterKeyDownListener: () => void;
14
- getMouseActiveTime: () => number;
15
- getStopTiming: () => boolean;
16
- setStopTiming: (value: boolean) => void;
17
- getStartMouseDown: () => {
18
- x: number;
19
- y: number;
20
- };
21
- setStartMouseDown: (x: number, y: number) => void;
22
- setMouseActiveTime: (time: number) => void;
23
15
  disabledBodyScroll: () => void;
24
16
  enabledBodyScroll: () => void;
25
17
  getSetDownloadFunc: () => (src: string) => string;
18
+ isValidTarget: (e: any) => boolean;
26
19
  }
27
20
  export default class PreviewInnerFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<PreviewInnerAdapter<P, S>, P, S> {
28
21
  constructor(adapter: PreviewInnerAdapter<P, S>);
22
+ _timer: any;
23
+ _startMouseDown: {
24
+ x: number;
25
+ y: number;
26
+ };
29
27
  beforeShow(): void;
30
28
  afterHide(): void;
31
29
  handleViewVisibleChange: () => void;
32
- handleMouseMoveEvent: (e: any, event: string) => void;
33
30
  handleMouseMove: (e: any) => void;
31
+ mouseMoveHandler: import("lodash").DebouncedFunc<(e: any) => void>;
32
+ updateTimer: () => void;
33
+ clearTimer: () => void;
34
+ handleWheel: (e: any) => void;
35
+ onWheel: (e: any) => void;
34
36
  handleMouseUp: (e: any) => void;
35
37
  handleMouseDown: (e: any) => void;
36
38
  handleKeyDown: (e: any) => void;
@@ -4,6 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
+ var _throttle2 = _interopRequireDefault(require("lodash/throttle"));
8
+ var _isUndefined2 = _interopRequireDefault(require("lodash/isUndefined"));
9
+ var _a11y = require("../utils/a11y");
7
10
  var _foundation = _interopRequireDefault(require("../base/foundation"));
8
11
  var _keyCode = _interopRequireDefault(require("../utils/keyCode"));
9
12
  var _utils = require("./utils");
@@ -13,35 +16,83 @@ const STOP_CLOSE_TARGET = ["icon", "footer", "header"];
13
16
  class PreviewInnerFoundation extends _foundation.default {
14
17
  constructor(adapter) {
15
18
  super(Object.assign({}, adapter));
19
+ this._timer = null;
20
+ this._startMouseDown = {
21
+ x: 0,
22
+ y: 0
23
+ };
16
24
  this.handleViewVisibleChange = () => {
17
- const nowTime = new Date().getTime();
18
- const mouseActiveTime = this._adapter.getMouseActiveTime();
19
- const stopTiming = this._adapter.getStopTiming();
20
- const {
21
- viewerVisibleDelay
22
- } = this.getProps();
23
25
  const {
24
26
  viewerVisible
25
27
  } = this.getStates();
26
- if (nowTime - mouseActiveTime > viewerVisibleDelay && !stopTiming) {
27
- viewerVisible && this.setState({
28
+ if (viewerVisible) {
29
+ this.setState({
28
30
  viewerVisible: false
29
31
  });
32
+ this.clearTimer();
30
33
  }
31
34
  };
32
- this.handleMouseMoveEvent = (e, event) => {
33
- const isTarget = (0, _utils.isTargetEmit)(e, STOP_CLOSE_TARGET);
34
- if (isTarget && event === "over") {
35
- this._adapter.setStopTiming(true);
36
- } else if (isTarget && event === "out") {
37
- this._adapter.setStopTiming(false);
35
+ this.handleMouseMove = e => {
36
+ this._persistEvent(e);
37
+ this.mouseMoveHandler(e);
38
+ };
39
+ this.mouseMoveHandler = (0, _throttle2.default)(e => {
40
+ const {
41
+ viewerVisible
42
+ } = this.getStates();
43
+ const isValidTarget = this._adapter.isValidTarget(e);
44
+ if (isValidTarget) {
45
+ if (!viewerVisible) {
46
+ this.setState({
47
+ viewerVisible: true
48
+ });
49
+ }
50
+ this.updateTimer();
51
+ } else {
52
+ this.clearTimer();
38
53
  }
54
+ }, 50);
55
+ this.updateTimer = () => {
56
+ const {
57
+ viewerVisibleDelay
58
+ } = this.getProps();
59
+ this.clearTimer();
60
+ this._timer = setTimeout(this.handleViewVisibleChange, viewerVisibleDelay);
39
61
  };
40
- this.handleMouseMove = e => {
41
- this._adapter.setMouseActiveTime(new Date().getTime());
42
- this.setState({
43
- viewerVisible: true
44
- });
62
+ this.clearTimer = () => {
63
+ if (this._timer) {
64
+ clearTimeout(this._timer);
65
+ this._timer = null;
66
+ }
67
+ };
68
+ this.handleWheel = e => {
69
+ this.onWheel(e);
70
+ (0, _a11y.handlePrevent)(e);
71
+ };
72
+ this.onWheel = e => {
73
+ const {
74
+ zoomStep,
75
+ maxZoom,
76
+ minZoom
77
+ } = this.getProps();
78
+ const {
79
+ zoom: currZoom
80
+ } = this.getStates();
81
+ let _zoom;
82
+ if (e.deltaY < 0) {
83
+ /* zoom in */
84
+ if (currZoom + zoomStep <= maxZoom) {
85
+ _zoom = Number((currZoom + zoomStep).toFixed(2));
86
+ }
87
+ } else if (e.deltaY > 0) {
88
+ /* zoom out */
89
+ if (currZoom - zoomStep >= minZoom) {
90
+ _zoom = Number((currZoom - zoomStep).toFixed(2));
91
+ }
92
+ }
93
+ if (!(0, _isUndefined2.default)(_zoom)) {
94
+ this.handleZoomImage(_zoom);
95
+ }
45
96
  };
46
97
  this.handleMouseUp = e => {
47
98
  const {
@@ -55,7 +106,7 @@ class PreviewInnerFoundation extends _foundation.default {
55
106
  const {
56
107
  x,
57
108
  y
58
- } = this._adapter.getStartMouseDown();
109
+ } = this._startMouseDown;
59
110
  // 对鼠标移动做容错处理,当 x 和 y 方向在 mouseUp 的时候移动距离都小于等于 5px 时候就可以关闭预览
60
111
  // 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
61
112
  // 不做容错处理的话,直接用 clientX !== x || y !== clientY 做判断,鼠标在用户点击时候无意识的轻微移动无法关闭预览,不符合用户预期
@@ -72,7 +123,10 @@ class PreviewInnerFoundation extends _foundation.default {
72
123
  clientX,
73
124
  clientY
74
125
  } = e;
75
- this._adapter.setStartMouseDown(clientX, clientY);
126
+ this._startMouseDown = {
127
+ x: clientX,
128
+ y: clientY
129
+ };
76
130
  };
77
131
  this.handleKeyDown = e => {
78
132
  const {
@@ -145,10 +199,12 @@ class PreviewInnerFoundation extends _foundation.default {
145
199
  const {
146
200
  zoom
147
201
  } = this.getStates();
148
- this._adapter.notifyZoom(newZoom, newZoom > zoom);
149
- this.setState({
150
- zoom: newZoom
151
- });
202
+ if (zoom !== newZoom) {
203
+ this._adapter.notifyZoom(newZoom, newZoom > zoom);
204
+ this.setState({
205
+ zoom: newZoom
206
+ });
207
+ }
152
208
  };
153
209
  // 当 visible 改变之后,预览组件完成首张图片加载后,启动预加载
154
210
  // 如: 1,2,3,4,5,6,7,8张图片, 点击第 4 张图片,preLoadGap 为 2
@@ -266,10 +322,12 @@ class PreviewInnerFoundation extends _foundation.default {
266
322
  beforeShow() {
267
323
  this._adapter.registerKeyDownListener();
268
324
  this._adapter.disabledBodyScroll();
325
+ this.updateTimer();
269
326
  }
270
327
  afterHide() {
271
328
  this._adapter.unregisterKeyDownListener();
272
329
  this._adapter.enabledBodyScroll();
330
+ this.clearTimer();
273
331
  }
274
332
  }
275
333
  exports.default = PreviewInnerFoundation;
@@ -337,6 +337,7 @@ class SelectFoundation extends _foundation.default {
337
337
  this.close(e);
338
338
  this._notifyBlur(e);
339
339
  this._adapter.updateFocusState(false);
340
+ this._adapter.unregisterClickOutsideHandler();
340
341
  });
341
342
  }
342
343
  toggle2SearchInput(isShow) {
@@ -354,7 +355,6 @@ class SelectFoundation extends _foundation.default {
354
355
  this._adapter.setIsFocusInContainer(false);
355
356
  // this.unBindKeyBoardEvent();
356
357
  // this._notifyBlur(e);
357
- this._adapter.unregisterClickOutsideHandler();
358
358
  // this._adapter.updateFocusState(false);
359
359
  const isFilterable = this._isFilterable();
360
360
  if (isFilterable) {
@@ -1,6 +1,5 @@
1
1
  import BaseFoundation, { DefaultAdapter } from "../base/foundation";
2
2
  export interface PreviewFooterAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
3
- setStartMouseOffset: (time: number) => void;
4
3
  }
5
4
  export default class PreviewFooterFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<PreviewFooterAdapter<P, S>, P, S> {
6
5
  changeSliderValue: (type: string) => void;
@@ -28,7 +28,6 @@ export default class PreviewFooterFoundation extends BaseFoundation {
28
28
  } else {
29
29
  onZoomOut(Number((value / 100).toFixed(2)));
30
30
  }
31
- this._adapter.setStartMouseOffset(value);
32
31
  };
33
32
  this.handleRatioClick = () => {
34
33
  const {
@@ -1,26 +1,7 @@
1
- /// <reference types="lodash" />
2
1
  import BaseFoundation, { DefaultAdapter } from "../base/foundation";
3
2
  export interface PreviewImageAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
4
- getOriginImageSize: () => {
5
- originImageWidth: number;
6
- originImageHeight: number;
7
- };
8
- setOriginImageSize: (size: {
9
- originImageWidth: number;
10
- originImageHeight: number;
11
- }) => void;
12
3
  getContainer: () => HTMLDivElement;
13
4
  getImage: () => HTMLImageElement;
14
- getMouseMove: () => boolean;
15
- setStartMouseMove: (move: boolean) => void;
16
- getMouseOffset: () => {
17
- x: number;
18
- y: number;
19
- };
20
- setStartMouseOffset: (offset: {
21
- x: number;
22
- y: number;
23
- }) => void;
24
5
  setLoading: (loading: boolean) => void;
25
6
  setImageCursor: (canDrag: boolean) => void;
26
7
  }
@@ -38,6 +19,12 @@ export interface ImageOffset {
38
19
  }
39
20
  export default class PreviewImageFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<PreviewImageAdapter<P, S>, P, S> {
40
21
  constructor(adapter: PreviewImageAdapter<P, S>);
22
+ startMouseOffset: {
23
+ x: number;
24
+ y: number;
25
+ };
26
+ originImageWidth: any;
27
+ originImageHeight: any;
41
28
  _isImageVertical: () => boolean;
42
29
  _getImageBounds: () => DOMRect;
43
30
  _getContainerBounds: () => DOMRect;
@@ -47,13 +34,11 @@ export default class PreviewImageFoundation<P = Record<string, any>, S = Record<
47
34
  handleLoad: (e: any) => void;
48
35
  handleError: (e: any) => void;
49
36
  handleResizeImage: () => void;
37
+ handleRatioChange: () => void;
50
38
  handleRightClickImage: (e: any) => boolean;
51
- handleWheel: (e: any) => void;
52
- onWheel: import("lodash").DebouncedFunc<(e: any) => void>;
53
39
  calcCanDragDirection: () => DragDirection;
54
- handleZoomChange: (newZoom: number, e: any) => void;
40
+ calculatePreviewImage: (newZoom: number, e: any) => void;
55
41
  calcExtremeBounds: () => ExtremeBounds;
56
42
  handleMoveImage: (e: any) => void;
57
43
  handleImageMouseDown: (e: any) => void;
58
- handleImageMouseUp: () => void;
59
44
  }