Skip to content
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

SITES-16562 - [Xwalk] Open Universal Editor from Franklin Sidekick #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import {
loadCSS,
toClassName,
buildBlock,
decorateBlock,
readBlockConfig,
} from './lib-franklin.js';

import { initSidekick } from './sidekick/sidekick.js';

const LCP_BLOCKS = []; // add your LCP blocks to the list
window.hlx.RUM_GENERATION = 'project-1'; // add your RUM generation information here

Expand Down Expand Up @@ -273,6 +274,7 @@ 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
initSidekick();
}

async function loadPage() {
Expand Down
62 changes: 62 additions & 0 deletions scripts/sidekick/sidekick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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();
if (!env) {
return null;
}
return env.contentSourceUrl;
}
async function openAemEditor(event) {
const { owner, repo, ref } = event.detail.data.config;
const contentSourceUrl = await getContentSourceUrl(owner, repo, ref);
const path = window.location.pathname;
const editorUrl = `${contentSourceUrl}${path}?cmd=open`;
// open the editor in a new tab
window.open(editorUrl, '_blank');
}

function getButton(sk, selector) {
let btn = sk.shadowRoot.querySelector(selector);
if (btn) {
return btn;
}
return new Promise((resolve) => {
const check = () => {
btn = sk.shadowRoot.querySelector(selector);
if (btn) {
resolve(btn);
} else {
setTimeout(check, 100);
}
};
check();
});
}

async function overrideEditButton(sk) {
const oldEditBtn = await getButton(sk, '.edit.plugin');
const newEditBtn = await getButton(sk, '.aemedit.plugin');
oldEditBtn.replaceWith(newEditBtn);
Copy link
Collaborator

@buuhuu buuhuu Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need a new button actually? can we get the old button and just replace the click behaviour?

If so we would not need to add any sidekick config.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it but it didn't work because:

  • the Edit button is disabled
  • even if we enable it, the ootb logic is triggered when the button is clicked and it cannot be overridden

// hack to remove the original edit button that is generated again in the DOM
const oldEditBtn1 = await getButton(sk, '.edit.plugin');
oldEditBtn1.remove();
sk.addEventListener('custom:aemedit', openAemEditor);
}

// eslint-disable-next-line import/prefer-default-export
export function initSidekick() {
let sk = document.querySelector('helix-sidekick');
if (sk) {
// sidekick already loaded
overrideEditButton(sk);
} else {
// wait for sidekick to be loaded
document.addEventListener('sidekick-ready', () => {
sk = document.querySelector('helix-sidekick');
overrideEditButton(sk);
}, { once: true });
}
}
6 changes: 6 additions & 0 deletions tools/sidekick/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
"paletteRect": "top: auto; bottom: 20px; left: 20px; height: 398px; width: 360px;",
"url": "/block-library/library",
"includePaths": [ "**.docx**" ]
},
{
"id": "aemedit",
"title": "Edit",
"environments": [ "dev", "preview", "live" ],
"event": "aemedit"
}
]
}
Loading