lisichatbot 1.6.6 → 1.6.8

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +76 -90
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lisichatbot",
3
- "version": "1.6.6",
3
+ "version": "1.6.8",
4
4
  "type": "module",
5
5
  "main": "./src/index.js",
6
6
  "exports": {
package/src/index.js CHANGED
@@ -48,16 +48,29 @@ let flowData = null;
48
48
  // =============================================================================
49
49
 
50
50
  function scrollToBottom() {
51
- if (elements.messages) {
52
- // ✅ Use requestAnimationFrame to ensure DOM has updated
51
+ if (!elements.messages) return;
52
+
53
+ // ✅ NEW: Ensure the very last child is fully visible
54
+ const scrollToLastElement = () => {
55
+ const lastChild = elements.messages.lastElementChild;
56
+ if (lastChild) {
57
+ lastChild.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'nearest' });
58
+ console.log(` 📜 Scrolled last element into view`);
59
+ } else {
60
+ // Fallback to scrollHeight
61
+ elements.messages.scrollTop = elements.messages.scrollHeight;
62
+ }
63
+ };
64
+
65
+ // Immediate scroll
66
+ scrollToLastElement();
67
+
68
+ // ✅ Use requestAnimationFrame to ensure DOM layout is complete
69
+ requestAnimationFrame(() => {
53
70
  requestAnimationFrame(() => {
54
- requestAnimationFrame(() => {
55
- // Double RAF ensures layout is complete
56
- elements.messages.scrollTop = elements.messages.scrollHeight;
57
- console.log(` 📜 Scrolled to bottom: ${elements.messages.scrollHeight}px`);
58
- });
71
+ scrollToLastElement();
59
72
  });
60
- }
73
+ });
61
74
  }
62
75
 
