@@ -11,7 +11,7 @@ import { FSApi, API_MINIMUM_VERSION } from "../common/api";
1111import URI from "@theia/core/lib/common/uri" ;
1212import { FSDragOperation } from "./widget/drag" ;
1313import { FSTemplate } from "../classes/template" ;
14- import { FSConnectionDialog , FSNewFromTemplateDialog , FSPropertiesDialog } from "./dialogs" ;
14+ import { FSConnectionDialog , FSNewFromTemplateDialog , FSNewFromTemplateDialogResult , FSPropertiesDialog } from "./dialogs" ;
1515import { FSFiles , FSFileList } from "../classes/files" ;
1616import { isArray } from "util" ;
1717import { 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
0 commit comments