drakosfire commited on
Commit
d4b9975
1 Parent(s): dcc8bc7

Resolved blocks not sorting correctly after trash event by always reordering by block-id which is static

Browse files
Files changed (1) hide show
  1. scripts.js +96 -77
scripts.js CHANGED
@@ -109,7 +109,6 @@ document.addEventListener("DOMContentLoaded", function() {
109
 
110
  // Store initial positions of the blocks
111
  function storeInitialPositions() {
112
- initialPositions = []; // Clear initialPositions before updating
113
  const blocks = blockContainer.querySelectorAll('.block-item');
114
  blocks.forEach((block, index) => {
115
  const blockId = block.getAttribute('data-block-id');
@@ -123,6 +122,80 @@ document.addEventListener("DOMContentLoaded", function() {
123
  });
124
  console.log('Initial positions:', initialPositions);
125
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  function insertHtmlBlocks(blocks) {
128
  console.log('blockContainerPage = ', blockContainerPage)
@@ -155,7 +228,6 @@ document.addEventListener("DOMContentLoaded", function() {
155
  el.style.height = 'auto';
156
  el.style.height = (el.scrollHeight) + 'px';
157
  }
158
- console.log('Original height:', el.style.height);
159
  }
160
 
161
  function initializeTextareaResizing() {
@@ -170,13 +242,8 @@ document.addEventListener("DOMContentLoaded", function() {
170
 
171
  classes.forEach(className => {
172
  const textareas = document.querySelectorAll(`.${className}`);
173
- console.log(`Textareas found for ${className}:`, textareas.length); // Debugging line
174
  textareas.forEach(textarea => {
175
- console.log('scrollHeight:', textarea.scrollHeight);
176
- console.log('clientHeight:', textarea.clientHeight);
177
- console.log('offsetHeight:', textarea.offsetHeight);
178
- console.log('Computed line-height:', window.getComputedStyle(textarea).lineHeight);
179
-
180
  // Adjust height on page load
181
  adjustTextareaHeight(textarea);
182
  // Adjust height on input
@@ -326,9 +393,14 @@ document.addEventListener("DOMContentLoaded", function() {
326
 
327
  function handleDragOver(e) {
328
  e.preventDefault();
329
- e.dataTransfer.dropEffect = 'move';
330
  console.log('Drag over event');
331
-
 
 
 
 
 
 
332
  const targetPage = e.target.closest('.page');
333
  if (targetPage) {
334
  targetPage.classList.add('highlight-page'); // Add highlight class for pages
@@ -350,13 +422,19 @@ document.addEventListener("DOMContentLoaded", function() {
350
 
351
  function handleDrop(e) {
352
  e.preventDefault();
 
 
 
 
 
 
353
  const blockId = e.dataTransfer.getData('block-id');
354
  const originalPageId = e.dataTransfer.getData('data-page-id');
355
  const innerHTML = e.dataTransfer.getData('text/plain');
356
  console.log(`Drop event for block ID: ${blockId} from page ID: ${originalPageId}`);
357
 
358
  // Ensure we are not dropping into a textarea or another block
359
- if (event.target.classList.contains('block-item', 'block-content') || event.target.tagName === 'TEXTAREA') {
360
  console.log('Cannot drop block inside another block or textarea');
361
  return;
362
  }
@@ -610,7 +688,7 @@ document.addEventListener("DOMContentLoaded", function() {
610
  const newPage = document.querySelector(`[data-page-id="${newPageId}"] .block-container`);
611
  newPage.appendChild(block);
612
  }
613
-
614
  // Handle the drop event on the trash area
615
  function handleTrashDrop(e) {
616
  e.preventDefault();
@@ -640,16 +718,6 @@ document.addEventListener("DOMContentLoaded", function() {
640
  console.log(`Removed duplicate block with ID: ${blockId} from block-container`);
641
  }
642
 
643
- // Create a new block-item to be placed back in the block-container
644
- const newBlock = document.createElement('div');
645
- newBlock.classList.add('block-item');
646
- newBlock.setAttribute('data-block-id', blockId);
647
- newBlock.setAttribute('data-page-id', 'block-container');
648
- newBlock.innerHTML = innerHTML;
649
- newBlock.setAttribute('draggable', true);
650
- newBlock.addEventListener('dragstart', handleDragStart);
651
- newBlock.addEventListener('dragend', handleDragEnd);
652
-
653
  // Ensure the block is appended to the page wrapper inside blockContainer
654
  let pageWrapper = blockContainer.querySelector('.page');
655
  if (!pageWrapper) {
@@ -659,46 +727,12 @@ document.addEventListener("DOMContentLoaded", function() {
659
  blockContainer.appendChild(pageWrapper);
660
  }
661
 
662
- // Debugging output
663
- console.log('Page wrapper:', pageWrapper);
664
- console.log('New block:', newBlock);
665
-
666
- // Find the original position to insert the new block
667
- const originalPosition = initialPositions.find(pos => pos.id === blockId);
668
- console.log('Original position:', originalPosition);
669
-
670
- if (originalPosition) {
671
- const blocks = pageWrapper.querySelectorAll('.block-item');
672
- console.log('Blocks in pageWrapper:', blocks);
673
- console.log('Inserting at position:', originalPosition.index);
674
-
675
- if (originalPosition.index < blocks.length) {
676
- const referenceNode = blocks[originalPosition.index];
677
- if (referenceNode && referenceNode.parentNode === pageWrapper) {
678
- console.log('Inserting before block at index:', originalPosition.index);
679
- pageWrapper.insertBefore(newBlock, referenceNode);
680
- console.log(`Moved block back to original position ${originalPosition.index} in block-container`);
681
- } else {
682
- console.warn('Reference node does not belong to pageWrapper, appending to the end');
683
- pageWrapper.appendChild(newBlock);
684
- console.log('Appended block to the end of block-container');
685
- }
686
- } else {
687
- console.log('Appending block to the end of pageWrapper');
688
- pageWrapper.appendChild(newBlock);
689
- console.log('Appended block to the end of block-container');
690
- }
691
  } else {
692
- console.log('Original position not found, appending block to the end of pageWrapper');
693
- pageWrapper.appendChild(newBlock);
694
- console.log('Appended block to the end of block-container');
695
  }
696
-
697
- console.log(`Restored block with ID: ${blockId}`);
698
- } else {
699
- console.log('No data transferred');
700
- }
701
-
702
  // Remove the "over" class and reset the background image
703
  trashArea.classList.remove('over');
704
  trashArea.style.backgroundImage = "url('./closed-mimic-trashcan.png')";
@@ -758,23 +792,8 @@ document.addEventListener("DOMContentLoaded", function() {
758
  initialPositions.forEach(pos => {
759
  const blockData = allBlocks.find(block => block.id === pos.id);
760
  if (blockData) {
761
- const newBlock = document.createElement('div');
762
- newBlock.classList.add('block-item');
763
- newBlock.setAttribute('data-block-id', blockData.id);
764
- newBlock.setAttribute('data-page-id', 'block-container');
765
- newBlock.innerHTML = blockData.innerHTML;
766
- newBlock.setAttribute('draggable', true);
767
- newBlock.addEventListener('dragstart', handleDragStart);
768
- newBlock.addEventListener('dragend', handleDragEnd);
769
-
770
- const blocks = pageWrapper.querySelectorAll('.block-item');
771
- if (pos.index < blocks.length) {
772
- pageWrapper.insertBefore(newBlock, blocks[pos.index]);
773
- console.log(`Moved block back to original position ${pos.index} in block-container`);
774
- } else {
775
- pageWrapper.appendChild(newBlock);
776
- console.log('Appended block to the end of block-container');
777
- }
778
  }
779
  });
780
  createNewPage();
 
109
 
110
  // Store initial positions of the blocks
111
  function storeInitialPositions() {
 
112
  const blocks = blockContainer.querySelectorAll('.block-item');
113
  blocks.forEach((block, index) => {
114
  const blockId = block.getAttribute('data-block-id');
 
122
  });
123
  console.log('Initial positions:', initialPositions);
124
  }
125
+
126
+ function sortBlocksById() {
127
+ // Select all blocks inside the block-container
128
+ const blocks = Array.from(pageWrapper.querySelectorAll('.block-item'));
129
+
130
+ // Sort the blocks based on their block-id attribute
131
+ blocks.sort((a, b) => {
132
+ const idA = parseInt(a.getAttribute('data-block-id'), 10);
133
+ const idB = parseInt(b.getAttribute('data-block-id'), 10);
134
+ return idA - idB; // Ascending order
135
+ });
136
+
137
+ // Clear the block-container before re-appending the sorted blocks
138
+ pageWrapper.innerHTML = '';
139
+
140
+ // Re-append the blocks in the sorted order
141
+ blocks.forEach(block => pageWrapper.appendChild(block));
142
+
143
+ console.log('Blocks have been sorted and re-appended based on block-id');
144
+ }
145
+
146
+ function reinsertBlock(pageWrapper, blockId, innerHTML) {
147
+ const originalPosition = initialPositions.find(pos => pos.id === blockId);
148
+ console.log('Original position:', originalPosition);
149
+
150
+ if (originalPosition) {
151
+ const blocks = pageWrapper.querySelectorAll('.block-item');
152
+ console.log('Blocks in pageWrapper:', blocks);
153
+
154
+ // Adding debugging output for index details
155
+ console.log(`Attempting to insert block with ID: ${blockId} at original index: ${originalPosition.index}`);
156
+
157
+ const newBlock = document.createElement('div');
158
+ newBlock.classList.add('block-item');
159
+ newBlock.setAttribute('data-block-id', blockId);
160
+ newBlock.setAttribute('data-page-id', 'block-container');
161
+ newBlock.innerHTML = innerHTML;
162
+ newBlock.setAttribute('draggable', true);
163
+ newBlock.addEventListener('dragstart', handleDragStart);
164
+ newBlock.addEventListener('dragend', handleDragEnd);
165
+
166
+ if (originalPosition.index < blocks.length) {
167
+ const referenceNode = blocks[originalPosition.index];
168
+
169
+ // Debugging output to ensure the correct reference node is identified
170
+ console.log(`Reference node index: ${originalPosition.index}, Node:`, referenceNode);
171
+
172
+ if (referenceNode && referenceNode.parentNode === pageWrapper) {
173
+ console.log(`Inserting before block at index: ${originalPosition.index}`);
174
+ pageWrapper.insertBefore(newBlock, referenceNode);
175
+ } else {
176
+ console.warn('Reference node does not belong to pageWrapper, appending to the end');
177
+ pageWrapper.appendChild(newBlock);
178
+ }
179
+ } else {
180
+ console.log('Original index exceeds current blocks, appending block to the end');
181
+ pageWrapper.appendChild(newBlock);
182
+ }
183
+ } else {
184
+ console.warn('Original position not found, appending block to the end of pageWrapper');
185
+ const newBlock = document.createElement('div');
186
+ newBlock.classList.add('block-item');
187
+ newBlock.setAttribute('data-block-id', blockId);
188
+ newBlock.setAttribute('data-page-id', 'block-container');
189
+ newBlock.innerHTML = innerHTML;
190
+ newBlock.setAttribute('draggable', true);
191
+ newBlock.addEventListener('dragstart', handleDragStart);
192
+ newBlock.addEventListener('dragend', handleDragEnd);
193
+
194
+ pageWrapper.appendChild(newBlock);
195
+ }
196
+
197
+ console.log(`Restored block with ID: ${blockId}`);
198
+ }
199
 
200
  function insertHtmlBlocks(blocks) {
201
  console.log('blockContainerPage = ', blockContainerPage)
 
228
  el.style.height = 'auto';
229
  el.style.height = (el.scrollHeight) + 'px';
230
  }
 
231
  }
232
 
233
  function initializeTextareaResizing() {
 
242
 
243
  classes.forEach(className => {
244
  const textareas = document.querySelectorAll(`.${className}`);
 
245
  textareas.forEach(textarea => {
246
+
 
 
 
 
247
  // Adjust height on page load
248
  adjustTextareaHeight(textarea);
249
  // Adjust height on input
 
393
 
394
  function handleDragOver(e) {
395
  e.preventDefault();
 
396
  console.log('Drag over event');
397
+ // Check if the drop target is a TEXTAREA or any other non-droppable area
398
+ if (e.target.tagName === 'TEXTAREA' || e.target.closest('.block-item')) {
399
+ e.dataTransfer.dropEffect = 'none'; // Indicate that drop is not allowed
400
+ return;
401
+ }
402
+ e.dataTransfer.dropEffect = 'move'; // Indicate that drop is allowed
403
+
404
  const targetPage = e.target.closest('.page');
405
  if (targetPage) {
406
  targetPage.classList.add('highlight-page'); // Add highlight class for pages
 
422
 
423
  function handleDrop(e) {
424
  e.preventDefault();
425
+
426
+ // Ensure we are not dropping into a textarea or another block
427
+ if (e.target.classList.contains('block-item', 'block-content') || e.target.tagName === 'TEXTAREA') {
428
+ console.log('Cannot drop block inside another block or textarea');
429
+ return;
430
+ }
431
  const blockId = e.dataTransfer.getData('block-id');
432
  const originalPageId = e.dataTransfer.getData('data-page-id');
433
  const innerHTML = e.dataTransfer.getData('text/plain');
434
  console.log(`Drop event for block ID: ${blockId} from page ID: ${originalPageId}`);
435
 
436
  // Ensure we are not dropping into a textarea or another block
437
+ if (r.target.classList.contains('block-item', 'block-content') || event.target.tagName === 'TEXTAREA') {
438
  console.log('Cannot drop block inside another block or textarea');
439
  return;
440
  }
 
688
  const newPage = document.querySelector(`[data-page-id="${newPageId}"] .block-container`);
689
  newPage.appendChild(block);
690
  }
691
+
692
  // Handle the drop event on the trash area
693
  function handleTrashDrop(e) {
694
  e.preventDefault();
 
718
  console.log(`Removed duplicate block with ID: ${blockId} from block-container`);
719
  }
720
 
 
 
 
 
 
 
 
 
 
 
721
  // Ensure the block is appended to the page wrapper inside blockContainer
722
  let pageWrapper = blockContainer.querySelector('.page');
723
  if (!pageWrapper) {
 
727
  blockContainer.appendChild(pageWrapper);
728
  }
729
 
730
+ // Reinsert the block using the refactored function
731
+ reinsertBlock(pageWrapper, blockId, innerHTML);
732
+ sortBlocksById();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
733
  } else {
734
+ console.log('No data transferred');
 
 
735
  }
 
 
 
 
 
 
736
  // Remove the "over" class and reset the background image
737
  trashArea.classList.remove('over');
738
  trashArea.style.backgroundImage = "url('./closed-mimic-trashcan.png')";
 
792
  initialPositions.forEach(pos => {
793
  const blockData = allBlocks.find(block => block.id === pos.id);
794
  if (blockData) {
795
+ reinsertBlock(pageWrapper, blockData.id, blockData.innerHTML);
796
+ sortBlocksById();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
797
  }
798
  });
799
  createNewPage();