UNPKG

@fesjs/fes-design

Version:
87 lines (82 loc) 3.16 kB
import _defineProperty from '@babel/runtime/helpers/esm/defineProperty'; import { ref } from 'vue'; import { cloneDeep } from 'lodash-es'; import { useEventListener } from '@vueuse/core'; import { depx } from '../_util/utils'; 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 useResize = (columns, widthMap, handleHeaderResize, isWatchX, isWidthAuto) => { const current = ref(null); let _widthMap = null; const onMousedown = (column, columnIndex, event) => { current.value = { id: column.id, clientX: event.clientX, columnIndex, width: depx(event.target.parentElement.offsetWidth) }; _widthMap = cloneDeep(widthMap.value); isWatchX.value = false; }; const onMousemove = event => { if (!current.value) { return; } if (!_widthMap) { return; } const currentClientX = event.clientX; const offset = currentClientX - current.value.clientX; if (!isWidthAuto.value) { const rightColumns = columns.slice(current.value.columnIndex + 1); widthMap.value[current.value.id].width = offset + _widthMap[current.value.id].width; rightColumns.forEach(col => { widthMap.value[col.id].width = -offset / rightColumns.length + _widthMap[col.id].width; }); } else { widthMap.value[current.value.id] = _objectSpread(_objectSpread({}, _widthMap[current.value.id]), {}, { width: offset + current.value.width, minWidth: offset + current.value.width, maxWidth: offset + current.value.width }); } }; const onMouseup = event => { if (!current.value) { return; } // emit header resize event const currentColumnInstance = columns.find(c => c.id === current.value.id); if (!currentColumnInstance) { return; } handleHeaderResize({ current: { prop: currentColumnInstance.props.prop, width: current.value.width, index: current.value.columnIndex }, columns: columns.map((c, i) => { const width = widthMap.value[c.id].width; return { width, prop: c.props.prop, index: i }; }) }, event); // reset current current.value = null; _widthMap = null; isWatchX.value = true; }; useEventListener(window.document, 'mousemove', onMousemove); useEventListener(window.document, 'mouseup', onMouseup); return { onMousedown, onMousemove, onMouseup, current }; }; export { useResize as default };