|
| 1 | +import { |
| 2 | + ApplicationRef, |
| 3 | + ComponentRef, |
| 4 | + createComponent, |
| 5 | + Directive, |
| 6 | + ElementRef, |
| 7 | + EmbeddedViewRef, |
| 8 | + EnvironmentInjector, |
| 9 | + Inject, |
| 10 | + Input, |
| 11 | + NgZone, |
| 12 | + OnDestroy, |
| 13 | + OnInit |
| 14 | +} from '@angular/core'; |
| 15 | +import { TextSelectionTooltipComponent } from './text-selection-tooltip/text-selection-tooltip.component'; |
| 16 | +import { DOCUMENT } from '@angular/common'; |
| 17 | + |
| 18 | +@Directive({ |
| 19 | + selector: '[dsTextSelectTooltip]', |
| 20 | +}) |
| 21 | +export class TextSelectDirective implements OnInit, OnDestroy { |
| 22 | + |
| 23 | + @Input() |
| 24 | + showTTSControls = true; |
| 25 | + |
| 26 | + hasSelection = false; |
| 27 | + selectedText = ''; |
| 28 | + |
| 29 | + componentRef: ComponentRef<any> = null; |
| 30 | + |
| 31 | + // Initialize the directive. |
| 32 | + constructor( |
| 33 | + @Inject(DOCUMENT) private _document: Document, |
| 34 | + private elementRef: ElementRef, |
| 35 | + private zone: NgZone, |
| 36 | + private appRef: ApplicationRef, |
| 37 | + private injector: EnvironmentInjector |
| 38 | + ) { |
| 39 | + } |
| 40 | + |
| 41 | + // Clean up when the directive is destroyed. |
| 42 | + ngOnDestroy(): void { |
| 43 | + this.elementRef.nativeElement.removeEventListener('mousedown', this.handleMousedown, false); |
| 44 | + this._document.removeEventListener('mouseup', this.handleMouseup, false); |
| 45 | + } |
| 46 | + |
| 47 | + // Set up event listeners when the directive is initialized. |
| 48 | + ngOnInit(): void { |
| 49 | + this.zone.runOutsideAngular(() => { |
| 50 | + this.elementRef.nativeElement.addEventListener('mousedown', this.handleMousedown, false); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + // Get the deepest Element node in the DOM tree that contains the entire range. |
| 55 | + getRangeContainer(range: Range): Node { |
| 56 | + let container = range.commonAncestorContainer; |
| 57 | + while (container.nodeType !== Node.ELEMENT_NODE) { |
| 58 | + container = container.parentNode; |
| 59 | + } |
| 60 | + return (container); |
| 61 | + } |
| 62 | + |
| 63 | + // Handle mousedown events inside the current element. |
| 64 | + handleMousedown = (): void => { |
| 65 | + this._document.addEventListener('mouseup', this.handleMouseup, false); |
| 66 | + }; |
| 67 | + |
| 68 | + // Handle mouseup events anywhere in the document. |
| 69 | + private handleMouseup = (): void => { |
| 70 | + this._document.removeEventListener('mouseup', this.handleMouseup, false); |
| 71 | + this.processSelection(); |
| 72 | + }; |
| 73 | + |
| 74 | + createTooltipComponent(): ComponentRef<TextSelectionTooltipComponent> { |
| 75 | + return createComponent(TextSelectionTooltipComponent, {environmentInjector: this.injector}); |
| 76 | + } |
| 77 | + |
| 78 | + processSelection(): void { |
| 79 | + const selection = this._document.getSelection(); |
| 80 | + const stringSelection = selection.toString().trim(); |
| 81 | + const previousSelection = this.selectedText; |
| 82 | + |
| 83 | + if (this.hasSelection) { |
| 84 | + this.zone.runGuarded(() => { |
| 85 | + this.hasSelection = false; |
| 86 | + this.selectedText = ''; |
| 87 | + this.componentRef.destroy(); |
| 88 | + this.componentRef = null; |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + // check if there is a selection and if it is different from the previous one |
| 93 | + // (to handle a bug in browsers that fires the mouseup event again if clicking on the selection) |
| 94 | + if (!selection.rangeCount || !stringSelection || previousSelection === stringSelection) { |
| 95 | + return; |
| 96 | + } |
| 97 | + console.warn('selection', stringSelection); |
| 98 | + let range = selection.getRangeAt(0); |
| 99 | + let rangeContainer = this.getRangeContainer(range); |
| 100 | + // check if the range container is inside the current element |
| 101 | + // (to avoid showing the tooltip when selecting text in other elements) |
| 102 | + if (this.elementRef.nativeElement.contains(rangeContainer)) { |
| 103 | + let viewportRectangle = range.getBoundingClientRect(); |
| 104 | + if (stringSelection) { |
| 105 | + this.zone.runGuarded(() => { |
| 106 | + this.hasSelection = true; |
| 107 | + if (this.componentRef === null) { |
| 108 | + this.componentRef = this.createTooltipComponent(); |
| 109 | + this.appRef.attachView(this.componentRef.hostView); |
| 110 | + |
| 111 | + const domElem = (this.componentRef.hostView as EmbeddedViewRef<any>).rootNodes[0] as HTMLElement; |
| 112 | + this._document.body.appendChild(domElem); |
| 113 | + |
| 114 | + this.componentRef.instance.elementRectangleLeft = viewportRectangle.left + window.scrollX; |
| 115 | + this.componentRef.instance.elementRectangleTop = viewportRectangle.top + window.scrollY; |
| 116 | + this.componentRef.instance.elementRectangleWidth = viewportRectangle.width; |
| 117 | + this.componentRef.instance.elementRectangleHeight = viewportRectangle.height; |
| 118 | + this.componentRef.instance.text = stringSelection; |
| 119 | + |
| 120 | + this.componentRef.instance.showTTSControls = this.showTTSControls; |
| 121 | + |
| 122 | + this.selectedText = stringSelection; |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments