Skip to content

Commit

Permalink
Merge pull request gitpod-io#1884 from gitpod-io/ak/expose_server_to_ide
Browse files Browse the repository at this point in the history
[supervisor/frontend] expose gitpod server on ide frame
  • Loading branch information
akosyakov authored Sep 23, 2020
1 parent 7f9c561 commit 9cb4125
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
11 changes: 11 additions & 0 deletions components/gitpod-protocol/src/typings/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) 2020 TypeFox GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

interface Window {
gitpod: {
service: import('../gitpod-service').GitpodService
}
}
4 changes: 3 additions & 1 deletion components/supervisor/frontend/src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* See License-AGPL.txt in the project root for license information.
*/

/// <reference types='@gitpod/gitpod-protocol/lib/typings/globals'/>

/**
* API specified by https://wicg.github.io/keyboard-map/
*/
Expand All @@ -13,4 +15,4 @@ interface Navigator {
interface Keyboard {
getLayoutMap?(): Promise<Map<string, string>>;
addEventListener?(type: 'layoutchange', listener: EventListenerOrEventListenerObject): void;
}
}
17 changes: 12 additions & 5 deletions components/supervisor/frontend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ import { createGitpodService } from "@gitpod/gitpod-protocol";
import { GitpodHostUrl } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url";

const workspaceUrl = new GitpodHostUrl(window.location.href);
window.gitpod = {
service: createGitpodService(workspaceUrl.withoutWorkspacePrefix().toString())
};
const { workspaceId } = workspaceUrl;
if (workspaceId) {
const gitpodService = createGitpodService(workspaceUrl.withoutWorkspacePrefix().toString());
gitpodService.server.getWorkspace(workspaceId).then(info => {
const workspaceInfo = workspaceId ? window.gitpod.service.server.getWorkspace(workspaceId) : undefined;
if (!workspaceId) {
document.title += ': Unknown workspace';
console.error(`Failed to extract a workspace id from '${window.location.href}'.`)
} else if (workspaceInfo) {
workspaceInfo.then(info => {
document.title = info.workspace.description;
});
} else {
document.title += ': Unknown workspace';
}

const checkReady: (kind: 'content' | 'ide') => Promise<void> = kind =>
Expand Down Expand Up @@ -59,6 +63,9 @@ Promise.all([onDOMContentLoaded, checkReady('ide'), checkReady('content')]).then
ideFrame.onload = () => loadingFrame.remove();
document.body.appendChild(ideFrame);
ideFrame.contentWindow?.addEventListener('DOMContentLoaded', () => {
if (ideFrame.contentWindow) {
ideFrame.contentWindow.gitpod = window.gitpod;
}
if (navigator.keyboard?.getLayoutMap && ideFrame.contentWindow?.navigator.keyboard?.getLayoutMap) {
ideFrame.contentWindow.navigator.keyboard.getLayoutMap = navigator.keyboard.getLayoutMap.bind(navigator.keyboard);
}
Expand Down
12 changes: 4 additions & 8 deletions components/theia/packages/gitpod-extension/src/browser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
* See License-AGPL.txt in the project root for license information.
*/

import { createGitpodService } from "@gitpod/gitpod-protocol";
/// <reference types='@gitpod/gitpod-protocol/lib/typings/globals'/>

import { GitpodService } from "@gitpod/gitpod-protocol";
import { GitpodHostUrl, workspaceIDRegex } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url";
import { workspaceIDRegex } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url";

export function getGitpodService(): GitpodService {
if (!(window as any)['_gitpodService']) {
const serverUrl = new GitpodHostUrl(window.location.href).withoutWorkspacePrefix().toString();
const service = createGitpodService(serverUrl);
(window as any)['_gitpodService'] = service;
}
return (window as any)['_gitpodService'];
return window.gitpod.service;
}

export function getWorkspaceID() {
Expand Down

0 comments on commit 9cb4125

Please sign in to comment.