forked from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 31
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4fb0ead
SITES-16562 - [Xwalk] Open Universal Editor from Franklin Sidekick
jckautzmann ce8a8b0
SITES-16562 - [Xwalk] Open Universal Editor from Franklin Sidekick
jckautzmann 8fd5333
SITES-16562 - [Xwalk] Open Universal Editor from Franklin Sidekick
jckautzmann 5c102d6
SITES-16562 - [Xwalk] Open Universal Editor from Franklin Sidekick
jckautzmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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) => { ... })
insteadThere was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed in 5c102d6