From 4a1d52382a742efeb2921737b43abaa8498891cb Mon Sep 17 00:00:00 2001 From: sumeyye Date: Fri, 13 Feb 2026 16:09:13 +0300 Subject: [PATCH] add: `THEME_CHANGE_TOKEN` implementation --- .../toast-ui/toastui-editor.component.ts | 41 ++++++++++++++++++- .../theme-shared/src/lib/tokens/index.ts | 1 + .../src/lib/tokens/theme-change.token.ts | 6 +++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 npm/ng-packs/packages/theme-shared/src/lib/tokens/theme-change.token.ts diff --git a/npm/ng-packs/packages/cms-kit/src/components/toast-ui/toastui-editor.component.ts b/npm/ng-packs/packages/cms-kit/src/components/toast-ui/toastui-editor.component.ts index b07ababe23..f9a6aa5469 100644 --- a/npm/ng-packs/packages/cms-kit/src/components/toast-ui/toastui-editor.component.ts +++ b/npm/ng-packs/packages/cms-kit/src/components/toast-ui/toastui-editor.component.ts @@ -1,5 +1,6 @@ import { isPlatformBrowser } from '@angular/common'; import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; +import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { Component, AfterViewInit, @@ -8,10 +9,12 @@ import { forwardRef, inject, PLATFORM_ID, + DestroyRef, } from '@angular/core'; import Editor from '@toast-ui/editor'; import { AbpLocalStorageService } from '@abp/ng.core'; +import { THEME_CHANGE_TOKEN } from '@abp/ng.theme.shared'; @Component({ selector: 'abp-toastui-editor', @@ -33,6 +36,8 @@ export class ToastuiEditorComponent implements AfterViewInit, ControlValueAccess private platformId = inject(PLATFORM_ID); private localStorageService = inject(AbpLocalStorageService); + private destroyRef = inject(DestroyRef); + private themeChange$ = inject(THEME_CHANGE_TOKEN, { optional: true }); private onChange: (value: string) => void = () => {}; private onTouched: () => void = () => {}; @@ -58,8 +63,9 @@ export class ToastuiEditorComponent implements AfterViewInit, ControlValueAccess } this.initializeEditor(); - // TODO: Revise this approach on lepton side :: Service injection through DI - // this.setupThemeListener(); + if (this.themeChange$) { + this.setupThemeListener(); + } } private getTheme(): string { @@ -81,4 +87,35 @@ export class ToastuiEditorComponent implements AfterViewInit, ControlValueAccess this.onChange(value); }); } + + private setupThemeListener(): void { + this.themeChange$!.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(style => { + if (!this.editor) { + return; + } + const wrapper = + this.editorContainer.nativeElement.querySelector('.toastui-editor-defaultUI') ?? + this.editorContainer.nativeElement; + + const { styleName } = style; + + switch (styleName) { + case 'dark': + wrapper.classList.add('toastui-editor-dark'); + break; + case 'system': + const isSystemDark = + typeof window !== 'undefined' && + window.matchMedia('(prefers-color-scheme: dark)').matches; + if (!isSystemDark) { + wrapper.classList.remove('toastui-editor-dark'); + } else { + wrapper.classList.add('toastui-editor-dark'); + } + break; + default: + wrapper.classList.remove('toastui-editor-dark'); + } + }); + } } diff --git a/npm/ng-packs/packages/theme-shared/src/lib/tokens/index.ts b/npm/ng-packs/packages/theme-shared/src/lib/tokens/index.ts index fc60386b7d..97bccdebb5 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/tokens/index.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/tokens/index.ts @@ -4,3 +4,4 @@ export * from './ngx-datatable-messages.token'; export * from './suppress-unsaved-changes-warning.token'; export * from './confirmation-icons.token'; export * from './logo.token'; +export * from './theme-change.token'; diff --git a/npm/ng-packs/packages/theme-shared/src/lib/tokens/theme-change.token.ts b/npm/ng-packs/packages/theme-shared/src/lib/tokens/theme-change.token.ts new file mode 100644 index 0000000000..e527dff1bc --- /dev/null +++ b/npm/ng-packs/packages/theme-shared/src/lib/tokens/theme-change.token.ts @@ -0,0 +1,6 @@ +import { InjectionToken } from '@angular/core'; +import { Observable } from 'rxjs'; + +export const THEME_CHANGE_TOKEN = new InjectionToken>( + 'THEME_CHANGE_TOKEN', +);