63
76
  function enableNextButton() {
@@ -2011,10 +2024,6 @@ async function handleNext() {
2011
2024
  console.log('💬 showMessage called:', message);
2012
2025
  addMessage(message, 'bot', false, null);
2013
2026
  scrollToBottom();
2014
- // ✅ Delayed scroll to ensure message is fully rendered
2015
- setTimeout(() => {
2016
- scrollToBottom();
2017
- }, 100);
2018
2027
  };
2019
2028
 
2020
2029
  // ✅ Pass showMessage as third parameter
@@ -2029,10 +2038,6 @@ async function handleNext() {
2029
2038
  console.log('💬 onNext displaying message from return:', result.showMessage);
2030
2039
  addMessage(result.showMessage, 'bot', false, null);
2031
2040
  scrollToBottom();
2032
- // ✅ Delayed scroll to ensure message is fully rendered
2033
- setTimeout(() => {
2034
- scrollToBottom();
2035
- }, 100);
2036
2041
  }
2037
2042
 
2038
2043
  // ✅ NEW: Check if onNext wants to update steps by field name
@@ -2167,20 +2172,8 @@ async function handleNext() {
2167
2172
  disableNextButton();
2168
2173
  await showNextStep();
2169
2174
 
2170
- // ✅ NEW: Extra scroll after jump to ensure inputs are visible
2171
- setTimeout(() => {
2172
- scrollToBottom();
2173
- }, 350);
2174
-
2175
- // ✅ NEW: Additional delayed scroll for cases with multiple showMessage calls
2176
- setTimeout(() => {
2177
- scrollToBottom();
2178
- }, 500);
2179
-
2180
- // ✅ NEW: Final scroll for steps with multiple inputs
2181
- setTimeout(() => {
2182
- scrollToBottom();
2183
- }, 800);
2175
+ // ✅ Single scroll call - MutationObserver will handle the rest
2176
+ scrollToBottom();
2184
2177
 
2185
2178
  return;
2186
2179
  } else {
@@ -2406,16 +2399,8 @@ async function handleNext() {
2406
2399
 
2407
2400
  await showNextStep();
2408
2401
 
2409
- // ✅ NEW: Extra scroll after step is shown to ensure visibility
2410
- // This catches cases where showMessage was called in onNext before progressing
2411
- setTimeout(() => {
2412
- scrollToBottom();
2413
- }, 350);
2414
-
2415
- // ✅ NEW: Additional scroll for steps with multiple inputs
2416
- setTimeout(() => {
2417
- scrollToBottom();
2418
- }, 700);
2402
+ // ✅ Final scroll after showNextStep completes
2403
+ scrollToBottom();
2419
2404
  }
2420
2405
 
2421
2406
  // ✅ NEW: Helper function to find next step that is accessible in normal flow
@@ -2482,10 +2467,6 @@ async function showNextStep() {
2482
2467
  console.log('💬 showMessage called:', message);
2483
2468
  addMessage(message, 'bot', false, null);
2484
2469
  scrollToBottom();
2485
- // ✅ Delayed scroll to ensure message is fully rendered
2486
- setTimeout(() => {
2487
- scrollToBottom();
2488
- }, 100);
2489
2470
  };
2490
2471
 
2491
2472
  // ✅ Pass showMessage as second parameter
@@ -2496,10 +2477,6 @@ async function showNextStep() {
2496
2477
  console.log('💬 onStart displaying message from return:', result.showMessage);
2497
2478
  addMessage(result.showMessage, 'bot', false, null);
2498
2479
  scrollToBottom();
2499
- // ✅ Delayed scroll to ensure message is fully rendered
2500
- setTimeout(() => {
2501
- scrollToBottom();
2502
- }, 100);
2503
2480
  }
2504
2481
 
2505
2482
  // ✅ NEW: Check if onStart wants to update steps by field name
@@ -2675,25 +2652,16 @@ async function showNextStep() {
2675
2652
  console.log(` 🔓 Override active for fields:`, chatState.prefillOverrideFields);
2676
2653
  }
2677
2654
 
2655
+ // ✅ Check if we're jumping from the very first step (needs extra time to initialize)
2656
+ const isJumpingFromFirstStep = chatState.step === 0;
2657
+
2678
2658
  chatState.step = targetStepIndex;
2679
2659
  chatState.currentSelection = null;
2680
2660
  disableNextButton();
2681
2661
  await showNextStep();
2682
2662
 
2683
- // ✅ NEW: Extra scroll after jump to ensure inputs are visible
2684
- setTimeout(() => {
2685
- scrollToBottom();
2686
- }, 350);
2687
-
2688
- // ✅ NEW: Additional delayed scroll for cases with multiple showMessage calls
2689
- setTimeout(() => {
2690
- scrollToBottom();
2691
- }, 500);
2692
-
2693
- // ✅ NEW: Final scroll for steps with multiple inputs
2694
- setTimeout(() => {
2695
- scrollToBottom();
2696
- }, 800);
2663
+ // ✅ Single scroll call - MutationObserver will handle the rest
2664
+ scrollToBottom();
2697
2665
 
2698
2666
  return;
2699
2667
  } else {
@@ -2723,25 +2691,16 @@ async function showNextStep() {
2723
2691
  console.log(` 🔓 Override active for fields:`, chatState.prefillOverrideFields);
2724
2692
  }
2725
2693
 
2694
+ // ✅ Check if we're jumping from the very first step (needs extra time to initialize)
2695
+ const isJumpingFromFirstStep = chatState.step === 0;
2696
+
2726
2697
  chatState.step = targetStep;
2727
2698
  chatState.currentSelection = null;
2728
2699
  disableNextButton();
2729
2700
  await showNextStep();
2730
2701
 
2731
- // ✅ NEW: Extra scroll after jump to ensure inputs are visible
2732
- setTimeout(() => {
2733
- scrollToBottom();
2734
- }, 350);
2735
-
2736
- // ✅ NEW: Additional delayed scroll for cases with multiple showMessage calls
2737
- setTimeout(() => {
2738
- scrollToBottom();
2739
- }, 500);
2740
-
2741
- // ✅ NEW: Final scroll for steps with multiple inputs
2742
- setTimeout(() => {
2743
- scrollToBottom();
2744
- }, 800);
2702
+ // ✅ Single scroll call - MutationObserver will handle the rest
2703
+ scrollToBottom();
2745
2704
 
2746
2705
  return;
2747
2706
  } else {
@@ -2818,6 +2777,38 @@ async function showNextStep() {
2818
2777
 
2819
2778
  addMessage(messageToDisplay, 'bot', hasInput, chatState.step);
2820
2779
 
2780
+ // ✅ NEW: Add custom elements if specified
2781
+ if (nextStep.addElements && Array.isArray(nextStep.addElements)) {
2782
+ console.log(` 🎨 Adding ${nextStep.addElements.length} custom element(s) to chat`);
2783
+
2784
+ nextStep.addElements.forEach((selector, index) => {
2785
+ try {
2786
+ // Find the element in the DOM
2787
+ const element = document.querySelector(selector);
2788
+
2789
+ if (element) {
2790
+ // Clone the element to avoid removing it from its original location
2791
+ const clonedElement = element.cloneNode(true);
2792
+
2793
+ // Make it visible (in case it was hidden)
2794
+ clonedElement.style.display = '';
2795
+
2796
+ // Add to messages container
2797
+ elements.messages.appendChild(clonedElement);
2798
+
2799
+ console.log(` ✅ Added element ${index + 1}: ${selector}`);
2800
+ } else {
2801
+ console.warn(` ⚠️ Element not found: ${selector}`);
2802
+ }
2803
+ } catch (error) {
2804
+ console.error(` ❌ Error adding element ${selector}:`, error);
2805
+ }
2806
+ });
2807
+
2808
+ // Scroll after adding elements
2809
+ scrollToBottom();
2810
+ }
2811
+
2821
2812
  const inputRequired = nextStep.inputRequired === true;
2822
2813
 
2823
2814
  if (nextStep.input) {
@@ -2961,20 +2952,15 @@ async function showNextStep() {
2961
2952
  updateEditIcons();
2962
2953
  }, 10);
2963
2954
 
2964
- // ✅ NEW: Delayed scroll to ensure inputs are fully rendered and visible
2965
- setTimeout(() => {
2966
- scrollToBottom();
2967
- }, 150);
2968
-
2969
- // ✅ NEW: Extra delayed scroll to catch cases where showMessage was called before this step
2970
- setTimeout(() => {
2971
- scrollToBottom();
2972
- }, 300);
2973
-
2974
- // ✅ NEW: Final scroll for complex steps with multiple inputs or slow rendering
2975
- setTimeout(() => {
2976
- scrollToBottom();
2977
- }, 600);
2955
+ // ✅ Final scroll after all rendering completes
2956
+ requestAnimationFrame(() => {
2957
+ requestAnimationFrame(() => {
2958
+ requestAnimationFrame(() => {
2959
+ // Triple RAF to ensure everything is laid out
2960
+ scrollToBottom();
2961
+ });
2962
+ });
2963
+ });
2978
2964
  }
2979
2965
 
2980
2966
  function handleCompletion() {