bb7f174a604506d810e2741ca10fa47e63ef667896cd96da3f7ad7c7d9bf55559df29296ae428f479e263b51e4ad2218e9cd206fd7d2dab9980d14b5f05ca8 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { defineComponent, getCurrentInstance, ref, computed, watch, onMounted, openBlock, createElementBlock, normalizeClass, normalizeStyle, createElementVNode } 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: "ElSlPanel",
  8. props: {
  9. color: {
  10. type: Object,
  11. required: true
  12. },
  13. disabled: Boolean
  14. },
  15. setup(props) {
  16. const ns = useNamespace("color-svpanel");
  17. const instance = getCurrentInstance();
  18. const cursorTop = ref(0);
  19. const cursorLeft = ref(0);
  20. const background = ref("hsl(0, 100%, 50%)");
  21. const colorValue = computed(() => {
  22. const hue = props.color.get("hue");
  23. const value = props.color.get("value");
  24. return { hue, value };
  25. });
  26. function update() {
  27. const saturation = props.color.get("saturation");
  28. const value = props.color.get("value");
  29. const el = instance.vnode.el;
  30. const { clientWidth: width, clientHeight: height } = el;
  31. cursorLeft.value = saturation * width / 100;
  32. cursorTop.value = (100 - value) * height / 100;
  33. background.value = `hsl(${props.color.get("hue")}, 100%, 50%)`;
  34. }
  35. function handleDrag(event) {
  36. if (props.disabled)
  37. return;
  38. const el = instance.vnode.el;
  39. const rect = el.getBoundingClientRect();
  40. const { clientX, clientY } = getClientXY(event);
  41. let left = clientX - rect.left;
  42. let top = clientY - rect.top;
  43. left = Math.max(0, left);
  44. left = Math.min(left, rect.width);
  45. top = Math.max(0, top);
  46. top = Math.min(top, rect.height);
  47. cursorLeft.value = left;
  48. cursorTop.value = top;
  49. props.color.set({
  50. saturation: left / rect.width * 100,
  51. value: 100 - top / rect.height * 100
  52. });
  53. }
  54. watch(() => colorValue.value, () => {
  55. update();
  56. });
  57. onMounted(() => {
  58. draggable(instance.vnode.el, {
  59. drag: (event) => {
  60. handleDrag(event);
  61. },
  62. end: (event) => {
  63. handleDrag(event);
  64. }
  65. });
  66. update();
  67. });
  68. return {
  69. cursorTop,
  70. cursorLeft,
  71. background,
  72. colorValue,
  73. handleDrag,
  74. update,
  75. ns
  76. };
  77. }
  78. });
  79. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  80. return openBlock(), createElementBlock("div", {
  81. class: normalizeClass(_ctx.ns.b()),
  82. style: normalizeStyle({
  83. backgroundColor: _ctx.background
  84. })
  85. }, [
  86. createElementVNode("div", {
  87. class: normalizeClass(_ctx.ns.e("white"))
  88. }, null, 2),
  89. createElementVNode("div", {
  90. class: normalizeClass(_ctx.ns.e("black"))
  91. }, null, 2),
  92. createElementVNode("div", {
  93. class: normalizeClass(_ctx.ns.e("cursor")),
  94. style: normalizeStyle({
  95. top: _ctx.cursorTop + "px",
  96. left: _ctx.cursorLeft + "px"
  97. })
  98. }, [
  99. createElementVNode("div")
  100. ], 6)
  101. ], 6);
  102. }
  103. var SvPanel = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "sv-panel.vue"]]);
  104. export { SvPanel as default };
  105. //# sourceMappingURL=sv-panel.mjs.map