@leafer-in/viewport 1.8.0 → 1.9.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.
@@ -1,26 +1,30 @@
1
- import { MoveEvent, ZoomEvent, DataHelper, Debug, PointHelper, Platform, MathHelper, RotateEvent, Leafer, Bounds, InteractionBase, Dragger, BoundsHelper, Plugin } from '@leafer-ui/core';
1
+ import { MoveEvent, ZoomEvent, DataHelper, Debug, PointHelper, Platform, MathHelper, RotateEvent, Leafer, Bounds, InteractionBase, Dragger, isNumber, BoundsHelper, Plugin } from "@leafer-ui/core";
2
2
 
3
3
  function addViewport(leafer, mergeConfig, custom) {
4
4
  addViewportConfig(leafer.parentApp ? leafer.parentApp : leafer, mergeConfig);
5
- if (leafer.isApp || custom)
6
- return;
7
- leafer.__eventIds.push(leafer.on_(MoveEvent.BEFORE_MOVE, (e) => {
5
+ if (leafer.isApp || custom) return;
6
+ leafer.__eventIds.push(leafer.on_(MoveEvent.BEFORE_MOVE, e => {
8
7
  leafer.zoomLayer.move(leafer.getValidMove(e.moveX, e.moveY));
9
- }), leafer.on_(ZoomEvent.BEFORE_ZOOM, (e) => {
10
- const { zoomLayer } = leafer;
8
+ }), leafer.on_(ZoomEvent.BEFORE_ZOOM, e => {
9
+ const {zoomLayer: zoomLayer} = leafer;
11
10
  const changeScale = leafer.getValidScale(e.scale);
12
- if (changeScale !== 1)
13
- zoomLayer.scaleOfWorld(e, changeScale);
11
+ if (changeScale !== 1) zoomLayer.scaleOfWorld(e, changeScale);
14
12
  }));
15
13
  }
14
+
16
15
  function addViewportConfig(leafer, mergeConfig) {
17
16
  const viewportConfig = {
18
- wheel: { preventDefault: true },
19
- touch: { preventDefault: true },
20
- pointer: { preventDefaultMenu: true }
17
+ wheel: {
18
+ preventDefault: true
19
+ },
20
+ touch: {
21
+ preventDefault: true
22
+ },
23
+ pointer: {
24
+ preventDefaultMenu: true
25
+ }
21
26
  };
22
- if (mergeConfig)
23
- DataHelper.assign(viewportConfig, mergeConfig);
27
+ if (mergeConfig) DataHelper.assign(viewportConfig, mergeConfig);
24
28
  DataHelper.assign(leafer.config, viewportConfig, leafer.userConfig);
25
29
  }
26
30
 
@@ -31,24 +35,29 @@ function custom(leafer) {
31
35
  function design(leafer) {
32
36
  addViewport(leafer, {
33
37
  zoom: {
34
- min: 0.01,
38
+ min: .01,
35
39
  max: 256
36
40
  },
37
41
  move: {
38
42
  holdSpaceKey: true,
39
- holdMiddleKey: true,
43
+ holdMiddleKey: true
40
44
  }
41
45
  });
42
46
  }
43
47
 
44
48
  function document(leafer) {
45
49
  addViewport(leafer, {
46
- zoom: { min: 1 },
47
- move: { scroll: 'limit' }
50
+ zoom: {
51
+ min: 1
52
+ },
53
+ move: {
54
+ scroll: "limit"
55
+ }
48
56
  });
49
57
  }
50
58
 
51
- const debug = Debug.get('LeaferTypeCreator');
59
+ const debug = Debug.get("LeaferTypeCreator");
60
+
52
61
  const LeaferTypeCreator = {
53
62
  list: {},
54
63
  register(name, fn) {
@@ -60,78 +69,92 @@ const LeaferTypeCreator = {
60
69
  fn && fn(leafer);
61
70
  }
62
71
  };
63
- const { list, register } = LeaferTypeCreator;
64
- register('viewport', addViewport);
65
- register('custom', custom);
66
- register('design', design);
67
- register('document', document);
72
+
73
+ const {list: list, register: register} = LeaferTypeCreator;
74
+
75
+ register("viewport", addViewport);
76
+
77
+ register("custom", custom);
78
+
79
+ register("design", design);
80
+
81
+ register("document", document);
68
82
 
69
83
  const MultiTouchHelper = {
70
84
  getData(list) {
71
85
  const a = list[0], b = list[1];
72
86
  const lastCenter = PointHelper.getCenter(a.from, b.from);
73
87
  const center = PointHelper.getCenter(a.to, b.to);
74
- const move = { x: center.x - lastCenter.x, y: center.y - lastCenter.y };
88
+ const move = {
89
+ x: center.x - lastCenter.x,
90
+ y: center.y - lastCenter.y
91
+ };
75
92
  const lastDistance = PointHelper.getDistance(a.from, b.from);
76
93
  const distance = PointHelper.getDistance(a.to, b.to);
77
94
  const scale = distance / lastDistance;
78
95
  const rotation = PointHelper.getRotation(a.from, b.from, a.to, b.to);
79
- return { move, scale, rotation, center };
96
+ return {
97
+ move: move,
98
+ scale: scale,
99
+ rotation: rotation,
100
+ center: center
101
+ };
80
102
  }
81
103
  };
82
104
 
83
105
  const WheelEventHelper = {
84
106
  getMove(event, config) {
85
- let { moveSpeed } = config;
86
- let { deltaX, deltaY } = event;
107
+ let {moveSpeed: moveSpeed} = config;
108
+ let {deltaX: deltaX, deltaY: deltaY} = event;
87
109
  if (event.shiftKey && !deltaX) {
88
110
  deltaX = deltaY;
89
111
  deltaY = 0;
90
112
  }
91
- if (deltaX > 50)
92
- deltaX = Math.max(50, deltaX / 3);
93
- if (deltaY > 50)
94
- deltaY = Math.max(50, deltaY / 3);
95
- return { x: -deltaX * moveSpeed * 2, y: -deltaY * moveSpeed * 2 };
113
+ if (deltaX > 50) deltaX = Math.max(50, deltaX / 3);
114
+ if (deltaY > 50) deltaY = Math.max(50, deltaY / 3);
115
+ return {
116
+ x: -deltaX * moveSpeed * 2,
117
+ y: -deltaY * moveSpeed * 2
118
+ };
96
119
  },
97
120
  getScale(event, config) {
98
121
  let zoom;
99
122
  let scale = 1;
100
- let { zoomMode, zoomSpeed } = config;
123
+ let {zoomMode: zoomMode, zoomSpeed: zoomSpeed} = config;
101
124
  const delta = event.deltaY || event.deltaX;
102
125
  if (zoomMode) {
103
- zoom = (zoomMode === 'mouse') ? true : (!event.deltaX && (Platform.intWheelDeltaY ? Math.abs(delta) > 17 : Math.ceil(delta) !== delta));
104
- if (event.shiftKey || event.metaKey || event.ctrlKey)
105
- zoom = true;
106
- }
107
- else {
126
+ zoom = zoomMode === "mouse" ? true : !event.deltaX && (Platform.intWheelDeltaY ? Math.abs(delta) > 17 : Math.ceil(delta) !== delta);
127
+ if (event.shiftKey || event.metaKey || event.ctrlKey) zoom = true;
128
+ } else {
108
129
  zoom = !event.shiftKey && (event.metaKey || event.ctrlKey);
109
130
  }
110
131
  if (zoom) {
111
132
  zoomSpeed = MathHelper.within(zoomSpeed, 0, 1);
112
133
  const min = event.deltaY ? config.delta.y : config.delta.x;
113
134
  scale = 1 - delta / (min * 4) * zoomSpeed;
114
- if (scale < 0.5)
115
- scale = 0.5;
116
- if (scale >= 1.5)
117
- scale = 1.5;
135
+ if (scale < .5) scale = .5;
136
+ if (scale >= 1.5) scale = 1.5;
118
137
  }
119
138
  return scale;
120
139
  }
121
140
  };
122
141
 
123
142
  class Transformer {
124
- get transforming() { return !!(this.moveData || this.zoomData || this.rotateData); }
143
+ get transforming() {
144
+ return !!(this.moveData || this.zoomData || this.rotateData);
145
+ }
125
146
  constructor(interaction) {
126
147
  this.interaction = interaction;
127
148
  }
128
149
  move(data) {
129
- const { interaction } = this;
130
- if (!data.moveType)
131
- data.moveType = 'move';
150
+ const {interaction: interaction} = this;
151
+ if (!data.moveType) data.moveType = "move";
132
152
  if (!this.moveData) {
133
153
  this.setPath(data);
134
- this.moveData = Object.assign(Object.assign({}, data), { moveX: 0, moveY: 0 });
154
+ this.moveData = Object.assign(Object.assign({}, data), {
155
+ moveX: 0,
156
+ moveY: 0
157
+ });
135
158
  interaction.emit(MoveEvent.START, this.moveData);
136
159
  }
137
160
  data.path = this.moveData.path;
@@ -140,10 +163,12 @@ class Transformer {
140
163
  this.transformEndWait();
141
164
  }
142
165
  zoom(data) {
143
- const { interaction } = this;
166
+ const {interaction: interaction} = this;
144
167
  if (!this.zoomData) {
145
168
  this.setPath(data);
146
- this.zoomData = Object.assign(Object.assign({}, data), { scale: 1 });
169
+ this.zoomData = Object.assign(Object.assign({}, data), {
170
+ scale: 1
171
+ });
147
172
  interaction.emit(ZoomEvent.START, this.zoomData);
148
173
  }
149
174
  data.path = this.zoomData.path;
@@ -152,10 +177,12 @@ class Transformer {
152
177
  this.transformEndWait();
153
178
  }
154
179
  rotate(data) {
155
- const { interaction } = this;
180
+ const {interaction: interaction} = this;
156
181
  if (!this.rotateData) {
157
182
  this.setPath(data);
158
- this.rotateData = Object.assign(Object.assign({}, data), { rotation: 0 });
183
+ this.rotateData = Object.assign(Object.assign({}, data), {
184
+ rotation: 0
185
+ });
159
186
  interaction.emit(RotateEvent.START, this.rotateData);
160
187
  }
161
188
  data.path = this.rotateData.path;
@@ -164,8 +191,8 @@ class Transformer {
164
191
  this.transformEndWait();
165
192
  }
166
193
  setPath(data) {
167
- const { interaction } = this;
168
- const { path } = interaction.selector.getByPoint(data, interaction.hitRadius);
194
+ const {interaction: interaction} = this;
195
+ const {path: path} = interaction.selector.getByPoint(data, interaction.hitRadius);
169
196
  data.path = path;
170
197
  interaction.cancelHover();
171
198
  }
@@ -176,13 +203,10 @@ class Transformer {
176
203
  }, this.interaction.p.transformTime);
177
204
  }
178
205
  transformEnd() {
179
- const { interaction, moveData, zoomData, rotateData } = this;
180
- if (moveData)
181
- interaction.emit(MoveEvent.END, moveData);
182
- if (zoomData)
183
- interaction.emit(ZoomEvent.END, zoomData);
184
- if (rotateData)
185
- interaction.emit(RotateEvent.END, rotateData);
206
+ const {interaction: interaction, moveData: moveData, zoomData: zoomData, rotateData: rotateData} = this;
207
+ if (moveData) interaction.emit(MoveEvent.END, moveData);
208
+ if (zoomData) interaction.emit(ZoomEvent.END, zoomData);
209
+ if (rotateData) interaction.emit(RotateEvent.END, rotateData);
186
210
  this.reset();
187
211
  }
188
212
  reset() {
@@ -194,103 +218,99 @@ class Transformer {
194
218
  }
195
219
 
196
220
  const leafer = Leafer.prototype;
197
- const bounds = new Bounds();
198
- leafer.initType = function (type) {
221
+
222
+ const bounds = new Bounds;
223
+
224
+ leafer.initType = function(type) {
199
225
  LeaferTypeCreator.run(type, this);
200
226
  };
201
- leafer.getValidMove = function (moveX, moveY) {
202
- const { scroll, disabled } = this.app.config.move;
227
+
228
+ leafer.getValidMove = function(moveX, moveY) {
229
+ const {scroll: scroll, disabled: disabled} = this.app.config.move;
203
230
  if (scroll) {
204
- const type = scroll === true ? '' : scroll;
205
- if (type.includes('x'))
206
- moveX = moveX || moveY, moveY = 0;
207
- else if (type.includes('y'))
208
- moveY = moveY || moveX, moveX = 0;
209
- else
210
- Math.abs(moveX) > Math.abs(moveY) ? moveY = 0 : moveX = 0;
211
- if (type.includes('limit')) {
212
- const { x, y, width, height } = bounds.set(this.__world).addPoint(this.zoomLayer);
231
+ const type = scroll === true ? "" : scroll;
232
+ if (type.includes("x")) moveX = moveX || moveY, moveY = 0; else if (type.includes("y")) moveY = moveY || moveX,
233
+ moveX = 0; else Math.abs(moveX) > Math.abs(moveY) ? moveY = 0 : moveX = 0;
234
+ if (type.includes("limit")) {
235
+ const {x: x, y: y, width: width, height: height} = bounds.set(this.__world).addPoint(this.zoomLayer);
213
236
  const right = x + width - this.width, bottom = y + height - this.height;
214
- if (x >= 0 && right <= 0)
215
- moveX = 0;
216
- else if (moveX > 0) {
217
- if (x + moveX > 0)
218
- moveX = -x;
219
- }
220
- else if (moveX < 0 && right + moveX < 0)
221
- moveX = -right;
222
- if (y >= 0 && bottom <= 0)
223
- moveY = 0;
224
- else if (moveY > 0) {
225
- if (y + moveY > 0)
226
- moveY = -y;
227
- }
228
- else if (moveY < 0 && bottom + moveY < 0)
229
- moveY = -bottom;
237
+ if (x >= 0 && right <= 0) moveX = 0; else if (moveX > 0) {
238
+ if (x + moveX > 0) moveX = -x;
239
+ } else if (moveX < 0 && right + moveX < 0) moveX = -right;
240
+ if (y >= 0 && bottom <= 0) moveY = 0; else if (moveY > 0) {
241
+ if (y + moveY > 0) moveY = -y;
242
+ } else if (moveY < 0 && bottom + moveY < 0) moveY = -bottom;
230
243
  }
231
244
  }
232
- return { x: disabled ? 0 : moveX, y: disabled ? 0 : moveY };
245
+ return {
246
+ x: disabled ? 0 : moveX,
247
+ y: disabled ? 0 : moveY
248
+ };
233
249
  };
234
- leafer.getValidScale = function (changeScale) {
235
- const { scaleX } = this.zoomLayer.__, { min, max, disabled } = this.app.config.zoom, absScale = Math.abs(scaleX * changeScale);
236
- if (min && absScale < min)
237
- changeScale = min / scaleX;
238
- else if (max && absScale > max)
239
- changeScale = max / scaleX;
250
+
251
+ leafer.getValidScale = function(changeScale) {
252
+ const {scaleX: scaleX} = this.zoomLayer.__, {min: min, max: max, disabled: disabled} = this.app.config.zoom, absScale = Math.abs(scaleX * changeScale);
253
+ if (min && absScale < min) changeScale = min / scaleX; else if (max && absScale > max) changeScale = max / scaleX;
240
254
  return disabled ? 1 : changeScale;
241
255
  };
242
256
 
243
257
  function getMoveEventData(move, event) {
244
- return Object.assign(Object.assign({}, event), { moveX: move.x, moveY: move.y });
258
+ return Object.assign(Object.assign({}, event), {
259
+ moveX: move.x,
260
+ moveY: move.y
261
+ });
245
262
  }
263
+
246
264
  function getRotateEventData(rotation, event) {
247
- return Object.assign(Object.assign({}, event), { rotation });
265
+ return Object.assign(Object.assign({}, event), {
266
+ rotation: rotation
267
+ });
248
268
  }
269
+
249
270
  function getZoomEventData(scale, event) {
250
- return Object.assign(Object.assign({}, event), { scale });
271
+ return Object.assign(Object.assign({}, event), {
272
+ scale: scale
273
+ });
251
274
  }
275
+
252
276
  const interaction = InteractionBase.prototype;
253
- interaction.createTransformer = function () {
277
+
278
+ interaction.createTransformer = function() {
254
279
  this.transformer = new Transformer(this);
255
280
  };
256
- interaction.move = function (data) {
281
+
282
+ interaction.move = function(data) {
257
283
  this.transformer.move(data);
258
284
  };
259
- interaction.zoom = function (data) {
285
+
286
+ interaction.zoom = function(data) {
260
287
  this.transformer.zoom(data);
261
288
  };
262
- interaction.rotate = function (data) {
289
+
290
+ interaction.rotate = function(data) {
263
291
  this.transformer.rotate(data);
264
292
  };
265
- interaction.transformEnd = function () {
293
+
294
+ interaction.transformEnd = function() {
266
295
  this.transformer.transformEnd();
267
296
  };
268
- interaction.wheel = function (data) {
269
- const { wheel, pointer } = this.config, { posDeltaSpeed, negDeltaSpeed } = wheel;
270
- if (wheel.disabled)
271
- return;
272
- if (data.deltaX > 0)
273
- posDeltaSpeed && (data.deltaX *= posDeltaSpeed);
274
- else
275
- negDeltaSpeed && (data.deltaX *= negDeltaSpeed);
276
- if (data.deltaY > 0)
277
- posDeltaSpeed && (data.deltaY *= posDeltaSpeed);
278
- else
279
- negDeltaSpeed && (data.deltaY *= negDeltaSpeed);
297
+
298
+ interaction.wheel = function(data) {
299
+ const {wheel: wheel, pointer: pointer} = this.config, {posDeltaSpeed: posDeltaSpeed, negDeltaSpeed: negDeltaSpeed} = wheel;
300
+ if (wheel.disabled) return;
301
+ if (data.deltaX > 0) posDeltaSpeed && (data.deltaX *= posDeltaSpeed); else negDeltaSpeed && (data.deltaX *= negDeltaSpeed);
302
+ if (data.deltaY > 0) posDeltaSpeed && (data.deltaY *= posDeltaSpeed); else negDeltaSpeed && (data.deltaY *= negDeltaSpeed);
280
303
  const scale = wheel.getScale ? wheel.getScale(data, wheel) : WheelEventHelper.getScale(data, wheel);
281
- if (scale !== 1)
282
- this.zoom(getZoomEventData(scale, data));
283
- else {
304
+ if (scale !== 1) this.zoom(getZoomEventData(scale, data)); else {
284
305
  const move = wheel.getMove ? wheel.getMove(data, wheel) : WheelEventHelper.getMove(data, wheel);
285
- if (pointer.snap)
286
- PointHelper.round(move);
306
+ if (pointer.snap) PointHelper.round(move);
287
307
  this.move(getMoveEventData(move, data));
288
308
  }
289
309
  };
290
- interaction.multiTouch = function (data, list) {
291
- if (this.config.multiTouch.disabled)
292
- return;
293
- const { move, rotation, scale, center } = MultiTouchHelper.getData(list);
310
+
311
+ interaction.multiTouch = function(data, list) {
312
+ if (this.config.multiTouch.disabled) return;
313
+ const {move: move, rotation: rotation, scale: scale, center: center} = MultiTouchHelper.getData(list);
294
314
  Object.assign(data, center);
295
315
  data.multiTouch = true;
296
316
  this.pointerWaitCancel();
@@ -300,65 +320,73 @@ interaction.multiTouch = function (data, list) {
300
320
  };
301
321
 
302
322
  const dragger = Dragger.prototype;
303
- const { abs } = Math;
304
- dragger.checkDragEndAnimate = function (data, speed) {
305
- const { moveX, moveY } = this.dragData;
306
- const absMoveX = abs(moveX), absMoveY = abs(moveY), minMove = speed ? 1 : 0.1;
307
- const dragAnimate = this.interaction.m.dragAnimate && this.canAnimate && this.moving && (absMoveX > minMove || absMoveY > minMove);
323
+
324
+ const {abs: abs} = Math;
325
+
326
+ dragger.checkDragEndAnimate = function(data, speed) {
327
+ const {moveX: moveX, moveY: moveY} = this.dragData;
328
+ const absMoveX = abs(moveX), absMoveY = abs(moveY), minMove = speed ? 1 : .1;
329
+ const dragAnimate = this.canAnimate && this.moving && (absMoveX > minMove || absMoveY > minMove) && this.interaction.m.dragAnimate;
308
330
  if (dragAnimate) {
309
- const inertia = data.pointerType === 'touch' ? 3 : 1, maxMove = 70;
310
- speed = speed ? 0.95 : inertia;
311
- if (absMoveX * speed > maxMove)
312
- speed = maxMove / absMoveX;
313
- else if (absMoveY * speed > maxMove)
314
- speed = maxMove / absMoveY;
331
+ const inertia = data.pointerType === "touch" ? 3 : 1, maxMove = 70;
332
+ speed = speed ? isNumber(dragAnimate) ? dragAnimate : .95 : inertia;
333
+ if (absMoveX * speed > maxMove) speed = maxMove / absMoveX; else if (absMoveY * speed > maxMove) speed = maxMove / absMoveY;
315
334
  data = Object.assign({}, data);
316
335
  PointHelper.move(data, moveX * speed, moveY * speed);
317
336
  this.drag(data);
318
- this.animate(() => { this.dragEnd(data, 1); });
337
+ this.animate(() => {
338
+ this.dragEnd(data, 1);
339
+ });
319
340
  }
320
341
  return dragAnimate;
321
342
  };
322
- dragger.animate = function (func, off) {
343
+
344
+ dragger.animate = function(func, off) {
323
345
  const animateWait = func || this.animateWait;
324
- if (animateWait)
325
- this.interaction.target.nextRender(animateWait, null, off);
346
+ if (animateWait) this.interaction.target.nextRender(animateWait, null, off);
326
347
  this.animateWait = func;
327
348
  };
328
- dragger.checkDragOut = function (data) {
329
- const { interaction } = this;
349
+
350
+ dragger.checkDragOut = function(data) {
351
+ const {interaction: interaction} = this;
330
352
  this.autoMoveCancel();
331
- if (this.dragging && !interaction.shrinkCanvasBounds.hitPoint(data))
332
- this.autoMoveOnDragOut(data);
353
+ if (this.dragging && !interaction.shrinkCanvasBounds.hitPoint(data)) this.autoMoveOnDragOut(data);
333
354
  };
334
- dragger.autoMoveOnDragOut = function (data) {
335
- const { interaction, downData, canDragOut } = this;
336
- const { autoDistance, dragOut } = interaction.m;
337
- if (!dragOut || !canDragOut || !autoDistance)
338
- return;
355
+
356
+ dragger.autoMoveOnDragOut = function(data) {
357
+ const {interaction: interaction, downData: downData, canDragOut: canDragOut} = this;
358
+ const {autoDistance: autoDistance, dragOut: dragOut} = interaction.m;
359
+ if (!dragOut || !canDragOut || !autoDistance) return;
339
360
  const bounds = interaction.shrinkCanvasBounds;
340
- const { x, y } = bounds;
361
+ const {x: x, y: y} = bounds;
341
362
  const right = BoundsHelper.maxX(bounds);
342
363
  const bottom = BoundsHelper.maxY(bounds);
343
- const moveX = data.x < x ? autoDistance : (right < data.x ? -autoDistance : 0);
344
- const moveY = data.y < y ? autoDistance : (bottom < data.y ? -autoDistance : 0);
364
+ const moveX = data.x < x ? autoDistance : right < data.x ? -autoDistance : 0;
365
+ const moveY = data.y < y ? autoDistance : bottom < data.y ? -autoDistance : 0;
345
366
  let totalX = 0, totalY = 0;
346
367
  this.autoMoveTimer = setInterval(() => {
347
368
  totalX += moveX;
348
369
  totalY += moveY;
349
370
  PointHelper.move(downData, moveX, moveY);
350
371
  PointHelper.move(this.dragData, moveX, moveY);
351
- interaction.move(Object.assign(Object.assign({}, data), { moveX, moveY, totalX, totalY, moveType: 'drag' }));
372
+ interaction.move(Object.assign(Object.assign({}, data), {
373
+ moveX: moveX,
374
+ moveY: moveY,
375
+ totalX: totalX,
376
+ totalY: totalY,
377
+ moveType: "drag"
378
+ }));
352
379
  interaction.pointerMoveReal(data);
353
380
  }, 10);
354
381
  };
355
- dragger.autoMoveCancel = function () {
382
+
383
+ dragger.autoMoveCancel = function() {
356
384
  if (this.autoMoveTimer) {
357
385
  clearInterval(this.autoMoveTimer);
358
386
  this.autoMoveTimer = 0;
359
387
  }
360
388
  };
361
389
 
362
- Plugin.add('viewport');
390
+ Plugin.add("viewport");
363
391
 
364
392
  export { LeaferTypeCreator, MultiTouchHelper, Transformer, WheelEventHelper, addViewport, addViewportConfig };
@@ -1,2 +1,2 @@
1
- import{MoveEvent as t,ZoomEvent as e,DataHelper as o,Debug as a,PointHelper as i,Platform as n,MathHelper as s,RotateEvent as r,Leafer as c,Bounds as m,InteractionBase as h,Dragger as l,BoundsHelper as u,Plugin as d}from"@leafer-ui/core";function f(o,a,i){g(o.parentApp?o.parentApp:o,a),o.isApp||i||o.__eventIds.push(o.on_(t.BEFORE_MOVE,(t=>{o.zoomLayer.move(o.getValidMove(t.moveX,t.moveY))})),o.on_(e.BEFORE_ZOOM,(t=>{const{zoomLayer:e}=o,a=o.getValidScale(t.scale);1!==a&&e.scaleOfWorld(t,a)})))}function g(t,e){const a={wheel:{preventDefault:!0},touch:{preventDefault:!0},pointer:{preventDefaultMenu:!0}};e&&o.assign(a,e),o.assign(t.config,a,t.userConfig)}const v=a.get("LeaferTypeCreator"),p={list:{},register(t,e){D[t]&&v.repeat(t),D[t]=e},run(t,e){const o=D[t];o&&o(e)}},{list:D,register:O}=p;O("viewport",f),O("custom",(function(t){f(t,null,!0)})),O("design",(function(t){f(t,{zoom:{min:.01,max:256},move:{holdSpaceKey:!0,holdMiddleKey:!0}})})),O("document",(function(t){f(t,{zoom:{min:1},move:{scroll:"limit"}})}));const y={getData(t){const e=t[0],o=t[1],a=i.getCenter(e.from,o.from),n=i.getCenter(e.to,o.to),s={x:n.x-a.x,y:n.y-a.y},r=i.getDistance(e.from,o.from);return{move:s,scale:i.getDistance(e.to,o.to)/r,rotation:i.getRotation(e.from,o.from,e.to,o.to),center:n}}},M={getMove(t,e){let{moveSpeed:o}=e,{deltaX:a,deltaY:i}=t;return t.shiftKey&&!a&&(a=i,i=0),a>50&&(a=Math.max(50,a/3)),i>50&&(i=Math.max(50,i/3)),{x:-a*o*2,y:-i*o*2}},getScale(t,e){let o,a=1,{zoomMode:i,zoomSpeed:r}=e;const c=t.deltaY||t.deltaX;if(i?(o="mouse"===i||!t.deltaX&&(n.intWheelDeltaY?Math.abs(c)>17:Math.ceil(c)!==c),(t.shiftKey||t.metaKey||t.ctrlKey)&&(o=!0)):o=!t.shiftKey&&(t.metaKey||t.ctrlKey),o){r=s.within(r,0,1);a=1-c/(4*(t.deltaY?e.delta.y:e.delta.x))*r,a<.5&&(a=.5),a>=1.5&&(a=1.5)}return a}};class T{get transforming(){return!!(this.moveData||this.zoomData||this.rotateData)}constructor(t){this.interaction=t}move(e){const{interaction:o}=this;e.moveType||(e.moveType="move"),this.moveData||(this.setPath(e),this.moveData=Object.assign(Object.assign({},e),{moveX:0,moveY:0}),o.emit(t.START,this.moveData)),e.path=this.moveData.path,o.emit(t.BEFORE_MOVE,e),o.emit(t.MOVE,e),this.transformEndWait()}zoom(t){const{interaction:o}=this;this.zoomData||(this.setPath(t),this.zoomData=Object.assign(Object.assign({},t),{scale:1}),o.emit(e.START,this.zoomData)),t.path=this.zoomData.path,o.emit(e.BEFORE_ZOOM,t),o.emit(e.ZOOM,t),this.transformEndWait()}rotate(t){const{interaction:e}=this;this.rotateData||(this.setPath(t),this.rotateData=Object.assign(Object.assign({},t),{rotation:0}),e.emit(r.START,this.rotateData)),t.path=this.rotateData.path,e.emit(r.BEFORE_ROTATE,t),e.emit(r.ROTATE,t),this.transformEndWait()}setPath(t){const{interaction:e}=this,{path:o}=e.selector.getByPoint(t,e.hitRadius);t.path=o,e.cancelHover()}transformEndWait(){clearTimeout(this.transformTimer),this.transformTimer=setTimeout((()=>{this.transformEnd()}),this.interaction.p.transformTime)}transformEnd(){const{interaction:o,moveData:a,zoomData:i,rotateData:n}=this;a&&o.emit(t.END,a),i&&o.emit(e.END,i),n&&o.emit(r.END,n),this.reset()}reset(){this.zoomData=this.moveData=this.rotateData=null}destroy(){this.reset()}}const E=c.prototype,b=new m;function z(t,e){return Object.assign(Object.assign({},e),{moveX:t.x,moveY:t.y})}function x(t,e){return Object.assign(Object.assign({},e),{scale:t})}E.initType=function(t){p.run(t,this)},E.getValidMove=function(t,e){const{scroll:o,disabled:a}=this.app.config.move;if(o){const a=!0===o?"":o;if(a.includes("x")?(t=t||e,e=0):a.includes("y")?(e=e||t,t=0):Math.abs(t)>Math.abs(e)?e=0:t=0,a.includes("limit")){const{x:o,y:a,width:i,height:n}=b.set(this.__world).addPoint(this.zoomLayer),s=o+i-this.width,r=a+n-this.height;o>=0&&s<=0?t=0:t>0?o+t>0&&(t=-o):t<0&&s+t<0&&(t=-s),a>=0&&r<=0?e=0:e>0?a+e>0&&(e=-a):e<0&&r+e<0&&(e=-r)}}return{x:a?0:t,y:a?0:e}},E.getValidScale=function(t){const{scaleX:e}=this.zoomLayer.__,{min:o,max:a,disabled:i}=this.app.config.zoom,n=Math.abs(e*t);return o&&n<o?t=o/e:a&&n>a&&(t=a/e),i?1:t};const j=h.prototype;j.createTransformer=function(){this.transformer=new T(this)},j.move=function(t){this.transformer.move(t)},j.zoom=function(t){this.transformer.zoom(t)},j.rotate=function(t){this.transformer.rotate(t)},j.transformEnd=function(){this.transformer.transformEnd()},j.wheel=function(t){const{wheel:e,pointer:o}=this.config,{posDeltaSpeed:a,negDeltaSpeed:n}=e;if(e.disabled)return;t.deltaX>0?a&&(t.deltaX*=a):n&&(t.deltaX*=n),t.deltaY>0?a&&(t.deltaY*=a):n&&(t.deltaY*=n);const s=e.getScale?e.getScale(t,e):M.getScale(t,e);if(1!==s)this.zoom(x(s,t));else{const a=e.getMove?e.getMove(t,e):M.getMove(t,e);o.snap&&i.round(a),this.move(z(a,t))}},j.multiTouch=function(t,e){if(this.config.multiTouch.disabled)return;const{move:o,rotation:a,scale:i,center:n}=y.getData(e);Object.assign(t,n),t.multiTouch=!0,this.pointerWaitCancel(),this.rotate(function(t,e){return Object.assign(Object.assign({},e),{rotation:t})}(a,t)),this.zoom(x(i,t)),this.move(z(o,t))};const R=l.prototype,{abs:S}=Math;R.checkDragEndAnimate=function(t,e){const{moveX:o,moveY:a}=this.dragData,n=S(o),s=S(a),r=e?1:.1,c=this.interaction.m.dragAnimate&&this.canAnimate&&this.moving&&(n>r||s>r);if(c){const r="touch"===t.pointerType?3:1,c=70;n*(e=e?.95:r)>c?e=c/n:s*e>c&&(e=c/s),t=Object.assign({},t),i.move(t,o*e,a*e),this.drag(t),this.animate((()=>{this.dragEnd(t,1)}))}return c},R.animate=function(t,e){const o=t||this.animateWait;o&&this.interaction.target.nextRender(o,null,e),this.animateWait=t},R.checkDragOut=function(t){const{interaction:e}=this;this.autoMoveCancel(),this.dragging&&!e.shrinkCanvasBounds.hitPoint(t)&&this.autoMoveOnDragOut(t)},R.autoMoveOnDragOut=function(t){const{interaction:e,downData:o,canDragOut:a}=this,{autoDistance:n,dragOut:s}=e.m;if(!s||!a||!n)return;const r=e.shrinkCanvasBounds,{x:c,y:m}=r,h=u.maxX(r),l=u.maxY(r),d=t.x<c?n:h<t.x?-n:0,f=t.y<m?n:l<t.y?-n:0;let g=0,v=0;this.autoMoveTimer=setInterval((()=>{g+=d,v+=f,i.move(o,d,f),i.move(this.dragData,d,f),e.move(Object.assign(Object.assign({},t),{moveX:d,moveY:f,totalX:g,totalY:v,moveType:"drag"})),e.pointerMoveReal(t)}),10)},R.autoMoveCancel=function(){this.autoMoveTimer&&(clearInterval(this.autoMoveTimer),this.autoMoveTimer=0)},d.add("viewport");export{p as LeaferTypeCreator,y as MultiTouchHelper,T as Transformer,M as WheelEventHelper,f as addViewport,g as addViewportConfig};
1
+ import{MoveEvent as t,ZoomEvent as e,DataHelper as o,Debug as a,PointHelper as i,Platform as n,MathHelper as s,RotateEvent as r,Leafer as c,Bounds as m,InteractionBase as h,Dragger as l,isNumber as u,BoundsHelper as d,Plugin as f}from"@leafer-ui/core";function g(o,a,i){v(o.parentApp?o.parentApp:o,a),o.isApp||i||o.__eventIds.push(o.on_(t.BEFORE_MOVE,t=>{o.zoomLayer.move(o.getValidMove(t.moveX,t.moveY))}),o.on_(e.BEFORE_ZOOM,t=>{const{zoomLayer:e}=o,a=o.getValidScale(t.scale);1!==a&&e.scaleOfWorld(t,a)}))}function v(t,e){const a={wheel:{preventDefault:!0},touch:{preventDefault:!0},pointer:{preventDefaultMenu:!0}};e&&o.assign(a,e),o.assign(t.config,a,t.userConfig)}const p=a.get("LeaferTypeCreator"),D={list:{},register(t,e){O[t]&&p.repeat(t),O[t]=e},run(t,e){const o=O[t];o&&o(e)}},{list:O,register:y}=D;y("viewport",g),y("custom",function(t){g(t,null,!0)}),y("design",function(t){g(t,{zoom:{min:.01,max:256},move:{holdSpaceKey:!0,holdMiddleKey:!0}})}),y("document",function(t){g(t,{zoom:{min:1},move:{scroll:"limit"}})});const M={getData(t){const e=t[0],o=t[1],a=i.getCenter(e.from,o.from),n=i.getCenter(e.to,o.to),s={x:n.x-a.x,y:n.y-a.y},r=i.getDistance(e.from,o.from);return{move:s,scale:i.getDistance(e.to,o.to)/r,rotation:i.getRotation(e.from,o.from,e.to,o.to),center:n}}},T={getMove(t,e){let{moveSpeed:o}=e,{deltaX:a,deltaY:i}=t;return t.shiftKey&&!a&&(a=i,i=0),a>50&&(a=Math.max(50,a/3)),i>50&&(i=Math.max(50,i/3)),{x:-a*o*2,y:-i*o*2}},getScale(t,e){let o,a=1,{zoomMode:i,zoomSpeed:r}=e;const c=t.deltaY||t.deltaX;if(i?(o="mouse"===i||!t.deltaX&&(n.intWheelDeltaY?Math.abs(c)>17:Math.ceil(c)!==c),(t.shiftKey||t.metaKey||t.ctrlKey)&&(o=!0)):o=!t.shiftKey&&(t.metaKey||t.ctrlKey),o){r=s.within(r,0,1);a=1-c/(4*(t.deltaY?e.delta.y:e.delta.x))*r,a<.5&&(a=.5),a>=1.5&&(a=1.5)}return a}};class E{get transforming(){return!!(this.moveData||this.zoomData||this.rotateData)}constructor(t){this.interaction=t}move(e){const{interaction:o}=this;e.moveType||(e.moveType="move"),this.moveData||(this.setPath(e),this.moveData=Object.assign(Object.assign({},e),{moveX:0,moveY:0}),o.emit(t.START,this.moveData)),e.path=this.moveData.path,o.emit(t.BEFORE_MOVE,e),o.emit(t.MOVE,e),this.transformEndWait()}zoom(t){const{interaction:o}=this;this.zoomData||(this.setPath(t),this.zoomData=Object.assign(Object.assign({},t),{scale:1}),o.emit(e.START,this.zoomData)),t.path=this.zoomData.path,o.emit(e.BEFORE_ZOOM,t),o.emit(e.ZOOM,t),this.transformEndWait()}rotate(t){const{interaction:e}=this;this.rotateData||(this.setPath(t),this.rotateData=Object.assign(Object.assign({},t),{rotation:0}),e.emit(r.START,this.rotateData)),t.path=this.rotateData.path,e.emit(r.BEFORE_ROTATE,t),e.emit(r.ROTATE,t),this.transformEndWait()}setPath(t){const{interaction:e}=this,{path:o}=e.selector.getByPoint(t,e.hitRadius);t.path=o,e.cancelHover()}transformEndWait(){clearTimeout(this.transformTimer),this.transformTimer=setTimeout(()=>{this.transformEnd()},this.interaction.p.transformTime)}transformEnd(){const{interaction:o,moveData:a,zoomData:i,rotateData:n}=this;a&&o.emit(t.END,a),i&&o.emit(e.END,i),n&&o.emit(r.END,n),this.reset()}reset(){this.zoomData=this.moveData=this.rotateData=null}destroy(){this.reset()}}const b=c.prototype,z=new m;function x(t,e){return Object.assign(Object.assign({},e),{moveX:t.x,moveY:t.y})}function j(t,e){return Object.assign(Object.assign({},e),{scale:t})}b.initType=function(t){D.run(t,this)},b.getValidMove=function(t,e){const{scroll:o,disabled:a}=this.app.config.move;if(o){const a=!0===o?"":o;if(a.includes("x")?(t=t||e,e=0):a.includes("y")?(e=e||t,t=0):Math.abs(t)>Math.abs(e)?e=0:t=0,a.includes("limit")){const{x:o,y:a,width:i,height:n}=z.set(this.__world).addPoint(this.zoomLayer),s=o+i-this.width,r=a+n-this.height;o>=0&&s<=0?t=0:t>0?o+t>0&&(t=-o):t<0&&s+t<0&&(t=-s),a>=0&&r<=0?e=0:e>0?a+e>0&&(e=-a):e<0&&r+e<0&&(e=-r)}}return{x:a?0:t,y:a?0:e}},b.getValidScale=function(t){const{scaleX:e}=this.zoomLayer.__,{min:o,max:a,disabled:i}=this.app.config.zoom,n=Math.abs(e*t);return o&&n<o?t=o/e:a&&n>a&&(t=a/e),i?1:t};const R=h.prototype;R.createTransformer=function(){this.transformer=new E(this)},R.move=function(t){this.transformer.move(t)},R.zoom=function(t){this.transformer.zoom(t)},R.rotate=function(t){this.transformer.rotate(t)},R.transformEnd=function(){this.transformer.transformEnd()},R.wheel=function(t){const{wheel:e,pointer:o}=this.config,{posDeltaSpeed:a,negDeltaSpeed:n}=e;if(e.disabled)return;t.deltaX>0?a&&(t.deltaX*=a):n&&(t.deltaX*=n),t.deltaY>0?a&&(t.deltaY*=a):n&&(t.deltaY*=n);const s=e.getScale?e.getScale(t,e):T.getScale(t,e);if(1!==s)this.zoom(j(s,t));else{const a=e.getMove?e.getMove(t,e):T.getMove(t,e);o.snap&&i.round(a),this.move(x(a,t))}},R.multiTouch=function(t,e){if(this.config.multiTouch.disabled)return;const{move:o,rotation:a,scale:i,center:n}=M.getData(e);Object.assign(t,n),t.multiTouch=!0,this.pointerWaitCancel(),this.rotate(function(t,e){return Object.assign(Object.assign({},e),{rotation:t})}(a,t)),this.zoom(j(i,t)),this.move(x(o,t))};const S=l.prototype,{abs:X}=Math;S.checkDragEndAnimate=function(t,e){const{moveX:o,moveY:a}=this.dragData,n=X(o),s=X(a),r=e?1:.1,c=this.canAnimate&&this.moving&&(n>r||s>r)&&this.interaction.m.dragAnimate;if(c){const r="touch"===t.pointerType?3:1,m=70;n*(e=e?u(c)?c:.95:r)>m?e=m/n:s*e>m&&(e=m/s),t=Object.assign({},t),i.move(t,o*e,a*e),this.drag(t),this.animate(()=>{this.dragEnd(t,1)})}return c},S.animate=function(t,e){const o=t||this.animateWait;o&&this.interaction.target.nextRender(o,null,e),this.animateWait=t},S.checkDragOut=function(t){const{interaction:e}=this;this.autoMoveCancel(),this.dragging&&!e.shrinkCanvasBounds.hitPoint(t)&&this.autoMoveOnDragOut(t)},S.autoMoveOnDragOut=function(t){const{interaction:e,downData:o,canDragOut:a}=this,{autoDistance:n,dragOut:s}=e.m;if(!s||!a||!n)return;const r=e.shrinkCanvasBounds,{x:c,y:m}=r,h=d.maxX(r),l=d.maxY(r),u=t.x<c?n:h<t.x?-n:0,f=t.y<m?n:l<t.y?-n:0;let g=0,v=0;this.autoMoveTimer=setInterval(()=>{g+=u,v+=f,i.move(o,u,f),i.move(this.dragData,u,f),e.move(Object.assign(Object.assign({},t),{moveX:u,moveY:f,totalX:g,totalY:v,moveType:"drag"})),e.pointerMoveReal(t)},10)},S.autoMoveCancel=function(){this.autoMoveTimer&&(clearInterval(this.autoMoveTimer),this.autoMoveTimer=0)},f.add("viewport");export{D as LeaferTypeCreator,M as MultiTouchHelper,E as Transformer,T as WheelEventHelper,g as addViewport,v as addViewportConfig};
2
2
  //# sourceMappingURL=viewport.esm.min.js.map