Dynamically import addon-clipboard

This commit is contained in:
Daniel Imms 2024-06-06 07:14:12 -07:00
parent 30e78576e7
commit 249357b046
No known key found for this signature in database
GPG key ID: E5CF412B63651C69
2 changed files with 24 additions and 37 deletions

View file

@ -1,31 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ClipboardAddon, type ClipboardSelectionType, type IClipboardProvider } from '@xterm/addon-clipboard';
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
export class VscodeClipboardAddon extends ClipboardAddon {
constructor(
@IInstantiationService instantiationService: IInstantiationService
) {
super(undefined, instantiationService.createInstance(VscodeClipboardProvider));
}
}
class VscodeClipboardProvider implements IClipboardProvider {
constructor(
@IClipboardService private readonly _clipboardService: IClipboardService
) {
}
public async readText(type: ClipboardSelectionType): Promise<string> {
return this._clipboardService.readText(type === 'p' ? 'selection' : 'clipboard');
}
public async writeText(type: ClipboardSelectionType, text: string): Promise<void> {
return this._clipboardService.writeText(text, type === 'p' ? 'selection' : 'clipboard');
}
}

View file

@ -9,7 +9,7 @@ import type { Unicode11Addon as Unicode11AddonType } from '@xterm/addon-unicode1
import type { WebglAddon as WebglAddonType } from '@xterm/addon-webgl';
import type { SerializeAddon as SerializeAddonType } from '@xterm/addon-serialize';
import type { ImageAddon as ImageAddonType } from '@xterm/addon-image';
import type { ClipboardAddon as ClipboardAddonType } from '@xterm/addon-clipboard';
import type { ClipboardAddon as ClipboardAddonType, ClipboardSelectionType } from '@xterm/addon-clipboard';
import * as dom from 'vs/base/browser/dom';
import { IXtermCore } from 'vs/workbench/contrib/terminal/browser/xterm-private';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
@ -40,12 +40,12 @@ import { MouseWheelClassifier } from 'vs/base/browser/ui/scrollbar/scrollableEle
import { IMouseWheelEvent, StandardWheelEvent } from 'vs/base/browser/mouseEvent';
import { ILayoutService } from 'vs/platform/layout/browser/layoutService';
import { AccessibilitySignal, IAccessibilitySignalService } from 'vs/platform/accessibilitySignal/browser/accessibilitySignalService';
import { VscodeClipboardAddon } from 'vs/workbench/contrib/terminal/browser/xterm/vscodeClipboardAddon';
const enum RenderConstants {
SmoothScrollDuration = 125
}
let ClipboardAddon: typeof ClipboardAddonType;
let ImageAddon: typeof ImageAddonType;
let SearchAddon: typeof SearchAddonType;
let SerializeAddon: typeof SerializeAddonType;
@ -119,7 +119,9 @@ export class XtermTerminal extends Disposable implements IXtermTerminal, IDetach
private _markNavigationAddon: MarkNavigationAddon;
private _shellIntegrationAddon: ShellIntegrationAddon;
private _decorationAddon: DecorationAddon;
private _clipboardAddon: ClipboardAddonType;
// Always on dynamicly imported addons
private _clipboardAddon?: ClipboardAddonType;
// Optional addons
private _searchAddon?: SearchAddonType;
@ -276,8 +278,17 @@ export class XtermTerminal extends Disposable implements IXtermTerminal, IDetach
this.raw.loadAddon(this._decorationAddon);
this._shellIntegrationAddon = new ShellIntegrationAddon(shellIntegrationNonce, disableShellIntegrationReporting, this._telemetryService, this._logService);
this.raw.loadAddon(this._shellIntegrationAddon);
this._clipboardAddon = this._instantiationService.createInstance(VscodeClipboardAddon);
this.raw.loadAddon(this._clipboardAddon);
this._getClipboardAddonConstructor().then(ClipboardAddon => {
this._clipboardAddon = this._instantiationService.createInstance(ClipboardAddon, undefined, {
async readText(type: ClipboardSelectionType): Promise<string> {
return _clipboardService.readText(type === 'p' ? 'selection' : 'clipboard');
},
async writeText(type: ClipboardSelectionType, text: string): Promise<void> {
return _clipboardService.writeText(text, type === 'p' ? 'selection' : 'clipboard');
}
});
this.raw.loadAddon(this._clipboardAddon);
});
this._anyTerminalFocusContextKey = TerminalContextKeys.focusInAny.bindTo(contextKeyService);
this._anyFocusedTerminalHasSelection = TerminalContextKeys.textSelectedInFocused.bindTo(contextKeyService);
@ -330,7 +341,7 @@ export class XtermTerminal extends Disposable implements IXtermTerminal, IDetach
this.raw.open(container);
}
// TODO: Move before open to the DOM renderer doesn't initialize
// TODO: Move before open so the DOM renderer doesn't initialize
if (options.enableGpu) {
if (this._shouldLoadWebgl()) {
this._enableWebglRenderer();
@ -715,6 +726,13 @@ export class XtermTerminal extends Disposable implements IXtermTerminal, IDetach
}
}
protected async _getClipboardAddonConstructor(): Promise<typeof ClipboardAddonType> {
if (!ClipboardAddon) {
ClipboardAddon = (await importAMDNodeModule<typeof import('@xterm/addon-clipboard')>('@xterm/addon-clipboard', 'lib/addon-clipboard.js')).ClipboardAddon;
}
return ClipboardAddon;
}
protected async _getImageAddonConstructor(): Promise<typeof ImageAddonType> {
if (!ImageAddon) {
ImageAddon = (await importAMDNodeModule<typeof import('@xterm/addon-image')>('@xterm/addon-image', 'lib/addon-image.js')).ImageAddon;