mirror of
https://github.com/stablyai/orca.git
synced 2026-11-17 03:15:01 +00:00
fix(dictation): space page rich-text dictation within its block and offer editors the beforeinput
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import { dictationSeparators } from '../../../shared/dictation-transcript-spacing'
|
||||
import {
|
||||
DICTATION_TEXT_BLOCK_SELECTOR,
|
||||
dictationSeparators
|
||||
} from '../../../shared/dictation-transcript-spacing'
|
||||
|
||||
// The focused text field of this frame's document, looking through open shadow roots, or null.
|
||||
const FOCUSED_FIELD_EXPRESSION = `(() => {
|
||||
@@ -27,19 +30,30 @@ const READ_FOCUSED_FIELD_SCRIPT = `(() => {
|
||||
const selection = root.getSelection ? root.getSelection() : document.getSelection()
|
||||
if (!selection || selection.rangeCount === 0) return { before: '', after: '' }
|
||||
const caret = selection.getRangeAt(0)
|
||||
const blockOf = (node) => {
|
||||
const block = (node instanceof Element ? node : node.parentElement)?.closest(${JSON.stringify(DICTATION_TEXT_BLOCK_SELECTOR)})
|
||||
return block && field.contains(block) ? block : field
|
||||
}
|
||||
const before = document.createRange()
|
||||
before.selectNodeContents(field)
|
||||
before.selectNodeContents(blockOf(caret.startContainer))
|
||||
before.setEnd(caret.startContainer, caret.startOffset)
|
||||
const after = document.createRange()
|
||||
after.selectNodeContents(field)
|
||||
after.selectNodeContents(blockOf(caret.endContainer))
|
||||
after.setStart(caret.endContainer, caret.endOffset)
|
||||
return { before: before.toString().slice(-limit), after: after.toString().slice(0, limit) }
|
||||
})()`
|
||||
|
||||
// execCommand types into the frame that owns the field; webContents.insertText only reaches the top frame.
|
||||
// Rich-text editors that model input themselves take a cancelable beforeinput first, as in Orca's own editors.
|
||||
const typeIntoFocusedFieldScript = (text: string): string => `(() => {
|
||||
const text = ${JSON.stringify(text)}
|
||||
const field = ${FOCUSED_FIELD_EXPRESSION}
|
||||
return field ? document.execCommand('insertText', false, ${JSON.stringify(text)}) : false
|
||||
if (!field) return false
|
||||
if (field.isContentEditable) {
|
||||
const beforeInput = new InputEvent('beforeinput', { bubbles: true, cancelable: true, inputType: 'insertText', data: text })
|
||||
if (!field.dispatchEvent(beforeInput)) return true
|
||||
}
|
||||
return document.execCommand('insertText', false, text)
|
||||
})()`
|
||||
|
||||
type FocusedFieldText = { before: string; after: string }
|
||||
|
||||
+6
-10
@@ -47,17 +47,13 @@ function pressDictationInPage(isComposing: boolean) {
|
||||
}
|
||||
|
||||
describe('dictation shortcut from a focused browser page', () => {
|
||||
it('hands the chord to Orca so dictation can target the page', () => {
|
||||
const { prevented, renderer } = pressDictationInPage(false)
|
||||
|
||||
expect(prevented).toBe(true)
|
||||
expect(renderer.send).toHaveBeenCalledWith('ui:dictationKeyDown')
|
||||
})
|
||||
|
||||
it('leaves the chord to the page while an IME composition owns it', () => {
|
||||
const { prevented, renderer } = pressDictationInPage(true)
|
||||
const composing = pressDictationInPage(true)
|
||||
expect(composing.prevented).toBe(false)
|
||||
expect(composing.renderer.send).not.toHaveBeenCalledWith('ui:dictationKeyDown')
|
||||
|
||||
expect(prevented).toBe(false)
|
||||
expect(renderer.send).not.toHaveBeenCalledWith('ui:dictationKeyDown')
|
||||
const typed = pressDictationInPage(false)
|
||||
expect(typed.prevented).toBe(true)
|
||||
expect(typed.renderer.send).toHaveBeenCalledWith('ui:dictationKeyDown')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -8,7 +8,10 @@ import {
|
||||
measureTextControlPasteByteLengthWithYield,
|
||||
pasteTextIntoTextControl
|
||||
} from '@/lib/text-control-paste'
|
||||
import { dictationSeparators } from '../../../../shared/dictation-transcript-spacing'
|
||||
import {
|
||||
DICTATION_TEXT_BLOCK_SELECTOR,
|
||||
dictationSeparators
|
||||
} from '../../../../shared/dictation-transcript-spacing'
|
||||
import { focusedBrowserPageId } from '@/hooks/resolve-zoom-target'
|
||||
import { DICTATION_FOCUS_INPUT_EVENT } from './dictation-control-events'
|
||||
|
||||
@@ -114,9 +117,6 @@ function readSelectionRangeIn(element: HTMLElement): Range | null {
|
||||
return range && element.contains(range.commonAncestorContainer) ? range : null
|
||||
}
|
||||
|
||||
const CONTENT_EDITABLE_BLOCK_SELECTOR =
|
||||
'p, li, h1, h2, h3, h4, h5, h6, pre, blockquote, td, th, div'
|
||||
|
||||
/** The selection's neighbours within its own block, so a new paragraph's start has none. */
|
||||
function readContentEditableNeighbours(element: HTMLElement): { before: string; after: string } {
|
||||
const range = readSelectionRangeIn(element)
|
||||
@@ -125,7 +125,7 @@ function readContentEditableNeighbours(element: HTMLElement): { before: string;
|
||||
}
|
||||
const blockOf = (node: Node): Node => {
|
||||
const block = (node instanceof Element ? node : node.parentElement)?.closest(
|
||||
CONTENT_EDITABLE_BLOCK_SELECTOR
|
||||
DICTATION_TEXT_BLOCK_SELECTOR
|
||||
)
|
||||
return block && element.contains(block) ? block : element
|
||||
}
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
/** Blocks that bound a rich-text caret's neighbours, so a new paragraph's start has none. */
|
||||
export const DICTATION_TEXT_BLOCK_SELECTOR =
|
||||
'p, li, h1, h2, h3, h4, h5, h6, pre, blockquote, td, th, div'
|
||||
|
||||
const CJK_BOUNDARY_CHAR_RE =
|
||||
/^[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]$/u
|
||||
const NO_SPACE_BEFORE_CHAR_RE = /^[,.;:!?%。,、!?;:))\]}”’»]$/u
|
||||
|
||||
Reference in New Issue
Block a user