Archived
1
0

Enable native clipboard for editor and inputs

StackOverflow will be useful again.
This commit is contained in:
Asher
2019-01-30 16:57:22 -06:00
committed by Kyle Carberry
parent ebe5e1b1a9
commit bef46391fa
4 changed files with 160 additions and 11 deletions

View File

@ -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;

View File

@ -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.
*/