b7bae3c9982087324ca7e7ee41fe0b76d42da62a238b2d9c1f5063ce16ce6c8811878f6f32422ae36e396da44f4873c51e80a0ed5404ab584390d80e63794a 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var style = require('../../../../utils/dom/style.js');
  5. var index = require('../../../../hooks/use-namespace/index.js');
  6. var types = require('../../../../utils/types.js');
  7. function useDragTag({
  8. wrapperRef,
  9. handleDragged,
  10. afterDragged
  11. }) {
  12. const ns = index.useNamespace("input-tag");
  13. const dropIndicatorRef = vue.shallowRef();
  14. const showDropIndicator = vue.ref(false);
  15. let draggingIndex;
  16. let draggingTag;
  17. let dropIndex;
  18. let dropType;
  19. function getTagClassName(index) {
  20. return `.${ns.e("inner")} .${ns.namespace.value}-tag:nth-child(${index + 1})`;
  21. }
  22. function handleDragStart(event, index) {
  23. draggingIndex = index;
  24. draggingTag = wrapperRef.value.querySelector(getTagClassName(index));
  25. if (draggingTag) {
  26. draggingTag.style.opacity = "0.5";
  27. }
  28. event.dataTransfer.effectAllowed = "move";
  29. }
  30. function handleDragOver(event, index) {
  31. dropIndex = index;
  32. event.preventDefault();
  33. event.dataTransfer.dropEffect = "move";
  34. if (types.isUndefined(draggingIndex) || draggingIndex === index) {
  35. showDropIndicator.value = false;
  36. return;
  37. }
  38. const dropPosition = wrapperRef.value.querySelector(getTagClassName(index)).getBoundingClientRect();
  39. const dropPrev = !(draggingIndex + 1 === index);
  40. const dropNext = !(draggingIndex - 1 === index);
  41. const distance = event.clientX - dropPosition.left;
  42. const prevPercent = dropPrev ? dropNext ? 0.5 : 1 : -1;
  43. const nextPercent = dropNext ? dropPrev ? 0.5 : 0 : 1;
  44. if (distance <= dropPosition.width * prevPercent) {
  45. dropType = "before";
  46. } else if (distance > dropPosition.width * nextPercent) {
  47. dropType = "after";
  48. } else {
  49. dropType = void 0;
  50. }
  51. const innerEl = wrapperRef.value.querySelector(`.${ns.e("inner")}`);
  52. const innerPosition = innerEl.getBoundingClientRect();
  53. const gap = Number.parseFloat(style.getStyle(innerEl, "gap")) / 2;
  54. const indicatorTop = dropPosition.top - innerPosition.top;
  55. let indicatorLeft = -9999;
  56. if (dropType === "before") {
  57. indicatorLeft = Math.max(dropPosition.left - innerPosition.left - gap, Math.floor(-gap / 2));
  58. } else if (dropType === "after") {
  59. const left = dropPosition.right - innerPosition.left;
  60. indicatorLeft = left + (innerPosition.width === left ? Math.floor(gap / 2) : gap);
  61. }
  62. style.setStyle(dropIndicatorRef.value, {
  63. top: `${indicatorTop}px`,
  64. left: `${indicatorLeft}px`
  65. });
  66. showDropIndicator.value = !!dropType;
  67. }
  68. function handleDragEnd(event) {
  69. event.preventDefault();
  70. if (draggingTag) {
  71. draggingTag.style.opacity = "";
  72. }
  73. if (dropType && !types.isUndefined(draggingIndex) && !types.isUndefined(dropIndex) && draggingIndex !== dropIndex) {
  74. handleDragged(draggingIndex, dropIndex, dropType);
  75. }
  76. showDropIndicator.value = false;
  77. draggingIndex = void 0;
  78. draggingTag = null;
  79. dropIndex = void 0;
  80. dropType = void 0;
  81. afterDragged == null ? void 0 : afterDragged();
  82. }
  83. return {
  84. dropIndicatorRef,
  85. showDropIndicator,
  86. handleDragStart,
  87. handleDragOver,
  88. handleDragEnd
  89. };
  90. }
  91. exports.useDragTag = useDragTag;
  92. //# sourceMappingURL=use-drag-tag.js.map