Spaces:
Sleeping
Sleeping
Commit ·
d4b9975
1
Parent(s): dcc8bc7
Resolved blocks not sorting correctly after trash event by always reordering by block-id which is static
Browse files108 unmodified lines1091101111121131141157 unmodified lines12312412512612712826 unmodified lines1551561571581591601618 unmodified lines170171172173174175176177178179180181182143 unmodified lines32632732832933033133233333415 unmodified lines350351352353354355356357358359360361362247 unmodified lines61061161261361461561623 unmodified lines6406416426436446456466476486496506516526536546553 unmodified lines65966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370453 unmodified lines758759760761762763764765766767768769770771772773774775776777778779780108 unmodified lines// Store initial positions of the blocksfunction storeInitialPositions() {initialPositions = []; // Clear initialPositions before updatingconst blocks = blockContainer.querySelectorAll('.block-item');blocks.forEach((block, index) => {const blockId = block.getAttribute('data-block-id');7 unmodified lines});console.log('Initial positions:', initialPositions);}function insertHtmlBlocks(blocks) {console.log('blockContainerPage = ', blockContainerPage)26 unmodified linesel.style.height = 'auto';el.style.height = (el.scrollHeight) + 'px';}console.log('Original height:', el.style.height);}function initializeTextareaResizing() {8 unmodified linesclasses.forEach(className => {const textareas = document.querySelectorAll(`.${className}`);console.log(`Textareas found for ${className}:`, textareas.length); // Debugging linetextareas.forEach(textarea => {console.log('scrollHeight:', textarea.scrollHeight);console.log('clientHeight:', textarea.clientHeight);console.log('offsetHeight:', textarea.offsetHeight);console.log('Computed line-height:', window.getComputedStyle(textarea).lineHeight);// Adjust height on page loadadjustTextareaHeight(textarea);// Adjust height on input143 unmodified linesfunction handleDragOver(e) {e.preventDefault();e.dataTransfer.dropEffect = 'move';console.log('Drag over event');const targetPage = e.target.closest('.page');if (targetPage) {targetPage.classList.add('highlight-page'); // Add highlight class for pages15 unmodified linesfunction handleDrop(e) {e.preventDefault();const blockId = e.dataTransfer.getData('block-id');const originalPageId = e.dataTransfer.getData('data-page-id');const innerHTML = e.dataTransfer.getData('text/plain');console.log(`Drop event for block ID: ${blockId} from page ID: ${originalPageId}`);// Ensure we are not dropping into a textarea or another blockif (event.target.classList.contains('block-item', 'block-content') || event.target.tagName === 'TEXTAREA') {console.log('Cannot drop block inside another block or textarea');return;}247 unmodified linesconst newPage = document.querySelector(`[data-page-id="${newPageId}"] .block-container`);newPage.appendChild(block);}// Handle the drop event on the trash areafunction handleTrashDrop(e) {e.preventDefault();23 unmodified linesconsole.log(`Removed duplicate block with ID: ${blockId} from block-container`);}// Create a new block-item to be placed back in the block-containerconst newBlock = document.createElement('div');newBlock.classList.add('block-item');newBlock.setAttribute('data-block-id', blockId);newBlock.setAttribute('data-page-id', 'block-container');newBlock.innerHTML = innerHTML;newBlock.setAttribute('draggable', true);newBlock.addEventListener('dragstart', handleDragStart);newBlock.addEventListener('dragend', handleDragEnd);// Ensure the block is appended to the page wrapper inside blockContainerlet pageWrapper = blockContainer.querySelector('.page');if (!pageWrapper) {3 unmodified linesblockContainer.appendChild(pageWrapper);}// Debugging outputconsole.log('Page wrapper:', pageWrapper);console.log('New block:', newBlock);// Find the original position to insert the new blockconst originalPosition = initialPositions.find(pos => pos.id === blockId);console.log('Original position:', originalPosition);if (originalPosition) {const blocks = pageWrapper.querySelectorAll('.block-item');console.log('Blocks in pageWrapper:', blocks);console.log('Inserting at position:', originalPosition.index);if (originalPosition.index < blocks.length) {const referenceNode = blocks[originalPosition.index];if (referenceNode && referenceNode.parentNode === pageWrapper) {console.log('Inserting before block at index:', originalPosition.index);pageWrapper.insertBefore(newBlock, referenceNode);console.log(`Moved block back to original position ${originalPosition.index} in block-container`);} else {console.warn('Reference node does not belong to pageWrapper, appending to the end');pageWrapper.appendChild(newBlock);console.log('Appended block to the end of block-container');}} else {console.log('Appending block to the end of pageWrapper');pageWrapper.appendChild(newBlock);console.log('Appended block to the end of block-container');}} else {console.log('Original position not found, appending block to the end of pageWrapper');pageWrapper.appendChild(newBlock);console.log('Appended block to the end of block-container');}console.log(`Restored block with ID: ${blockId}`);} else {console.log('No data transferred');}// Remove the "over" class and reset the background imagetrashArea.classList.remove('over');trashArea.style.backgroundImage = "url('./closed-mimic-trashcan.png')";53 unmodified linesinitialPositions.forEach(pos => {const blockData = allBlocks.find(block => block.id === pos.id);if (blockData) {const newBlock = document.createElement('div');newBlock.classList.add('block-item');newBlock.setAttribute('data-block-id', blockData.id);newBlock.setAttribute('data-page-id', 'block-container');newBlock.innerHTML = blockData.innerHTML;newBlock.setAttribute('draggable', true);newBlock.addEventListener('dragstart', handleDragStart);newBlock.addEventListener('dragend', handleDragEnd);const blocks = pageWrapper.querySelectorAll('.block-item');if (pos.index < blocks.length) {pageWrapper.insertBefore(newBlock, blocks[pos.index]);console.log(`Moved block back to original position ${pos.index} in block-container`);} else {pageWrapper.appendChild(newBlock);console.log('Appended block to the end of block-container');}}});createNewPage();108 unmodified lines1091101111121131147 unmodified lines12212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020126 unmodified lines2282292302312322338 unmodified lines242243244245246247248249143 unmodified lines39339439539639739839940040140240340440540615 unmodified lines422423424425426427428429430431432433434435436437438439440247 unmodified lines68868969069169269369423 unmodified lines7187197207217227233 unmodified lines72772872973073173273373473573673773853 unmodified lines792793794795796797798799108 unmodified lines// Store initial positions of the blocksfunction storeInitialPositions() {const blocks = blockContainer.querySelectorAll('.block-item');blocks.forEach((block, index) => {const blockId = block.getAttribute('data-block-id');7 unmodified lines});console.log('Initial positions:', initialPositions);}function sortBlocksById() {// Select all blocks inside the block-containerconst blocks = Array.from(pageWrapper.querySelectorAll('.block-item'));// Sort the blocks based on their block-id attributeblocks.sort((a, b) => {const idA = parseInt(a.getAttribute('data-block-id'), 10);const idB = parseInt(b.getAttribute('data-block-id'), 10);return idA - idB; // Ascending order});// Clear the block-container before re-appending the sorted blockspageWrapper.innerHTML = '';// Re-append the blocks in the sorted orderblocks.forEach(block => pageWrapper.appendChild(block));console.log('Blocks have been sorted and re-appended based on block-id');}function reinsertBlock(pageWrapper, blockId, innerHTML) {const originalPosition = initialPositions.find(pos => pos.id === blockId);console.log('Original position:', originalPosition);if (originalPosition) {const blocks = pageWrapper.querySelectorAll('.block-item');console.log('Blocks in pageWrapper:', blocks);// Adding debugging output for index detailsconsole.log(`Attempting to insert block with ID: ${blockId} at original index: ${originalPosition.index}`);const newBlock = document.createElement('div');newBlock.classList.add('block-item');newBlock.setAttribute('data-block-id', blockId);newBlock.setAttribute('data-page-id', 'block-container');newBlock.innerHTML = innerHTML;newBlock.setAttribute('draggable', true);newBlock.addEventListener('dragstart', handleDragStart);newBlock.addEventListener('dragend', handleDragEnd);if (originalPosition.index < blocks.length) {const referenceNode = blocks[originalPosition.index];// Debugging output to ensure the correct reference node is identifiedconsole.log(`Reference node index: ${originalPosition.index}, Node:`, referenceNode);if (referenceNode && referenceNode.parentNode === pageWrapper) {console.log(`Inserting before block at index: ${originalPosition.index}`);pageWrapper.insertBefore(newBlock, referenceNode);} else {console.warn('Reference node does not belong to pageWrapper, appending to the end');pageWrapper.appendChild(newBlock);}} else {console.log('Original index exceeds current blocks, appending block to the end');pageWrapper.appendChild(newBlock);}} else {console.warn('Original position not found, appending block to the end of pageWrapper');const newBlock = document.createElement('div');newBlock.classList.add('block-item');newBlock.setAttribute('data-block-id', blockId);newBlock.setAttribute('data-page-id', 'block-container');newBlock.innerHTML = innerHTML;newBlock.setAttribute('draggable', true);newBlock.addEventListener('dragstart', handleDragStart);newBlock.addEventListener('dragend', handleDragEnd);pageWrapper.appendChild(newBlock);}console.log(`Restored block with ID: ${blockId}`);}function insertHtmlBlocks(blocks) {console.log('blockContainerPage = ', blockContainerPage)26 unmodified linesel.style.height = 'auto';el.style.height = (el.scrollHeight) + 'px';}}function initializeTextareaResizing() {8 unmodified linesclasses.forEach(className => {const textareas = document.querySelectorAll(`.${className}`);textareas.forEach(textarea => {// Adjust height on page loadadjustTextareaHeight(textarea);// Adjust height on input143 unmodified linesfunction handleDragOver(e) {e.preventDefault();console.log('Drag over event');// Check if the drop target is a TEXTAREA or any other non-droppable areaif (e.target.tagName === 'TEXTAREA' || e.target.closest('.block-item')) {e.dataTransfer.dropEffect = 'none'; // Indicate that drop is not allowedreturn;}e.dataTransfer.dropEffect = 'move'; // Indicate that drop is allowedconst targetPage = e.target.closest('.page');if (targetPage) {targetPage.classList.add('highlight-page'); // Add highlight class for pages15 unmodified linesfunction handleDrop(e) {e.preventDefault();// Ensure we are not dropping into a textarea or another blockif (e.target.classList.contains('block-item', 'block-content') || e.target.tagName === 'TEXTAREA') {console.log('Cannot drop block inside another block or textarea');return;}const blockId = e.dataTransfer.getData('block-id');const originalPageId = e.dataTransfer.getData('data-page-id');const innerHTML = e.dataTransfer.getData('text/plain');console.log(`Drop event for block ID: ${blockId} from page ID: ${originalPageId}`);// Ensure we are not dropping into a textarea or another blockif (r.target.classList.contains('block-item', 'block-content') || event.target.tagName === 'TEXTAREA') {console.log('Cannot drop block inside another block or textarea');return;}247 unmodified linesconst newPage = document.querySelector(`[data-page-id="${newPageId}"] .block-container`);newPage.appendChild(block);}// Handle the drop event on the trash areafunction handleTrashDrop(e) {e.preventDefault();23 unmodified linesconsole.log(`Removed duplicate block with ID: ${blockId} from block-container`);}// Ensure the block is appended to the page wrapper inside blockContainerlet pageWrapper = blockContainer.querySelector('.page');if (!pageWrapper) {3 unmodified linesblockContainer.appendChild(pageWrapper);}// Reinsert the block using the refactored functionreinsertBlock(pageWrapper, blockId, innerHTML);sortBlocksById();} else {console.log('No data transferred');}// Remove the "over" class and reset the background imagetrashArea.classList.remove('over');trashArea.style.backgroundImage = "url('./closed-mimic-trashcan.png')";53 unmodified linesinitialPositions.forEach(pos => {const blockData = allBlocks.find(block => block.id === pos.id);if (blockData) {reinsertBlock(pageWrapper, blockData.id, blockData.innerHTML);sortBlocksById();}});createNewPage();
scripts.js+96-77