UNPKG

tdesign-vue

Version:
369 lines (365 loc) 13 kB
/** * tdesign v1.14.1 * (c) 2025 tdesign * @license MIT */ import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { defineComponent, ref, reactive, computed, watch, onMounted, onBeforeUnmount } from '@vue/composition-api'; import { cloneDeep } from 'lodash-es'; import { GRADIENT_SLIDER_DEFAULT_WIDTH } from '../../_common/js/color-picker/constants.js'; import { InputNumber } from '../../input-number/index.js'; import { useBaseClassName } from '../hooks.js'; import useCommonClassName from '../../hooks/useCommonClassName.js'; import baseProps from './base-props.js'; import { gradientColors2string, genGradientPoint } from '../../_common/js/color-picker/color.js'; import '../../input-number/input-number.js'; import '../../_chunks/dep-6a4dc7bb.js'; import 'tdesign-icons-vue'; import '../../button/index.js'; import '../../button/button.js'; import '../../loading/index.js'; import '../../loading/directive.js'; import '../../loading/plugin.js'; import 'vue'; import '../../loading/loading.js'; import '../../loading/icon/gradient.js'; import '../../_common/js/loading/circle-adapter.js'; import '../../_common/js/utils/setStyle.js'; import '../../_common/js/utils/helper.js'; import '@babel/runtime/helpers/toConsumableArray'; import '@babel/runtime/helpers/objectWithoutProperties'; import '@babel/runtime/helpers/slicedToArray'; import '../../config-provider/config-receiver.js'; import '../../config-provider/context.js'; import '../../_common/js/global-config/default-config.js'; import '../../_common/js/global-config/locale/zh_CN.js'; import '../../_chunks/dep-ba613a02.js'; import '@babel/runtime/helpers/typeof'; import '../../_chunks/dep-fdb1b253.js'; import 'dayjs'; import '../../_common/js/global-config/t.js'; import '../../utils/mixins.js'; import '../../utils/dom.js'; import 'raf'; import '../../utils/easing.js'; import '../../utils/render-tnode.js'; import '@babel/runtime/helpers/readOnlyError'; import '../../utils/transfer-dom.js'; import '../../loading/props.js'; import '../../config.js'; import '../../utils/withInstall.js'; import '../../button/props.js'; import '../../utils/ripple.js'; import '../../input/index.js'; import '../../input/input.js'; import '../../utils/helper.js'; import '../../utils/event.js'; import '../../input/props.js'; import '../../_common/js/log/log.js'; import '../../input/input-group.js'; import '../../utils/map-props.js'; import '../../input-number/props.js'; import '../../input-number/useInputNumber.js'; import '../../hooks/useVModel.js'; import '../../_common/js/input-number/number.js'; import '../../_common/js/input-number/large-number.js'; import '../../hooks/useFormDisabled.js'; import '../../config-provider/useConfig.js'; import '../../hooks/useGlobalIcon.js'; import '../../hooks/useConfig.js'; import '@babel/runtime/helpers/classCallCheck'; import '@babel/runtime/helpers/createClass'; import 'tinycolor2'; import '../../_common/js/color-picker/cmyk.js'; import '../../_common/js/color-picker/gradient.js'; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } var DELETE_KEYS = ["delete", "backspace"]; var LinearGradient = defineComponent({ name: "LinearGradient", components: { TInputNumber: InputNumber }, inheritAttrs: false, props: _objectSpread(_objectSpread({}, baseProps), {}, { enableMultipleGradient: { type: Boolean, "default": true } }), setup: function setup(props) { var baseClassName = useBaseClassName(); var _useCommonClassName = useCommonClassName(), statusClassNames = _useCommonClassName.statusClassNames; var refSlider = ref(null); var sliderRect = reactive({ left: 0, width: GRADIENT_SLIDER_DEFAULT_WIDTH }); var isDragging = ref(false); var isMoved = ref(false); var colorInstance = computed(function () { return cloneDeep(props.color); }); var degree = ref(colorInstance.value.gradientDegree); var selectedId = ref(colorInstance.value.gradientSelectedId); var colors = ref(cloneDeep(colorInstance.value.gradientColors)); watch(function () { return props.color.gradientDegree; }, function (value) { return degree.value = value; }); watch(function () { return props.color.gradientSelectedId; }, function (value) { return selectedId.value = value; }); watch(function () { return props.color.gradientColors; }, function (value) { colors.value = cloneDeep(value); }, { deep: true }); var handleChange = function handleChange(key, payload, addUsedColor) { if (props.disabled) { return; } props.handleChange({ key: key, payload: payload, addUsedColor: addUsedColor }); }; var handleDegreeChange = function handleDegreeChange(value) { if (props.disabled || value === colorInstance.value.gradientDegree) { return; } degree.value = value; handleChange("degree", value, true); }; var handleSelectedIdChange = function handleSelectedIdChange(value) { if (props.disabled) { return; } selectedId.value = value; handleChange("selectedId", value); }; var handleColorsChange = function handleColorsChange(value, isEnded) { if (props.disabled) { return; } colors.value = value; handleChange("colors", value, isEnded); }; var updateActiveThumbLeft = function updateActiveThumbLeft(left) { var index = colors.value.findIndex(function (c) { return c.id === selectedId.value; }); if (index === -1) { return; } var point = colors.value[index]; left = Math.max(0, Math.min(sliderRect.width, left)); var percentLeft = left / sliderRect.width * 100; colors.value.splice(index, 1, { color: point.color, left: percentLeft, id: point.id }); handleColorsChange(colors.value); }; var handleStart = function handleStart(id) { var rect = refSlider.value.getBoundingClientRect(); sliderRect.left = rect.left; sliderRect.width = rect.width || GRADIENT_SLIDER_DEFAULT_WIDTH; if (isDragging.value || props.disabled) { return; } isMoved.value = false; isDragging.value = true; handleSelectedIdChange(id); refSlider.value.focus(); window.addEventListener("mousemove", handleMove, false); window.addEventListener("mouseup", _handleEnd, false); window.addEventListener("contextmenu", _handleEnd, false); }; var handleMove = function handleMove(e) { if (!isDragging.value || props.disabled) { return; } var left = e.clientX - sliderRect.left; isMoved.value = true; updateActiveThumbLeft(left); }; var _handleEnd = function handleEnd() { if (!isDragging.value) { return; } setTimeout(function () { isDragging.value = false; }, 0); if (isMoved.value) { handleColorsChange(colors.value, true); isMoved.value = false; } window.removeEventListener("mousemove", handleMove, false); window.removeEventListener("mouseup", _handleEnd, false); window.removeEventListener("contextmenu", _handleEnd, false); }; var handleKeyup = function handleKeyup(e) { if (props.disabled) { return; } var points = colors.value; var pos = points.findIndex(function (c) { return c.id === selectedId.value; }); var length = points.length; if (DELETE_KEYS.includes(e.key.toLocaleLowerCase()) && length > 2 && pos >= 0 && pos <= length - 1) { points.splice(pos, 1); if (!points[pos]) { pos = points[pos + 1] ? pos + 1 : points[pos - 1] ? pos - 1 : 0; } var current = points[pos]; handleColorsChange(points, true); handleSelectedIdChange(current === null || current === void 0 ? void 0 : current.id); } }; var handleThumbBarClick = function handleThumbBarClick(e) { if (props.disabled || !props.enableMultipleGradient) { return; } var left = e.clientX - sliderRect.left; left = Math.max(0, Math.min(sliderRect.width, left)); var percentLeft = left / sliderRect.width * 100; var newPoint = genGradientPoint(percentLeft, colorInstance.value.rgba); colors.value.push(newPoint); handleColorsChange(colors.value, true); handleSelectedIdChange(newPoint.id); }; onMounted(function () { var rect = refSlider.value.getBoundingClientRect(); sliderRect.left = rect.left; sliderRect.width = rect.width || GRADIENT_SLIDER_DEFAULT_WIDTH; }); onBeforeUnmount(function () { window.removeEventListener("mousemove", handleMove, false); window.removeEventListener("mouseup", _handleEnd, false); window.removeEventListener("contextmenu", _handleEnd, false); }); return { baseClassName: baseClassName, statusClassNames: statusClassNames, refSlider: refSlider, degree: degree, selectedId: selectedId, colors: colors, colorInstance: colorInstance, handleDegreeChange: handleDegreeChange, handleStart: handleStart, handleMove: handleMove, handleEnd: _handleEnd, handleKeyup: handleKeyup, handleThumbBarClick: handleThumbBarClick }; }, render: function render() { var _this = this; var h = arguments[0]; var _this$colorInstance = this.colorInstance, linearGradient = _this$colorInstance.linearGradient, gradientColors = _this$colorInstance.gradientColors; var colors = this.colors, selectedId = this.selectedId, degree = this.degree, disabled = this.disabled, baseClassName = this.baseClassName, statusClassNames = this.statusClassNames; var thumbBackground = gradientColors2string({ points: gradientColors, degree: 90 }); return h("div", { "class": "".concat(baseClassName, "__gradient") }, [h("div", { "class": "".concat(baseClassName, "__gradient-slider") }, [h("div", { "class": ["".concat(baseClassName, "__slider"), "".concat(baseClassName, "--bg-alpha")], "on": { "keyup": this.handleKeyup }, "attrs": { "tabindex": 0 }, "ref": "refSlider" }, [h("ul", { "class": "gradient-thumbs", "on": { "click": this.handleThumbBarClick }, "style": { background: thumbBackground } }, [colors.map(function (t) { var left = "".concat(Math.round(t.left * 100) / 100, "%"); return h("li", { "class": ["".concat(baseClassName, "__thumb"), "gradient-thumbs__item", selectedId === t.id ? statusClassNames.active : ""], "key": t.id, "attrs": { "title": "".concat(t.color, " ").concat(left) }, "style": { color: t.color, left: left }, "on": { "click": function click(e) { return e.stopPropagation(); }, "mousedown": function mousedown() { return _this.handleStart(t.id); } } }, [h("span", { "class": ["gradient-thumbs__item-inner", "".concat(baseClassName, "--bg-alpha")] })]); })])])]), h("div", { "class": "".concat(baseClassName, "__gradient-degree"), "attrs": { "title": "".concat(degree, "deg") } }, [h("t-input-number", { "attrs": { "theme": "normal", "size": "small", "min": 0, "max": 360, "step": 1, "format": function format(value) { return "".concat(value, "\xB0"); }, "disabled": disabled }, "on": { "blur": this.handleDegreeChange, "enter": this.handleDegreeChange }, "model": { value: _this.degree, callback: function callback($$v) { _this.degree = $$v; } } })]), h("div", { "class": ["".concat(baseClassName, "__gradient-preview"), "".concat(baseClassName, "--bg-alpha")] }, [h("span", { "class": "preview-inner", "style": { background: linearGradient } })])]); } }); export { LinearGradient as default }; //# sourceMappingURL=linear-gradient.js.map