-
Notifications
You must be signed in to change notification settings - Fork 652
Expand file tree
/
Copy pathAposAreas.js
More file actions
293 lines (260 loc) · 8.95 KB
/
Copy pathAposAreas.js
File metadata and controls
293 lines (260 loc) · 8.95 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import createApp, { pinia } from 'Modules/@apostrophecms/ui/lib/vue';
import { useWidgetGraphStore } from 'Modules/@apostrophecms/ui/stores/widgetGraph.js';
import { nextTick } from 'vue';
import { createId } from '@paralleldrive/cuid2';
export default function() {
const mountedApps = new Map();
let widgetsRendering = 0;
apos.area.widgetOptions = [];
createWidgetClipboardApp();
createAreaApps();
document.documentElement.style.setProperty('--a-widget-margin', apos.ui.widgetMargin);
apos.bus.$on('widget-rendering', function() {
widgetsRendering++;
});
apos.bus.$on('widget-rendered', options => {
widgetsRendering--;
createAreaAppsAndRunPlayersIfDone(options);
});
apos.bus.$on('refreshed', function() {
// Re-instantiate the on-page widget graph before remounting areas.
// The normal mounted hooks will rebuild it from fresh data.
const graphStore = useWidgetGraphStore(pinia);
if (apos.adminBar?.contextId) {
graphStore.resetGraph(apos.adminBar.contextId);
}
createAreaAppsAndRunPlayersIfDone();
});
function createAreaAppsAndRunPlayersIfDone({ edit = true, el = null } = {}) {
if (edit) {
createAreaApps(el);
nextTick(() => {
cleanupOrphanedApps();
});
}
if (widgetsRendering === 0) {
apos.util.runPlayers();
}
}
function createAreaApps(el) {
// Create apps only for the areas at the top of the nesting
// hierarchy in the given context. widget-rendered events will
// cause more invocations later, avoiding double invocations,
// orphaned apps and wasted time. -Tom
const els = Array.from((el || document).querySelectorAll('[data-apos-area-newly-editable]'));
const lowest = Math.min(...els.map(el => depth(el)));
els.filter(el => depth(el) === lowest).forEach(el => createAreaApp(el));
}
// Determine how deeply it is nested in other areas. We don't care about
// non-area levels
function depth(el) {
let depth = 0;
while (el) {
el = el.parentNode;
if (el?.hasAttribute) {
if (el.hasAttribute('data-apos-area-newly-editable') || el.hasAttribute('data-apos-area-editable')) {
depth++;
}
}
}
return depth;
}
function createAreaApp(el) {
const options = JSON.parse(el.getAttribute('data-options')) || {};
const data = JSON.parse(el.getAttribute('data')) || {};
const fieldId = el.getAttribute('data-field-id');
const moduleName = el.getAttribute('data-module');
const choices = JSON.parse(el.getAttribute('data-choices'));
const renderings = {};
const _docId = data._docId;
const parentOptionsStr = el.getAttribute('data-parent-options');
const parentOptions = parentOptionsStr ? JSON.parse(parentOptionsStr) : null;
let componentName = options.editorComponent || 'AposAreaEditor';
if (!apos.vueComponents[componentName]) {
// eslint-disable-next-line no-console
console.error(`Area Editor component "${componentName}" not found. Switching to default.`);
componentName = 'AposAreaEditor';
}
const component = apos.vueComponents[componentName];
for (const widgetEl of el.querySelectorAll('[data-apos-widget]')) {
const _id = widgetEl.getAttribute('data-apos-widget');
const item = data.items.find(item => _id === item._id);
// This will only match our own widgets, leaving the nested matches alone,
// another area app will handle them when the time comes
if (item) {
renderings[_id] = {
html: widgetEl.innerHTML,
parameters: {
_docId,
widget: item,
areaFieldId: fieldId,
type: item.type
}
};
widgetEl.remove();
}
}
el.removeAttribute('data-apos-area-newly-editable');
el.setAttribute('data-apos-area-editable', true);
let created = false;
let observer;
if (apos.area.activeEditor && (apos.area.activeEditor.id === data._id)) {
// Editing a piece causes a refresh of the main content area,
// but this may contain the area we originally intended to add
// a widget to when we created a piece for that purpose. Preserve
// the editing experience by restoring that widget's editor to the DOM
// rather than creating a new one.
el.parentNode.replaceChild(apos.area.activeEditor.$el, el);
} else {
const rect = el.getBoundingClientRect();
const isInViewport = rect.bottom >= 0 &&
rect.top <= window.innerHeight;
if (isInViewport) {
mountApp();
} else {
observer = new IntersectionObserver(observed, {
rootMargin: '600px'
});
observer.observe(el);
}
}
function observed(entries) {
const intersects = entries[0].isIntersecting;
if (!intersects) {
return;
}
if (created) {
observer.disconnect();
return;
}
mountApp();
observer.disconnect();
}
function mountApp() {
const app = createApp(component, {
options,
id: data._id,
items: data.items,
choices,
docId: _docId,
fieldId,
moduleName,
parentOptions,
renderings
});
// Resolve graphKey: if this area is inside a modal that owns a
// graph (data-apos-graph-key), use that key. Otherwise fall back
// to the on-page contextId. This single DOM lookup bridges the
// provide/inject gap created by createApp.
const graphKey = el.closest('[data-apos-graph-key]')
?.getAttribute('data-apos-graph-key') || apos.adminBar?.contextId || null;
// Provide the resolved graphKey so every descendant component
// can simply inject('aposGraphKey') and get the correct value.
if (graphKey) {
app.provide('aposGraphKey', graphKey);
}
app.mount(el);
mountedApps.set(el, app);
created = true;
}
}
function createWidgetClipboardApp() {
const key = 'aposWidgetClipboard';
const marker = 'apos-widget:';
// Simpler and more reliable to just talk to localStorage always and avoid the
// storage event handle
class Clipboard {
set(widget) {
const id = createId();
localStorage.setItem(key, JSON.stringify({
id,
widget
}));
// Stamp the OS clipboard so a later paste can tell whether this
// widget copy is still the most recent thing the user copied
navigator.clipboard?.writeText(`${marker}${id}`).catch(e => {
// eslint-disable-next-line no-console
console.warn('Unable to write the widget marker to the clipboard', e);
});
}
get() {
return this.getEntry()?.widget || null;
}
getId() {
return this.getEntry()?.id || null;
}
// Returns { id, widget } or null. Entries written by older releases
// hold a bare widget object and are returned with a null id.
getEntry() {
const existing = window.localStorage.getItem(key);
if (!existing) {
return null;
}
let parsed;
try {
parsed = JSON.parse(existing);
} catch (e) {
return null;
}
if (!parsed || typeof parsed !== 'object') {
return null;
}
if (typeof parsed.id === 'string' && parsed.widget?.type) {
return parsed;
}
return {
id: null,
widget: parsed
};
}
}
apos.area.widgetClipboard = new Clipboard();
// Widget paste arrives through the native paste event rather than a
// Ctrl+V keydown interception, so the clipboard contents decide: a
// widget is pasted only when our marker is still the most recent copy
document.addEventListener('paste', e => {
if (isInsideEditable()) {
// User is typing, native paste wins
return;
}
const text = e.clipboardData?.getData('text/plain') || '';
if (!text.startsWith(marker)) {
return;
}
// Never paste the marker itself as text at area level
e.preventDefault();
if (text.slice(marker.length) === apos.area.widgetClipboard.getId()) {
apos.bus.$emit('command-menu-area-paste-widget');
}
});
function isInsideEditable() {
const el = document.activeElement;
return !!el && (
el.nodeName === 'INPUT' ||
el.nodeName === 'TEXTAREA' ||
el.isContentEditable ||
!!el.closest?.('[contenteditable]')
);
}
}
function cleanupOrphanedApps() {
// Check all tracked apps and unmount those whose elements are no longer in the DOM
for (const el of mountedApps.keys()) {
if (!document.body.contains(el)) {
unmountApp(el);
}
}
}
function unmountApp(el) {
const app = mountedApps.get(el);
if (app) {
try {
app.unmount();
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error unmounting Vue app:', error);
}
mountedApps.delete(el);
}
}
}