|
| 1 | +import { t } from '../../../i18n/i18n'; |
| 2 | +import { setButtonTooltip } from '../../../shared/dom/tooltip'; |
| 3 | + |
| 4 | +export const COMPOSER_MIN_HEIGHT = 140; |
| 5 | +export const COMPOSER_MAX_HEIGHT_PERCENT = 0.75; |
| 6 | +export const COMPOSER_KEYBOARD_RESIZE_STEP = 24; |
| 7 | + |
| 8 | +export interface ComposerResizeController { |
| 9 | + destroy(): void; |
| 10 | + refreshLocale(): void; |
| 11 | + reset(): void; |
| 12 | +} |
| 13 | + |
| 14 | +export function calculateComposerMaxHeight(viewHeight: number): number { |
| 15 | + return Math.max(COMPOSER_MIN_HEIGHT, Math.floor(viewHeight * COMPOSER_MAX_HEIGHT_PERCENT)); |
| 16 | +} |
| 17 | + |
| 18 | +export function clampComposerHeight(height: number, viewHeight: number): number { |
| 19 | + return Math.min( |
| 20 | + calculateComposerMaxHeight(viewHeight), |
| 21 | + Math.max(COMPOSER_MIN_HEIGHT, Math.round(height)), |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Makes the top edge of the composer draggable. The footer is anchored at the |
| 27 | + * bottom of the chat view, so dragging upward increases the input area. |
| 28 | + * Double-clicking the handle (or pressing Home) returns to content-driven size. |
| 29 | + */ |
| 30 | +export function attachComposerResize( |
| 31 | + wrapperEl: HTMLElement, |
| 32 | + handleEl: HTMLElement, |
| 33 | + onResize?: () => void, |
| 34 | +): ComposerResizeController { |
| 35 | + const doc = handleEl.ownerDocument; |
| 36 | + const viewWindow = doc.defaultView; |
| 37 | + const containerEl = wrapperEl.closest<HTMLElement>('.qoderian-container'); |
| 38 | + let dragging = false; |
| 39 | + let startClientY = 0; |
| 40 | + let startHeight = COMPOSER_MIN_HEIGHT; |
| 41 | + |
| 42 | + const getViewHeight = (): number => { |
| 43 | + const containerHeight = containerEl?.clientHeight; |
| 44 | + return containerHeight || doc.defaultView?.innerHeight || COMPOSER_MIN_HEIGHT; |
| 45 | + }; |
| 46 | + |
| 47 | + const currentHeight = (): number => { |
| 48 | + const measured = wrapperEl.getBoundingClientRect().height || wrapperEl.clientHeight; |
| 49 | + return measured || COMPOSER_MIN_HEIGHT; |
| 50 | + }; |
| 51 | + |
| 52 | + const updateAriaBounds = (height: number): void => { |
| 53 | + handleEl.setAttribute('aria-valuemin', String(COMPOSER_MIN_HEIGHT)); |
| 54 | + handleEl.setAttribute('aria-valuemax', String(calculateComposerMaxHeight(getViewHeight()))); |
| 55 | + handleEl.setAttribute('aria-valuenow', String(Math.round(height))); |
| 56 | + }; |
| 57 | + |
| 58 | + const applyHeight = (height: number): void => { |
| 59 | + const nextHeight = clampComposerHeight(height, getViewHeight()); |
| 60 | + wrapperEl.classList.add('qoderian-input-wrapper--resized'); |
| 61 | + wrapperEl.style.setProperty('--qoderian-input-wrapper-height', `${nextHeight}px`); |
| 62 | + updateAriaBounds(nextHeight); |
| 63 | + onResize?.(); |
| 64 | + }; |
| 65 | + |
| 66 | + const stopDragging = (): void => { |
| 67 | + if (!dragging) return; |
| 68 | + dragging = false; |
| 69 | + doc.removeEventListener('pointermove', handlePointerMove); |
| 70 | + doc.removeEventListener('pointerup', stopDragging); |
| 71 | + doc.removeEventListener('pointercancel', stopDragging); |
| 72 | + doc.body?.classList.remove('qoderian-composer-resizing'); |
| 73 | + }; |
| 74 | + |
| 75 | + const handlePointerMove = (event: PointerEvent): void => { |
| 76 | + if (!dragging) return; |
| 77 | + applyHeight(startHeight + startClientY - event.clientY); |
| 78 | + }; |
| 79 | + |
| 80 | + const handlePointerDown = (event: PointerEvent): void => { |
| 81 | + if (event.button !== 0) return; |
| 82 | + event.preventDefault(); |
| 83 | + stopDragging(); |
| 84 | + dragging = true; |
| 85 | + startClientY = event.clientY; |
| 86 | + startHeight = currentHeight(); |
| 87 | + doc.addEventListener('pointermove', handlePointerMove); |
| 88 | + doc.addEventListener('pointerup', stopDragging); |
| 89 | + doc.addEventListener('pointercancel', stopDragging); |
| 90 | + doc.body?.classList.add('qoderian-composer-resizing'); |
| 91 | + }; |
| 92 | + |
| 93 | + const reset = (): void => { |
| 94 | + stopDragging(); |
| 95 | + wrapperEl.classList.remove('qoderian-input-wrapper--resized'); |
| 96 | + wrapperEl.style.removeProperty('--qoderian-input-wrapper-height'); |
| 97 | + updateAriaBounds(currentHeight()); |
| 98 | + onResize?.(); |
| 99 | + }; |
| 100 | + |
| 101 | + const handleKeydown = (event: KeyboardEvent): void => { |
| 102 | + if (event.key === 'Home') { |
| 103 | + event.preventDefault(); |
| 104 | + reset(); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + if (event.key !== 'ArrowUp' && event.key !== 'ArrowDown') return; |
| 109 | + event.preventDefault(); |
| 110 | + const delta = event.key === 'ArrowUp' |
| 111 | + ? COMPOSER_KEYBOARD_RESIZE_STEP |
| 112 | + : -COMPOSER_KEYBOARD_RESIZE_STEP; |
| 113 | + applyHeight(currentHeight() + delta); |
| 114 | + }; |
| 115 | + |
| 116 | + const refreshLocale = (): void => { |
| 117 | + setButtonTooltip(handleEl, t('composer.resize')); |
| 118 | + }; |
| 119 | + |
| 120 | + handleEl.setAttribute('role', 'separator'); |
| 121 | + handleEl.setAttribute('aria-orientation', 'horizontal'); |
| 122 | + handleEl.setAttribute('tabindex', '0'); |
| 123 | + updateAriaBounds(currentHeight()); |
| 124 | + refreshLocale(); |
| 125 | + handleEl.addEventListener('pointerdown', handlePointerDown); |
| 126 | + handleEl.addEventListener('dblclick', reset); |
| 127 | + handleEl.addEventListener('keydown', handleKeydown); |
| 128 | + viewWindow?.addEventListener('blur', stopDragging); |
| 129 | + |
| 130 | + let resizeObserver: ResizeObserver | null = null; |
| 131 | + if (typeof ResizeObserver !== 'undefined' && containerEl) { |
| 132 | + resizeObserver = new ResizeObserver(() => { |
| 133 | + if (wrapperEl.classList.contains('qoderian-input-wrapper--resized')) { |
| 134 | + applyHeight(currentHeight()); |
| 135 | + } else { |
| 136 | + updateAriaBounds(currentHeight()); |
| 137 | + } |
| 138 | + }); |
| 139 | + resizeObserver.observe(containerEl); |
| 140 | + } |
| 141 | + |
| 142 | + return { |
| 143 | + destroy: () => { |
| 144 | + stopDragging(); |
| 145 | + resizeObserver?.disconnect(); |
| 146 | + handleEl.removeEventListener('pointerdown', handlePointerDown); |
| 147 | + handleEl.removeEventListener('dblclick', reset); |
| 148 | + handleEl.removeEventListener('keydown', handleKeydown); |
| 149 | + viewWindow?.removeEventListener('blur', stopDragging); |
| 150 | + }, |
| 151 | + refreshLocale, |
| 152 | + reset, |
| 153 | + }; |
| 154 | +} |
0 commit comments