Skip to content

Commit 1f01891

Browse files
committed
<iqui-highlightjs />, Added .selectedSyntax property to the component
1 parent ed20309 commit 1f01891

File tree

1 file changed

+45
-3
lines changed
  • projects/iqui-ngx/src/lib/components/code/highlight-js

1 file changed

+45
-3
lines changed

projects/iqui-ngx/src/lib/components/code/highlight-js/index.ts

+45-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import {
77
Directive,
88
Component,
9+
OnInit,
10+
OnDestroy,
911
OnChanges,
1012
AfterContentInit,
1113
HostBinding,
@@ -16,7 +18,6 @@ import {
1618
QueryList,
1719
TemplateRef,
1820
ChangeDetectorRef,
19-
SecurityContext,
2021
} from '@angular/core';
2122
import { default as he } from 'he';
2223
import { default as hljs } from 'highlight.js';
@@ -142,7 +143,7 @@ export class HighlightJsInjectBottomDirective {}
142143
templateUrl: './index.html',
143144
styleUrls: [`./style.scss`],
144145
})
145-
export class HighlightJsComponent implements OnChanges, AfterContentInit {
146+
export class HighlightJsComponent implements OnInit, OnDestroy, OnChanges, AfterContentInit {
146147
/**
147148
* Static method allowing registration of additional language syntaxes
148149
* @param languageName Name by which the language will be referenced
@@ -247,10 +248,28 @@ export class HighlightJsComponent implements OnChanges, AfterContentInit {
247248
return !this.highlight && !this.lineNumbers;
248249
}
249250

251+
// Raw syntax HTML
252+
public _rawSyntax = '';
250253
// Rendered, highlighted syntax HTML
251254
public _highlightedSyntax = '';
252255

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+
}
254273

255274
public ngAfterContentInit() {
256275
// If no syntax attribute set, try extracting value from <textarea /> child
@@ -268,6 +287,11 @@ export class HighlightJsComponent implements OnChanges, AfterContentInit {
268287
this._renderHighlightedSyntax();
269288
}
270289

290+
public ngOnDestroy() {
291+
// Detach listener for selection changes
292+
document.removeEventListener('selectionchange', this._handleSelectionChange);
293+
}
294+
271295
/**
272296
* Forces a refresh of the component and it's syntax
273297
*/
@@ -334,6 +358,9 @@ export class HighlightJsComponent implements OnChanges, AfterContentInit {
334358
.join('\n');
335359
}
336360

361+
// Store raw syntax
362+
this._rawSyntax = syntax;
363+
337364
// HighlightSyntax
338365
try {
339366
if (this.highlight) {
@@ -436,4 +463,19 @@ export class HighlightJsComponent implements OnChanges, AfterContentInit {
436463
this.lineNumberGaps ? 'showing-line-number-gaps' : '',
437464
].join(' ');
438465
}
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+
}
439481
}

0 commit comments

Comments
 (0)