@ibragim-dev-org/ui-forge-library 1.4.34 → 1.4.36

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.
@@ -2,10 +2,10 @@ import * as i2$1 from '@angular/router';
2
2
  import { NavigationEnd, NavigationStart, ActivationEnd } from '@angular/router';
3
3
  import { BehaviorSubject, filter, take, scheduled, fromEvent, asyncScheduler, mergeAll, tap, debounceTime, Subject, Subscription, timer, switchMap, interval, takeWhile, finalize, takeUntil as takeUntil$1 } from 'rxjs';
4
4
  import * as i0 from '@angular/core';
5
- import { model, effect, Directive, Injectable, output, Input, input, PLATFORM_ID, Inject, EventEmitter, Output, Pipe, Attribute, ChangeDetectionStrategy, Component, signal, computed, viewChild, untracked, Injector, afterRenderEffect, contentChildren, contentChild, ViewChildren } from '@angular/core';
5
+ import { model, effect, Directive, Injectable, output, Input, input, PLATFORM_ID, Inject, EventEmitter, Output, inject, Pipe, Attribute, ChangeDetectionStrategy, Component, signal, computed, viewChild, untracked, Injector, afterRenderEffect, contentChildren, contentChild, ViewChildren } from '@angular/core';
6
6
  import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
7
7
  import { isPlatformBrowser, DOCUMENT, NgStyle } from '@angular/common';
8
- import { throttleTime, tap as tap$1, takeUntil } from 'rxjs/operators';
8
+ import { throttleTime, tap as tap$1, takeUntil, debounceTime as debounceTime$1 } from 'rxjs/operators';
9
9
  import * as i2 from '@angular/platform-browser';
10
10
  import { PhoneNumberUtil, AsYouTypeFormatter } from 'google-libphonenumber';
11
11
  import { ulid } from 'ulid';
@@ -787,12 +787,25 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.1", ngImpor
787
787
 
