@@ -11,15 +11,18 @@ import {
11
11
KeybindingRegistry ,
12
12
} from './contribution' ;
13
13
import { 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 ' ;
16
16
17
17
@injectable ( )
18
18
export class SaveAsSketch extends SketchContribution {
19
19
20
20
@inject ( ApplicationShell )
21
21
protected readonly applicationShell : ApplicationShell ;
22
22
23
+ @inject ( EditorManager )
24
+ protected readonly editorManager : EditorManager ;
25
+
23
26
registerCommands ( registry : CommandRegistry ) : void {
24
27
registry . registerCommand ( SaveAsSketch . Commands . SAVE_AS_SKETCH , {
25
28
execute : ( args ) => this . saveAs ( args ) ,
@@ -93,11 +96,12 @@ export class SaveAsSketch extends SketchContribution {
93
96
if ( ! destinationUri ) {
94
97
return false ;
95
98
}
96
- await this . applicationShell . saveAll ( ) ;
97
- await timeout ( 20 ) ;
98
99
const workspaceUri = await this . sketchService . copy ( sketch , {
99
100
destinationUri,
100
101
} ) ;
102
+ if ( workspaceUri ) {
103
+ await this . saveOntoCopiedSketch ( sketch . mainFileUri , sketch . uri , workspaceUri ) ;
104
+ }
101
105
if ( workspaceUri && openAfterMove ) {
102
106
if ( wipeOriginal || ( openAfterMove && execOnlyIfTemp ) ) {
103
107
try {
@@ -114,6 +118,41 @@ export class SaveAsSketch extends SketchContribution {
114
118
}
115
119
return ! ! workspaceUri ;
116
120
}
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
+ }
117
156
}
118
157
119
158
export namespace SaveAsSketch {
0 commit comments