-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor-support.js
More file actions
111 lines (102 loc) · 3.82 KB
/
editor-support.js
File metadata and controls
111 lines (102 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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;
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'));