1
- import { injectable } from 'inversify' ;
1
+ import { inject , injectable } from 'inversify' ;
2
2
import * as remote from '@theia/core/electron-shared/@electron/remote' ;
3
3
import * as dateFormat from 'dateformat' ;
4
4
import { ArduinoMenus } from '../menu/arduino-menus' ;
@@ -11,9 +11,22 @@ import {
11
11
KeybindingRegistry ,
12
12
} from './contribution' ;
13
13
import { nls } from '@theia/core/lib/common' ;
14
+ import { ApplicationShell , NavigatableWidget , Saveable } from '@theia/core/lib/browser' ;
15
+ import { EditorManager } from '@theia/editor/lib/browser' ;
16
+ import { WindowService } from '@theia/core/lib/browser/window/window-service' ;
14
17
15
18
@injectable ( )
16
19
export class SaveAsSketch extends SketchContribution {
20
+
21
+ @inject ( ApplicationShell )
22
+ protected readonly applicationShell : ApplicationShell ;
23
+
24
+ @inject ( EditorManager )
25
+ protected readonly editorManager : EditorManager ;
26
+
27
+ @inject ( WindowService )
28
+ protected readonly windowService : WindowService ;
29
+
17
30
registerCommands ( registry : CommandRegistry ) : void {
18
31
registry . registerCommand ( SaveAsSketch . Commands . SAVE_AS_SKETCH , {
19
32
execute : ( args ) => this . saveAs ( args ) ,
@@ -90,6 +103,9 @@ export class SaveAsSketch extends SketchContribution {
90
103
const workspaceUri = await this . sketchService . copy ( sketch , {
91
104
destinationUri,
92
105
} ) ;
106
+ if ( workspaceUri ) {
107
+ await this . saveOntoCopiedSketch ( sketch . mainFileUri , sketch . uri , workspaceUri ) ;
108
+ }
93
109
if ( workspaceUri && openAfterMove ) {
94
110
if ( wipeOriginal || ( openAfterMove && execOnlyIfTemp ) ) {
95
111
try {
@@ -100,12 +116,48 @@ export class SaveAsSketch extends SketchContribution {
100
116
/* NOOP: from time to time, it's not possible to wipe the old resource from the temp dir on Windows */
101
117
}
102
118
}
119
+ this . windowService . setSafeToShutDown ( ) ;
103
120
this . workspaceService . open ( new URI ( workspaceUri ) , {
104
121
preserveWindow : true ,
105
122
} ) ;
106
123
}
107
124
return ! ! workspaceUri ;
108
125
}
126
+
127
+ private async saveOntoCopiedSketch ( mainFileUri : string , sketchUri : string , newSketchUri : string ) : Promise < void > {
128
+ const widgets = this . applicationShell . widgets ;
129
+ const snapshots = new Map < string , object > ( ) ;
130
+ for ( const widget of widgets ) {
131
+ const saveable = Saveable . getDirty ( widget ) ;
132
+ const uri = NavigatableWidget . getUri ( widget ) ;
133
+ const uriString = uri ?. toString ( ) ;
134
+ let relativePath : string ;
135
+ if ( uri && uriString ! . includes ( sketchUri ) && saveable && saveable . createSnapshot ) {
136
+ // The main file will change its name during the copy process
137
+ // We need to store the new name in the map
138
+ if ( mainFileUri === uriString ) {
139
+ const lastPart = new URI ( newSketchUri ) . path . base + uri . path . ext ;
140
+ relativePath = '/' + lastPart ;
141
+ } else {
142
+ relativePath = uri . toString ( ) . substring ( sketchUri . length ) ;
143
+ }
144
+ snapshots . set ( relativePath , saveable . createSnapshot ( ) ) ;
145
+ }
146
+ }
147
+ await Promise . all ( Array . from ( snapshots . entries ( ) ) . map ( async ( [ path , snapshot ] ) => {
148
+ const widgetUri = new URI ( newSketchUri + path ) ;
149
+ try {
150
+ const widget = await this . editorManager . getOrCreateByUri ( widgetUri ) ;
151
+ const saveable = Saveable . get ( widget ) ;
152
+ if ( saveable && saveable . applySnapshot ) {
153
+ saveable . applySnapshot ( snapshot ) ;
154
+ await saveable . save ( ) ;
155
+ }
156
+ } catch ( e ) {
157
+ console . error ( e ) ;
158
+ }
159
+ } ) ) ;
160
+ }
109
161
}
110
162
111
163
export namespace SaveAsSketch {
0 commit comments