From 9e87bf43b0ece7cefe6bfcc69df8ede32bd50958 Mon Sep 17 00:00:00 2001 From: waterplea Date: Thu, 19 Sep 2024 19:19:43 +0400 Subject: [PATCH] fix: add proper `Appearance` support --- .../components/editor/editor.component.html | 4 +- .../components/editor/editor.component.less | 6 -- .../src/components/editor/editor.component.ts | 63 ++++++++++++++----- .../src/components/toolbar/toolbar.style.less | 3 + .../components/toolbar/toolbar.template.html | 35 ++++++----- 5 files changed, 71 insertions(+), 40 deletions(-) diff --git a/projects/editor/src/components/editor/editor.component.html b/projects/editor/src/components/editor/editor.component.html index 1e69684fc..25c06c6e4 100644 --- a/projects/editor/src/components/editor/editor.component.html +++ b/projects/editor/src/components/editor/editor.component.html @@ -5,7 +5,7 @@
@@ -66,7 +66,7 @@
diff --git a/projects/editor/src/components/editor/editor.component.less b/projects/editor/src/components/editor/editor.component.less index 1bb4742bf..6de819005 100644 --- a/projects/editor/src/components/editor/editor.component.less +++ b/projects/editor/src/components/editor/editor.component.less @@ -6,17 +6,11 @@ isolation: isolate; font: var(--tui-font-text-m); border-radius: var(--tui-radius-m); - outline: 0.0625rem solid var(--tui-border-normal); max-block-size: inherit; min-block-size: 10rem; box-sizing: border-box; overflow: auto; - &._has-focus { - outline-color: var(--tui-background-accent-1); - outline-width: 0.125rem; - } - &::after { content: ''; display: table; diff --git a/projects/editor/src/components/editor/editor.component.ts b/projects/editor/src/components/editor/editor.component.ts index df0e42ef4..7e2f6467b 100644 --- a/projects/editor/src/components/editor/editor.component.ts +++ b/projects/editor/src/components/editor/editor.component.ts @@ -1,20 +1,27 @@ import {AsyncPipe, NgIf, NgTemplateOutlet} from '@angular/common'; -import type {OnDestroy} from '@angular/core'; import { ChangeDetectionStrategy, Component, + computed, DestroyRef, ElementRef, EventEmitter, inject, Input, NgZone, + type OnDestroy, Output, ViewChild, } from '@angular/core'; -import {takeUntilDestroyed} from '@angular/core/rxjs-interop'; +import {takeUntilDestroyed, toSignal} from '@angular/core/rxjs-interop'; import {WA_WINDOW} from '@ng-web-apis/common'; -import type {TuiBooleanHandler, TuiValueTransformer} from '@taiga-ui/cdk'; +import { + TUI_TRUE_HANDLER, + type TuiBooleanHandler, + tuiInjectElement, + type TuiValueTransformer, + tuiWatch, +} from '@taiga-ui/cdk'; import { TUI_FALSE_HANDLER, TuiActiveZone, @@ -22,14 +29,18 @@ import { TuiControl, tuiZonefree, } from '@taiga-ui/cdk'; -import type {TuiDropdownDirective} from '@taiga-ui/core'; import { TUI_ANIMATIONS_DEFAULT_DURATION, + TUI_APPEARANCE_OPTIONS, + TuiAppearance, + tuiAppearanceFocus, + tuiAppearanceMode, + tuiAppearanceState, TuiDropdown, + type TuiDropdownDirective, TuiScrollbar, - TuiWithAppearance, } from '@taiga-ui/core'; -import {delay, fromEvent, throttleTime} from 'rxjs'; +import {delay, fromEvent, map, merge, throttleTime} from 'rxjs'; import type {AbstractTuiEditor} from '../../abstract/editor-adapter.abstract'; import {TUI_EDITOR_RESIZE_EVENT} from '../../constants/default-events'; @@ -73,9 +84,16 @@ import {TuiEditorPortalHost} from './portal/editor-portal-host.component'; providers: [ tuiAutoFocusOptionsProvider({delay: TUI_ANIMATIONS_DEFAULT_DURATION}), TUI_EDITOR_PROVIDERS, + { + provide: TUI_APPEARANCE_OPTIONS, + useValue: {appearance: 'textfield'}, + }, ], hostDirectives: [ - TuiWithAppearance, + { + directive: TuiAppearance, + inputs: ['tuiAppearance: appearance'], + }, { directive: TuiActiveZone, outputs: ['tuiActiveZoneChange'], @@ -83,7 +101,6 @@ import {TuiEditorPortalHost} from './portal/editor-portal-host.component'; ], host: { ngSkipHydration: 'true', - '[class._has-focus]': 'computedFocused', '(tuiActiveZoneChange)': 'onActiveZone($event)', '(click)': 'focus($event)', }, @@ -147,7 +164,25 @@ export class TuiEditor extends TuiControl implements OnDestroy { public readonly focusOut = new EventEmitter(); public hasMentionPlugin = false; - public focused = false; + public readonly hovered = toSignal( + merge( + fromEvent(tuiInjectElement(), 'mouseenter').pipe(map(TUI_TRUE_HANDLER)), + fromEvent(tuiInjectElement(), 'mouseleave').pipe(map(TUI_FALSE_HANDLER)), + ).pipe(tuiWatch(this.cdr)), + ); + + public readonly focused = tuiAppearanceFocus(false); + public readonly m = tuiAppearanceMode(this.mode); + public readonly s = tuiAppearanceState( + computed(() => { + if (this.disabled()) { + return 'disabled'; + } + + return this.hovered() ? 'hover' : null; + }), + ); + public readonly editorService = inject(TuiTiptapEditorService); @Input('readOnly') @@ -165,10 +200,6 @@ export class TuiEditor extends TuiControl implements OnDestroy { ); } - public get computedFocused(): boolean { - return (this.editor?.isFocused || this.focused) ?? false; - } - public get selectionState(): TuiSelectionState { return tuiGetSelectionState(this.editor); } @@ -194,7 +225,7 @@ export class TuiEditor extends TuiControl implements OnDestroy { this.firstInitialValue = processed ?? ''; } - if (!this.focused) { + if (!this.focused()) { this.doc?.getSelection?.()?.removeAllRanges(); } } @@ -204,7 +235,7 @@ export class TuiEditor extends TuiControl implements OnDestroy { } protected get dropdownSelectionHandler(): TuiBooleanHandler { - if (!this.focused || this.readOnly()) { + if (!this.focused() || this.readOnly()) { return TUI_FALSE_HANDLER; } @@ -240,7 +271,7 @@ export class TuiEditor extends TuiControl implements OnDestroy { } protected onActiveZone(focused: boolean): void { - this.focused = focused; + this.focused.set(focused); if (focused) { this.focusIn.emit(); diff --git a/projects/editor/src/components/toolbar/toolbar.style.less b/projects/editor/src/components/toolbar/toolbar.style.less index 1e759830c..f1246347c 100644 --- a/projects/editor/src/components/toolbar/toolbar.style.less +++ b/projects/editor/src/components/toolbar/toolbar.style.less @@ -23,6 +23,9 @@ display: flex; overflow: auto; flex-wrap: wrap; + padding: 0; + margin: 0; + border: 0; &:has(.t-block:not(:empty)) { padding: 0.25rem 0.75rem; diff --git a/projects/editor/src/components/toolbar/toolbar.template.html b/projects/editor/src/components/toolbar/toolbar.template.html index f6e7e1d6b..db52f7bd2 100644 --- a/projects/editor/src/components/toolbar/toolbar.template.html +++ b/projects/editor/src/components/toolbar/toolbar.template.html @@ -1,23 +1,26 @@ - + + +
+
+
-
-
-
-
+