From aaaeec012c31c1a609b03ef624dedf3306294fc4 Mon Sep 17 00:00:00 2001 From: splincode Date: Fri, 19 Jul 2024 14:09:54 +0300 Subject: [PATCH] fix: correct position for mention dropdown --- .../mention/examples/1/mention/index.html | 30 ++++++++++--------- .../pages/mention/examples/1/mention/index.ts | 21 +++++++++++++ .../dropdown/dropdown-toolbar.directive.ts | 3 +- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/projects/demo/src/app/pages/mention/examples/1/mention/index.html b/projects/demo/src/app/pages/mention/examples/1/mention/index.html index c06d4d021..0c958ebaf 100644 --- a/projects/demo/src/app/pages/mention/examples/1/mention/index.html +++ b/projects/demo/src/app/pages/mention/examples/1/mention/index.html @@ -1,15 +1,17 @@ - - + +
+ +
diff --git a/projects/demo/src/app/pages/mention/examples/1/mention/index.ts b/projects/demo/src/app/pages/mention/examples/1/mention/index.ts index a00823322..0903de75f 100644 --- a/projects/demo/src/app/pages/mention/examples/1/mention/index.ts +++ b/projects/demo/src/app/pages/mention/examples/1/mention/index.ts @@ -2,9 +2,12 @@ import {NgForOf} from '@angular/common'; import { ChangeDetectionStrategy, Component, + ElementRef, EventEmitter, + HostListener, Input, Output, + ViewChild, } from '@angular/core'; import {TuiAutoFocus, tuiPure} from '@taiga-ui/cdk'; import {TuiDataList, TuiInitialsPipe} from '@taiga-ui/core'; @@ -24,6 +27,9 @@ export interface User { changeDetection: ChangeDetectionStrategy.OnPush, }) export class Mentions { + @ViewChild('container', {read: ElementRef}) + protected container?: ElementRef; + protected readonly items: readonly User[] = [ { name: 'Alexander Inkin', @@ -53,4 +59,19 @@ export class Mentions { ) : items; } + + @HostListener('window:keydown.arrowUp', ['$event', 'false']) + @HostListener('window:keydown.arrowDown', ['$event', 'true']) + protected down(event: Event, isDown: boolean): void { + const buttons = Array.from(this.el?.querySelectorAll('button') ?? []); + const button = isDown ? buttons[0] : buttons[buttons.length - 1]; + + if (!this.el?.contains(event.target as any)) { + button.focus(); + } + } + + private get el(): HTMLDivElement | null { + return this.container?.nativeElement ?? null; + } } diff --git a/projects/editor/src/components/editor/dropdown/dropdown-toolbar.directive.ts b/projects/editor/src/components/editor/dropdown/dropdown-toolbar.directive.ts index 7586d44c1..73f3b27e8 100644 --- a/projects/editor/src/components/editor/dropdown/dropdown-toolbar.directive.ts +++ b/projects/editor/src/components/editor/dropdown/dropdown-toolbar.directive.ts @@ -65,7 +65,8 @@ export class TuiEditorDropdownToolbar range.commonAncestorContainer.parentElement?.closest('tui-dropdown'); this.range = - contained && tuiIsTextNode(range.commonAncestorContainer) + (contained && tuiIsTextNode(range.commonAncestorContainer)) || + range.commonAncestorContainer?.nodeName === 'P' ? range : this.range;