c588cfc7e601f658cc7e4bc982bf9b2d1768c4c329ccf8f7559657b37089295fc92d4a1f584904c5eaded3b9021f16fc3ee66c4b6d3b712245fd0bb1d2878f 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { defineComponent, getCurrentInstance, ref, computed, watch, onMounted, openBlock, createElementBlock, normalizeClass, createElementVNode, normalizeStyle } from 'vue';
  2. import { draggable } from '../utils/draggable.mjs';
  3. import _export_sfc from '../../../../_virtual/plugin-vue_export-helper.mjs';
  4. import { useNamespace } from '../../../../hooks/use-namespace/index.mjs';
  5. import { getClientXY } from '../../../../utils/dom/position.mjs';
  6. const _sfc_main = defineComponent({
  7. name: "ElColorHueSlider",
  8. props: {
  9. color: {
  10. type: Object,
  11. required: true
  12. },
  13. vertical: Boolean,
  14. disabled: Boolean
  15. },
  16. setup(props) {
  17. const ns = useNamespace("color-hue-slider");
  18. const instance = getCurrentInstance();
  19. const thumb = ref();
  20. const bar = ref();
  21. const thumbLeft = ref(0);
  22. const thumbTop = ref(0);
  23. const hueValue = computed(() => {
  24. return props.color.get("hue");
  25. });
  26. watch(() => hueValue.value, () => {
  27. update();
  28. });
  29. function handleClick(event) {
  30. if (props.disabled)
  31. return;
  32. const target = event.target;
  33. if (target !== thumb.value) {
  34. handleDrag(event);
  35. }
  36. }
  37. function handleDrag(event) {
  38. if (!bar.value || !thumb.value || props.disabled)
  39. return;
  40. const el = instance.vnode.el;
  41. const rect = el.getBoundingClientRect();
  42. const { clientX, clientY } = getClientXY(event);
  43. let hue;
  44. if (!props.vertical) {
  45. let left = clientX - rect.left;
  46. left = Math.min(left, rect.width - thumb.value.offsetWidth / 2);
  47. left = Math.max(thumb.value.offsetWidth / 2, left);
  48. hue = Math.round((left - thumb.value.offsetWidth / 2) / (rect.width - thumb.value.offsetWidth) * 360);
  49. } else {
  50. let top = clientY - rect.top;
  51. top = Math.min(top, rect.height - thumb.value.offsetHeight / 2);
  52. top = Math.max(thumb.value.offsetHeight / 2, top);
  53. hue = Math.round((top - thumb.value.offsetHeight / 2) / (rect.height - thumb.value.offsetHeight) * 360);
  54. }
  55. props.color.set("hue", hue);
  56. }
  57. function getThumbLeft() {
  58. if (!thumb.value)
  59. return 0;
  60. const el = instance.vnode.el;
  61. if (props.vertical)
  62. return 0;
  63. const hue = props.color.get("hue");
  64. if (!el)
  65. return 0;
  66. return Math.round(hue * (el.offsetWidth - thumb.value.offsetWidth / 2) / 360);
  67. }
  68. function getThumbTop() {
  69. if (!thumb.value)
  70. return 0;
  71. const el = instance.vnode.el;
  72. if (!props.vertical)
  73. return 0;
  74. const hue = props.color.get("hue");
  75. if (!el)
  76. return 0;
  77. return Math.round(hue * (el.offsetHeight - thumb.value.offsetHeight / 2) / 360);
  78. }
  79. function update() {
  80. thumbLeft.value = getThumbLeft();
  81. thumbTop.value = getThumbTop();
  82. }
  83. onMounted(() => {
  84. if (!bar.value || !thumb.value || props.disabled)
  85. return;
  86. const dragConfig = {
  87. drag: (event) => {
  88. handleDrag(event);
  89. },
  90. end: (event) => {
  91. handleDrag(event);
  92. }
  93. };
  94. draggable(bar.value, dragConfig);
  95. draggable(thumb.value, dragConfig);
  96. update();
  97. });
  98. return {
  99. bar,
  100. thumb,
  101. thumbLeft,
  102. thumbTop,
  103. hueValue,
  104. handleClick,
  105. update,
  106. ns
  107. };
  108. }
  109. });
  110. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  111. return openBlock(), createElementBlock("div", {
  112. class: normalizeClass([_ctx.ns.b(), _ctx.ns.is("vertical", _ctx.vertical)])
  113. }, [
  114. createElementVNode("div", {
  115. ref: "bar",
  116. class: normalizeClass(_ctx.ns.e("bar")),
  117. onClick: _ctx.handleClick
  118. }, null, 10, ["onClick"]),
  119. createElementVNode("div", {
  120. ref: "thumb",
  121. class: normalizeClass(_ctx.ns.e("thumb")),
  122. style: normalizeStyle({
  123. left: _ctx.thumbLeft + "px",
  124. top: _ctx.thumbTop + "px"
  125. })
  126. }, null, 6)
  127. ], 2);
  128. }
  129. var HueSlider = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "hue-slider.vue"]]);
  130. export { HueSlider as default };
  131. //# sourceMappingURL=hue-slider.mjs.map