lisichatbot 1.1.9 → 1.2.1

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 +107 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lisichatbot",
3
- "version": "1.1.9",
3
+ "version": "1.2.1",
4
4
  "type": "module",
5
5
  "main": "./src/index.js",
6
6
  "exports": {
package/src/index.js CHANGED
@@ -15,7 +15,8 @@ let chatState = {
15
15
  step: 0,
16
16
  data: {},
17
17
  history: [],
18
- currentSelection: null
18
+ currentSelection: null,
19
+ returnToStep: null // Track where to return after editing
19
20
  };
20
21
 
21
22
  let elements = {
@@ -115,10 +116,15 @@ function addMessage(content, type = 'bot', hasInput = false, stepNumber = null)
115
116
  editIcon.setAttribute('data-chat-step', stepNumber);
116
117
  editIcon.onclick = (e) => {
117
118
  e.stopPropagation();
119
+ e.preventDefault();
120
+ console.log(`\n🖱️ EDIT ICON CLICKED - Step ${stepNumber}`);
121
+ console.log(` Current step: ${chatState.step}`);
122
+ console.log(` Calling editStep(${stepNumber})...`);
118
123
  editStep(stepNumber);
119
124
  };
120
125
  editIcon.style.setProperty('display', 'block', 'important');
121
126
  editIcon.style.setProperty('margin-left', '8px', 'important');
127
+ editIcon.style.setProperty('cursor', 'pointer', 'important');
122
128
  console.log(` ✏️ Edit icon SHOWN (step ${stepNumber} is previous step with input)`);
123
129
  } else if (hasInput && stepNumber === chatState.step) {
124
130
  console.log(` ⏸️ Edit icon HIDDEN (step ${stepNumber} is CURRENT step)`);
@@ -166,6 +172,53 @@ function addMessage(content, type = 'bot', hasInput = false, stepNumber = null)
166
172
  scrollToBottom();
167
173
  }
168
174
 
175
+ // =============================================================================
176
+ // UPDATE EDIT ICONS FOR PREVIOUS STEPS
177
+ // =============================================================================
178
+
179
+ function updateEditIcons() {
180
+ // Find all rendered bot message wrappers
181
+ const allBotMessages = elements.messages.querySelectorAll('[data-chat-element="bot-message-wrapper"]');
182
+
183
+ console.log(`\n🔄 Updating edit icons - Current step: ${chatState.step}`);
184
+
185
+ allBotMessages.forEach(wrapper => {
186
+ const stepNumber = parseInt(wrapper.getAttribute('data-chat-step'));
187
+ const editIcon = wrapper.querySelector('[data-chat-element="bot-edit-icon"]');
188
+
189
+ if (editIcon && !isNaN(stepNumber)) {
190
+ // Check if this step has input by looking at the flow
191
+ const stepData = flowData.flow[stepNumber];
192
+ const hasInput = stepData && !!stepData.input;
193
+
194
+ // Show icon if: has input AND is previous step
195
+ if (hasInput && stepNumber < chatState.step) {
196
+ // Set onclick handler
197
+ editIcon.onclick = (e) => {
198
+ e.stopPropagation();
199
+ e.preventDefault();
200
+ console.log(`\n🖱️ EDIT ICON CLICKED (from updateEditIcons) - Step ${stepNumber}`);
201
+ console.log(` Current step: ${chatState.step}`);
202
+ console.log(` Calling editStep(${stepNumber})...`);
203
+ editStep(stepNumber);
204
+ };
205
+ editIcon.setAttribute('data-chat-step', stepNumber);
206
+ editIcon.style.setProperty('display', 'block', 'important');
207
+ editIcon.style.setProperty('cursor', 'pointer', 'important');
208
+ editIcon.style.setProperty('margin-left', '8px', 'important');
209
+ console.log(` ✏️ Step ${stepNumber}: Icon SHOWN (previous step)`);
210
+ } else {
211
+ editIcon.style.setProperty('display', 'none', 'important');
212
+ if (stepNumber === chatState.step) {
213
+ console.log(` ⏸️ Step ${stepNumber}: Icon HIDDEN (current step)`);
214
+ } else if (!hasInput) {
215
+ console.log(` ⚪ Step ${stepNumber}: Icon HIDDEN (no input)`);
216
+ }
217
+ }
218
+ }
219
+ });
220
+ }
221
+
169
222
  // =============================================================================
170
223
  // OPTIONS RENDERING WITH CUSTOM ATTRIBUTES
171
224
  // =============================================================================
@@ -500,8 +553,33 @@ async function handleNext() {
500
553
  chatState.currentSelection = null;
501
554
  disableNextButton();
502
555
 
503
- // Move to next step
556
+ // Check if we should return to a saved step (after editing)
557
+ if (chatState.returnToStep !== null) {
558
+ const targetStep = chatState.returnToStep;
559
+ console.log(`\n🔙 Returning to saved step ${targetStep} (edited step complete)`);
560
+ chatState.returnToStep = null; // Clear the return step
561
+
562
+ // Check if target step is valid
563
+ if (targetStep >= 0 && targetStep < flowData.flow.length) {
564
+ chatState.step = targetStep;
565
+ console.log(` ✅ Jumped to step ${targetStep}`);
566
+
567
+ // Update edit icons for the new position
568
+ updateEditIcons();
569
+
570
+ // Show the step we returned to
571
+ await showNextStep();
572
+ return;
573
+ } else {
574
+ console.log(` ⚠️ Invalid return step ${targetStep}, continuing normally`);
575
+ }
576
+ }
577
+
578
+ // Normal flow: Move to next step
504
579
  chatState.step++;
580
+
581
+ // Update edit icons for all previous steps
582
+ updateEditIcons();
505
583
 
506
584
  // Check if finished
507
585
  if (chatState.step >= flowData.flow.length) {
@@ -575,6 +653,10 @@ async function showNextStep() {
575
653
  // Auto-advance for steps without input
576
654
  setTimeout(() => {
577
655
  chatState.step++;
656
+
657
+ // Update edit icons after auto-advance
658
+ updateEditIcons();
659
+
578
660
  if (chatState.step < flowData.flow.length) {
579
661
  showNextStep();
580
662
  } else {
@@ -582,6 +664,12 @@ async function showNextStep() {
582
664
  }
583
665
  }, config.autoAdvanceDelay);
584
666
  }
667
+
668
+ // Always update edit icons at the end to ensure correct state
669
+ // Use setTimeout to ensure it runs after any DOM updates
670
+ setTimeout(() => {
671
+ updateEditIcons();
672
+ }, 10);
585
673
  }
586
674
 
587
675
  function handleCompletion() {
@@ -714,39 +802,56 @@ function reset() {
714
802
  }
715
803
 
716
804
  function goToStep(stepNumber) {
805
+ console.log(`\n🎯 goToStep() called with stepNumber: ${stepNumber}`);
806
+ console.log(` Current step before: ${chatState.step}`);
807
+
717
808
  if (stepNumber < 0 || stepNumber >= flowData.flow.length) {
718
809
  console.error('Invalid step number:', stepNumber);
719
810
  return;
720
811
  }
721
812
 
813
+ console.log(` Setting chatState.step to ${stepNumber}`);
722
814
  chatState.step = stepNumber;
723
815
  chatState.currentSelection = null;
724
816
 
817
+ console.log(` Clearing messages container...`);
725
818
  if (elements.messages) {
726
819
  elements.messages.innerHTML = '';
727
820
  }
728
821
 
822
+ console.log(` Disabling next button and calling showNextStep()...`);
729
823
  disableNextButton();
730
824
  showNextStep();
731
825
  }
732
826
 
733
827
  function editStep(stepNumber) {
828
+ console.log(`\n📝 editStep() called with stepNumber: ${stepNumber}`);
829
+ console.log(` Current step before edit: ${chatState.step}`);
830
+
734
831
  if (stepNumber < 0 || stepNumber >= flowData.flow.length) {
735
832
  console.error('Invalid step number:', stepNumber);
736
833
  return;
737
834
  }
738
835
 
836
+ // Save where we are now so we can return after editing
837
+ chatState.returnToStep = chatState.step;
838
+ console.log(` 💾 Saved return step: ${chatState.returnToStep}`);
839
+
840
+ console.log(` Clearing history from step ${stepNumber} forward...`);
739
841
  // Clear history from this step forward
740
842
  chatState.history = chatState.history.filter(h => h.step < stepNumber);
741
843
 
844
+ console.log(` Resetting data for step ${stepNumber} and beyond...`);
742
845
  // Reset data for this field and beyond
743
846
  const stepsToReset = flowData.flow.slice(stepNumber);
744
847
  stepsToReset.forEach(step => {
745
848
  if (step.input && step.input.field) {
849
+ console.log(` Deleting field: ${step.input.field}`);
746
850
  delete chatState.data[step.input.field];
747
851
  }
748
852
  });
749
853
 
854
+ console.log(` Calling goToStep(${stepNumber})...`);
750
855
  // Go to that step
751
856
  goToStep(stepNumber);
752
857
  }