6
6
import {
7
7
Directive ,
8
8
Component ,
9
+ OnInit ,
10
+ OnDestroy ,
9
11
OnChanges ,
10
12
AfterContentInit ,
11
13
HostBinding ,
@@ -16,7 +18,6 @@ import {
16
18
QueryList ,
17
19
TemplateRef ,
18
20
ChangeDetectorRef ,
19
- SecurityContext ,
20
21
} from '@angular/core' ;
21
22
import { default as he } from 'he' ;
22
23
import { default as hljs } from 'highlight.js' ;
@@ -142,7 +143,7 @@ export class HighlightJsInjectBottomDirective {}
142
143
templateUrl : './index.html' ,
143
144
styleUrls : [ `./style.scss` ] ,
144
145
} )
145
- export class HighlightJsComponent implements OnChanges , AfterContentInit {
146
+ export class HighlightJsComponent implements OnInit , OnDestroy , OnChanges , AfterContentInit {
146
147
/**
147
148
* Static method allowing registration of additional language syntaxes
148
149
* @param languageName Name by which the language will be referenced
@@ -247,10 +248,28 @@ export class HighlightJsComponent implements OnChanges, AfterContentInit {
247
248
return ! this . highlight && ! this . lineNumbers ;
248
249
}
249
250
251
+ // Raw syntax HTML
252
+ public _rawSyntax = '' ;
250
253
// Rendered, highlighted syntax HTML
251
254
public _highlightedSyntax = '' ;
252
255
253
- constructor ( private _cd : ChangeDetectorRef ) { }
256
+ /**
257
+ * Holds selected text within the component
258
+ */
259
+ private _selectedSyntax = '' ;
260
+ /**
261
+ * Gets selected syntax or if none selected falls back to full displayed (filtered) syntax
262
+ */
263
+ public get selectedSyntax ( ) {
264
+ return this . _selectedSyntax || this . _rawSyntax ;
265
+ }
266
+
267
+ constructor ( private _el : ElementRef , private _cd : ChangeDetectorRef ) { }
268
+
269
+ public ngOnInit ( ) {
270
+ // Attach listener for selection changes
271
+ document . addEventListener ( 'selectionchange' , this . _handleSelectionChange . bind ( this ) ) ;
272
+ }
254
273
255
274
public ngAfterContentInit ( ) {
256
275
// If no syntax attribute set, try extracting value from <textarea /> child
@@ -268,6 +287,11 @@ export class HighlightJsComponent implements OnChanges, AfterContentInit {
268
287
this . _renderHighlightedSyntax ( ) ;
269
288
}
270
289
290
+ public ngOnDestroy ( ) {
291
+ // Detach listener for selection changes
292
+ document . removeEventListener ( 'selectionchange' , this . _handleSelectionChange ) ;
293
+ }
294
+
271
295
/**
272
296
* Forces a refresh of the component and it's syntax
273
297
*/
@@ -334,6 +358,9 @@ export class HighlightJsComponent implements OnChanges, AfterContentInit {
334
358
. join ( '\n' ) ;
335
359
}
336
360
361
+ // Store raw syntax
362
+ this . _rawSyntax = syntax ;
363
+
337
364
// HighlightSyntax
338
365
try {
339
366
if ( this . highlight ) {
@@ -436,4 +463,19 @@ export class HighlightJsComponent implements OnChanges, AfterContentInit {
436
463
this . lineNumberGaps ? 'showing-line-number-gaps' : '' ,
437
464
] . join ( ' ' ) ;
438
465
}
466
+
467
+ /**
468
+ * Handles selection change event and stores selected syntax
469
+ * @param e Selection changed event
470
+ */
471
+ public _handleSelectionChange ( e : any ) {
472
+ // Get selection
473
+ const selection = document . getSelection ( ) ;
474
+ // Check if selection inside the host element
475
+ if ( this . _el . nativeElement . contains ( selection . anchorNode ) ) {
476
+ this . _selectedSyntax = selection . toString ( ) ;
477
+ } else {
478
+ this . _selectedSyntax = '' ;
479
+ }
480
+ }
439
481
}
0 commit comments