import {iInputCompositionData} from "./input.utils"; import {fixInputCursor, iKeyboardEvent, reactive, useClasses, useStyles} from "@peryl/react-compose"; import {unit} from "@peryl/utils/unit"; import {createEffects} from "@peryl/utils/createEffects"; import {ClientZoom} from "../ClientZoom"; const _Textarea = fixInputCursor('textarea'); export function useTextareaInput(data: iInputCompositionData) { const { props, hooks, model, editComputed } = data; const state = reactive({ height: null as null | number }); const { effects } = createEffects(); const handler = { onRef: (el: any) => hooks.onRefInput.exec(el), onInput: (e: any) => { const val = e.target.value as string; if (!!props.maxLength && val.length > props.maxLength) { e.target.value = model.value; return; } model.value = val; hooks.onInput.exec(model.value); }, onKeydown: (e: iKeyboardEvent) => hooks.onInputKeydown.exec(e), }; effects.push(hooks.onClasses.use(prev => [ ...prev || [], { 'input-textarea': true, } ])); const classes = useClasses(() => [ 'input-box input-fill input-textarea-container', { 'input-textarea-has-height': !!state.height || !!props.height || !!props.maxHeight, }, ], effects); const takeoverStyles = useStyles(style => { if (!!props.height || !!state.height) { style.height = unit(props.height || state.height); } if (!!props.minHeight) { style.minHeight = unit(props.minHeight); } if (!!props.maxHeight) { style.maxHeight = unit(props.maxHeight); } }, effects); effects.push(useTextareaResize({ ...data, onResize: (val) => state.height = val })); effects.push(hooks.onRenderDefault.use(() => ( {data.styleComputed.value.inputMode === 'text' ? {model.value} : <> {(model.value?.split('\n') as string[] | undefined)?.map((item, index) => (
{item}
))} {!model.value && <> }
<_Textarea ref={handler.onRef} value={model.value} onInput={handler.onInput} onKeyDown={handler.onKeydown} placeholder={editComputed.value.placeholder!} disabled={!!editComputed.value.disabled} readOnly={!!editComputed.value.readonly || !!props.customReadonly || !!editComputed.value.loading} {...props.nativeAttrs} /> }
))); return effects.clear; } function useTextareaResize({ hooks, onResize }: iInputCompositionData & { onResize: (height: number) => void }) { const { effects } = createEffects(); const staticState = { isDragging: false, start: { height: 0, y: 0, }, move: { y: 0, } }; const handler = { textarea: null as null | HTMLInputElement, mousedown: (e: MouseEvent) => { const { offsetX, offsetY, clientY } = ClientZoom.getClientPosition(e); let { offsetWidth, offsetHeight } = e.target as any; const dragSize = 12; if (!(offsetWidth - offsetX < dragSize && offsetHeight - offsetY < dragSize)) { return; } staticState.isDragging = false; staticState.start = { height: offsetHeight, y: clientY, }; document.documentElement.addEventListener('mousemove', handler.mousemove); document.documentElement.addEventListener('mouseup', handler.mouseup); }, mousemove: (e: MouseEvent) => { const { clientY } = ClientZoom.getClientPosition(e); staticState.move = { y: clientY }; const durY = staticState.move.y - staticState.start.y; if (!staticState.isDragging) { if (Math.abs(durY) > 5) { staticState.isDragging = true; } } if (!staticState.isDragging) { return; } const newHeight = staticState.start.height + durY; onResize(newHeight); }, mouseup: () => { document.documentElement.removeEventListener('mousemove', handler.mousemove); document.documentElement.removeEventListener('mouseup', handler.mouseup); }, }; effects.push(hooks.onRefInput.use(input => { if (!!handler.textarea) { handler.textarea.removeEventListener('mousedown', handler.mousedown); } handler.textarea = input; if (!handler.textarea) {return;} handler.textarea.addEventListener('mousedown', handler.mousedown); })); effects.push(() => { if (!!handler.textarea) { handler.textarea.removeEventListener('mousedown', handler.mousedown); } }); return effects.clear; }