Repository navigation
fix(browser): emulate CDP page focus for rich-editor insertText #10376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
innocarpe
wants to merge
3
commits into
stablyai:main
from
innocarpe:fix/browser-cdp-focus-emulation
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { CDP_FOCUS_EMULATION_METHOD, enableCdpFocusEmulation } from './cdp-focus-emulation' | ||
|
|
||
| describe('enableCdpFocusEmulation', () => { | ||
| it('enables Emulation.setFocusEmulationEnabled over CDP', async () => { | ||
| const send = vi.fn(async () => ({})) | ||
|
|
||
| await enableCdpFocusEmulation(send) | ||
|
|
||
| expect(send).toHaveBeenCalledWith(CDP_FOCUS_EMULATION_METHOD, { enabled: true }) | ||
| }) | ||
|
|
||
| it('bounds a stalled debugger command', async () => { | ||
| vi.useFakeTimers() | ||
| try { | ||
| const send = vi.fn(() => new Promise<unknown>(() => {})) | ||
| const result = enableCdpFocusEmulation(send) | ||
| const expectation = expect(result).rejects.toThrow('CDP focus emulation timed out') | ||
|
|
||
| await vi.advanceTimersByTimeAsync(5_000) | ||
|
|
||
| await expectation | ||
| expect(vi.getTimerCount()).toBe(0) | ||
| } finally { | ||
| vi.useRealTimers() | ||
| } | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /** | ||
| * CDP focus emulation so `document.hasFocus()` reports true even when the | ||
| * guest WebContents is not the OS first responder (Electron 43+ / multi-view). | ||
| * | ||
| * Required for rich editors (Draft.js, etc.) that gate Input.insertText and | ||
| * execCommand on frame focus — see #10375 / regression of #7035. | ||
| */ | ||
| export const CDP_FOCUS_EMULATION_METHOD = 'Emulation.setFocusEmulationEnabled' as const | ||
| const CDP_FOCUS_EMULATION_TIMEOUT_MS = 5_000 | ||
|
|
||
| export type CdpFocusEmulationSender = ( | ||
| method: string, | ||
| params?: Record<string, unknown> | ||
| ) => Promise<unknown> | ||
|
|
||
| export async function enableCdpFocusEmulation(send: CdpFocusEmulationSender): Promise<void> { | ||
| let timer: ReturnType<typeof setTimeout> | null = null | ||
| try { | ||
| await Promise.race([ | ||
| send(CDP_FOCUS_EMULATION_METHOD, { enabled: true }), | ||
| new Promise<never>((_, reject) => { | ||
| timer = setTimeout( | ||
| () => reject(new Error(`CDP focus emulation timed out: ${CDP_FOCUS_EMULATION_METHOD}`)), | ||
| CDP_FOCUS_EMULATION_TIMEOUT_MS | ||
| ) | ||
| }) | ||
| ]) | ||
| } finally { | ||
| if (timer) { | ||
| clearTimeout(timer) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.