| |
| |
| |
| export function insertTextAtCursor(element: HTMLTextAreaElement, textToInsert: string) { |
| element.focus(); |
|
|
| |
| if (window.getSelection() && document.queryCommandSupported('insertText')) { |
| document.execCommand('insertText', false, textToInsert); |
| } else { |
| console.warn('insertTextAtCursor: document.execCommand is not supported'); |
| const startPos = element.selectionStart; |
| const endPos = element.selectionEnd; |
| const beforeText = element.value.substring(0, startPos); |
| const afterText = element.value.substring(endPos); |
| element.value = beforeText + textToInsert + afterText; |
| element.selectionStart = element.selectionEnd = startPos + textToInsert.length; |
| const event = new Event('input', { bubbles: true }); |
| element.dispatchEvent(event); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export const forceResize = (element: HTMLTextAreaElement | null) => { |
| if (!element) { |
| return; |
| } |
| element.style.height = 'auto'; |
| element.style.height = `${element.scrollHeight}px`; |
| }; |
|
|
| |
| |
| |
| export const trimUndoneRange = (textAreaRef: React.RefObject<HTMLTextAreaElement>) => { |
| if (!textAreaRef.current) { |
| return; |
| } |
| const { value, selectionStart, selectionEnd } = textAreaRef.current; |
| const afterCursor = value.substring(selectionEnd).trim(); |
| if (afterCursor.length) { |
| return; |
| } |
| const beforeCursor = value.substring(0, selectionStart); |
| const newValue = beforeCursor + afterCursor; |
| textAreaRef.current.value = newValue; |
| textAreaRef.current.setSelectionRange(selectionStart, selectionStart); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function removeCharIfLast(textarea: HTMLTextAreaElement, charToRemove: string) { |
| if (textarea.value.endsWith(charToRemove)) { |
| textarea.value = textarea.value.slice(0, -1); |
| textarea.setSelectionRange(textarea.value.length, textarea.value.length); |
| textarea.dispatchEvent(new Event('input', { bubbles: true })); |
| } |
|
|
| textarea.focus(); |
| } |
|
|
| |
| |
| |
| |
| |
| export const checkIfScrollable = (element: HTMLTextAreaElement | null) => { |
| if (!element) { |
| return false; |
| } |
| return element.scrollHeight > element.clientHeight; |
| }; |
|
|