UNPKG

tdesign-mobile-vue

Version:
228 lines (224 loc) 8.47 kB
/** * tdesign v1.9.3 * (c) 2025 TDesign Group * @license MIT */ import _slicedToArray from '@babel/runtime/helpers/slicedToArray'; import { defineComponent, ref, computed, onMounted, watch, createVNode, mergeProps } from 'vue'; import config from '../config.js'; import FabProps from './props.js'; import { useTNodeJSX } from '../hooks/tnode.js'; import { usePrefixClass } from '../hooks/useClass.js'; import { Button } from '../button/index.js'; import { reconvertUnit } from '../shared/util.js'; import 'lodash-es'; import '../hooks/render-tnode.js'; import '../_common/js/utils/general.js'; import '../config-provider/useConfig.js'; import '@babel/runtime/helpers/defineProperty'; import '../config-provider/context.js'; import '../_common/js/global-config/mobile/default-config.js'; import '../_common/js/global-config/mobile/locale/zh_CN.js'; import '../_chunks/dep-f785f3d3.js'; import '@babel/runtime/helpers/typeof'; import '../_chunks/dep-0d44b254.js'; import 'dayjs'; import '../button/button.js'; import '../loading/index.js'; 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 '../loading/icon/spinner.js'; import '../loading/props.js'; import '../shared/dom.js'; import '../loading/plugin.js'; import '../shared/component.js'; import '../button/props.js'; import '../form/hooks.js'; import '../shared/hover.js'; var prefix = config.prefix; var _Fab = defineComponent({ name: "".concat(prefix, "-fab"), props: FabProps, setup: function setup(props) { var renderTNodeJSX = useTNodeJSX(); var fabClass = usePrefixClass("fab"); var fabRef = ref(); var fabButtonRef = ref(); var customNodeRef = ref(); var handleClick = function handleClick(e) { var _props$onClick; (_props$onClick = props.onClick) === null || _props$onClick === void 0 || _props$onClick.call(props, { e: e }); }; var mounted = ref(false); var fabButtonSize = ref({ width: 48, height: 48 }); var btnSwitchPos = ref({ x: 16, y: 32 }); var switchPos = ref({ hasMoved: false, x: btnSwitchPos.value.x, y: btnSwitchPos.value.y, startX: 0, startY: 0, endX: 0, endY: 0 }); var onTouchStart = function onTouchStart(e) { var _props$onDragStart; (_props$onDragStart = props.onDragStart) === null || _props$onDragStart === void 0 || _props$onDragStart.call(props, { e: e }); switchPos.value.startX = e.touches[0].pageX; switchPos.value.startY = e.touches[0].pageY; }; var onTouchMove = function onTouchMove(e) { e.stopPropagation(); e.preventDefault(); if (!props.draggable) { return; } if (e.touches.length <= 0) { return; } var offsetX = e.touches[0].pageX - switchPos.value.startX; var offsetY = e.touches[0].pageY - switchPos.value.startY; var x = Math.floor(switchPos.value.x - offsetX); var y = Math.floor(switchPos.value.y - offsetY); var _getSwitchButtonSafeA = getSwitchButtonSafeAreaXY(x, y); var _getSwitchButtonSafeA2 = _slicedToArray(_getSwitchButtonSafeA, 2); x = _getSwitchButtonSafeA2[0]; y = _getSwitchButtonSafeA2[1]; if (props.draggable !== "vertical") { btnSwitchPos.value.x = x; switchPos.value.endX = x; } if (props.draggable !== "horizontal") { btnSwitchPos.value.y = y; switchPos.value.endY = y; } switchPos.value.hasMoved = true; }; var getSwitchButtonSafeAreaXY = function getSwitchButtonSafeAreaXY(x, y) { var _props$yBounds$, _props$yBounds, _props$yBounds$2, _props$yBounds2; var bottomThreshold = reconvertUnit((_props$yBounds$ = (_props$yBounds = props.yBounds) === null || _props$yBounds === void 0 ? void 0 : _props$yBounds[1]) !== null && _props$yBounds$ !== void 0 ? _props$yBounds$ : 0); var topThreshold = reconvertUnit((_props$yBounds$2 = (_props$yBounds2 = props.yBounds) === null || _props$yBounds2 === void 0 ? void 0 : _props$yBounds2[0]) !== null && _props$yBounds$2 !== void 0 ? _props$yBounds$2 : 0); var docWidth = Math.min(window.innerWidth, document.documentElement.clientWidth, screen.width); var docHeight = Math.min(window.innerHeight, document.documentElement.clientHeight, screen.height); var maxY = docHeight - fabButtonSize.value.height - topThreshold; var maxX = docWidth - fabButtonSize.value.width; x = Math.max(0, Math.min(maxX, x)); y = Math.max(bottomThreshold, Math.min(maxY, y)); return [x, y]; }; var onTouchEnd = function onTouchEnd(e) { var _props$onDragEnd; if (!switchPos.value.hasMoved) { return; } (_props$onDragEnd = props.onDragEnd) === null || _props$onDragEnd === void 0 || _props$onDragEnd.call(props, { e: e }); switchPos.value.startX = 0; switchPos.value.startY = 0; switchPos.value.hasMoved = false; setSwitchPosition(switchPos.value.endX, switchPos.value.endY); }; var setSwitchPosition = function setSwitchPosition(switchX, switchY) { var _getSwitchButtonSafeA3 = getSwitchButtonSafeAreaXY(switchX, switchY); var _getSwitchButtonSafeA4 = _slicedToArray(_getSwitchButtonSafeA3, 2); switchX = _getSwitchButtonSafeA4[0]; switchY = _getSwitchButtonSafeA4[1]; switchPos.value.x = switchX; switchPos.value.y = switchY; if (props.draggable !== "vertical") { btnSwitchPos.value.x = switchX; } if (props.draggable !== "horizontal") { btnSwitchPos.value.y = switchY; } }; var fabStyle = computed(function () { var _btnSwitchPos$value = btnSwitchPos.value, x = _btnSwitchPos$value.x, y = _btnSwitchPos$value.y; return "right: ".concat(x, "px;bottom: ").concat(y, "px;"); }); onMounted(function () { mounted.value = true; resetDraggableParams(); if (fabButtonRef.value || customNodeRef.value) { var _fabButtonRef$value; var info = window.getComputedStyle(((_fabButtonRef$value = fabButtonRef.value) === null || _fabButtonRef$value === void 0 ? void 0 : _fabButtonRef$value.$el) || customNodeRef.value); fabButtonSize.value.height = +reconvertUnit(info.height); fabButtonSize.value.width = +reconvertUnit(info.width); } }); var getFabOriginStyle = function getFabOriginStyle() { var info = window.getComputedStyle(fabRef.value); var _ref = info || {}, right = _ref.right, bottom = _ref.bottom; return { right: +(reconvertUnit(right) || 0), bottom: +(reconvertUnit(bottom) || 0) }; }; var resetDraggableParams = function resetDraggableParams() { var _getFabOriginStyle = getFabOriginStyle(), right = _getFabOriginStyle.right, bottom = _getFabOriginStyle.bottom; btnSwitchPos.value.x = right; btnSwitchPos.value.y = bottom; switchPos.value.x = right; switchPos.value.y = bottom; }; watch(function () { return props.style; }, function () { resetDraggableParams(); }); return function () { var icon = function icon() { return renderTNodeJSX("icon"); }; var defaultContent = createVNode(Button, mergeProps({ "size": "large", "theme": "primary", "shape": props.text ? "round" : "circle", "class": "".concat(fabClass.value, "__button") }, props.buttonProps, { "icon": icon, "ref": fabButtonRef }), { default: function _default() { return [props.text]; } }); var customNode = renderTNodeJSX("default"); return createVNode("div", { "class": fabClass.value, "style": mounted.value && props.draggable ? fabStyle.value : props.style, "onClick": handleClick, "onTouchstart": onTouchStart, "onTouchmove": onTouchMove, "onTouchend": onTouchEnd, "ref": fabRef }, [customNode ? createVNode("div", { "ref": customNodeRef }, [customNode]) : defaultContent]); }; } }); export { _Fab as default }; //# sourceMappingURL=fab.js.map