@@ -11,15 +11,18 @@ import {
1111 KeybindingRegistry ,
1212} from './contribution' ;
1313import { nls } from '@theia/core/lib/common' ;
14- import { ApplicationShell } from '@theia/core/lib/browser' ;
15- import { timeout } from '@theia/core /lib/common/promise-util ' ;
14+ import { ApplicationShell , NavigatableWidget , Saveable } from '@theia/core/lib/browser' ;
15+ import { EditorManager } from '@theia/editor /lib/browser ' ;
1616
1717@injectable ( )
1818export class SaveAsSketch extends SketchContribution {
1919
2020 @inject ( ApplicationShell )
2121 protected readonly applicationShell : ApplicationShell ;
2222
23+ @inject ( EditorManager )
24+ protected readonly editorManager : EditorManager ;
25+
2326 registerCommands ( registry : CommandRegistry ) : void {
2427 registry . registerCommand ( SaveAsSketch . Commands . SAVE_AS_SKETCH , {
2528 execute : ( args ) => this . saveAs ( args ) ,
@@ -93,11 +96,12 @@ export class SaveAsSketch extends SketchContribution {
9396 if ( ! destinationUri ) {
9497 return false ;
9598 }
96- await this . applicationShell . saveAll ( ) ;
97- await timeout ( 20 ) ;
9899 const workspaceUri = await this . sketchService . copy ( sketch , {
99100 destinationUri,
100101 } ) ;
102+ if ( workspaceUri ) {
103+ await this . saveOntoCopiedSketch ( sketch . mainFileUri , sketch . uri , workspaceUri ) ;
104+ }
101105 if ( workspaceUri && openAfterMove ) {
102106 if ( wipeOriginal || ( openAfterMove && execOnlyIfTemp ) ) {
103107 try {
@@ -114,6 +118,41 @@ export class SaveAsSketch extends SketchContribution {
114118 }
115119 return ! ! workspaceUri ;
116120 }
121+
122+ private async saveOntoCopiedSketch ( mainFileUri : string , sketchUri : string , newSketchUri : string ) : Promise < void > {
123+ const widgets = this . applicationShell . widgets ;
124+ const snapshots = new Map < string , object > ( ) ;
125+ for ( const widget of widgets ) {
126+ const saveable = Saveable . getDirty ( widget ) ;
127+ const uri = NavigatableWidget . getUri ( widget ) ;
128+ const uriString = uri ?. toString ( ) ;
129+ let relativePath : string ;
130+ if ( uri && uriString ! . includes ( sketchUri ) && saveable && saveable . createSnapshot ) {
131+ // The main file will change its name during the copy process
132+ // We need to store the new name in the map
133+ if ( mainFileUri === uriString ) {
134+ const lastPart = new URI ( newSketchUri ) . path . base + uri . path . ext ;
135+ relativePath = '/' + lastPart ;
136+ } else {
137+ relativePath = uri . toString ( ) . substring ( sketchUri . length ) ;
138+ }
139+ snapshots . set ( relativePath , saveable . createSnapshot ( ) ) ;
140+ }
141+ }
142+ await Promise . all ( Array . from ( snapshots . entries ( ) ) . map ( async ( [ path , snapshot ] ) => {
143+ const widgetUri = new URI ( newSketchUri + path ) ;
144+ try {
145+ const widget = await this . editorManager . getOrCreateByUri ( widgetUri ) ;
146+ const saveable = Saveable . get ( widget ) ;
147+ if ( saveable && saveable . applySnapshot ) {
148+ saveable . applySnapshot ( snapshot ) ;
149+ await saveable . save ( ) ;
150+ }
151+ } catch ( e ) {
152+ console . error ( e ) ;
153+ }
154+ } ) ) ;
155+ }
117156}
118157
119158export namespace SaveAsSketch {
0 commit comments