e31a9f61873c90e998413b24e8a73341baeba3456caa349ff76901619b58e9e468e86ca90d9328a86a84c5bc4a586e926a45bfdc2ea731d3c11f96da8b28b3 5.0 KB

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