1
1
import { Component , Input , OnInit } from '@angular/core' ;
2
2
import { ContentService } from '../../../services/content.service' ;
3
3
import { ContentSlide , CustomBlock } from '../../../types' ;
4
+ import { MonacoConfigService } from '@codelab/code-demos' ;
5
+ import { Observable } from 'rxjs' ;
6
+ import { Selection } from 'monaco-editor' ;
7
+ import { tap } from 'rxjs/operators' ;
4
8
5
- interface SelectableFiles {
9
+ interface SelectableFile {
6
10
selected : boolean ;
7
11
name : string ;
12
+ highlights : Highlight [ ] ;
13
+ }
14
+
15
+ interface Highlight {
16
+ file : string ;
17
+ selection : Selection ;
18
+ prefix : string ;
19
+ text : string ;
8
20
}
9
21
10
22
@Component ( {
@@ -19,18 +31,48 @@ export class CodelabCodeDemoConsoleEditorComponent implements OnInit {
19
31
@Input ( ) block ! : CustomBlock ;
20
32
@Input ( ) slide ! : ContentSlide ;
21
33
@Input ( ) presentationId ! : string ;
22
- @Input ( ) selectedFiles : SelectableFiles [ ] = [ ] ;
34
+ @Input ( ) selectedFiles : SelectableFile [ ] = [ ] ;
23
35
@Input ( ) showPreview = true ;
24
36
@Input ( ) allowSwitchingFiles = true ;
25
37
@Input ( ) displayFileName = false ;
26
38
readonly defaultNewFileName = 'new.ts' ;
27
39
40
+ readonly selection$ = new Observable < Highlight | undefined > ( subscriber => {
41
+ // TODO: Unsubscribe
42
+ MonacoConfigService . monacoReady . then ( ( monaco : any ) => {
43
+ monaco . editor . onDidCreateEditor ( editor => {
44
+ const subscription = editor . onDidChangeCursorPosition ( ( ) => {
45
+ const selection = editor . getSelection ( ) ;
46
+ const text = editor . getModel ( ) . getValueInRange ( selection ) ;
47
+ const match = editor . getModel ( ) . uri . match ( / p r e f i x \/ ( .+ ?) \/ ( .* ) $ / ) ;
48
+ if ( text === '' ) {
49
+ subscriber . next ( ) ;
50
+ return ;
51
+ }
52
+ if ( ! match ) {
53
+ return ;
54
+ }
55
+ const [ , prefix , file ] = match ;
56
+
57
+ subscriber . next ( { file, prefix, selection, text } ) ;
58
+ } ) ;
59
+
60
+ editor . onDidDispose ( ( ) => subscription . dispose ( ) ) ;
61
+ } ) ;
62
+ } ) ;
63
+ } ) ;
64
+
28
65
openFiles : string [ ] = [ ] ;
66
+ highlights : Record < string , Selection > ;
29
67
30
- ngOnInit ( ) {
68
+ async ngOnInit ( ) {
31
69
this . inferVars ( ) ;
32
70
}
33
- constructor ( private readonly contentService : ContentService ) { }
71
+
72
+ constructor (
73
+ private readonly monacoConfigService : MonacoConfigService ,
74
+ private readonly contentService : ContentService
75
+ ) { }
34
76
35
77
update ( ) {
36
78
this . inferVars ( ) ;
@@ -48,14 +90,23 @@ export class CodelabCodeDemoConsoleEditorComponent implements OnInit {
48
90
49
91
private inferVars ( ) {
50
92
this . files = Object . keys ( this . code ) ;
93
+
51
94
this . selectedFiles =
52
95
this . selectedFiles . length === 0
53
96
? this . files . map ( ( name , i ) => ( {
54
97
name,
55
- selected : i === 0
98
+ selected : i === 0 ,
99
+ highlights : [ ]
56
100
} ) )
57
101
: this . selectedFiles ;
58
102
103
+ this . highlights = this . selectedFiles . reduce ( ( result , file ) => {
104
+ result [ file . name ] = file . highlights . map ( h => h . selection ) ;
105
+ return result ;
106
+ } , { } ) ;
107
+
108
+ console . log ( this . highlights ) ;
109
+
59
110
this . openFiles = this . selectedFiles
60
111
. filter ( file => file . selected )
61
112
. map ( ( { name } ) => name ) ;
@@ -75,7 +126,8 @@ export class CodelabCodeDemoConsoleEditorComponent implements OnInit {
75
126
if ( ! this . code [ this . defaultNewFileName ] ) {
76
127
this . selectedFiles . push ( {
77
128
name : this . defaultNewFileName ,
78
- selected : false
129
+ selected : false ,
130
+ highlights : [ ]
79
131
} ) ;
80
132
this . code [ this . defaultNewFileName ] = '// code' ;
81
133
}
@@ -85,4 +137,14 @@ export class CodelabCodeDemoConsoleEditorComponent implements OnInit {
85
137
delete this . code [ name ] ;
86
138
this . selectedFiles = this . selectedFiles . filter ( file => file . name !== name ) ;
87
139
}
140
+
141
+ addHighlight ( file : SelectableFile , highlight : Highlight ) {
142
+ file . highlights . push ( JSON . parse ( JSON . stringify ( highlight ) ) ) ;
143
+ this . update ( ) ;
144
+ }
145
+
146
+ deleteHighlight ( file : SelectableFile , highlightIndex : number ) {
147
+ file . highlights . splice ( highlightIndex , 1 ) ;
148
+ this . update ( ) ;
149
+ }
88
150
}
0 commit comments