Skip to content

Commit

Permalink
add CHANGELOG, improve validation check, add cancellationtoken
Browse files Browse the repository at this point in the history
Signed-off-by: Uni Sayo <unibtc@gmail.com>
  • Loading branch information
uniibu committed Apr 2, 2019
1 parent 3ba4c30 commit 07d0f43
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
## v0.6.0

- [filesystem] added the menu item `Upload Files...` to easily upload files into a workspace
- [workspace] allow creation of files and folders using recursive paths

Breaking changes:

- [dialog] `validate` and `accept` methods are now Promisified [#4764](https://github.com/theia-ide/theia/pull/4764)

## v0.5.0

Expand Down
35 changes: 25 additions & 10 deletions packages/core/src/browser/dialogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
********************************************************************************/

import { injectable, inject } from 'inversify';
import { Disposable, MaybePromise } from '../common';
import { Disposable, MaybePromise, CancellationTokenSource } from '../common';
import { Key } from './keys';
import { Widget, BaseWidget, Message } from './widgets';

Expand Down Expand Up @@ -199,25 +199,40 @@ export abstract class AbstractDialog<T> extends BaseWidget {
this.validate();
}

protected validateCancellationSource = new CancellationTokenSource();
protected async validate(): Promise<void> {
if (!this.resolve) {
return;
}
this.validateCancellationSource.cancel();
this.validateCancellationSource = new CancellationTokenSource();
const token = this.validateCancellationSource.token;
const value = this.value;
const error = await this.isValid(value, 'preview');
if (token.isCancellationRequested) {
return;
}
this.setErrorMessage(error);
}

protected acceptCancellationSource = new CancellationTokenSource();
protected async accept(): Promise<void> {
if (this.resolve) {
const value = this.value;
const error = await this.isValid(value, 'open');
if (!DialogError.getResult(error)) {
this.setErrorMessage(error);
} else {
this.resolve(value);
Widget.detach(this);
}
if (!this.resolve) {
return;
}
this.validateCancellationSource.cancel();
this.validateCancellationSource = new CancellationTokenSource();
const token = this.validateCancellationSource.token;
const value = this.value;
const error = await this.isValid(value, 'open');
if (token.isCancellationRequested) {
return;
}
if (!DialogError.getResult(error)) {
this.setErrorMessage(error);
} else {
this.resolve(value);
Widget.detach(this);
}
}

Expand Down
17 changes: 12 additions & 5 deletions packages/workspace/src/browser/workspace-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import { inject, injectable } from 'inversify';
import URI from '@theia/core/lib/common/uri';
import * as path from 'path';
import { SelectionService } from '@theia/core/lib/common/selection-service';
import { Command, CommandContribution, CommandRegistry } from '@theia/core/lib/common/command';
import { MenuContribution, MenuModelRegistry } from '@theia/core/lib/common/menu';
Expand Down Expand Up @@ -317,20 +316,28 @@ export class WorkspaceCommandContribution implements CommandContribution {
if (!name) {
return '';
}
// do not allow recursive rename
if (!recursive && !validFilename(name)) {
return 'Invalid file or folder name';
}
const fileName = path.basename(name);
if (!validFilename(fileName) || name.startsWith('/') || name.startsWith(' ') || name.endsWith(' ')) {
return `The name <strong>${fileName}</strong> is not a valid file or folder name.`;
// check and validate each sub-paths
const names = name.split(/[\\/]/).some(file => !file || !validFilename(file) || /^\s+$/.test(file));
if (names || name.startsWith('/') || name.startsWith(' ') || name.endsWith(' ')) {
return `The name <strong>${this.trimFileName(name)}</strong> is not a valid file or folder name.`;
}
const childUri = new URI(parent.uri).resolve(name).toString();
const exists = await this.fileSystem.exists(childUri);
if (exists) {
return `A file or folder <strong>${fileName}</strong> already exists at this location.`;
return `A file or folder <strong>${this.trimFileName(name)}</strong> already exists at this location.`;
}
return '';
}
protected trimFileName(name: string): string {
if (name && name.length > 30) {
return `${name.substr(0, 30)}...`;
}
return name;
}

protected async getDirectory(candidate: URI): Promise<FileStat | undefined> {
const stat = await this.fileSystem.getFileStat(candidate.toString());
Expand Down

0 comments on commit 07d0f43

Please sign in to comment.