mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-14 18:46:34 -05:00
116 lines
2.8 KiB
TypeScript
116 lines
2.8 KiB
TypeScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
|
|
import { URI } from 'vs/base/common/uri';
|
|
import { $ } from 'vs/base/browser/dom';
|
|
|
|
export class BrowserClipboardService implements IClipboardService {
|
|
|
|
_serviceBrand: undefined;
|
|
|
|
private readonly mapTextToType = new Map<string, string>(); // unsupported in web (only in-memory)
|
|
|
|
async writeText(text: string, type?: string): Promise<void> {
|
|
|
|
// With type: only in-memory is supported
|
|
if (type) {
|
|
this.mapTextToType.set(type, text);
|
|
|
|
return;
|
|
}
|
|
|
|
// Guard access to navigator.clipboard with try/catch
|
|
// as we have seen DOMExceptions in certain browsers
|
|
// due to security policies.
|
|
try {
|
|
return await navigator.clipboard.writeText(text);
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
|
|
// Fallback to textarea and execCommand solution
|
|
|
|
const activeElement = document.activeElement;
|
|
|
|
const textArea: HTMLTextAreaElement = document.body.appendChild($('textarea', { 'aria-hidden': true }));
|
|
textArea.style.height = '1px';
|
|
textArea.style.width = '1px';
|
|
textArea.style.position = 'absolute';
|
|
|
|
textArea.value = text;
|
|
textArea.focus();
|
|
textArea.select();
|
|
|
|
document.execCommand('copy');
|
|
|
|
if (activeElement instanceof HTMLElement) {
|
|
activeElement.focus();
|
|
}
|
|
|
|
document.body.removeChild(textArea);
|
|
|
|
return;
|
|
}
|
|
|
|
async readText(type?: string): Promise<string> {
|
|
|
|
// With type: only in-memory is supported
|
|
if (type) {
|
|
return this.mapTextToType.get(type) || '';
|
|
}
|
|
|
|
// Guard access to navigator.clipboard with try/catch
|
|
// as we have seen DOMExceptions in certain browsers
|
|
// due to security policies.
|
|
try {
|
|
return await navigator.clipboard.readText();
|
|
} catch (error) {
|
|
console.error(error);
|
|
|
|
return '';
|
|
}
|
|
}
|
|
|
|
private findText = ''; // unsupported in web (only in-memory)
|
|
|
|
async readFindText(): Promise<string> {
|
|
return this.findText;
|
|
}
|
|
|
|
async writeFindText(text: string): Promise<void> {
|
|
this.findText = text;
|
|
}
|
|
|
|
private resources: URI[] = []; // unsupported in web (only in-memory)
|
|
|
|
async writeResources(resources: URI[]): Promise<void> {
|
|
this.resources = resources;
|
|
}
|
|
|
|
async readResources(): Promise<URI[]> {
|
|
return this.resources;
|
|
}
|
|
|
|
async hasResources(): Promise<boolean> {
|
|
return this.resources.length > 0;
|
|
}
|
|
|
|
/** @deprecated */
|
|
readTextSync(): string | undefined {
|
|
return undefined;
|
|
}
|
|
|
|
/** @deprecated */
|
|
readFindTextSync(): string {
|
|
return this.findText;
|
|
}
|
|
|
|
/** @deprecated */
|
|
writeFindTextSync(text: string): void {
|
|
this.findText = text;
|
|
}
|
|
}
|