fix(page-controller): address PR review feedback

## Changes

1. **Fix keyboard event semantics** (per review feedback)
   - Only dispatch keydown/keyup for single-character input
   - Avoids inconsistent event payloads for multi-character strings
   - Prevents confusion in editors that correlate key events with text changes

2. **Remove extra blank line**
   - Formatting consistency

Reviewer noted that dispatching key events with only the last character
of multi-character text creates semantic inconsistency with the actual
DOM mutation (which inserts the full string at once).

This fix follows the suggested change from the review.
This commit is contained in:
JasonOA888
2026-03-10 12:00:47 +08:00
parent 28bb2204e7
commit 4e7f755ae9

View File

@@ -140,20 +140,23 @@ export async function inputTextElement(element: HTMLElement, text: string) {
// Dispatch input event (standard)
editableElement.dispatchEvent(new Event('input', { bubbles: true }))
// Dispatch keydown/keyup events for frameworks that listen to keyboard
const keydownEvent = new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
key: text.slice(-1), // Last character
})
editableElement.dispatchEvent(keydownEvent)
// Dispatch keydown/keyup events for frameworks that listen to keyboard.
// To avoid inconsistent semantics, only do this for single-character input.
if (text.length === 1) {
const keydownEvent = new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
key: text,
})
editableElement.dispatchEvent(keydownEvent)
const keyupEvent = new KeyboardEvent('keyup', {
bubbles: true,
cancelable: true,
key: text.slice(-1),
})
editableElement.dispatchEvent(keyupEvent)
const keyupEvent = new KeyboardEvent('keyup', {
bubbles: true,
cancelable: true,
key: text,
})
editableElement.dispatchEvent(keyupEvent)
}
// Dispatch change event (for good measure)
editableElement.dispatchEvent(new Event('change', { bubbles: true }))