Skip to content
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

Add support for copy and paste in the playground #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions front_end/panels/screencast/InputModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ const MOUSE_EVENT_TYPES: {[key: string]: Protocol.Input.DispatchMouseEventReques

export class InputModel extends SDK.SDKModel.SDKModel<void> {
private readonly inputAgent: ProtocolProxyApi.InputApi;
private readonly runtimeAgent: ProtocolProxyApi.RuntimeApi;
private activeMouseOffsetTop: number|null;

constructor(target: SDK.Target.Target) {
super(target);
this.inputAgent = target.inputAgent();
this.runtimeAgent = target.runtimeAgent();
this.activeMouseOffsetTop = null;
}

Expand All @@ -45,6 +47,13 @@ export class InputModel extends SDK.SDKModel.SDKModel<void> {
return;
}
const text = event.type === 'keypress' ? String.fromCharCode(event.charCode) : undefined;

if (type === Protocol.Input.DispatchKeyEventRequestType.KeyDown &&
((event.ctrlKey || event.metaKey) && event.key === 'v')) {
void this.handlePasteShortcut();
return;
}

void this.inputAgent.invoke_dispatchKeyEvent({
type: type,
modifiers: this.modifiersForEvent(event),
Expand All @@ -60,6 +69,34 @@ export class InputModel extends SDK.SDKModel.SDKModel<void> {
isSystemKey: false,
location: event.location !== 3 ? event.location : undefined,
});

if (type === Protocol.Input.DispatchKeyEventRequestType.KeyDown &&
((event.ctrlKey || event.metaKey) && event.key === 'c')) {
void this.handleCopyShortcut();
}
}

async handleCopyShortcut(): Promise<void> {
try {
const result = await this.runtimeAgent.invoke_evaluate({ expression: 'navigator.clipboard.readText()' });
if (result.result.type === 'string') {
const clipboardText = result.result.value;
await navigator.clipboard.writeText(clipboardText);
}
} catch (error) {
console.error('Error reading clipboard text:', error);
}
}

async handlePasteShortcut(): Promise<void> {
try {
const clipboardText = await navigator.clipboard.readText();
void this.inputAgent.invoke_insertText({
text: clipboardText,
});
} catch (error) {
console.error('Error reading clipboard text:', error);
}
}

emitMouseEvent(event: MouseEvent, offsetTop: number, zoom: number): void {
Expand Down