83c230efc2acbf0e9984c45abb0b4cf0bbfdcbab7e3f7804b8c7980dda9dc3e9517d54facc9e90a48b1f242981c6c720df17c49ea4a6d9e32dbba2e1195e77 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { getCurrentInstance, shallowRef, computed, ref, onMounted, watch } from 'vue';
  2. import { draggable } from '../utils/draggable.mjs';
  3. import { getClientXY } from '../../../../utils/dom/position.mjs';
  4. import { useLocale } from '../../../../hooks/use-locale/index.mjs';
  5. import { EVENT_CODE } from '../../../../constants/aria.mjs';
  6. import { useNamespace } from '../../../../hooks/use-namespace/index.mjs';
  7. import { addUnit } from '../../../../utils/dom/style.mjs';
  8. const useAlphaSlider = (props) => {
  9. const instance = getCurrentInstance();
  10. const { t } = useLocale();
  11. const thumb = shallowRef();
  12. const bar = shallowRef();
  13. const alpha = computed(() => props.color.get("alpha"));
  14. const alphaLabel = computed(() => t("el.colorpicker.alphaLabel"));
  15. function handleClick(event) {
  16. var _a;
  17. if (props.disabled)
  18. return;
  19. const target = event.target;
  20. if (target !== thumb.value) {
  21. handleDrag(event);
  22. }
  23. (_a = thumb.value) == null ? void 0 : _a.focus();
  24. }
  25. function handleDrag(event) {
  26. if (!bar.value || !thumb.value || props.disabled)
  27. return;
  28. const el = instance.vnode.el;
  29. const rect = el.getBoundingClientRect();
  30. const { clientX, clientY } = getClientXY(event);
  31. if (!props.vertical) {
  32. let left = clientX - rect.left;
  33. left = Math.max(thumb.value.offsetWidth / 2, left);
  34. left = Math.min(left, rect.width - thumb.value.offsetWidth / 2);
  35. props.color.set("alpha", Math.round((left - thumb.value.offsetWidth / 2) / (rect.width - thumb.value.offsetWidth) * 100));
  36. } else {
  37. let top = clientY - rect.top;
  38. top = Math.max(thumb.value.offsetHeight / 2, top);
  39. top = Math.min(top, rect.height - thumb.value.offsetHeight / 2);
  40. props.color.set("alpha", Math.round((top - thumb.value.offsetHeight / 2) / (rect.height - thumb.value.offsetHeight) * 100));
  41. }
  42. }
  43. function handleKeydown(event) {
  44. if (props.disabled)
  45. return;
  46. const { code, shiftKey } = event;
  47. const step = shiftKey ? 10 : 1;
  48. switch (code) {
  49. case EVENT_CODE.left:
  50. case EVENT_CODE.down:
  51. event.preventDefault();
  52. event.stopPropagation();
  53. incrementPosition(-step);
  54. break;
  55. case EVENT_CODE.right:
  56. case EVENT_CODE.up:
  57. event.preventDefault();
  58. event.stopPropagation();
  59. incrementPosition(step);
  60. break;
  61. }
  62. }
  63. function incrementPosition(step) {
  64. let next = alpha.value + step;
  65. next = next < 0 ? 0 : next > 100 ? 100 : next;
  66. props.color.set("alpha", next);
  67. }
  68. return {
  69. thumb,
  70. bar,
  71. alpha,
  72. alphaLabel,
  73. handleDrag,
  74. handleClick,
  75. handleKeydown
  76. };
  77. };
  78. const useAlphaSliderDOM = (props, {
  79. bar,
  80. thumb,
  81. handleDrag
  82. }) => {
  83. const instance = getCurrentInstance();
  84. const ns = useNamespace("color-alpha-slider");
  85. const thumbLeft = ref(0);
  86. const thumbTop = ref(0);
  87. const background = ref();
  88. function getThumbLeft() {
  89. if (!thumb.value)
  90. return 0;
  91. if (props.vertical)
  92. return 0;
  93. const el = instance.vnode.el;
  94. const alpha = props.color.get("alpha");
  95. if (!el)
  96. return 0;
  97. return Math.round(alpha * (el.offsetWidth - thumb.value.offsetWidth / 2) / 100);
  98. }
  99. function getThumbTop() {
  100. if (!thumb.value)
  101. return 0;
  102. const el = instance.vnode.el;
  103. if (!props.vertical)
  104. return 0;
  105. const alpha = props.color.get("alpha");
  106. if (!el)
  107. return 0;
  108. return Math.round(alpha * (el.offsetHeight - thumb.value.offsetHeight / 2) / 100);
  109. }
  110. function getBackground() {
  111. if (props.color && props.color.value) {
  112. const { r, g, b } = props.color.toRgb();
  113. return `linear-gradient(to right, rgba(${r}, ${g}, ${b}, 0) 0%, rgba(${r}, ${g}, ${b}, 1) 100%)`;
  114. }
  115. return "";
  116. }
  117. function update() {
  118. thumbLeft.value = getThumbLeft();
  119. thumbTop.value = getThumbTop();
  120. background.value = getBackground();
  121. }
  122. onMounted(() => {
  123. if (!bar.value || !thumb.value)
  124. return;
  125. const dragConfig = {
  126. drag: (event) => {
  127. handleDrag(event);
  128. },
  129. end: (event) => {
  130. handleDrag(event);
  131. }
  132. };
  133. draggable(bar.value, dragConfig);
  134. draggable(thumb.value, dragConfig);
  135. update();
  136. });
  137. watch(() => props.color.get("alpha"), () => update());
  138. watch(() => props.color.value, () => update());
  139. const rootKls = computed(() => [
  140. ns.b(),
  141. ns.is("vertical", props.vertical),
  142. ns.is("disabled", props.disabled)
  143. ]);
  144. const barKls = computed(() => ns.e("bar"));
  145. const thumbKls = computed(() => ns.e("thumb"));
  146. const barStyle = computed(() => ({ background: background.value }));
  147. const thumbStyle = computed(() => ({
  148. left: addUnit(thumbLeft.value),
  149. top: addUnit(thumbTop.value)
  150. }));
  151. return { rootKls, barKls, barStyle, thumbKls, thumbStyle, update };
  152. };
  153. export { useAlphaSlider, useAlphaSliderDOM };
  154. //# sourceMappingURL=use-alpha-slider.mjs.map