@rxap/ngx-theme 19.0.0-dev.1 → 19.0.0-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -295,23 +295,28 @@
295
295
  import {
296
296
  inject,
297
297
  Injectable,
298
+ isDevMode,
298
299
  signal,
299
300
  WritableSignal,
300
301
  } from '@angular/core';
301
302
  import { ConfigService } from '@rxap/config';
302
- import { PubSubService } from '@rxap/ngx-pub-sub';
303
+ import {
304
+ PubSubService,
305
+ RXAP_TOPICS,
306
+ } from '@rxap/ngx-pub-sub';
307
+ import { isDefined } from '@rxap/rxjs';
308
+ import { ThemeDensity } from '@rxap/utilities';
309
+ import {
310
+ debounceTime,
311
+ map,
312
+ Subscription,
313
+ tap,
314
+ } from 'rxjs';
303
315
  import {
304
316
  ColorPalette,
305
317
  ComputeColorPalette,
306
318
  } from './compute-color-palette';
307
319
 
308
- export enum ThemeDensity {
309
- Normal = 0,
310
- Compact = -1,
311
- Dense = -2,
312
- VeryDense = -3,
313
- }
314
-
315
320
  export interface ColorPaletteConfigWithName extends ColorPaletteConfig {
316
321
  name?: string;
317
322
  }
