Skip to content

Commit

Permalink
Merge pull request #58 from codyworthen/master
Browse files Browse the repository at this point in the history
fixed bug with editor-bridge.blade.php
  • Loading branch information
RicLeP authored Dec 14, 2024
2 parents 7df0d09 + ad774f2 commit 92b9117
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 23 deletions.
11 changes: 11 additions & 0 deletions config/storyblok.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,17 @@
*/
'live_element' => '.storyblok-live',

/*
|--------------------------------------------------------------------------
| Allow live preview links
|--------------------------------------------------------------------------
|
| Links in the visual editor will be clickable and navigate to the page
| with the Storyblok editing query string appended
|
*/
'live_links' => true,

/*
|--------------------------------------------------------------------------
| Name of the field to be used for settings
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/LiveContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function show(Request $request): string

$page = Storyblok::setData($data['story'])->render();
$dom = new HTML5DOMDocument();
$dom->loadHTML($page);
$dom->loadHTML($page, HTML5DOMDocument::ALLOW_DUPLICATE_IDS);

return $dom->querySelector(config('storyblok.live_element'))->innerHTML;
}
Expand Down
91 changes: 69 additions & 22 deletions src/resources/views/editor-bridge.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,74 @@
});
@if (config('storyblok.live_preview'))
storyblokInstance.on('input', (event) => {
const CancelToken = axios.CancelToken;
let source = CancelToken.source();
source && source.cancel('Operation canceled due to new request.');
// save the new request for cancellation
source = axios.CancelToken.source();
axios.post('{{ url()->current() }}', {
data: event
}, {
cancelToken: source.token
}).then((response) => {
document.querySelector('{{ config('storyblok.live_element') }}').innerHTML = response.data;
const storyblokInstance = new StoryblokBridge({
accessToken: '{{ config('storyblok.api_preview_key') }}'
});
});
});
storyblokInstance.on('input', (event) => {
const CancelToken = axios.CancelToken;
let source = CancelToken.source();
source && source.cancel('Operation canceled due to new request.');
// save the new request for cancellation
source = axios.CancelToken.source();
axios.post(@js(request()->getRequestUri()), {
data: event
}, {
cancelToken: source.token
}).then((response) => {
document.querySelector('{{ config('storyblok.live_element') }}').innerHTML = response.data;
@if (config('storyblok.live_links'))
document.dispatchEvent(new Event('DOMContentLoaded'));
@endif
const storyblokInstance = new StoryblokBridge({
accessToken: '{{ config('storyblok.api_preview_key') }}'
});
});
});
@endif
@if (config('storyblok.live_links'))
function appendQueryParamsToPath(path) {
const link = new URL(path, window.location.origin);
if (link.origin !== window.location.origin) {
return path;
}
const currentUrl = window.location.href;
const queryParams = currentUrl.split('?')[1];
if (queryParams) {
path += (path.includes('?') ? '&' : '?') + queryParams;
}
return path;
}
function updateAllLinks() {
const urlParams = new URLSearchParams(window.location.search);
let condition = false;
for (const [key] of urlParams) {
if (key.startsWith('_storyblok')) {
condition = true;
break;
}
}
if (condition) {
const links = document.querySelectorAll('a[href]');
links.forEach(link => {
const originalHref = link.getAttribute('href');
const updatedHref = appendQueryParamsToPath(originalHref);
link.setAttribute('href', updatedHref);
});
}
}
document.addEventListener('DOMContentLoaded', updateAllLinks);
@endif
</script>
@endif
@endif

0 comments on commit 92b9117

Please sign in to comment.