/
githubmirror
/
components
Обзор
Документация
Войти
/
githubmirror
/
components
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/cdk/bidi/directionality.ts
61 строка
2 KB
Kristiyan Kostadinov
refactor(cdk/bidi): switch to service decorator
25 апр 2026, 23:10
25 апр 2026, 23:10
36755bc
Код
Авторство
О чём код?
/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ import {EventEmitter, Service, OnDestroy, inject, signal} from '@angular/core'; import {DIR_DOCUMENT} from './dir-document-token'; export type Direction = 'ltr' | 'rtl'; /** Regex that matches locales with an RTL script. Taken from `goog.i18n.bidi.isRtlLanguage`. */ const RTL_LOCALE_PATTERN = /^(ar|ckb|dv|he|iw|fa|nqo|ps|sd|ug|ur|yi|.*[-_](Adlm|Arab|Hebr|Nkoo|Rohg|Thaa))(?!.*[-_](Latn|Cyrl)($|-|_))($|-|_)/i; /** Resolves a string value to a specific direction. */ export function _resolveDirectionality(rawValue: string): Direction { const value = rawValue?.toLowerCase() || ''; if (value === 'auto' && typeof navigator !== 'undefined' && navigator?.language) { return RTL_LOCALE_PATTERN.test(navigator.language) ? 'rtl' : 'ltr'; } return value === 'rtl' ? 'rtl' : 'ltr'; } /** * The directionality (LTR / RTL) context for the application (or a subtree of it). * Exposes the current direction and a stream of direction changes. */ @Service() export class Directionality implements OnDestroy { /** The current 'ltr' or 'rtl' value. */ get value() { return this.valueSignal(); } /** * The current 'ltr' or 'rtl' value. */ readonly valueSignal = signal<Direction>('ltr'); /** Stream that emits whenever the 'ltr' / 'rtl' state changes. */ readonly change = new EventEmitter<Direction>(); constructor() { const _document = inject(DIR_DOCUMENT, {optional: true}); if (_document) { const bodyDir = _document.body ? _document.body.dir : null; const htmlDir = _document.documentElement ? _document.documentElement.dir : null; this.valueSignal.set(_resolveDirectionality(bodyDir || htmlDir || 'ltr')); } } ngOnDestroy() { this.change.complete(); } }