Skip to content

Commit

Permalink
feat: add editor-support.js
Browse files Browse the repository at this point in the history
  • Loading branch information
buuhuu committed Feb 7, 2024
1 parent fb21bd9 commit 34ad59c
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions scripts/editor-support.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
decorateBlock,
decorateButtons,
decorateIcons,
loadBlock,
} from './aem.js';

const connectionPrefix = 'urn:aemconnection:';

async function handleEditorUpdate(event) {
const { detail } = event;

const resource = detail?.requestData?.target?.resource;
if (!resource) return;

const element = document.querySelector(`[data-aue-resource="${resource}"]`);
const block = element?.parentElement?.closest('.block') || element?.closest('.block');
const blockResource = block?.getAttribute('data-aue-resource');
if (!block || !blockResource?.startsWith(connectionPrefix)) return;

const updates = detail?.responseData?.updates;
if (updates.length > 0) {
const { content } = updates[0];
const newBlockDocument = new DOMParser().parseFromString(content, 'text/html');
const newBlock = newBlockDocument?.querySelector(`[data-aue-resource="${blockResource}"]`);
if (newBlock) {
newBlock.style.display = 'none';
block.insertAdjacentElement('afterend', newBlock);
// decorate buttons and icons
decorateButtons(newBlock);
decorateIcons(newBlock);
// decorate and load the block
decorateBlock(newBlock);
await loadBlock(newBlock);
// remove the old block and show the new one
block.remove();
newBlock.style.display = null;
}
}
}

document.querySelector('main')?.addEventListener('aue:content-patch', handleEditorUpdate);

0 comments on commit 34ad59c

Please sign in to comment.