13
13
});
14
14
15
15
@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
+ });
37
41
@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
38
85
</script >
39
- @endif
86
+ @endif
0 commit comments