handle a clipboard paste event, if supported. Returns whether or not the cursor blink should reset.
(v string)
| 299 | // handle a clipboard paste event, if supported. Returns whether or not the |
| 300 | // cursor blink should reset. |
| 301 | func (m *TextInputModel) handlePaste(v string) bool { |
| 302 | paste := []rune(v) |
| 303 | |
| 304 | var availSpace int |
| 305 | if m.CharLimit > 0 { |
| 306 | availSpace = m.CharLimit - len(m.value) |
| 307 | } |
| 308 | |
| 309 | // If the char limit's been reached cancel |
| 310 | if m.CharLimit > 0 && availSpace <= 0 { |
| 311 | return false |
| 312 | } |
| 313 | |
| 314 | // If there's not enough space to paste the whole thing cut the pasted |
| 315 | // runes down so they'll fit |
| 316 | if m.CharLimit > 0 && availSpace < len(paste) { |
| 317 | paste = paste[:len(paste)-availSpace] |
| 318 | } |
| 319 | |
| 320 | // Stuff before and after the cursor |
| 321 | head := m.value[:m.pos] |
| 322 | tailSrc := m.value[m.pos:] |
| 323 | tail := make([]rune, len(tailSrc)) |
| 324 | copy(tail, tailSrc) |
| 325 | |
| 326 | // Insert pasted runes |
| 327 | for _, r := range paste { |
| 328 | head = append(head, r) |
| 329 | m.pos++ |
| 330 | if m.CharLimit > 0 { |
| 331 | availSpace-- |
| 332 | if availSpace <= 0 { |
| 333 | break |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // Put it all back together |
| 339 | m.value = append(head, tail...) |
| 340 | |
| 341 | // Reset blink state if necessary and run overflow checks |
| 342 | return m.setCursor(m.pos) |
| 343 | } |
| 344 | |
| 345 | // If a max width is defined, perform some logic to treat the visible area |
| 346 | // as a horizontally scrolling Viewport. |