Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,16 @@ function loadDelayed() {
// load anything that can be postponed to the latest here
}

async function loadPage() {
export async function loadPage() {
await loadEager(document);
await loadLazy(document);
loadDelayed();
}

loadPage();

(() => {
const hasQE = new URL(window.location.href).searchParams.has('quick-edit');
// eslint-disable-next-line import/no-cycle
if (hasQE) import('../tools/quick-edit/quick-edit.js').then((mod) => mod.default());
})();
62 changes: 62 additions & 0 deletions tools/quick-edit/quick-edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// eslint-disable-next-line import/no-cycle
import { loadPage } from '../../scripts/scripts.js';

const importMap = {
imports: {
'da-lit': 'https://da.live/deps/lit/dist/index.js',
'da-y-wrapper': 'https://da.live/deps/da-y-wrapper/dist/index.js',
},
};

function addImportmap() {
const importmapEl = document.createElement('script');
importmapEl.type = 'importmap';
importmapEl.textContent = JSON.stringify(importMap);
document.head.appendChild(importmapEl);
}

async function loadModule(origin, payload) {
const url = new URL(`${origin}/nx/public/plugins/quick-edit/quick-edit.js`);
// Guard against dynamic-import injection via the `quick-edit` URL param: only
// load from trusted da-nx origins. Validating the resolved hostname (rather
// than the raw `ref`) covers any valid ref length/charset and cannot be
// bypassed by path/query/userinfo tricks. Upstream aemsites/author-kit omits
// this; see the filed issue.
const trusted = url.hostname === 'da.live'
|| url.hostname === 'localhost'
|| url.hostname.endsWith('--da-nx--adobe.aem.live');
if (!trusted) return;
const { default: loadQuickEdit } = await import(url.href);
loadQuickEdit(payload, loadPage);
}

function generateSidekickPayload() {
let { hostname } = window.location;
if (hostname === 'localhost') {
hostname = document.querySelector('meta[property="hlx:proxyUrl"]').content;
}
const parts = hostname.split('.')[0].split('--');
const [, repo, owner] = parts;

return {
detail: {
config: {
mountpoint: `https://content.da.live/${owner}/${repo}/`,
},
location: {
pathname: window.location.pathname,
},
},
};
}

export default function init(payload) {
const { search } = window.location;
const ref = new URLSearchParams(search).get('quick-edit');
let origin;
if (ref === 'on' || !ref) origin = 'https://da.live';
if (ref === 'local') origin = 'http://localhost:6456';
if (!origin) origin = `https://${ref}--da-nx--adobe.aem.live`;
addImportmap();
loadModule(origin, payload || generateSidekickPayload());
}
Loading