Enable native clipboard for editor and inputs
StackOverflow will be useful again.
This commit is contained in:
@ -4,7 +4,7 @@ import { InitData, ISharedProcessData } from "@coder/protocol";
|
||||
import { retry } from "./retry";
|
||||
import { Upload } from "./upload";
|
||||
import { client } from "./fill/client";
|
||||
import { Clipboard, clipboard } from "./fill/clipboard";
|
||||
import { clipboard } from "./fill/clipboard";
|
||||
import { INotificationService, NotificationService, IProgressService, ProgressService } from "./fill/notification";
|
||||
import { IURIFactory } from "./fill/uri";
|
||||
|
||||
@ -19,7 +19,7 @@ import { IURIFactory } from "./fill/uri";
|
||||
export abstract class Client {
|
||||
|
||||
public readonly retry = retry;
|
||||
public readonly clipboard: Clipboard = clipboard;
|
||||
public readonly clipboard = clipboard;
|
||||
public readonly uriFactory: IURIFactory;
|
||||
public readonly upload = new Upload(new NotificationService(), new ProgressService());
|
||||
private start: Time | undefined;
|
||||
|
@ -36,6 +36,30 @@ export class Clipboard {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Paste currently copied text.
|
||||
*/
|
||||
public async paste(): Promise<boolean> {
|
||||
if (this.isEnabled) {
|
||||
try {
|
||||
const element = document.activeElement as HTMLInputElement | HTMLTextAreaElement;
|
||||
const start = element.selectionStart || 0;
|
||||
const end = element.selectionEnd;
|
||||
const allText = element.value;
|
||||
const newText = allText.substring(0, start)
|
||||
+ (await this.readText())
|
||||
+ allText.substring(end || start);
|
||||
element.value = newText;
|
||||
|
||||
return true;
|
||||
} catch (ex) {
|
||||
// Will try execCommand below.
|
||||
}
|
||||
}
|
||||
|
||||
return document.execCommand("paste");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the native clipboard is supported.
|
||||
*/
|
||||
|
Reference in New Issue
Block a user