11import { Autowired , Injectable , Provider } from '@opensumi/di' ;
22import { BrowserModule } from '@opensumi/ide-core-browser' ;
33import { IEditorDocumentModelContentProvider , BrowserEditorContribution , IEditorDocumentModelContentRegistry } from '@opensumi/ide-editor/lib/browser'
4- import { URI , Emitter , Event , Domain , Deferred } from '@opensumi/ide-core-common'
4+ import { URI , Emitter , Event , Domain , IEditorDocumentChange , IEditorDocumentModelSaveResult , SaveTaskResponseState } from '@opensumi/ide-core-common'
55
6- export const diffsDeferred = new Deferred < { filePath : string , oldFileContent : string | null , newFileContent : string } [ ] > ( )
6+ export interface IDiffData {
7+ filePath : string ;
8+ oldFileContent : string | null ;
9+ newFileContent : string ;
10+ }
11+
12+ class DiffService {
13+ #data: IDiffData [ ] | null = null
14+ #dataEmitter = new Emitter < URI [ ] > ( )
15+ onDataChange = this . #dataEmitter. event ;
16+
17+ get data ( ) {
18+ return this . #data;
19+ }
20+ updateData ( data : IDiffData [ ] ) {
21+ const lastData = this . #data
22+ this . #data = data
23+ const lastDataMap : Record < string , IDiffData > = lastData ?. reduce ( ( obj , item ) => {
24+ obj [ item . filePath ] = item
25+ return obj
26+ } , { } ) || { }
27+ const uris : URI [ ] = [ ]
28+ data . forEach ( item => {
29+ const lastItem = lastDataMap ?. [ item . filePath ]
30+ if ( ! lastItem ) return
31+ if ( item . oldFileContent !== lastItem . oldFileContent ) {
32+ uris . push ( new URI ( `sample://old/${ item . filePath } ` ) )
33+ }
34+ if ( item . newFileContent !== lastItem . newFileContent ) {
35+ uris . push ( new URI ( `sample://new/${ item . filePath } ` ) )
36+ }
37+ } )
38+ this . #dataEmitter. fire ( uris )
39+ }
40+
41+ clear ( ) {
42+ this . #data = null
43+ }
44+ }
45+
46+ export const diffService = new DiffService ( )
747
848@Injectable ( )
949export class SampleSchemeDocumentProvider implements IEditorDocumentModelContentProvider {
50+ private _onDidChangeContent : Emitter < URI > = new Emitter ( ) ;
51+ onDidChangeContent : Event < URI > = this . _onDidChangeContent . event ;
52+
53+ constructor ( ) {
54+ diffService . onDataChange ( ( uris ) => {
55+ uris . forEach ( uri => this . _onDidChangeContent . fire ( uri ) )
56+ } )
57+ }
58+
1059 handlesScheme ( scheme : string ) {
1160 return scheme === 'sample' ;
1261 }
1362
1463 async provideEditorDocumentModelContent ( uri : URI ) : Promise < string > {
15- const diffs = await diffsDeferred . promise
64+ const diffs = diffService . data || [ ]
1665 const diff = diffs . find ( item => item . filePath === uri . codeUri . path . slice ( 1 ) )
1766 if ( ! diff ) return ''
1867 if ( uri . authority === 'new' ) return diff . newFileContent
@@ -23,8 +72,12 @@ export class SampleSchemeDocumentProvider implements IEditorDocumentModelContent
2372 return false ;
2473 }
2574
26- private _onDidChangeContent : Emitter < URI > = new Emitter ( ) ;
27- onDidChangeContent : Event < URI > = this . _onDidChangeContent . event ;
75+ async saveDocumentModel ( uri : URI , content : string , baseContent : string , changes : IEditorDocumentChange [ ] ) : Promise < IEditorDocumentModelSaveResult > {
76+ console . log ( uri , content , baseContent , changes )
77+ return {
78+ state : SaveTaskResponseState . SUCCESS ,
79+ }
80+ }
2881}
2982
3083@Domain ( BrowserEditorContribution )
0 commit comments