Skip to content

Commit

Permalink
Add dmtemplateembed block
Browse files Browse the repository at this point in the history
  • Loading branch information
Ritwik Srivastava authored and Ritwik Srivastava committed Mar 7, 2025
1 parent 591e367 commit 5d8732a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 67 deletions.
23 changes: 0 additions & 23 deletions blocks/dmtemplateembed/dmtemplateembed.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,3 @@
max-width: 1000px; /* matches your image width */
margin: 0 auto;
}

/* Hide the raw HTML/text that might be showing */
.dmtemplateembed > div > div:first-child {
display: none;
}

/* Ensure map and areas work properly */
.dmtemplate-container map {
position: relative;
display: inline-block;
}

.dmtemplate-container area {
cursor: pointer;
}

/* Responsive adjustments */
@media (max-width: 1000px) {
.dmtemplate-container img {
width: 100%;
height: auto;
}
}
46 changes: 2 additions & 44 deletions blocks/dmtemplateembed/dmtemplateembed.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,8 @@ export default function decorate(block) {
if (!embedContent) return;

try {
// Get the raw HTML string
const htmlString = embedContent.textContent.trim();

// Create a temporary div to parse the HTML
const tempDiv = document.createElement('div');
tempDiv.innerHTML = htmlString;

// Get the image and map elements from the parsed HTML
const img = tempDiv.querySelector('img');
const map = tempDiv.querySelector('map');

if (img) {
// Create and append the image
const newImg = document.createElement('img');
newImg.src = img.src;
newImg.alt = img.alt || '';
newImg.width = img.width || '';
newImg.height = img.height || '';
newImg.id = img.id || '';
newImg.setAttribute('usemap', img.getAttribute('usemap') || '');
block.appendChild(newImg);
}

if (map) {
// Create and append the map
const newMap = document.createElement('map');
newMap.name = map.name;

// Copy all area elements
map.querySelectorAll('area').forEach(area => {
const newArea = document.createElement('area');
Array.from(area.attributes).forEach(attr => {
newArea.setAttribute(attr.name, attr.value);
});
newMap.appendChild(newArea);
});

block.appendChild(newMap);
}

// Remove the original content
while (block.firstChild !== newImg) {
block.firstChild.remove();
}
// Just set the innerHTML directly from the content
block.innerHTML = embedContent.textContent;

Check failure on line 11 in blocks/dmtemplateembed/dmtemplateembed.js

View workflow job for this annotation

GitHub Actions / build

Block must not be padded by blank lines

Check failure on line 12 in blocks/dmtemplateembed/dmtemplateembed.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
} catch (e) {
console.error('Error creating DM template embed:', e);

Check warning on line 14 in blocks/dmtemplateembed/dmtemplateembed.js

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
Expand Down

0 comments on commit 5d8732a

Please sign in to comment.