From f1505b2c557ef4759615e4999d015cdb9a0ea9bc Mon Sep 17 00:00:00 2001 From: Markus Haack Date: Fri, 22 Nov 2024 13:27:33 +0100 Subject: [PATCH] fix: sidekick v7 update (#54) * Update config.json * Update config.json * Delete scripts/sidekick.js * Remove sidekick.js --- scripts/scripts.js | 1 - scripts/sidekick.js | 81 -------------------------------------- tools/sidekick/config.json | 11 ++---- 3 files changed, 3 insertions(+), 90 deletions(-) delete mode 100644 scripts/sidekick.js diff --git a/scripts/scripts.js b/scripts/scripts.js index 3490e9aa65..df93524071 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -136,7 +136,6 @@ function loadDelayed() { // eslint-disable-next-line import/no-cycle window.setTimeout(() => import('./delayed.js'), 3000); // load anything that can be postponed to the latest here - import('./sidekick.js').then(({ initSidekick }) => initSidekick()); } async function loadPage() { diff --git a/scripts/sidekick.js b/scripts/sidekick.js deleted file mode 100644 index 09ef4dd2df..0000000000 --- a/scripts/sidekick.js +++ /dev/null @@ -1,81 +0,0 @@ -async function getContentSourceUrl(owner, repo, ref) { - const res = await fetch(`https://admin.hlx.page/sidekick/${owner}/${repo}/${ref}/env.json`); - if (!res || !res.ok) { - return null; - } - const env = await res.json(); - return env?.contentSourceUrl; -} -async function openWithUniversalEditor(event) { - const { owner, repo, ref } = event.detail.data.config; - const contentSourceUrl = await getContentSourceUrl(owner, repo, ref); - if (!contentSourceUrl) { - // eslint-disable-next-line no-console - console.error('Content source URL not found'); - return; - } - const { pathname } = window.location; - const editorUrl = `${contentSourceUrl}${pathname}?cmd=open`; - // open the editor in a new tab - window.open(editorUrl, '_blank'); -} - -async function getElement(sk, selector) { - let elt = sk.shadowRoot.querySelector(selector); - return new Promise((resolve) => { - const check = () => { - elt = sk.shadowRoot.querySelector(selector); - if (elt) { - resolve(elt); - } else { - setTimeout(check, 100); - } - }; - check(); - }); -} - -function shouldHidePlugin(plugin) { - const [pluginCls] = plugin.classList; - return ['edit', 'reload', 'publish', 'delete', 'unpublish'].indexOf(pluginCls) !== -1; -} - -async function customizeButtons(sk) { - const container = await getElement(sk, '.plugin-container'); - container.style.visibility = 'hidden'; - - // hide the default buttons once - container.querySelectorAll('.plugin').forEach((plugin) => { - if (shouldHidePlugin(plugin)) { - plugin.style.display = 'none'; - } - }); - // listen for new buttons and hide them - new MutationObserver((mutations) => { - mutations.forEach((mutation) => mutation.addedNodes.forEach((node) => { - if (shouldHidePlugin(node)) { - node.style.display = 'none'; - } - })); - }).observe(container, { childList: true }); - - container.style.visibility = 'visible'; - - // initialize the custom edit button - sk.addEventListener('custom:aemedit', openWithUniversalEditor); -} - -// eslint-disable-next-line import/prefer-default-export -export async function initSidekick() { - let sk = document.querySelector('helix-sidekick'); - if (sk) { - // sidekick already loaded - await customizeButtons(sk); - } else { - // wait for sidekick to be loaded - document.addEventListener('sidekick-ready', async () => { - sk = document.querySelector('helix-sidekick'); - await customizeButtons(sk); - }, { once: true }); - } -} diff --git a/tools/sidekick/config.json b/tools/sidekick/config.json index b9b8e95a25..17dfc9e954 100644 --- a/tools/sidekick/config.json +++ b/tools/sidekick/config.json @@ -1,10 +1,5 @@ { - "plugins": [ - { - "id": "aemedit", - "title": "Edit", - "environments": [ "dev", "preview", "live" ], - "event": "aemedit" - } - ] + "project": "AEM XWalk Boilerplate", + "editUrlLabel": "AEM Editor", + "editUrlPattern": "{{contentSourceUrl}}{{pathname}}?cmd=open" }