UNPKG

@codingame/monaco-vscode-extensions-service-override

Version:

VSCode public API plugged on the monaco editor - extensions service-override

148 lines (144 loc) 6.7 kB
import { __decorate, __param } from '@codingame/monaco-vscode-api/external/tslib/tslib.es6'; import { Disposable, DisposableMap, DisposableStore, toDisposable } from '@codingame/monaco-vscode-api/vscode/vs/base/common/lifecycle'; import { IEditorService } from '@codingame/monaco-vscode-api/vscode/vs/workbench/services/editor/common/editorService.service'; import { extHostNamedCustomer } from '../../services/extensions/common/extHostCustomers.js'; import { ExtHostContext, MainContext } from '@codingame/monaco-vscode-api/vscode/vs/workbench/api/common/extHost.protocol'; import { IBrowserViewCDPService, IBrowserViewWorkbenchService } from '@codingame/monaco-vscode-api/vscode/vs/workbench/contrib/browserView/common/browserView.service'; import { BrowserViewUri } from '@codingame/monaco-vscode-api/vscode/vs/platform/browserView/common/browserViewUri'; import { generateUuid } from '@codingame/monaco-vscode-api/vscode/vs/base/common/uuid'; import { columnToEditorGroup } from '@codingame/monaco-vscode-api/vscode/vs/workbench/services/editor/common/editorGroupColumn'; import { IEditorGroupsService } from '@codingame/monaco-vscode-api/vscode/vs/workbench/services/editor/common/editorGroupsService.service'; import { IConfigurationService } from '@codingame/monaco-vscode-api/vscode/vs/platform/configuration/common/configuration.service'; import { BrowserEditorInput } from '@codingame/monaco-vscode-api/vscode/vs/workbench/contrib/browserView/common/browserEditorInput'; let MainThreadBrowsers = class MainThreadBrowsers extends Disposable { constructor( extHostContext, editorService, cdpService, browserViewService, editorGroupsService, configurationService ) { super(); this.editorService = editorService; this.cdpService = cdpService; this.browserViewService = browserViewService; this.editorGroupsService = editorGroupsService; this.configurationService = configurationService; this._cdpSessions = this._register(( new DisposableMap())); this._knownBrowsers = this._register(( new DisposableMap())); this._lastActiveBrowserId = undefined; this._proxy = ( extHostContext.getProxy(ExtHostContext.ExtHostBrowsers)); this._register(this.browserViewService.onDidChangeBrowserViews(() => { for (const editor of ( this.browserViewService.getKnownBrowserViews().values())) { this._track(editor); } })); this._register( this.editorService.onDidActiveEditorChange(() => this._syncActiveBrowserTab()) ); for (const editor of ( this.browserViewService.getKnownBrowserViews().values())) { this._track(editor); } this._syncActiveBrowserTab(); } async $openBrowserTab(url, viewColumn, options) { const id = generateUuid(); const browserUri = BrowserViewUri.forId(id); await this.editorService.openEditor({ resource: browserUri, options: { ...options, viewState: { url } } }, columnToEditorGroup(this.editorGroupsService, this.configurationService, viewColumn)); const known = this._knownBrowsers.get(id); if (!known) { throw ( new Error("Failed to open browser tab")); } return this._toDto(known.input); } async _syncActiveBrowserTab() { const active = this.editorService.activeEditorPane?.input; let activeId; if (active instanceof BrowserEditorInput) { this._track(active); activeId = active.id; } if (this._lastActiveBrowserId !== activeId) { this._lastActiveBrowserId = activeId; this._proxy.$onDidChangeActiveBrowserTab(activeId); } } _track(input) { if (( this._knownBrowsers.has(input.id))) { return; } const disposables = ( new DisposableStore()); disposables.add(input.onDidChangeLabel(() => { this._proxy.$onDidChangeBrowserTabState(this._toDto(input)); })); disposables.add(input.onWillDispose(() => { this._knownBrowsers.deleteAndDispose(input.id); })); disposables.add(toDisposable(() => { this._proxy.$onDidCloseBrowserTab(input.id); })); this._knownBrowsers.set(input.id, { input, dispose: () => disposables.dispose() }); this._proxy.$onDidOpenBrowserTab(this._toDto(input)); } _toDto(input) { return { id: input.id, url: input.url || "about:blank", title: input.getTitle(), favicon: input.favicon }; } async $startCDPSession(sessionId, browserId) { const known = this._knownBrowsers.get(browserId); if (!known) { throw ( new Error(`Unknown browser id: ${browserId}`)); } await known.input.resolve(); const groupId = await this.cdpService.createSessionGroup(browserId); const disposables = ( new DisposableStore()); disposables.add(this.cdpService.onCDPMessage(groupId)(message => { this._proxy.$onCDPSessionMessage(sessionId, message); })); disposables.add(this.cdpService.onDidDestroy(groupId)(() => { this._cdpSessions.deleteAndDispose(sessionId); })); disposables.add(toDisposable(() => { this.cdpService.destroySessionGroup(groupId).catch(() => {}); this._proxy.$onCDPSessionClosed(sessionId); })); this._cdpSessions.set(sessionId, { groupId, dispose: () => disposables.dispose() }); } async $closeCDPSession(sessionId) { this._cdpSessions.deleteAndDispose(sessionId); } async $sendCDPMessage(sessionId, message) { const session = this._cdpSessions.get(sessionId); if (session) { await this.cdpService.sendCDPMessage(session.groupId, message); } } async $closeBrowserTab(browserId) { const known = this._knownBrowsers.get(browserId); if (!known) { throw ( new Error(`Unknown browser id: ${browserId}`)); } known.input.dispose(); } }; MainThreadBrowsers = __decorate([extHostNamedCustomer(MainContext.MainThreadBrowsers), ( __param(1, IEditorService)), ( __param(2, IBrowserViewCDPService)), ( __param(3, IBrowserViewWorkbenchService)), ( __param(4, IEditorGroupsService)), ( __param(5, IConfigurationService))], MainThreadBrowsers); export { MainThreadBrowsers };