( textarea: HTMLTextAreaElement, selected: number[], )
| 43 | }; |
| 44 | |
| 45 | export const getCloseWord = ( |
| 46 | textarea: HTMLTextAreaElement, |
| 47 | selected: number[], |
| 48 | ): [string, number] => { |
| 49 | const [, end] = selected; |
| 50 | const index = textarea.value.substring(0, end).split('\n').length - 1; |
| 51 | const lines = textarea.value.split('\n'); |
| 52 | const line = lines[index]; |
| 53 | const preLines = lines.slice(0, index).join('\n'); |
| 54 | const offset = index === 0 ? 0 : 1; |
| 55 | const base = end - preLines.length - offset; |
| 56 | let lastIndex = 0; |
| 57 | let startIndex = 0; |
| 58 | |
| 59 | const found = line.split(' ').find((word) => { |
| 60 | startIndex = lastIndex; |
| 61 | const current = lastIndex + word.length; |
| 62 | |
| 63 | if (current >= base) { |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | lastIndex = current + 1; |
| 68 | |
| 69 | return false; |
| 70 | }); |
| 71 | |
| 72 | return [found ?? '', preLines.length + startIndex + offset]; |
| 73 | }; |
| 74 | |
| 75 | /** |
| 76 | * Enum to describe the current cursor's state. |
no outgoing calls
no test coverage detected