thumb2.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var core = require('@vueuse/core');
  5. var constants = require('./constants.js');
  6. var util = require('./util.js');
  7. var thumb = require('./thumb.js');
  8. var pluginVue_exportHelper = require('../../../_virtual/plugin-vue_export-helper.js');
  9. var index = require('../../../hooks/use-namespace/index.js');
  10. var error = require('../../../utils/error.js');
  11. const COMPONENT_NAME = "Thumb";
  12. const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  13. __name: "thumb",
  14. props: thumb.thumbProps,
  15. setup(__props) {
  16. const props = __props;
  17. const scrollbar = vue.inject(constants.scrollbarContextKey);
  18. const ns = index.useNamespace("scrollbar");
  19. if (!scrollbar)
  20. error.throwError(COMPONENT_NAME, "can not inject scrollbar context");
  21. const instance = vue.ref();
  22. const thumb = vue.ref();
  23. const thumbState = vue.ref({});
  24. const visible = vue.ref(false);
  25. let cursorDown = false;
  26. let cursorLeave = false;
  27. let originalOnSelectStart = core.isClient ? document.onselectstart : null;
  28. const bar = vue.computed(() => util.BAR_MAP[props.vertical ? "vertical" : "horizontal"]);
  29. const thumbStyle = vue.computed(() => util.renderThumbStyle({
  30. size: props.size,
  31. move: props.move,
  32. bar: bar.value
  33. }));
  34. const offsetRatio = vue.computed(() => instance.value[bar.value.offset] ** 2 / scrollbar.wrapElement[bar.value.scrollSize] / props.ratio / thumb.value[bar.value.offset]);
  35. const clickThumbHandler = (e) => {
  36. var _a;
  37. e.stopPropagation();
  38. if (e.ctrlKey || [1, 2].includes(e.button))
  39. return;
  40. (_a = window.getSelection()) == null ? void 0 : _a.removeAllRanges();
  41. startDrag(e);
  42. const el = e.currentTarget;
  43. if (!el)
  44. return;
  45. thumbState.value[bar.value.axis] = el[bar.value.offset] - (e[bar.value.client] - el.getBoundingClientRect()[bar.value.direction]);
  46. };
  47. const clickTrackHandler = (e) => {
  48. if (!thumb.value || !instance.value || !scrollbar.wrapElement)
  49. return;
  50. const offset = Math.abs(e.target.getBoundingClientRect()[bar.value.direction] - e[bar.value.client]);
  51. const thumbHalf = thumb.value[bar.value.offset] / 2;
  52. const thumbPositionPercentage = (offset - thumbHalf) * 100 * offsetRatio.value / instance.value[bar.value.offset];
  53. scrollbar.wrapElement[bar.value.scroll] = thumbPositionPercentage * scrollbar.wrapElement[bar.value.scrollSize] / 100;
  54. };
  55. const startDrag = (e) => {
  56. e.stopImmediatePropagation();
  57. cursorDown = true;
  58. document.addEventListener("mousemove", mouseMoveDocumentHandler);
  59. document.addEventListener("mouseup", mouseUpDocumentHandler);
  60. originalOnSelectStart = document.onselectstart;
  61. document.onselectstart = () => false;
  62. };
  63. const mouseMoveDocumentHandler = (e) => {
  64. if (!instance.value || !thumb.value)
  65. return;
  66. if (cursorDown === false)
  67. return;
  68. const prevPage = thumbState.value[bar.value.axis];
  69. if (!prevPage)
  70. return;
  71. const offset = (instance.value.getBoundingClientRect()[bar.value.direction] - e[bar.value.client]) * -1;
  72. const thumbClickPosition = thumb.value[bar.value.offset] - prevPage;
  73. const thumbPositionPercentage = (offset - thumbClickPosition) * 100 * offsetRatio.value / instance.value[bar.value.offset];
  74. scrollbar.wrapElement[bar.value.scroll] = thumbPositionPercentage * scrollbar.wrapElement[bar.value.scrollSize] / 100;
  75. };
  76. const mouseUpDocumentHandler = () => {
  77. cursorDown = false;
  78. thumbState.value[bar.value.axis] = 0;
  79. document.removeEventListener("mousemove", mouseMoveDocumentHandler);
  80. document.removeEventListener("mouseup", mouseUpDocumentHandler);
  81. restoreOnselectstart();
  82. if (cursorLeave)
  83. visible.value = false;
  84. };
  85. const mouseMoveScrollbarHandler = () => {
  86. cursorLeave = false;
  87. visible.value = !!props.size;
  88. };
  89. const mouseLeaveScrollbarHandler = () => {
  90. cursorLeave = true;
  91. visible.value = cursorDown;
  92. };
  93. vue.onBeforeUnmount(() => {
  94. restoreOnselectstart();
  95. document.removeEventListener("mouseup", mouseUpDocumentHandler);
  96. });
  97. const restoreOnselectstart = () => {
  98. if (document.onselectstart !== originalOnSelectStart)
  99. document.onselectstart = originalOnSelectStart;
  100. };
  101. core.useEventListener(vue.toRef(scrollbar, "scrollbarElement"), "mousemove", mouseMoveScrollbarHandler);
  102. core.useEventListener(vue.toRef(scrollbar, "scrollbarElement"), "mouseleave", mouseLeaveScrollbarHandler);
  103. return (_ctx, _cache) => {
  104. return vue.openBlock(), vue.createBlock(vue.Transition, {
  105. name: vue.unref(ns).b("fade"),
  106. persisted: ""
  107. }, {
  108. default: vue.withCtx(() => [
  109. vue.withDirectives(vue.createElementVNode("div", {
  110. ref_key: "instance",
  111. ref: instance,
  112. class: vue.normalizeClass([vue.unref(ns).e("bar"), vue.unref(ns).is(vue.unref(bar).key)]),
  113. onMousedown: clickTrackHandler,
  114. onClick: vue.withModifiers(() => {
  115. }, ["stop"])
  116. }, [
  117. vue.createElementVNode("div", {
  118. ref_key: "thumb",
  119. ref: thumb,
  120. class: vue.normalizeClass(vue.unref(ns).e("thumb")),
  121. style: vue.normalizeStyle(vue.unref(thumbStyle)),
  122. onMousedown: clickThumbHandler
  123. }, null, 38)
  124. ], 42, ["onClick"]), [
  125. [vue.vShow, _ctx.always || visible.value]
  126. ])
  127. ]),
  128. _: 1
  129. }, 8, ["name"]);
  130. };
  131. }
  132. });
  133. var Thumb = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["__file", "thumb.vue"]]);
  134. exports["default"] = Thumb;
  135. //# sourceMappingURL=thumb2.js.map