thumb2.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 = 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 baseScrollHeight = 0;
  28. let baseScrollWidth = 0;
  29. let originalOnSelectStart = core.isClient ? document.onselectstart : null;
  30. const bar = vue.computed(() => util.BAR_MAP[props.vertical ? "vertical" : "horizontal"]);
  31. const thumbStyle = vue.computed(
  32. () => util.renderThumbStyle({
  33. size: props.size,
  34. move: props.move,
  35. bar: bar.value
  36. })
  37. );
  38. const offsetRatio = vue.computed(
  39. () => instance.value[bar.value.offset] ** 2 / scrollbar.wrapElement[bar.value.scrollSize] / props.ratio / thumb.value[bar.value.offset]
  40. );
  41. const clickThumbHandler = (e) => {
  42. var _a;
  43. e.stopPropagation();
  44. if (e.ctrlKey || [1, 2].includes(e.button))
  45. return;
  46. (_a = window.getSelection()) == null ? void 0 : _a.removeAllRanges();
  47. startDrag(e);
  48. const el = e.currentTarget;
  49. if (!el)
  50. return;
  51. thumbState.value[bar.value.axis] = el[bar.value.offset] - (e[bar.value.client] - el.getBoundingClientRect()[bar.value.direction]);
  52. };
  53. const clickTrackHandler = (e) => {
  54. if (!thumb.value || !instance.value || !scrollbar.wrapElement)
  55. return;
  56. const offset = Math.abs(
  57. e.target.getBoundingClientRect()[bar.value.direction] - e[bar.value.client]
  58. );
  59. const thumbHalf = thumb.value[bar.value.offset] / 2;
  60. const thumbPositionPercentage = (offset - thumbHalf) * 100 * offsetRatio.value / instance.value[bar.value.offset];
  61. scrollbar.wrapElement[bar.value.scroll] = thumbPositionPercentage * scrollbar.wrapElement[bar.value.scrollSize] / 100;
  62. };
  63. const startDrag = (e) => {
  64. e.stopImmediatePropagation();
  65. cursorDown = true;
  66. baseScrollHeight = scrollbar.wrapElement.scrollHeight;
  67. baseScrollWidth = scrollbar.wrapElement.scrollWidth;
  68. document.addEventListener("mousemove", mouseMoveDocumentHandler);
  69. document.addEventListener("mouseup", mouseUpDocumentHandler);
  70. originalOnSelectStart = document.onselectstart;
  71. document.onselectstart = () => false;
  72. };
  73. const mouseMoveDocumentHandler = (e) => {
  74. if (!instance.value || !thumb.value)
  75. return;
  76. if (cursorDown === false)
  77. return;
  78. const prevPage = thumbState.value[bar.value.axis];
  79. if (!prevPage)
  80. return;
  81. const offset = (instance.value.getBoundingClientRect()[bar.value.direction] - e[bar.value.client]) * -1;
  82. const thumbClickPosition = thumb.value[bar.value.offset] - prevPage;
  83. const thumbPositionPercentage = (offset - thumbClickPosition) * 100 * offsetRatio.value / instance.value[bar.value.offset];
  84. if (bar.value.scroll === "scrollLeft") {
  85. scrollbar.wrapElement[bar.value.scroll] = thumbPositionPercentage * baseScrollWidth / 100;
  86. } else {
  87. scrollbar.wrapElement[bar.value.scroll] = thumbPositionPercentage * baseScrollHeight / 100;
  88. }
  89. };
  90. const mouseUpDocumentHandler = () => {
  91. cursorDown = false;
  92. thumbState.value[bar.value.axis] = 0;
  93. document.removeEventListener("mousemove", mouseMoveDocumentHandler);
  94. document.removeEventListener("mouseup", mouseUpDocumentHandler);
  95. restoreOnselectstart();
  96. if (cursorLeave)
  97. visible.value = false;
  98. };
  99. const mouseMoveScrollbarHandler = () => {
  100. cursorLeave = false;
  101. visible.value = !!props.size;
  102. };
  103. const mouseLeaveScrollbarHandler = () => {
  104. cursorLeave = true;
  105. visible.value = cursorDown;
  106. };
  107. vue.onBeforeUnmount(() => {
  108. restoreOnselectstart();
  109. document.removeEventListener("mouseup", mouseUpDocumentHandler);
  110. });
  111. const restoreOnselectstart = () => {
  112. if (document.onselectstart !== originalOnSelectStart)
  113. document.onselectstart = originalOnSelectStart;
  114. };
  115. core.useEventListener(
  116. vue.toRef(scrollbar, "scrollbarElement"),
  117. "mousemove",
  118. mouseMoveScrollbarHandler
  119. );
  120. core.useEventListener(
  121. vue.toRef(scrollbar, "scrollbarElement"),
  122. "mouseleave",
  123. mouseLeaveScrollbarHandler
  124. );
  125. return (_ctx, _cache) => {
  126. return vue.openBlock(), vue.createBlock(vue.Transition, {
  127. name: vue.unref(ns).b("fade"),
  128. persisted: ""
  129. }, {
  130. default: vue.withCtx(() => [
  131. vue.withDirectives(vue.createElementVNode(
  132. "div",
  133. {
  134. ref_key: "instance",
  135. ref: instance,
  136. class: vue.normalizeClass([vue.unref(ns).e("bar"), vue.unref(ns).is(bar.value.key)]),
  137. onMousedown: clickTrackHandler,
  138. onClick: _cache[0] || (_cache[0] = vue.withModifiers(() => {
  139. }, ["stop"]))
  140. },
  141. [
  142. vue.createElementVNode(
  143. "div",
  144. {
  145. ref_key: "thumb",
  146. ref: thumb,
  147. class: vue.normalizeClass(vue.unref(ns).e("thumb")),
  148. style: vue.normalizeStyle(thumbStyle.value),
  149. onMousedown: clickThumbHandler
  150. },
  151. null,
  152. 38
  153. )
  154. ],
  155. 34
  156. ), [
  157. [vue.vShow, _ctx.always || visible.value]
  158. ])
  159. ]),
  160. _: 1
  161. }, 8, ["name"]);
  162. };
  163. }
  164. });
  165. var Thumb = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["__file", "/home/runner/work/element-plus/element-plus/packages/components/scrollbar/src/thumb.vue"]]);
  166. exports["default"] = Thumb;
  167. //# sourceMappingURL=thumb2.js.map