788
788
  class StableScrollDirective {
789
789
  el;
790
- resizeObserver;
790
+ platformId = inject(PLATFORM_ID);
791
+ isBrowser = isPlatformBrowser(this.platformId);
791
792
  destroy$ = new Subject();
792
793
  scrolling$ = new Subject();
793
794
  _isScrolling = false;
794
- anchorElement = null;
795
- anchorOffset = 0;
795
+ mutationObserver;
796
+ restoreFrameId = null;
797
+ restoreMicrotaskPending = false;
798
+ // ---- thresholds ----
799
+ $topThreshold = input(0, ...(ngDevMode ? [{ debugName: "$topThreshold" }] : []));
800
+ $bottomThreshold = input(0, ...(ngDevMode ? [{ debugName: "$bottomThreshold" }] : []));
801
+ reachedTop = output();
802
+ reachedBottom = output();
803
+ // ---- anchor ----
804
+ anchorSnapshot = null;
805
+ // ---- direction tracking ----
806
+ lastScrollTop = 0;
807
+ scrollingUp = false;
808
+ scrollingDown = false;
796
809
  constructor(elRef) {
797
810
  this.el = elRef.nativeElement;
798
811
  }
@@ -800,82 +813,166 @@ class StableScrollDirective {
800
813
  return this._isScrolling;
801
814
  }
802
815
  ngAfterViewInit() {
803
- // scroll listener
816
+ if (!this.isBrowser) {
817
+ return;
818
+ }
819
+ this.lastScrollTop = this.el.scrollTop;
820
+ this.refreshAnchorSnapshot();
821
+ this.observeContentChanges();
804
822
  fromEvent(this.el, 'scroll')
805
823
  .pipe(throttleTime(50), tap$1(() => {
824
+ const { scrollTop } = this.el;
825
+ // detect direction
826
+ this.scrollingUp = scrollTop < this.lastScrollTop;
827
+ this.scrollingDown = scrollTop > this.lastScrollTop;
828
+ this.lastScrollTop = scrollTop;
806
829
  this._isScrolling = true;
807
830
  this.scrolling$.next();
831
+ this.checkThresholds();
832
+ this.refreshAnchorSnapshot();
808
833
  }), takeUntil(this.destroy$))
809
834
  .subscribe();
810
- // element resize
811
- this.resizeObserver = new ResizeObserver(() => {
812
- if (!this._isScrolling) {
813
- this.captureAnchor();
814
- requestAnimationFrame(() => this.restoreAnchor());
815
- }
816
- });
817
- this.resizeObserver.observe(this.el);
818
- // window resize
819
- fromEvent(window, 'resize')
820
- .pipe(throttleTime(50), tap$1(() => {
821
- if (!this._isScrolling) {
822
- this.captureAnchor();
823
- requestAnimationFrame(() => this.restoreAnchor());
824
- }
825
- }), takeUntil(this.destroy$))
835
+ this.scrolling$
836
+ .pipe(debounceTime$1(120), tap$1(() => (this._isScrolling = false)), takeUntil(this.destroy$))
826
837
  .subscribe();
827
838
  }
828
839
  ngOnDestroy() {
840
+ this.mutationObserver?.disconnect();
841
+ if (this.restoreFrameId !== null && this.isBrowser) {
842
+ cancelAnimationFrame(this.restoreFrameId);
843
+ }
829
844
  this.destroy$.next();
830
845
  this.destroy$.complete();
831
- if (this.resizeObserver) {
832
- this.resizeObserver.disconnect();
833
- }
834
846
  }
835
- /** Call this BEFORE you change data (prepend/append/remove). */
847
+ /** Call BEFORE data changes */
836
848
  captureBeforeChange() {
837
- this.captureAnchor();
849
+ this.refreshAnchorSnapshot();
838
850
  }
839
- /** Call this AFTER render (e.g. in afterEveryRender). */
851
+ /** Call AFTER render */
840
852
  restoreScrollPosition() {
841
- this.restoreAnchor();
842
- }
843
- // ---- anchor logic ----
844
- captureAnchor() {
845
- const el = this.el;
846
- const midY = el.scrollTop + el.clientHeight * 0.5;
847
- const children = Array.from(el.children);
848
- const anchor = children.find((c) => {
849
- const top = c.offsetTop;
850
- const bottom = top + c.offsetHeight;
851
- return top <= midY && bottom >= midY;
853
+ this.scheduleRestore();
854
+ }
855
+ // ---- threshold logic ----
856
+ checkThresholds() {
857
+ const { scrollTop, scrollHeight, clientHeight } = this.el;
858
+ const topThreshold = this.$topThreshold();
859
+ const topPx = topThreshold <= 1 ? scrollHeight * topThreshold : topThreshold;
860
+ const bottomThreshold = this.$bottomThreshold();
861
+ const bottomPx = bottomThreshold <= 1 ? scrollHeight * bottomThreshold : bottomThreshold;
862
+ // emit top ONLY when scrolling UP
863
+ if (this.scrollingUp && scrollTop <= topPx) {
864
+ this.reachedTop.emit();
865
+ }
866
+ // emit bottom ONLY when scrolling DOWN
867
+ if (this.scrollingDown && scrollTop + clientHeight >= scrollHeight - bottomPx) {
868
+ this.reachedBottom.emit();
869
+ }
870
+ }
871
+ observeContentChanges() {
872
+ if (typeof MutationObserver === 'undefined') {
873
+ return;
874
+ }
875
+ this.mutationObserver = new MutationObserver((mutations) => {
876
+ if (!mutations.some((mutation) => mutation.addedNodes.length || mutation.removedNodes.length)) {
877
+ return;
878
+ }
879
+ if (!this.anchorSnapshot) {
880
+ this.refreshAnchorSnapshot();
881
+ return;
882
+ }
883
+ this.scheduleRestore();
852
884
  });
853
- if (!anchor) {
854
- this.anchorElement = null;
885
+ this.mutationObserver.observe(this.el, {
886
+ childList: true,
887
+ });
888
+ }
889
+ scheduleRestore() {
890
+ if (!this.isBrowser || this.restoreMicrotaskPending) {
855
891
  return;
856
892
  }
857
- this.anchorElement = anchor;
858
- this.anchorOffset = midY - anchor.offsetTop;
893
+ this.restoreMicrotaskPending = true;
894
+ queueMicrotask(() => {
895
+ this.restoreMicrotaskPending = false;
896
+ this.restoreFromSnapshot();
897
+ this.scheduleAnimationFrameRestore();
898
+ });
859
899
  }
860
- restoreAnchor() {
861
- if (!this.anchorElement)
900
+ restoreFromSnapshot() {
901
+ if (!this.anchorSnapshot) {
862
902
  return;
863
- const el = this.el;
864
- const anchor = this.anchorElement;
865
- const midY = anchor.offsetTop + this.anchorOffset;
866
- el.scrollTop = midY - el.clientHeight * 0.5;
867
- this.anchorElement = null;
903
+ }
904
+ const { element, topOffset } = this.anchorSnapshot;
905
+ if (!element.isConnected || element.parentElement !== this.el) {
906
+ this.anchorSnapshot = null;
907
+ return;
908
+ }
909
+ const containerTop = this.el.getBoundingClientRect().top;
910
+ const nextTopOffset = element.getBoundingClientRect().top - containerTop;
911
+ const delta = nextTopOffset - topOffset;
912
+ if (delta !== 0) {
913
+ this.el.scrollTop += delta;
914
+ }
915
+ this.lastScrollTop = this.el.scrollTop;
916
+ this.refreshAnchorSnapshot();
917
+ }
918
+ scheduleAnimationFrameRestore() {
919
+ if (!this.isBrowser) {
920
+ return;
921
+ }
922
+ if (this.restoreFrameId !== null) {
923
+ cancelAnimationFrame(this.restoreFrameId);
924
+ }
925
+ this.restoreFrameId = requestAnimationFrame(() => {
926
+ this.restoreFrameId = null;
927
+ this.restoreFromSnapshot();
928
+ });
929
+ }
930
+ refreshAnchorSnapshot() {
931
+ const anchor = this.resolveAnchorElement();
932
+ if (!anchor) {
933
+ this.anchorSnapshot = null;
934
+ return;
935
+ }
936
+ const containerTop = this.el.getBoundingClientRect().top;
937
+ const topOffset = anchor.getBoundingClientRect().top - containerTop;
938
+ this.anchorSnapshot = {
939
+ element: anchor,
940
+ topOffset,
941
+ };
942
+ }
943
+ resolveAnchorElement() {
944
+ const containerRect = this.el.getBoundingClientRect();
945
+ const candidates = this.resolveAnchorCandidates();
946
+ if (!candidates.length) {
947
+ return null;
948
+ }
949
+ const visibleCandidate = candidates.find((candidate) => {
950
+ const rect = candidate.getBoundingClientRect();
951
+ return rect.bottom > containerRect.top && rect.top < containerRect.bottom;
952
+ });
953
+ if (visibleCandidate) {
954
+ return visibleCandidate;
955
+ }
956
+ return candidates.reduce((closest, candidate) => {
957
+ const closestDistance = Math.abs(closest.getBoundingClientRect().top - containerRect.top);
958
+ const candidateDistance = Math.abs(candidate.getBoundingClientRect().top - containerRect.top);
959
+ return candidateDistance < closestDistance ? candidate : closest;
960
+ });
961
+ }
962
+ resolveAnchorCandidates() {
963
+ const children = Array.from(this.el.children);
964
+ const messageChildren = children.filter((child) => child.classList.contains('message'));
965
+ return messageChildren.length ? messageChildren : children;
868
966
  }
869
967
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.1", ngImport: i0, type: StableScrollDirective, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
870
- static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "21.1.1", type: StableScrollDirective, isStandalone: true, selector: "[stableScroll]", ngImport: i0 });
968
+ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "21.1.1", type: StableScrollDirective, isStandalone: true, selector: "[stableScroll]", inputs: { $topThreshold: { classPropertyName: "$topThreshold", publicName: "$topThreshold", isSignal: true, isRequired: false, transformFunction: null }, $bottomThreshold: { classPropertyName: "$bottomThreshold", publicName: "$bottomThreshold", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { reachedTop: "reachedTop", reachedBottom: "reachedBottom" }, ngImport: i0 });
871
969
  }
872
970
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.1", ngImport: i0, type: StableScrollDirective, decorators: [{
873
971
  type: Directive,
874
972
  args: [{
875
973
  selector: '[stableScroll]',
876
- standalone: true,
877
974
  }]
878
- }], ctorParameters: () => [{ type: i0.ElementRef }] });
975
+ }], ctorParameters: () => [{ type: i0.ElementRef }], propDecorators: { $topThreshold: [{ type: i0.Input, args: [{ isSignal: true, alias: "$topThreshold", required: false }] }], $bottomThreshold: [{ type: i0.Input, args: [{ isSignal: true, alias: "$bottomThreshold", required: false }] }], reachedTop: [{ type: i0.Output, args: ["reachedTop"] }], reachedBottom: [{ type: i0.Output, args: ["reachedBottom"] }] } });
879
976
 
880
977
  class CenterEllipsisPipe {
881
978
  transform(value, visibleStart = 4, visibleEnd = 4) {