@@ -341,7 +346,9 @@ export class ThemeService {
341
346
  public readonly density: WritableSignal<ThemeDensity>;
342
347
  public readonly typography: WritableSignal<string>;
343
348
 
344
- private readonly darkModeMediaQuery: MediaQueryList;
349
+ protected readonly darkModeMediaQuery: MediaQueryList;
350
+
351
+ protected syncSubscription?: Subscription;
345
352
 
346
353
  constructor(private readonly mediaMatcher: MediaMatcher) {
347
354
  this.darkModeMediaQuery = this.mediaMatcher.matchMedia('(prefers-color-scheme: dark)');
@@ -352,10 +359,12 @@ export class ThemeService {
352
359
  this.darkModeMediaQuery.addEventListener('change', (event) => {
353
360
  this.setDarkTheme(event.matches, true);
354
361
  });
355
- this.restore();
356
362
  }
357
363
 
358
364
  public restore() {
365
+ if (isDevMode()) {
366
+ console.log('Restore theme settings from local storage');
367
+ }
359
368
  // region restore dark mode
360
369
  let darkMode = this.restoreDarkMode();
361
370
  // if the dark/light mode is not restored from the local storage
@@ -369,6 +378,39 @@ export class ThemeService {
369
378
  this.restoreThemeName();
370
379
  this.restoreDensity();
371
380
  this.restoreTypography();
381
+
382
+ this.restoreFromPubSub();
383
+ }
384
+
385
+ protected restoreFromPubSub() {
386
+ if (this.syncSubscription) {
387
+ return;
388
+ }
389
+ this.syncSubscription = new Subscription();
390
+ this.syncSubscription.add(this.pubSub.subscribe<ThemeDensity>(RXAP_TOPICS.theme.density.restore).pipe(
391
+ debounceTime(1000),
392
+ map(event => event.data),
393
+ isDefined(),
394
+ tap(data => this.setDensity(data, false, false))
395
+ ).subscribe());
396
+ this.syncSubscription.add(this.pubSub.subscribe<string>(RXAP_TOPICS.theme.preset.restore).pipe(
397
+ debounceTime(1000),
398
+ map(event => event.data),
399
+ isDefined(),
400
+ tap(data => this.setTheme(data, false, false))
401
+ ).subscribe());
402
+ this.syncSubscription.add(this.pubSub.subscribe<string>(RXAP_TOPICS.theme.typography.restore).pipe(
403
+ debounceTime(1000),
404
+ map(event => event.data),
405
+ isDefined(),
406
+ tap(data => this.setTypography(data, false, false))
407
+ ).subscribe());
408
+ this.syncSubscription.add(this.pubSub.subscribe<boolean>(RXAP_TOPICS.theme.darkMode.restore).pipe(
409
+ debounceTime(1000),
410
+ map(event => event.data),
411
+ isDefined(),
412
+ tap(data => this.setDarkTheme(data, false, false))
413
+ ).subscribe());
372
414
  }
373
415
 
374
416
  private get darkModeLocalStorageKey() {
@@ -448,40 +490,46 @@ export class ThemeService {
448
490
 
449
491
  // region set theme configuration state
450
492
 
451
- public setDarkTheme(darkMode: boolean, silent?: boolean): void {
493
+ public setDarkTheme(darkMode: boolean, silent = false, publish = true): void {
452
494
  this.applyDarkMode(darkMode);
453
495
  if (this.darkMode() !== darkMode) {
454
496
  this.darkMode.set(darkMode);
455
497
  if (!silent) {
456
498
  localStorage.setItem(this.darkModeLocalStorageKey, String(darkMode));
457
- this.pubSub.publish('rxap.theme.darkMode.change', darkMode);
499
+ if (publish) {
500
+ this.pubSub.publish(RXAP_TOPICS.theme.darkMode.changed, darkMode);
501
+ }
458
502
  }
459
503
  }
460
504
  }
461
505
 
462
- public setDensity(density: ThemeDensity, silent = false): void {
506
+ public setDensity(density: ThemeDensity, silent = false, publish = true): void {
463
507
  this.applyDensity(density);
464
508
  if (this.density() !== density) {
465
509
  this.density.set(density);
466
510
  if (!silent) {
467
511
  localStorage.setItem(this.densityLocalStorageKey, String(density));
468
- this.pubSub.publish('rxap.theme.density.change', density);
512
+ if (publish) {
513
+ this.pubSub.publish(RXAP_TOPICS.theme.density.changed, density);
514
+ }
469
515
  }
470
516
  }
471
517
  }
472
518
 
473
- public setTypography(typography: string, silent = false): void {
519
+ public setTypography(typography: string, silent = false, publish = true): void {
474
520
  this.applyTypography(typography);
475
521
  if (this.typography() !== typography) {
476
522
  this.typography.set(typography);
477
523
  if (!silent) {
478
524
  localStorage.setItem(this.typographyLocalStorageKey, typography);
479
- this.pubSub.publish('rxap.theme.typography.change', typography);
525
+ if (publish) {
526
+ this.pubSub.publish(RXAP_TOPICS.theme.typography.changed, typography);
527
+ }
480
528
  }
481
529
  }
482
530
  }
483
531
 
484
- public setTheme(themeName: string, silent = false) {
532
+ public setTheme(themeName: string, silent = false, publish = true) {
485
533
  this.applyTheme(themeName);
486
534
  this.density.set(this.getDensity());
487
535
  this.typography.set(this.getTypography());
@@ -489,7 +537,9 @@ export class ThemeService {
489
537
  this.themeName.set(themeName);
490
538
  if (!silent) {
491
539
  localStorage.setItem(this.themeNameLocalStorageKey, themeName);
492
- this.pubSub.publish('rxap.theme.preset.change', themeName);
540
+ if (publish) {
541
+ this.pubSub.publish(RXAP_TOPICS.theme.preset.changed, themeName);
542
+ }
493
543
  }
494
544
  }
495
545
  }
@@ -211,23 +211,28 @@
211
211
  import {
212
212
  inject,
213
213
  Injectable,
214
+ isDevMode,
214
215
  signal,
215
216
  WritableSignal,
216
217
  } from '@angular/core';
217
218
  import { ConfigService } from '@rxap/config';
218
- import { PubSubService } from '@rxap/ngx-pub-sub';
219
+ import {
220
+ PubSubService,
221
+ RXAP_TOPICS,
222
+ } from '@rxap/ngx-pub-sub';
223
+ import { isDefined } from '@rxap/rxjs';
224
+ import { ThemeDensity } from '@rxap/utilities';
225
+ import {
226
+ debounceTime,
227
+ map,
228
+ Subscription,
229
+ tap,
230
+ } from 'rxjs';
219
231
  import {
220
232
  ColorPalette,
221
233
  ComputeColorPalette,
222
234
  } from './compute-color-palette';
223
235
 
224
- export enum ThemeDensity {
225
- Normal = 0,
226
- Compact = -1,
227
- Dense = -2,
228
- VeryDense = -3,
229
- }
230
-
231
236
  export interface ColorPaletteConfigWithName extends ColorPaletteConfig {
232
237
  name?: string;
233
238
  }
@@ -257,7 +262,9 @@ export class ThemeService {
257
262
  public readonly density: WritableSignal<ThemeDensity>;
258
263
  public readonly typography: WritableSignal<string>;
259
264
 
260
- private readonly darkModeMediaQuery: MediaQueryList;
265
+ protected readonly darkModeMediaQuery: MediaQueryList;
266
+
267
+ protected syncSubscription?: Subscription;
261
268
 
262
269
  constructor(private readonly mediaMatcher: MediaMatcher) {
263
270
  this.darkModeMediaQuery = this.mediaMatcher.matchMedia('(prefers-color-scheme: dark)');
@@ -268,10 +275,12 @@ export class ThemeService {
268
275
  this.darkModeMediaQuery.addEventListener('change', (event) => {
269
276
  this.setDarkTheme(event.matches, true);
270
277
  });
271
- this.restore();
272
278
  }
273
279
 
274
280
  public restore() {
281
+ if (isDevMode()) {
282
+ console.log('Restore theme settings from local storage');
283
+ }
275
284
  // region restore dark mode
276
285
  let darkMode = this.restoreDarkMode();
277
286
  // if the dark/light mode is not restored from the local storage
@@ -285,6 +294,39 @@ export class ThemeService {
285
294
  this.restoreThemeName();
286
295
  this.restoreDensity();
287
296
  this.restoreTypography();
297
+
298
+ this.restoreFromPubSub();
299
+ }
300
+
301
+ protected restoreFromPubSub() {
302
+ if (this.syncSubscription) {
303
+ return;
304
+ }
305
+ this.syncSubscription = new Subscription();
306
+ this.syncSubscription.add(this.pubSub.subscribe<ThemeDensity>(RXAP_TOPICS.theme.density.restore).pipe(
307
+ debounceTime(1000),
308
+ map(event => event.data),
309
+ isDefined(),
310
+ tap(data => this.setDensity(data, false, false))
311
+ ).subscribe());
312
+ this.syncSubscription.add(this.pubSub.subscribe<string>(RXAP_TOPICS.theme.preset.restore).pipe(
313
+ debounceTime(1000),
314
+ map(event => event.data),
315
+ isDefined(),
316
+ tap(data => this.setTheme(data, false, false))
317
+ ).subscribe());
318
+ this.syncSubscription.add(this.pubSub.subscribe<string>(RXAP_TOPICS.theme.typography.restore).pipe(
319
+ debounceTime(1000),
320
+ map(event => event.data),
321
+ isDefined(),
322
+ tap(data => this.setTypography(data, false, false))
323
+ ).subscribe());
324
+ this.syncSubscription.add(this.pubSub.subscribe<boolean>(RXAP_TOPICS.theme.darkMode.restore).pipe(
325
+ debounceTime(1000),
326
+ map(event => event.data),
327
+ isDefined(),
328
+ tap(data => this.setDarkTheme(data, false, false))
329
+ ).subscribe());
288
330
  }
289
331
 
290
332
  private get darkModeLocalStorageKey() {
@@ -364,40 +406,46 @@ export class ThemeService {
364
406
 
365
407
  // region set theme configuration state
366
408
 
367
- public setDarkTheme(darkMode: boolean, silent?: boolean): void {
409
+ public setDarkTheme(darkMode: boolean, silent = false, publish = true): void {
368
410
  this.applyDarkMode(darkMode);
369
411
  if (this.darkMode() !== darkMode) {
370
412
  this.darkMode.set(darkMode);
371
413
  if (!silent) {
372
414
  localStorage.setItem(this.darkModeLocalStorageKey, String(darkMode));
373
- this.pubSub.publish('rxap.theme.darkMode.change', darkMode);
415
+ if (publish) {
416
+ this.pubSub.publish(RXAP_TOPICS.theme.darkMode.changed, darkMode);
417
+ }
374
418
  }
375
419
  }
376
420
  }
377
421
 
378
- public setDensity(density: ThemeDensity, silent = false): void {
422
+ public setDensity(density: ThemeDensity, silent = false, publish = true): void {
379
423
  this.applyDensity(density);
380
424
  if (this.density() !== density) {
381
425
  this.density.set(density);
382
426
  if (!silent) {
383
427
  localStorage.setItem(this.densityLocalStorageKey, String(density));
384
- this.pubSub.publish('rxap.theme.density.change', density);
428
+ if (publish) {
429
+ this.pubSub.publish(RXAP_TOPICS.theme.density.changed, density);
430
+ }
385
431
  }
386
432
  }
387
433
  }
388
434
 
389
- public setTypography(typography: string, silent = false): void {
435
+ public setTypography(typography: string, silent = false, publish = true): void {
390
436
  this.applyTypography(typography);
391
437
  if (this.typography() !== typography) {
392
438
  this.typography.set(typography);
393
439
  if (!silent) {
394
440
  localStorage.setItem(this.typographyLocalStorageKey, typography);
395
- this.pubSub.publish('rxap.theme.typography.change', typography);
441
+ if (publish) {
442
+ this.pubSub.publish(RXAP_TOPICS.theme.typography.changed, typography);
443
+ }
396
444
  }
397
445
  }
398
446
  }
399
447
 
400
- public setTheme(themeName: string, silent = false) {
448
+ public setTheme(themeName: string, silent = false, publish = true) {
401
449
  this.applyTheme(themeName);
402
450
  this.density.set(this.getDensity());
403
451
  this.typography.set(this.getTypography());
@@ -405,7 +453,9 @@ export class ThemeService {
405
453
  this.themeName.set(themeName);
406
454
  if (!silent) {
407
455
  localStorage.setItem(this.themeNameLocalStorageKey, themeName);
408
- this.pubSub.publish('rxap.theme.preset.change', themeName);
456
+ if (publish) {
457
+ this.pubSub.publish(RXAP_TOPICS.theme.preset.changed, themeName);
458
+ }
409
459
  }
410
460
  }
411
461
  }
@@ -238,7 +238,7 @@
238
238
  </tr>
239
239
  <tr>
240
240
  <td class="col-md-4">
241
- <code>density: <code><a href="../miscellaneous/enumerations.html#ThemeDensity" target="_self" >ThemeDensity</a></code>
241
+ <code>density: <code>ThemeDensity</code>
242
242
  </code>
243
243
  </td>
244
244
  </tr>
@@ -246,7 +246,7 @@
246
246
 
247
247
  <tr>
248
248
  <td class="col-md-4">
249
- <i>Type : </i> <code><a href="../miscellaneous/enumerations.html#ThemeDensity" target="_self" >ThemeDensity</a></code>
249
+ <i>Type : </i> <code>ThemeDensity</code>
250
250
 
251
251
  </td>
252
252
  </tr>
@@ -385,23 +385,28 @@
385
385
  import {
386
386
  inject,
387
387
  Injectable,
388
+ isDevMode,
388
389
  signal,
389
390
  WritableSignal,
390
391
  } from &#x27;@angular/core&#x27;;
391
392
  import { ConfigService } from &#x27;@rxap/config&#x27;;
392
- import { PubSubService } from &#x27;@rxap/ngx-pub-sub&#x27;;
393
+ import {
394
+ PubSubService,
395
+ RXAP_TOPICS,
396
+ } from &#x27;@rxap/ngx-pub-sub&#x27;;
397
+ import { isDefined } from &#x27;@rxap/rxjs&#x27;;
398
+ import { ThemeDensity } from &#x27;@rxap/utilities&#x27;;
399
+ import {
400
+ debounceTime,
401
+ map,
402
+ Subscription,
403
+ tap,
404
+ } from &#x27;rxjs&#x27;;
393
405
  import {
394
406
  ColorPalette,
395
407
  ComputeColorPalette,
396
408
  } from &#x27;./compute-color-palette&#x27;;
397
409
 
398
- export enum ThemeDensity {
399
- Normal &#x3D; 0,
400
- Compact &#x3D; -1,
401
- Dense &#x3D; -2,
402
- VeryDense &#x3D; -3,
403
- }
404
-
405
410
  export interface ColorPaletteConfigWithName extends ColorPaletteConfig {
406
411
  name?: string;
407
412
  }
@@ -431,7 +436,9 @@ export class ThemeService {
431
436
  public readonly density: WritableSignal&lt;ThemeDensity&gt;;
432
437
  public readonly typography: WritableSignal&lt;string&gt;;
433
438
 
434
- private readonly darkModeMediaQuery: MediaQueryList;
439
+ protected readonly darkModeMediaQuery: MediaQueryList;
440
+
441
+ protected syncSubscription?: Subscription;
435
442
 
436
443
  constructor(private readonly mediaMatcher: MediaMatcher) {
437
444
  this.darkModeMediaQuery &#x3D; this.mediaMatcher.matchMedia(&#x27;(prefers-color-scheme: dark)&#x27;);
@@ -442,10 +449,12 @@ export class ThemeService {
442
449
  this.darkModeMediaQuery.addEventListener(&#x27;change&#x27;, (event) &#x3D;&gt; {
443
450
  this.setDarkTheme(event.matches, true);
444
451
  });
445
- this.restore();
446
452
  }
447
453
 
448
454
  public restore() {
455
+ if (isDevMode()) {
456
+ console.log(&#x27;Restore theme settings from local storage&#x27;);
457
+ }
449
458
  // region restore dark mode
450
459
  let darkMode &#x3D; this.restoreDarkMode();
451
460
  // if the dark/light mode is not restored from the local storage
@@ -459,6 +468,39 @@ export class ThemeService {
459
468
  this.restoreThemeName();
460
469
  this.restoreDensity();
461
470
  this.restoreTypography();
471
+
472
+ this.restoreFromPubSub();
473
+ }
474
+
475
+ protected restoreFromPubSub() {
476
+ if (this.syncSubscription) {
477
+ return;
478
+ }
479
+ this.syncSubscription &#x3D; new Subscription();
480
+ this.syncSubscription.add(this.pubSub.subscribe&lt;ThemeDensity&gt;(RXAP_TOPICS.theme.density.restore).pipe(
481
+ debounceTime(1000),
482
+ map(event &#x3D;&gt; event.data),
483
+ isDefined(),
484
+ tap(data &#x3D;&gt; this.setDensity(data, false, false))
485
+ ).subscribe());
486
+ this.syncSubscription.add(this.pubSub.subscribe&lt;string&gt;(RXAP_TOPICS.theme.preset.restore).pipe(
487
+ debounceTime(1000),
488
+ map(event &#x3D;&gt; event.data),
489
+ isDefined(),
490
+ tap(data &#x3D;&gt; this.setTheme(data, false, false))
491
+ ).subscribe());
492
+ this.syncSubscription.add(this.pubSub.subscribe&lt;string&gt;(RXAP_TOPICS.theme.typography.restore).pipe(
493
+ debounceTime(1000),
494
+ map(event &#x3D;&gt; event.data),
495
+ isDefined(),
496
+ tap(data &#x3D;&gt; this.setTypography(data, false, false))
497
+ ).subscribe());
498
+ this.syncSubscription.add(this.pubSub.subscribe&lt;boolean&gt;(RXAP_TOPICS.theme.darkMode.restore).pipe(
499
+ debounceTime(1000),
500
+ map(event &#x3D;&gt; event.data),
501
+ isDefined(),
502
+ tap(data &#x3D;&gt; this.setDarkTheme(data, false, false))
503
+ ).subscribe());
462
504
  }
463
505
 
464
506
  private get darkModeLocalStorageKey() {
@@ -538,40 +580,46 @@ export class ThemeService {
538
580
 
539
581
  // region set theme configuration state
540
582
 
541
- public setDarkTheme(darkMode: boolean, silent?: boolean): void {
583
+ public setDarkTheme(darkMode: boolean, silent &#x3D; false, publish &#x3D; true): void {
542
584
  this.applyDarkMode(darkMode);
543
585
  if (this.darkMode() !&#x3D;&#x3D; darkMode) {
544
586
  this.darkMode.set(darkMode);
545
587
  if (!silent) {
546
588
  localStorage.setItem(this.darkModeLocalStorageKey, String(darkMode));
547
- this.pubSub.publish(&#x27;rxap.theme.darkMode.change&#x27;, darkMode);
589
+ if (publish) {
590
+ this.pubSub.publish(RXAP_TOPICS.theme.darkMode.changed, darkMode);
591
+ }
548
592
  }
549
593
  }
550
594
  }
551
595
 
552
- public setDensity(density: ThemeDensity, silent &#x3D; false): void {
596
+ public setDensity(density: ThemeDensity, silent &#x3D; false, publish &#x3D; true): void {
553
597
  this.applyDensity(density);
554
598
  if (this.density() !&#x3D;&#x3D; density) {
555
599
  this.density.set(density);
556
600
  if (!silent) {
557
601
  localStorage.setItem(this.densityLocalStorageKey, String(density));
558
- this.pubSub.publish(&#x27;rxap.theme.density.change&#x27;, density);
602
+ if (publish) {
603
+ this.pubSub.publish(RXAP_TOPICS.theme.density.changed, density);
604
+ }
559
605
  }
560
606
  }
561
607
  }
562
608
 
563
- public setTypography(typography: string, silent &#x3D; false): void {
609
+ public setTypography(typography: string, silent &#x3D; false, publish &#x3D; true): void {
564
610
  this.applyTypography(typography);
565
611
  if (this.typography() !&#x3D;&#x3D; typography) {
566
612
  this.typography.set(typography);
567
613
  if (!silent) {
568
614
  localStorage.setItem(this.typographyLocalStorageKey, typography);
569
- this.pubSub.publish(&#x27;rxap.theme.typography.change&#x27;, typography);
615
+ if (publish) {
616
+ this.pubSub.publish(RXAP_TOPICS.theme.typography.changed, typography);
617
+ }
570
618
  }
571
619
  }
572
620
  }
573
621
 
574
- public setTheme(themeName: string, silent &#x3D; false) {
622
+ public setTheme(themeName: string, silent &#x3D; false, publish &#x3D; true) {
575
623
  this.applyTheme(themeName);
576
624
  this.density.set(this.getDensity());
577
625
  this.typography.set(this.getTypography());
@@ -579,7 +627,9 @@ export class ThemeService {
579
627
  this.themeName.set(themeName);
580
628
  if (!silent) {
581
629
  localStorage.setItem(this.themeNameLocalStorageKey, themeName);
582
- this.pubSub.publish(&#x27;rxap.theme.preset.change&#x27;, themeName);
630
+ if (publish) {
631
+ this.pubSub.publish(RXAP_TOPICS.theme.preset.changed, themeName);
632
+ }
583
633
  }
584
634
  }
585
635
  }
@@ -37,4 +37,4 @@ customElements.define('compodoc-menu', /*#__PURE__*/function (_HTMLElement) {
37
37
  this.innerHTML = tp.strings;
38
38
  }
39
39
  }]);
40
- }( /*#__PURE__*/_wrapNativeSuper(HTMLElement)));
40
+ }(/*#__PURE__*/_wrapNativeSuper(HTMLElement)));