import Icon from "../Icon"; import {eLayoutSectionPosition} from "../Layout/layout.utils"; import {cacheComputed, computed, getComponentCls, iHTMLDivElement, iMouseEvent, useRefs} from "@peryl/react-compose"; import {createEffects} from "@peryl/utils/createEffects"; import {ClientZoom} from "../../utils/ClientZoom"; export function useLayoutSectionResizer( { model, collapseModel, props }: { model: { value?: number }, collapseModel: { value?: boolean }, props: { position: typeof eLayoutSectionPosition.TYPE } } ) { const { refs, onRef } = useRefs({ sep: iHTMLDivElement }); const isVertical = computed(() => props.position === eLayoutSectionPosition.top || props.position === eLayoutSectionPosition.bottom); const icon = cacheComputed(() => { switch (props.position) { case "center": return null; case "left": return collapseModel.value ? 'pi-caret-right' : 'pi-caret-left'; case "right": return collapseModel.value ? 'pi-caret-left' : 'pi-caret-right'; case "top": return collapseModel.value ? 'pi-caret-down' : 'pi-caret-up'; case "bottom": return collapseModel.value ? 'pi-caret-up' : 'pi-caret-down'; } return null; }); const draggier = computed(() => { return isVertical.value ? (() => { const { effects } = createEffects(); let staticState = null as null | { startY: number, moveY: number, startValue: number, shadowEl: HTMLDivElement, }; const onMousedown = (e: iMouseEvent) => { const { clientY } = ClientZoom.getClientPosition(e); const shadowEl = (() => { const rect = refs.sep!.getBoundingClientRect(); const el = document.createElement('div'); el.style.height = '1px'; el.style.position = 'fixed'; el.style.left = `${rect.left}px`; el.style.top = `${clientY}px`; el.style.width = `${rect.width}px`; el.style.zIndex = "10"; el.style.backgroundColor = `var(--${getComponentCls('primary-6')})`; document.body.appendChild(el); effects.push(() => { document.body.removeChild(el); }); return el; })(); staticState = { startY: clientY, moveY: clientY, startValue: model.value!, shadowEl, }; window.addEventListener('mousemove', onMousemove); window.addEventListener('mouseup', effects.clear); effects.push(() => { if (!staticState) {return;} let durY = staticState!.moveY - staticState!.startY; if (props.position === eLayoutSectionPosition.bottom) { durY = -durY; } model.value = staticState.startValue + durY; staticState = null; window.removeEventListener('mousemove', onMousemove); window.removeEventListener('mouseup', effects.clear); }); }; const onMousemove = (e: MouseEvent) => { if (!staticState) {return;} const { clientY } = ClientZoom.getClientPosition(e); staticState.moveY = clientY; staticState.shadowEl.style.top = `${clientY}px`; }; return { onMousedown }; })() : (() => { const { effects } = createEffects(); let staticState = null as null | { startX: number, moveX: number, startValue: number, shadowEl: HTMLDivElement, }; const onMousedown = (e: iMouseEvent) => { const { clientX } = ClientZoom.getClientPosition(e); const shadowEl = (() => { const rect = refs.sep!.getBoundingClientRect(); const el = document.createElement('div'); el.style.width = '1px'; el.style.position = 'fixed'; el.style.top = `${rect.top}px`; el.style.left = `${clientX}px`; el.style.height = `${rect.height}px`; el.style.zIndex = "10"; el.style.backgroundColor = `var(--${getComponentCls('primary-6')})`; document.body.appendChild(el); effects.push(() => { document.body.removeChild(el); }); return el; })(); staticState = { startX: clientX, moveX: clientX, startValue: model.value!, shadowEl, }; window.addEventListener('mousemove', onMousemove); window.addEventListener('mouseup', effects.clear); effects.push(() => { if (!staticState) {return;} let durX = staticState!.moveX - staticState!.startX; if (props.position === eLayoutSectionPosition.right) { durX = -durX; } model.value = staticState.startValue + durX; staticState = null; window.removeEventListener('mousemove', onMousemove); window.removeEventListener('mouseup', effects.clear); }); }; const onMousemove = (e: MouseEvent) => { if (!staticState) {return;} const { clientX } = ClientZoom.getClientPosition(e); staticState.moveX = clientX; staticState.shadowEl.style.left = `${clientX}px`; }; return { onMousedown }; })(); }); const toggleCollapse = () => collapseModel.value = !collapseModel.value; const render = () => { return ( !icon.value ? null : <> {!collapseModel.value &&
}
); }; return { render }; }