78 lines
3.2 KiB
JavaScript
Executable File
78 lines
3.2 KiB
JavaScript
Executable File
import * as i0 from '@angular/core';
|
|
import { InjectionToken, inject, DOCUMENT, signal, EventEmitter, Injectable } from '@angular/core';
|
|
|
|
/**
|
|
* Injection token used to inject the document into Directionality.
|
|
* This is used so that the value can be faked in tests.
|
|
*
|
|
* We can't use the real document in tests because changing the real `dir` causes geometry-based
|
|
* tests in Safari to fail.
|
|
*
|
|
* We also can't re-provide the DOCUMENT token from platform-browser because the unit tests
|
|
* themselves use things like `querySelector` in test code.
|
|
*
|
|
* This token is defined in a separate file from Directionality as a workaround for
|
|
* https://github.com/angular/angular/issues/22559
|
|
*
|
|
* @docs-private
|
|
*/
|
|
const DIR_DOCUMENT = new InjectionToken('cdk-dir-doc', {
|
|
providedIn: 'root',
|
|
factory: DIR_DOCUMENT_FACTORY,
|
|
});
|
|
/**
|
|
* @docs-private
|
|
* @deprecated No longer used, will be removed.
|
|
* @breaking-change 21.0.0
|
|
*/
|
|
function DIR_DOCUMENT_FACTORY() {
|
|
return inject(DOCUMENT);
|
|
}
|
|
|
|
/** 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. */
|
|
function _resolveDirectionality(rawValue) {
|
|
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.
|
|
*/
|
|
class Directionality {
|
|
/** The current 'ltr' or 'rtl' value. */
|
|
get value() {
|
|
return this.valueSignal();
|
|
}
|
|
/**
|
|
* The current 'ltr' or 'rtl' value.
|
|
*/
|
|
valueSignal = signal('ltr', ...(ngDevMode ? [{ debugName: "valueSignal" }] : []));
|
|
/** Stream that emits whenever the 'ltr' / 'rtl' state changes. */
|
|
change = new EventEmitter();
|
|
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();
|
|
}
|
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.2.0-next.2", ngImport: i0, type: Directionality, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.2.0-next.2", ngImport: i0, type: Directionality, providedIn: 'root' });
|
|
}
|
|
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.0-next.2", ngImport: i0, type: Directionality, decorators: [{
|
|
type: Injectable,
|
|
args: [{ providedIn: 'root' }]
|
|
}], ctorParameters: () => [] });
|
|
|
|
export { DIR_DOCUMENT, Directionality, _resolveDirectionality };
|
|
//# sourceMappingURL=directionality.mjs.map
|