-
Notifications
You must be signed in to change notification settings - Fork 1
QA to Main #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
QA to Main #7
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* eslint-disable no-console */ | ||
| /* eslint-disable no-cond-assign */ | ||
| /* eslint-disable import/prefer-default-export */ | ||
|
|
||
| // group editable texts in single wrappers if applicable. | ||
| // this script should execute after script.js but before the the universal editor cors script | ||
| // and any block being loaded | ||
|
|
||
| export function decorateRichtext(container = document) { | ||
| function deleteInstrumentation(element) { | ||
| delete element.dataset.richtextResource; | ||
| delete element.dataset.richtextProp; | ||
| delete element.dataset.richtextFilter; | ||
| delete element.dataset.richtextLabel; | ||
| } | ||
|
|
||
| let element; | ||
| while (element = container.querySelector('[data-richtext-prop]:not(div)')) { | ||
| const { | ||
| richtextResource, | ||
| richtextProp, | ||
| richtextFilter, | ||
| richtextLabel, | ||
| } = element.dataset; | ||
| deleteInstrumentation(element); | ||
| const siblings = []; | ||
| let sibling = element; | ||
| while (sibling = sibling.nextElementSibling) { | ||
| if (sibling.dataset.richtextResource === richtextResource | ||
| && sibling.dataset.richtextProp === richtextProp) { | ||
| deleteInstrumentation(sibling); | ||
| siblings.push(sibling); | ||
| } else break; | ||
| } | ||
|
|
||
| let orphanElements; | ||
| if (richtextResource && richtextProp) { | ||
| orphanElements = document.querySelectorAll(`[data-richtext-id="${richtextResource}"][data-richtext-prop="${richtextProp}"]`); | ||
| } else { | ||
| const editable = element.closest('[data-aue-resource]'); | ||
| if (editable) { | ||
| orphanElements = editable.querySelectorAll(`:scope > :not([data-aue-resource]) [data-richtext-prop="${richtextProp}"]`); | ||
| } else { | ||
| console.warn(`Editable parent not found or richtext property ${richtextProp}`); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (orphanElements.length) { | ||
| console.warn('Found orphan elements of a richtext, that were not consecutive siblings of ' | ||
| + 'the first paragraph', orphanElements); | ||
| orphanElements.forEach((orphanElement) => deleteInstrumentation(orphanElement)); | ||
| } else { | ||
| const group = document.createElement('div'); | ||
| if (richtextResource) { | ||
| group.dataset.aueResource = richtextResource; | ||
| group.dataset.aueBehavior = 'component'; | ||
| } | ||
| if (richtextProp) group.dataset.aueProp = richtextProp; | ||
| if (richtextLabel) group.dataset.aueLabel = richtextLabel; | ||
| if (richtextFilter) group.dataset.aueFilter = richtextFilter; | ||
| group.dataset.aueType = 'richtext'; | ||
| element.replaceWith(group); | ||
| group.append(element, ...siblings); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { | ||
| decorateBlock, | ||
| decorateBlocks, | ||
| decorateButtons, | ||
| decorateIcons, | ||
| decorateSections, | ||
| loadBlock, | ||
| loadScript, | ||
| loadSections, | ||
| } from './aem.js'; | ||
| import { decorateRichtext } from './editor-support-rte.js'; | ||
| import { decorateMain } from './scripts.js'; | ||
|
|
||
| async function applyChanges(event) { | ||
| // redecorate default content and blocks on patches (in the properties rail) | ||
| const { detail } = event; | ||
|
|
||
| const resource = detail?.request?.target?.resource // update, patch components | ||
| || detail?.request?.target?.container?.resource // update, patch, add to sections | ||
| || detail?.request?.to?.container?.resource; // move in sections | ||
| if (!resource) return false; | ||
| const updates = detail?.response?.updates; | ||
| if (!updates.length) return false; | ||
tusharmane-tm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const { content } = updates[0]; | ||
| if (!content) return false; | ||
|
|
||
| // load dompurify | ||
| await loadScript(`${window.hlx.codeBasePath}/scripts/dompurify.min.js`); | ||
|
|
||
| const sanitizedContent = window.DOMPurify.sanitize(content, { USE_PROFILES: { html: true } }); | ||
| const parsedUpdate = new DOMParser().parseFromString(sanitizedContent, 'text/html'); | ||
| const element = document.querySelector(`[data-aue-resource="${resource}"]`); | ||
|
|
||
| if (element) { | ||
| if (element.matches('main')) { | ||
| const newMain = parsedUpdate.querySelector(`[data-aue-resource="${resource}"]`); | ||
| newMain.style.display = 'none'; | ||
| element.insertAdjacentElement('afterend', newMain); | ||
| decorateMain(newMain); | ||
| decorateRichtext(newMain); | ||
| await loadSections(newMain); | ||
| element.remove(); | ||
| newMain.style.display = null; | ||
| // eslint-disable-next-line no-use-before-define | ||
| attachEventListners(newMain); | ||
| return true; | ||
| } | ||
|
|
||
| const block = element.parentElement?.closest('.block[data-aue-resource]') || element?.closest('.block[data-aue-resource]'); | ||
| if (block) { | ||
| const blockResource = block.getAttribute('data-aue-resource'); | ||
| const newBlock = parsedUpdate.querySelector(`[data-aue-resource="${blockResource}"]`); | ||
| if (newBlock) { | ||
| newBlock.style.display = 'none'; | ||
| block.insertAdjacentElement('afterend', newBlock); | ||
| decorateButtons(newBlock); | ||
| decorateIcons(newBlock); | ||
| decorateBlock(newBlock); | ||
| decorateRichtext(newBlock); | ||
| await loadBlock(newBlock); | ||
| block.remove(); | ||
| newBlock.style.display = null; | ||
| return true; | ||
| } | ||
| } else { | ||
| // sections and default content, may be multiple in the case of richtext | ||
| const newElements = parsedUpdate.querySelectorAll(`[data-aue-resource="${resource}"],[data-richtext-resource="${resource}"]`); | ||
| if (newElements.length) { | ||
| const { parentElement } = element; | ||
| if (element.matches('.section')) { | ||
| const [newSection] = newElements; | ||
| newSection.style.display = 'none'; | ||
| element.insertAdjacentElement('afterend', newSection); | ||
| decorateButtons(newSection); | ||
| decorateIcons(newSection); | ||
| decorateRichtext(newSection); | ||
| decorateSections(parentElement); | ||
| decorateBlocks(parentElement); | ||
| await loadSections(parentElement); | ||
| element.remove(); | ||
| newSection.style.display = null; | ||
| } else { | ||
| element.replaceWith(...newElements); | ||
| decorateButtons(parentElement); | ||
| decorateIcons(parentElement); | ||
| decorateRichtext(parentElement); | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| function attachEventListners(main) { | ||
| [ | ||
| 'aue:content-patch', | ||
| 'aue:content-update', | ||
| 'aue:content-add', | ||
| 'aue:content-move', | ||
| 'aue:content-remove', | ||
| 'aue:content-copy', | ||
| ].forEach((eventType) => main?.addEventListener(eventType, async (event) => { | ||
| event.stopPropagation(); | ||
| const applied = await applyChanges(event); | ||
| if (!applied) window.location.reload(); | ||
| })); | ||
| } | ||
|
|
||
| attachEventListners(document.querySelector('main')); | ||
|
|
||
| // decorate rich text | ||
| // this has to happen after decorateMain(), and everythime decorateBlocks() is called | ||
| decorateRichtext(); | ||
| // in cases where the block decoration is not done in one synchronous iteration we need to listen | ||
| // for new richtext-instrumented elements. this happens for example when using experimentation. | ||
| const observer = new MutationObserver(() => decorateRichtext()); | ||
| observer.observe(document, { attributeFilter: ['data-richtext-prop'], subtree: true }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.