@havue/drag-and-drop 1.1.0 → 1.1.2

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.
@@ -2,8 +2,8 @@ import './style.css';
2
2
  var __defProp = Object.defineProperty;
3
3
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
4
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- import { defineComponent, ref, reactive, computed, createElementBlock, openBlock, normalizeClass, renderSlot, createCommentVNode, normalizeStyle } from "vue";
6
- import { EventBus, isMobile } from "@havue/shared";
5
+ import { defineComponent, ref, reactive, computed, onMounted, onBeforeUnmount, createElementBlock, openBlock, normalizeClass, renderSlot, createCommentVNode, normalizeStyle } from "vue";
6
+ import { EventBus } from "@havue/shared";
7
7
  const withInstall = (main, extra) => {
8
8
  main.install = (app) => {
9
9
  for (const comp of [main, ...Object.values({})]) {
@@ -217,15 +217,12 @@ class DnDManager extends EventBus {
217
217
  const onMouseDown = this.onMouseDown.bind(this);
218
218
  const onMouseMove = this.onMouseMove.bind(this);
219
219
  const onMouseUp = this.onMouseUp.bind(this);
220
- if (isMobile) {
221
- document.body.addEventListener("touchstart", onTouchStart, { passive: false });
222
- document.body.addEventListener("touchmove", onTouchMove, { passive: false });
223
- document.body.addEventListener("touchend", onTouchEnd, { passive: false });
224
- } else {
225
- document.body.addEventListener("mousedown", onMouseDown);
226
- document.body.addEventListener("mousemove", onMouseMove);
227
- document.body.addEventListener("mouseup", onMouseUp);
228
- }
220
+ document.body.addEventListener("touchstart", onTouchStart, { passive: false });
221
+ document.body.addEventListener("touchmove", onTouchMove, { passive: false });
222
+ document.body.addEventListener("touchend", onTouchEnd, { passive: false });
223
+ document.body.addEventListener("mousedown", onMouseDown);
224
+ document.body.addEventListener("mousemove", onMouseMove);
225
+ document.body.addEventListener("mouseup", onMouseUp);
229
226
  this.destroy = () => {
230
227
  document.body.removeEventListener("touchstart", onTouchStart);
231
228
  document.body.removeEventListener("touchmove", onTouchMove);
@@ -291,7 +288,7 @@ const _sfc_main$1 = /* @__PURE__ */ defineComponent({
291
288
  transform: `translate(${cloneNodePosition.x}px, ${cloneNodePosition.y}px) translate(-50%, -50%)`
292
289
  };
293
290
  });
294
- DnDManagerInstance.on("down", (params) => {
291
+ const onDown = (params) => {
295
292
  const { x, y } = params;
296
293
  const startEl = document.elementFromPoint(x, y);
297
294
  if (!props.disabled && dragItemRef.value && dragItemRef.value.contains(startEl)) {
@@ -303,8 +300,8 @@ const _sfc_main$1 = /* @__PURE__ */ defineComponent({
303
300
  downPosition.x = x;
304
301
  downPosition.y = y;
305
302
  }
306
- });
307
- DnDManagerInstance.on("first-move", (params, event) => {
303
+ };
304
+ const onFirstMove = (params, event) => {
308
305
  const { x, y } = params;
309
306
  const directions = immediateDirections.value.filter((item) => item !== ImmediateEnumType.ALL);
310
307
  if (isDownThis.value && !props.disabled && directions.length) {
@@ -330,21 +327,21 @@ const _sfc_main$1 = /* @__PURE__ */ defineComponent({
330
327
  return;
331
328
  }
332
329
  }
333
- });
334
- DnDManagerInstance.on("start", (params) => {
330
+ };
331
+ const onStart = (params) => {
335
332
  const { x, y } = params;
336
333
  const startEl = document.elementFromPoint(x, y);
337
334
  if (!props.disabled && dragItemRef.value && dragItemRef.value.contains(startEl)) {
338
335
  handleStart(params);
339
336
  }
340
- });
341
- DnDManagerInstance.on("move", (params) => {
337
+ };
338
+ const onMove = (params) => {
342
339
  const { point } = params;
343
340
  if (isDragThis.value) {
344
341
  handleMove(point);
345
342
  }
346
- });
347
- DnDManagerInstance.on("end", () => {
343
+ };
344
+ const onEnd = () => {
348
345
  isDownThis.value = false;
349
346
  isDragThis.value = false;
350
347
  Object.assign(downPosition, {
@@ -355,7 +352,7 @@ const _sfc_main$1 = /* @__PURE__ */ defineComponent({
355
352
  x: 0,
356
353
  y: 0
357
354
  });
358
- });
355
+ };
359
356
  function handleStart(point) {
360
357
  isDragThis.value = true;
361
358
  cloneNodePosition.x = point.x;
@@ -366,6 +363,20 @@ const _sfc_main$1 = /* @__PURE__ */ defineComponent({
366
363
  cloneNodePosition.x = point.x;
367
364
  cloneNodePosition.y = point.y;
368
365
  }
366
+ onMounted(() => {
367
+ DnDManagerInstance.on("down", onDown);
368
+ DnDManagerInstance.on("first-move", onFirstMove);
369
+ DnDManagerInstance.on("start", onStart);
370
+ DnDManagerInstance.on("move", onMove);
371
+ DnDManagerInstance.on("end", onEnd);
372
+ });
373
+ onBeforeUnmount(() => {
374
+ DnDManagerInstance.off("down", onDown);
375
+ DnDManagerInstance.off("first-move", onFirstMove);
376
+ DnDManagerInstance.off("start", onStart);
377
+ DnDManagerInstance.off("move", onMove);
378
+ DnDManagerInstance.off("end", onEnd);
379
+ });
369
380
  return (_ctx, _cache) => {
370
381
  return openBlock(), createElementBlock("div", {
371
382
  ref_key: "dragItemRef",
@@ -393,7 +404,7 @@ const _export_sfc = (sfc, props) => {
393
404
  }
394
405
  return target;
395
406
  };
396
- const Draggable = /* @__PURE__ */ _export_sfc(_sfc_main$1, [["__scopeId", "data-v-33722ba1"]]);
407
+ const Draggable = /* @__PURE__ */ _export_sfc(_sfc_main$1, [["__scopeId", "data-v-21a10de6"]]);
397
408
  const _sfc_main = /* @__PURE__ */ defineComponent({
398
409
  ...{
399
410
  name: "HvDroppable"
@@ -433,7 +444,7 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
433
444
  }
434
445
  };
435
446
  }
436
- DnDManagerInstance.on("move", (params) => {
447
+ const onMove = (params) => {
437
448
  const { type, data, point } = params;
438
449
  if (dropAreaRef.value && acceptDragTypeList.value.includes(type)) {
439
450
  const { isInArea, position } = getPositionInArea(point);
@@ -449,12 +460,20 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
449
460
  emits("leave", type, data);
450
461
  }
451
462
  }
452
- });
453
- DnDManagerInstance.on("end", ({ type, point, data }) => {
463
+ };
464
+ const onEnd = ({ type, point, data }) => {
454
465
  if (dropAreaRef.value && acceptDragTypeList.value.includes(type) && isEntered.value) {
455
466
  const { position } = getPositionInArea(point);
456
467
  emits("drop", type, position, data);
457
468
  }
469
+ };
470
+ onMounted(() => {
471
+ DnDManagerInstance.on("move", onMove);
472
+ DnDManagerInstance.on("end", onEnd);
473
+ });
474
+ onBeforeUnmount(() => {
475
+ DnDManagerInstance.off("move", onMove);
476
+ DnDManagerInstance.off("end", onEnd);
458
477
  });
459
478
  return (_ctx, _cache) => {
460
479
  return openBlock(), createElementBlock("div", {
@@ -218,15 +218,12 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
218
218
  const onMouseDown = this.onMouseDown.bind(this);
219
219
  const onMouseMove = this.onMouseMove.bind(this);
220
220
  const onMouseUp = this.onMouseUp.bind(this);
221
- if (shared.isMobile) {
222
- document.body.addEventListener("touchstart", onTouchStart, { passive: false });
223
- document.body.addEventListener("touchmove", onTouchMove, { passive: false });
224
- document.body.addEventListener("touchend", onTouchEnd, { passive: false });
225
- } else {
226
- document.body.addEventListener("mousedown", onMouseDown);
227
- document.body.addEventListener("mousemove", onMouseMove);
228
- document.body.addEventListener("mouseup", onMouseUp);
229
- }
221
+ document.body.addEventListener("touchstart", onTouchStart, { passive: false });
222
+ document.body.addEventListener("touchmove", onTouchMove, { passive: false });
223
+ document.body.addEventListener("touchend", onTouchEnd, { passive: false });
224
+ document.body.addEventListener("mousedown", onMouseDown);
225
+ document.body.addEventListener("mousemove", onMouseMove);
226
+ document.body.addEventListener("mouseup", onMouseUp);
230
227
  this.destroy = () => {
231
228
  document.body.removeEventListener("touchstart", onTouchStart);
232
229
  document.body.removeEventListener("touchmove", onTouchMove);
@@ -292,7 +289,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
292
289
  transform: `translate(${cloneNodePosition.x}px, ${cloneNodePosition.y}px) translate(-50%, -50%)`
293
290
  };
294
291
  });
295
- DnDManagerInstance.on("down", (params) => {
292
+ const onDown = (params) => {
296
293
  const { x, y } = params;
297
294
  const startEl = document.elementFromPoint(x, y);
298
295
  if (!props.disabled && dragItemRef.value && dragItemRef.value.contains(startEl)) {
@@ -304,8 +301,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
304
301
  downPosition.x = x;
305
302
  downPosition.y = y;
306
303
  }
307
- });
308
- DnDManagerInstance.on("first-move", (params, event) => {
304
+ };
305
+ const onFirstMove = (params, event) => {
309
306
  const { x, y } = params;
310
307
  const directions = immediateDirections.value.filter((item) => item !== ImmediateEnumType.ALL);
311
308
  if (isDownThis.value && !props.disabled && directions.length) {
@@ -331,21 +328,21 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
331
328
  return;
332
329
  }
333
330
  }
334
- });
335
- DnDManagerInstance.on("start", (params) => {
331
+ };
332
+ const onStart = (params) => {
336
333
  const { x, y } = params;
337
334
  const startEl = document.elementFromPoint(x, y);
338
335
  if (!props.disabled && dragItemRef.value && dragItemRef.value.contains(startEl)) {
339
336
  handleStart(params);
340
337
  }
341
- });
342
- DnDManagerInstance.on("move", (params) => {
338
+ };
339
+ const onMove = (params) => {
343
340
  const { point } = params;
344
341
  if (isDragThis.value) {
345
342
  handleMove(point);
346
343
  }
347
- });
348
- DnDManagerInstance.on("end", () => {
344
+ };
345
+ const onEnd = () => {
349
346
  isDownThis.value = false;
350
347
  isDragThis.value = false;
351
348
  Object.assign(downPosition, {
@@ -356,7 +353,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
356
353
  x: 0,
357
354
  y: 0
358
355
  });
359
- });
356
+ };
360
357
  function handleStart(point) {
361
358
  isDragThis.value = true;
362
359
  cloneNodePosition.x = point.x;
@@ -367,6 +364,20 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
367
364
  cloneNodePosition.x = point.x;
368
365
  cloneNodePosition.y = point.y;
369
366
  }
367
+ vue.onMounted(() => {
368
+ DnDManagerInstance.on("down", onDown);
369
+ DnDManagerInstance.on("first-move", onFirstMove);
370
+ DnDManagerInstance.on("start", onStart);
371
+ DnDManagerInstance.on("move", onMove);
372
+ DnDManagerInstance.on("end", onEnd);
373
+ });
374
+ vue.onBeforeUnmount(() => {
375
+ DnDManagerInstance.off("down", onDown);
376
+ DnDManagerInstance.off("first-move", onFirstMove);
377
+ DnDManagerInstance.off("start", onStart);
378
+ DnDManagerInstance.off("move", onMove);
379
+ DnDManagerInstance.off("end", onEnd);
380
+ });
370
381
  return (_ctx, _cache) => {
371
382
  return vue.openBlock(), vue.createElementBlock("div", {
372
383
  ref_key: "dragItemRef",
@@ -394,7 +405,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
394
405
  }
395
406
  return target;
396
407
  };
397
- const Draggable = /* @__PURE__ */ _export_sfc(_sfc_main$1, [["__scopeId", "data-v-33722ba1"]]);
408
+ const Draggable = /* @__PURE__ */ _export_sfc(_sfc_main$1, [["__scopeId", "data-v-21a10de6"]]);
398
409
  const _sfc_main = /* @__PURE__ */ vue.defineComponent({
399
410
  ...{
400
411
  name: "HvDroppable"
@@ -434,7 +445,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
434
445
  }
435
446
  };
436
447
  }
437
- DnDManagerInstance.on("move", (params) => {
448
+ const onMove = (params) => {
438
449
  const { type, data, point } = params;
439
450
  if (dropAreaRef.value && acceptDragTypeList.value.includes(type)) {
440
451
  const { isInArea, position } = getPositionInArea(point);
@@ -450,12 +461,20 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
450
461
  emits("leave", type, data);
451
462
  }
452
463
  }
453
- });
454
- DnDManagerInstance.on("end", ({ type, point, data }) => {
464
+ };
465
+ const onEnd = ({ type, point, data }) => {
455
466
  if (dropAreaRef.value && acceptDragTypeList.value.includes(type) && isEntered.value) {
456
467
  const { position } = getPositionInArea(point);
457
468
  emits("drop", type, position, data);
458
469
  }
470
+ };
471
+ vue.onMounted(() => {
472
+ DnDManagerInstance.on("move", onMove);
473
+ DnDManagerInstance.on("end", onEnd);
474
+ });
475
+ vue.onBeforeUnmount(() => {
476
+ DnDManagerInstance.off("move", onMove);
477
+ DnDManagerInstance.off("end", onEnd);
459
478
  });
460
479
  return (_ctx, _cache) => {
461
480
  return vue.openBlock(), vue.createElementBlock("div", {
package/dist/style.css CHANGED
@@ -1,11 +1,11 @@
1
- .hv-draggable[data-v-33722ba1] {
1
+ .hv-draggable[data-v-21a10de6] {
2
2
  width: fit-content;
3
3
  cursor: grab;
4
4
  }
5
- .hv-draggable.disabled[data-v-33722ba1] {
5
+ .hv-draggable.disabled[data-v-21a10de6] {
6
6
  cursor: not-allowed;
7
7
  }
8
- .hv-draggable__clone-item[data-v-33722ba1] {
8
+ .hv-draggable__clone-item[data-v-21a10de6] {
9
9
  position: fixed;
10
10
  top: 0;
11
11
  left: 0;
@@ -4,7 +4,7 @@ export type DragAndDropPoint = {
4
4
  y: number;
5
5
  };
6
6
  export type DragAndDropDragType = string | number | symbol;
7
- type Events = {
7
+ export type DnDManagerEvents = {
8
8
  down: (p: DragAndDropPoint) => void;
9
9
  'first-move': (p: DragAndDropPoint, e: MouseEvent | TouchEvent) => void;
10
10
  start: (p: DragAndDropPoint) => void;
@@ -19,7 +19,7 @@ type Events = {
19
19
  point: DragAndDropPoint;
20
20
  }) => void;
21
21
  };
22
- export declare class DnDManager extends EventBus<Events> {
22
+ export declare class DnDManager extends EventBus<DnDManagerEvents> {
23
23
  /** 是否开始拖动 */
24
24
  isDragStart: boolean;
25
25
  /** 拖动元素类型 */
@@ -78,4 +78,3 @@ export declare class DnDManager extends EventBus<Events> {
78
78
  destroyed(): void;
79
79
  }
80
80
  export declare const DnDManagerInstance: DnDManager;
81
- export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@havue/drag-and-drop",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Drag and drop components for Vue3",
5
5
  "keywords": [
6
6
  "havue",
@@ -16,7 +16,7 @@
16
16
  "url": "git+https://github.com/HappyPedestrian/havue.git"
17
17
  },
18
18
  "dependencies": {
19
- "@havue/shared": "^1.1.0"
19
+ "@havue/shared": "^1.1.2"
20
20
  },
21
21
  "devDependencies": {
22
22
  "vue": "^3.3.0"