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 #17

Merged
merged 4 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,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
import('./sidekick.js').then(({ initSidekick }) => initSidekick());
}

async function loadPage() {
Expand Down
67 changes: 67 additions & 0 deletions scripts/sidekick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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();
});
}

async function customizeButtons(sk) {
// hide the default buttons
const container = await getElement(sk, '.plugin-container');
container.style.visibility = 'hidden';
for (let i = 0; i < ['edit', 'reload', 'publish', 'delete', 'unpublish'].length; i += 1) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

use ['edit', 'reload', 'publish', 'delete', 'unpublish'].forEach(async (action) => { ... }) instead

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was giving a linting error. That's why I used the for loop.

Copy link
Collaborator

Choose a reason for hiding this comment

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

but then at least define array as a variable and don't duplicate it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

addressed in 5c102d6

const action = ['edit', 'reload', 'publish', 'delete', 'unpublish'][i];
// eslint-disable-next-line no-await-in-loop
const btn = await getElement(sk, `.${action}.plugin`);
btn.style.display = 'none';
}
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 });
}
}
10 changes: 10 additions & 0 deletions tools/sidekick/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"plugins": [
{
"id": "aemedit",
"title": "Edit",
"environments": [ "dev", "preview", "live" ],
"event": "aemedit"
}
]
}