This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show recent Che workspaces in Theia editor
Signed-off-by: Vladyslav Zhukovskyi <vzhukovs@redhat.com>
- Loading branch information
Showing
6 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
node_modules | ||
.browser_modules | ||
lib | ||
*.log | ||
*-app/* | ||
!*-app/package.json | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "@eclipse-che/theia-workspace-extension", | ||
"keywords": [ | ||
"theia-extension" | ||
], | ||
"version": "0.0.1", | ||
"files": [ | ||
"lib", | ||
"src" | ||
], | ||
"dependencies": { | ||
"@eclipse-che/api": "latest", | ||
"@theia/workspace": "next", | ||
"@eclipse-che/theia-plugin-ext": "^0.0.1" | ||
}, | ||
"scripts": { | ||
"prepare": "yarn clean && yarn build", | ||
"clean": "rimraf lib", | ||
"format": "tsfmt -r --useTsfmt ../../configs/tsfmt.json", | ||
"lint": "eslint --cache=true --no-error-on-unmatched-pattern=true \"{src,test}/**/*.{ts,tsx}\"", | ||
"compile": "tsc", | ||
"build": "concurrently -n \"format,lint,compile\" -c \"red,green,blue\" \"yarn format\" \"yarn lint\" \"yarn compile\"", | ||
"watch": "tsc -w" | ||
}, | ||
"license": "EPL-2.0", | ||
"theiaExtensions": [ | ||
{ | ||
"frontend": "lib/browser/che-workspace-module" | ||
} | ||
] | ||
} |
229 changes: 229 additions & 0 deletions
229
extensions/eclipse-che-theia-workspace/src/browser/che-quick-open-workspace.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
/********************************************************************* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
**********************************************************************/ | ||
import { inject, injectable } from 'inversify'; | ||
import { | ||
QuickOpenGroupItem, | ||
QuickOpenItem, | ||
QuickOpenMode, | ||
QuickOpenModel | ||
} from '@theia/core/lib/common/quick-open-model'; | ||
import { LabelProvider, QuickOpenService } from '@theia/core/lib/browser'; | ||
import { CommandContribution, CommandRegistry } from '@theia/core/lib/common'; | ||
import { WorkspaceCommands } from '@theia/workspace/lib/browser/workspace-commands'; | ||
import { CheApiService } from '@eclipse-che/theia-plugin-ext/lib/common/che-protocol'; | ||
import { che as cheApi } from '@eclipse-che/api'; | ||
import { OauthUtils } from '@eclipse-che/theia-plugin-ext/lib/browser/oauth-utils'; | ||
import { AbstractDialog } from '@theia/core/lib/browser/dialogs'; | ||
import { MessageService } from '@theia/core/lib/common/message-service'; | ||
import * as moment from 'moment'; | ||
import { Message } from '@theia/core/lib/browser/widgets/index'; | ||
import { Key } from '@theia/core/lib/browser/keyboard/keys'; | ||
|
||
@injectable() | ||
export class QuickOpenCheWorkspace implements QuickOpenModel, CommandContribution { | ||
protected items: QuickOpenGroupItem[]; | ||
protected currentWorkspace: cheApi.workspace.Workspace; | ||
|
||
@inject(QuickOpenService) protected readonly quickOpenService: QuickOpenService; | ||
@inject(CommandRegistry) protected readonly commandRegistry: CommandRegistry; | ||
@inject(CheApiService) protected readonly cheApi: CheApiService; | ||
@inject(OauthUtils) protected readonly oAuthUtils: OauthUtils; | ||
@inject(LabelProvider) protected readonly labelProvider: LabelProvider; | ||
@inject(MessageService) protected readonly messageService: MessageService; | ||
|
||
registerCommands(commands: CommandRegistry): void { | ||
// VS Code command id | ||
commands.unregisterCommand('workbench.action.openRecent'); | ||
|
||
// Theia command id | ||
commands.unregisterCommand('workspace:openRecent'); | ||
|
||
commands.registerCommand(WorkspaceCommands.OPEN_RECENT_WORKSPACE, { | ||
execute: () => this.select() | ||
}); | ||
commands.registerCommand({ | ||
id: 'workspace:openRecent' | ||
}, { | ||
execute: () => this.select() | ||
}); | ||
} | ||
|
||
protected async open(workspaces: cheApi.workspace.Workspace[]): Promise<void> { | ||
this.items = []; | ||
|
||
if (!workspaces.length) { | ||
this.items.push(new QuickOpenGroupItem({ | ||
label: 'No Recent Workspaces', | ||
run: (mode: QuickOpenMode): boolean => false | ||
})); | ||
return; | ||
} | ||
|
||
for (const workspace of workspaces) { | ||
const icon = this.labelProvider.folderIcon; | ||
const iconClass = icon + ' file-icon'; | ||
this.items.push(new QuickOpenGroupItem({ | ||
label: this.getWorkspaceName(workspace) + (this.isCurrentWorkspace(workspace) ? ' (Current)' : ''), | ||
description: this.getWorkspaceStack(workspace), | ||
groupLabel: `last modified ${moment(this.getWorkspaceModificationTime(workspace)).fromNow()}`, | ||
iconClass, | ||
run: (mode: QuickOpenMode): boolean => { | ||
if (mode !== QuickOpenMode.OPEN) { | ||
return false; | ||
} | ||
|
||
if (this.isCurrentWorkspace(workspace)) { | ||
return true; | ||
} | ||
|
||
this.openWorkspace(workspace); | ||
|
||
return true; | ||
}, | ||
})); | ||
} | ||
|
||
this.quickOpenService.open(this, { | ||
placeholder: 'Type the name of the Che workspace you want to open', | ||
fuzzyMatchLabel: true, | ||
fuzzySort: false | ||
}); | ||
} | ||
|
||
onType(lookFor: string, acceptor: (items: QuickOpenItem[]) => void): void { | ||
acceptor(this.items); | ||
} | ||
|
||
async select(): Promise<void> { | ||
this.items = []; | ||
|
||
const token = await this.oAuthUtils.getUserToken(); | ||
|
||
if (!this.currentWorkspace) { | ||
this.currentWorkspace = await this.cheApi.currentWorkspace(); | ||
} | ||
|
||
if (!this.currentWorkspace.namespace) { | ||
return; | ||
} | ||
|
||
const workspaces = await this.cheApi.getAllByNamespace(this.currentWorkspace.namespace, token); | ||
|
||
workspaces.sort((a: cheApi.workspace.Workspace, b: cheApi.workspace.Workspace) => { | ||
const updatedA: number = this.getWorkspaceModificationTime(a); | ||
const updatedB: number = this.getWorkspaceModificationTime(b); | ||
|
||
if (isNaN(updatedA) || isNaN(updatedB)) { | ||
return 0; | ||
} else { | ||
return updatedB - updatedA; | ||
} | ||
}); | ||
|
||
await this.open(workspaces); | ||
} | ||
|
||
private getWorkspaceName(workspace: cheApi.workspace.Workspace): string | undefined { | ||
if (workspace.config) { | ||
return workspace.config.name; | ||
} else if (workspace.devfile && workspace.devfile.metadata) { | ||
return workspace.devfile.metadata.name; | ||
} | ||
} | ||
|
||
private getWorkspaceStack(workspace: cheApi.workspace.Workspace): string | undefined { | ||
if (workspace.attributes && workspace.attributes.stackName) { | ||
return `Stack: ${workspace.attributes.stackName}`; | ||
} else { | ||
return 'Stack: Custom'; | ||
} | ||
} | ||
|
||
private getWorkspaceModificationTime(workspace: cheApi.workspace.Workspace): number { | ||
if (workspace.attributes) { | ||
if (workspace.attributes.updated) { | ||
return parseInt(workspace.attributes.updated); | ||
} else if (workspace.attributes.created) { | ||
return parseInt(workspace.attributes.created); | ||
} | ||
} | ||
|
||
return NaN; | ||
} | ||
|
||
private stopCurrentWorkspace(): Promise<boolean | undefined> { | ||
class StopWorkspaceDialog extends AbstractDialog<boolean | undefined> { | ||
protected confirmed: boolean | undefined = true; | ||
protected readonly dontStopButton: HTMLButtonElement; | ||
|
||
constructor() { | ||
super({ | ||
title: 'Open Workspace' | ||
}); | ||
|
||
this.contentNode.appendChild(this.createMessageNode('Do you want to stop current workspace?')); | ||
this.appendCloseButton('Cancel'); | ||
this.dontStopButton = this.appendDontStopButton(); | ||
this.appendAcceptButton('Yes'); | ||
} | ||
|
||
get value(): boolean | undefined { | ||
return this.confirmed; | ||
} | ||
|
||
protected appendDontStopButton(): HTMLButtonElement { | ||
const button = this.createButton('No'); | ||
this.controlPanel.appendChild(button); | ||
button.classList.add('secondary'); | ||
return button; | ||
} | ||
|
||
protected onAfterAttach(msg: Message): void { | ||
super.onAfterAttach(msg); | ||
this.addKeyListener(this.dontStopButton, Key.ENTER, () => { | ||
this.confirmed = false; | ||
this.accept(); | ||
}, 'click'); | ||
} | ||
|
||
protected onCloseRequest(msg: Message): void { | ||
super.onCloseRequest(msg); | ||
this.confirmed = undefined; | ||
this.accept(); | ||
} | ||
|
||
protected createMessageNode(msg: string | HTMLElement): HTMLElement { | ||
if (typeof msg === 'string') { | ||
const messageNode = document.createElement('div'); | ||
messageNode.textContent = msg; | ||
return messageNode; | ||
} | ||
return msg; | ||
} | ||
|
||
} | ||
|
||
return new StopWorkspaceDialog().open(); | ||
} | ||
|
||
private async openWorkspace(workspace: cheApi.workspace.Workspace): Promise<void> { | ||
const result = await this.stopCurrentWorkspace(); | ||
if (typeof result === 'boolean') { | ||
if (result) { | ||
await this.cheApi.stop(); | ||
} | ||
window.parent.postMessage(`open-workspace:${workspace.id}`, '*'); | ||
} | ||
} | ||
|
||
private isCurrentWorkspace(workspace: cheApi.workspace.Workspace): boolean { | ||
return this.currentWorkspace.id === workspace.id; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
extensions/eclipse-che-theia-workspace/src/browser/che-workspace-module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/********************************************************************* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
**********************************************************************/ | ||
import { ContainerModule } from 'inversify'; | ||
import { QuickOpenCheWorkspace } from './che-quick-open-workspace'; | ||
import { CommandContribution } from '@theia/core/lib/common'; | ||
|
||
export default new ContainerModule((bind, unbind, isBound, rebind) => { | ||
bind(QuickOpenCheWorkspace).toSelf().inSingletonScope(); | ||
bind(CommandContribution).toService(QuickOpenCheWorkspace); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "../../configs/base.tsconfig.json", | ||
"compilerOptions": { | ||
"lib": [ | ||
"es6", | ||
"dom" | ||
], | ||
"rootDir": "src", | ||
"outDir": "lib" | ||
}, | ||
"include": [ | ||
"src" | ||
] | ||
} |