Skip to content

Commit 5b11ed3

Browse files
committed
new template based document: inline name
1 parent 8c7f095 commit 5b11ed3

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

fusion-studio-extension/src/browser/core.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { FSApi, API_MINIMUM_VERSION } from "../common/api";
1111
import URI from "@theia/core/lib/common/uri";
1212
import { FSDragOperation } from "./widget/drag";
1313
import { FSTemplate } from "../classes/template";
14-
import { FSConnectionDialog, FSNewFromTemplateDialog, FSPropertiesDialog } from "./dialogs";
14+
import { FSConnectionDialog, FSNewFromTemplateDialog, FSNewFromTemplateDialogResult, FSPropertiesDialog } from "./dialogs";
1515
import { FSFiles, FSFileList } from "../classes/files";
1616
import { isArray } from "util";
1717
import { lookup } from "mime-types";
@@ -1339,13 +1339,16 @@ export class FSCore {
13391339
}
13401340
}
13411341

1342-
public async newItem(isCollection?: boolean): Promise<boolean> {
1342+
public async newItem(isCollection?: boolean, content = '', extension = ''): Promise<boolean> {
13431343
if (!this.node) {
13441344
return false;
13451345
}
13461346
const collection = this.node as FSCollectionNode;
13471347
const validator = (input: string) => input !== '' && !this.fileExists(input);
1348-
const initialName = this.newName(validator);
1348+
let initialName = this.newName(validator);
1349+
if (extension) {
1350+
initialName += '.' + extension;
1351+
}
13491352
const name = collection.uri + TRAILING_SYMBOL + initialName;
13501353
this.nextName(initialName);
13511354
let item: FSItemNode;
@@ -1361,7 +1364,7 @@ export class FSCore {
13611364
});
13621365
} else {
13631366
item = this.addDocument(collection, {
1364-
content: '',
1367+
content,
13651368
name,
13661369
created: new Date(),
13671370
lastModified: new Date(),
@@ -1381,8 +1384,19 @@ export class FSCore {
13811384
await this.setRename(false);
13821385
await this.removeNode(item);
13831386
const documentNode = this.addDocument(collection, { ...(item as FSDocumentNode).document, name }, true);
1384-
const doc = this.openDocument(documentNode);
1387+
const doc = await this.openDocument(documentNode);
13851388
this.acceptName = safeAccept;
1389+
if (content !== '') {
1390+
doc.editor.document.setDirty(true);
1391+
doc.editor.document.contentChanges.push({
1392+
range: {
1393+
start: { line: 0, character: 0 },
1394+
end: { line: 0, character: 0 }
1395+
},
1396+
rangeLength: 0,
1397+
content,
1398+
});
1399+
}
13861400
return !!doc;
13871401
}
13881402
}
@@ -1445,21 +1459,25 @@ export class FSCore {
14451459
if (!this.node) {
14461460
return false;
14471461
}
1448-
const collection = this.node as FSCollectionNode;
1462+
// const collection = this.node as FSCollectionNode;
14491463
const validator = (filename: string) => filename !== '' && !this.fileExists(filename);
1450-
const dialog = new FSNewFromTemplateDialog({
1451-
title: 'New ' + template.name,
1452-
initialValue: this.newName(validator, template.ext({})),
1453-
template,
1454-
validate: validator,
1455-
});
1456-
let result = await dialog.open();
1457-
if (result) {
1458-
this.nextName(result.params.name);
1459-
const name = collection.uri + '/' + result.params.name;
1460-
const text = template.execute(result.params);
1461-
await this.createDocument(collection, name, text);
1464+
// const initialName = this.newName(validator, template.ext({}));
1465+
let result: FSNewFromTemplateDialogResult | undefined;
1466+
if (template.fields) {
1467+
const dialog = new FSNewFromTemplateDialog({
1468+
title: 'New ' + template.name,
1469+
template,
1470+
validate: validator,
1471+
});
1472+
result = await dialog.open();
1473+
if (!result) {
1474+
return false;
1475+
}
14621476
}
1477+
// this.nextName(initialName);
1478+
// const name = collection.uri + '/' + initialName;
1479+
const text = template.execute(result?.params);
1480+
await this.newItem(false, text, template.ext(result?.params));
14631481
return false;
14641482
}
14651483

fusion-studio-extension/src/browser/dialogs/new-from-template-dialog.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export interface FSNewFromTemplateDialogResult {
1616

1717
export class FSNewFromTemplateDialog extends AbstractDialog<FSNewFromTemplateDialogResult> {
1818

19-
protected readonly nameField: IDialogField;
2019
protected readonly containerDiv: HTMLDivElement = document.createElement('div');
2120
protected readonly fields: IDialogFields = {};
2221

@@ -25,10 +24,6 @@ export class FSNewFromTemplateDialog extends AbstractDialog<FSNewFromTemplateDia
2524
) {
2625
super(props);
2726

28-
this.nameField = createField('Name:', 'name-field');
29-
this.nameField.input.value = props.initialValue || '';
30-
this.containerDiv.appendChild(this.nameField.container);
31-
3227
this.containerDiv.className = 'dialog-container vertical-form';
3328
this.contentNode.appendChild(this.containerDiv);
3429

@@ -58,24 +53,24 @@ export class FSNewFromTemplateDialog extends AbstractDialog<FSNewFromTemplateDia
5853
for (const key in this.fields) {
5954
params[key] = this.fields[key].input.value;
6055
}
61-
params.name = this.nameField.input.value;
6256
return { params };
6357
}
6458

65-
protected isValid(value: FSNewFromTemplateDialogResult, mode: DialogMode): DialogError {
66-
return !this.props.validate || this.props.validate(value.params.name);
67-
}
59+
// protected isValid(value: FSNewFromTemplateDialogResult, mode: DialogMode): DialogError {
60+
// return !this.props.validate || this.props.validate(value.params.name);
61+
// }
6862

6963
protected onAfterAttach(msg: Message): void {
7064
super.onAfterAttach(msg);
71-
this.addUpdateListener(this.nameField.input, 'input');
7265
for (const key in this.fields) {
7366
this.addUpdateListener(this.fields[key].input, 'input');
7467
}
7568
}
7669

7770
protected onActivateRequest(msg: Message): void {
78-
this.nameField.input.focus();
71+
if (this.fields?.length) {
72+
this.fields[0].input.focus();
73+
}
7974
}
8075

8176
}

0 commit comments

Comments
 (0)