Skip to content

Commit d1528a6

Browse files
committed
2 parents fba987f + 92b9117 commit d1528a6

File tree

3 files changed

+81
-23
lines changed

3 files changed

+81
-23
lines changed

config/storyblok.php

+11
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,17 @@
249249
*/
250250
'live_element' => '.storyblok-live',
251251

252+
/*
253+
|--------------------------------------------------------------------------
254+
| Allow live preview links
255+
|--------------------------------------------------------------------------
256+
|
257+
| Links in the visual editor will be clickable and navigate to the page
258+
| with the Storyblok editing query string appended
259+
|
260+
*/
261+
'live_links' => true,
262+
252263
/*
253264
|--------------------------------------------------------------------------
254265
| Name of the field to be used for settings

src/Http/Controllers/LiveContentController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function show(Request $request): string
2929

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

3434
return $dom->querySelector(config('storyblok.live_element'))->innerHTML;
3535
}

src/resources/views/editor-bridge.blade.php

+69-22
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,74 @@
1313
});
1414
1515
@if (config('storyblok.live_preview'))
16-
storyblokInstance.on('input', (event) => {
17-
const CancelToken = axios.CancelToken;
18-
let source = CancelToken.source();
19-
20-
source && source.cancel('Operation canceled due to new request.');
21-
22-
// save the new request for cancellation
23-
source = axios.CancelToken.source();
24-
25-
axios.post('{{ url()->current() }}', {
26-
data: event
27-
}, {
28-
cancelToken: source.token
29-
}).then((response) => {
30-
document.querySelector('{{ config('storyblok.live_element') }}').innerHTML = response.data;
31-
32-
const storyblokInstance = new StoryblokBridge({
33-
accessToken: '{{ config('storyblok.api_preview_key') }}'
34-
});
35-
});
36-
});
16+
storyblokInstance.on('input', (event) => {
17+
const CancelToken = axios.CancelToken;
18+
let source = CancelToken.source();
19+
20+
source && source.cancel('Operation canceled due to new request.');
21+
22+
// save the new request for cancellation
23+
source = axios.CancelToken.source();
24+
25+
axios.post(@js(request()->getRequestUri()), {
26+
data: event
27+
}, {
28+
cancelToken: source.token
29+
}).then((response) => {
30+
document.querySelector('{{ config('storyblok.live_element') }}').innerHTML = response.data;
31+
32+
@if (config('storyblok.live_links'))
33+
document.dispatchEvent(new Event('DOMContentLoaded'));
34+
@endif
35+
36+
const storyblokInstance = new StoryblokBridge({
37+
accessToken: '{{ config('storyblok.api_preview_key') }}'
38+
});
39+
});
40+
});
3741
@endif
42+
43+
44+
@if (config('storyblok.live_links'))
45+
function appendQueryParamsToPath(path) {
46+
const link = new URL(path, window.location.origin);
47+
if (link.origin !== window.location.origin) {
48+
return path;
49+
}
50+
51+
const currentUrl = window.location.href;
52+
const queryParams = currentUrl.split('?')[1];
53+
54+
if (queryParams) {
55+
path += (path.includes('?') ? '&' : '?') + queryParams;
56+
}
57+
58+
return path;
59+
}
60+
61+
function updateAllLinks() {
62+
const urlParams = new URLSearchParams(window.location.search);
63+
let condition = false;
64+
65+
for (const [key] of urlParams) {
66+
if (key.startsWith('_storyblok')) {
67+
condition = true;
68+
break;
69+
}
70+
}
71+
72+
if (condition) {
73+
const links = document.querySelectorAll('a[href]');
74+
75+
links.forEach(link => {
76+
const originalHref = link.getAttribute('href');
77+
const updatedHref = appendQueryParamsToPath(originalHref);
78+
link.setAttribute('href', updatedHref);
79+
});
80+
}
81+
}
82+
83+
document.addEventListener('DOMContentLoaded', updateAllLinks);
84+
@endif
3885
</script>
39-
@endif
86+
@endif

0 commit comments

Comments
 (0)