@@ -8,11 +8,28 @@ import { UMB_SERVER_CONTEXT } from '@umbraco-cms/backoffice/server';
8
8
9
9
const UMB_LOCALSTORAGE_SESSION_KEY = 'umb:previewSessions' ;
10
10
11
+ interface UmbPreviewIframeArgs {
12
+ className ?: string ;
13
+ culture ?: string ;
14
+ height ?: string ;
15
+ segment ?: string ;
16
+ width ?: string ;
17
+ }
18
+
19
+ interface UmbPreviewUrlArgs {
20
+ culture ?: string | null ;
21
+ rnd ?: number ;
22
+ segment ?: string | null ;
23
+ serverUrl ?: string ;
24
+ unique ?: string | null ;
25
+ }
26
+
11
27
export class UmbPreviewContext extends UmbContextBase {
28
+ #unique?: string | null ;
12
29
#culture?: string | null ;
30
+ #segment?: string | null ;
13
31
#serverUrl: string = '' ;
14
32
#webSocket?: WebSocket ;
15
- #unique?: string | null ;
16
33
17
34
#iframeReady = new UmbBooleanState ( false ) ;
18
35
public readonly iframeReady = this . #iframeReady. asObservable ( ) ;
@@ -30,8 +47,9 @@ export class UmbPreviewContext extends UmbContextBase {
30
47
31
48
const params = new URLSearchParams ( window . location . search ) ;
32
49
33
- this . #culture = params . get ( 'culture' ) ;
34
50
this . #unique = params . get ( 'id' ) ;
51
+ this . #culture = params . get ( 'culture' ) ;
52
+ this . #segment = params . get ( 'segment' ) ;
35
53
36
54
if ( ! this . #unique) {
37
55
console . error ( 'No unique ID found in query string.' ) ;
@@ -75,16 +93,46 @@ export class UmbPreviewContext extends UmbContextBase {
75
93
return Math . max ( Number ( localStorage . getItem ( UMB_LOCALSTORAGE_SESSION_KEY ) ) , 0 ) || 0 ;
76
94
}
77
95
78
- #setPreviewUrl( args ?: { serverUrl ?: string ; unique ?: string | null ; culture ?: string | null ; rnd ?: number } ) {
96
+ #setPreviewUrl( args ?: UmbPreviewUrlArgs ) {
79
97
const host = args ?. serverUrl || this . #serverUrl;
80
- const path = args ?. unique || this . #unique;
81
- const params = new URLSearchParams ( ) ;
98
+ const unique = args ?. unique || this . #unique;
99
+
100
+ if ( ! unique ) {
101
+ throw new Error ( 'No unique ID found in query string.' ) ;
102
+ }
103
+
104
+ const url = new URL ( unique , host ) ;
105
+ const params = new URLSearchParams ( url . search ) ;
106
+
82
107
const culture = args ?. culture || this . #culture;
108
+ const segment = args ?. segment || this . #segment;
83
109
84
- if ( culture ) params . set ( 'culture' , culture ) ;
85
- if ( args ?. rnd ) params . set ( 'rnd' , args . rnd . toString ( ) ) ;
110
+ const cultureParam = 'culture' ;
111
+ const rndParam = 'rnd' ;
112
+ const segmentParam = 'segment' ;
86
113
87
- this . #previewUrl. setValue ( `${ host } /${ path } ?${ params } ` ) ;
114
+ if ( culture ) {
115
+ params . set ( cultureParam , culture ) ;
116
+ } else {
117
+ params . delete ( cultureParam ) ;
118
+ }
119
+
120
+ if ( args ?. rnd ) {
121
+ params . set ( rndParam , args . rnd . toString ( ) ) ;
122
+ } else {
123
+ params . delete ( rndParam ) ;
124
+ }
125
+
126
+ if ( segment ) {
127
+ params . set ( segmentParam , segment ) ;
128
+ } else {
129
+ params . delete ( segmentParam ) ;
130
+ }
131
+
132
+ const previewUrl = new URL ( url . pathname + '?' + params . toString ( ) , host ) ;
133
+ const previewUrlString = previewUrl . toString ( ) ;
134
+
135
+ this . #previewUrl. setValue ( previewUrlString ) ;
88
136
}
89
137
90
138
#setSessionCount( sessions : number ) {
@@ -165,8 +213,9 @@ export class UmbPreviewContext extends UmbContextBase {
165
213
this . #setSessionCount( sessions ) ;
166
214
}
167
215
168
- async updateIFrame ( args ?: { culture ?: string ; className ?: string ; height ?: string ; width ?: string } ) {
169
- if ( ! args ) return ;
216
+ #currentArgs: UmbPreviewIframeArgs = { } ;
217
+ async updateIFrame ( args ?: UmbPreviewIframeArgs ) {
218
+ const mergedArgs = { ...this . #currentArgs, ...args } ;
170
219
171
220
const wrapper = this . getIFrameWrapper ( ) ;
172
221
if ( ! wrapper ) return ;
@@ -185,20 +234,32 @@ export class UmbPreviewContext extends UmbContextBase {
185
234
window . addEventListener ( 'resize' , scaleIFrame ) ;
186
235
wrapper . addEventListener ( 'transitionend' , scaleIFrame ) ;
187
236
188
- if ( args . culture ) {
189
- this . #iframeReady. setValue ( false ) ;
237
+ this . #iframeReady. setValue ( false ) ;
190
238
191
- const params = new URLSearchParams ( window . location . search ) ;
192
- params . set ( 'culture' , args . culture ) ;
193
- const newRelativePathQuery = window . location . pathname + '?' + params . toString ( ) ;
194
- history . pushState ( null , '' , newRelativePathQuery ) ;
239
+ const params = new URLSearchParams ( window . location . search ) ;
240
+
241
+ if ( mergedArgs . culture ) {
242
+ params . set ( 'culture' , mergedArgs . culture ) ;
243
+ } else {
244
+ params . delete ( 'culture' ) ;
245
+ }
195
246
196
- this . #setPreviewUrl( { culture : args . culture } ) ;
247
+ if ( mergedArgs . segment ) {
248
+ params . set ( 'segment' , mergedArgs . segment ) ;
249
+ } else {
250
+ params . delete ( 'segment' ) ;
197
251
}
198
252
199
- if ( args . className ) wrapper . className = args . className ;
200
- if ( args . height ) wrapper . style . height = args . height ;
201
- if ( args . width ) wrapper . style . width = args . width ;
253
+ const newRelativePathQuery = window . location . pathname + '?' + params . toString ( ) ;
254
+ history . pushState ( null , '' , newRelativePathQuery ) ;
255
+
256
+ this . #currentArgs = mergedArgs ;
257
+
258
+ this . #setPreviewUrl( { culture : mergedArgs . culture , segment : mergedArgs . segment } ) ;
259
+
260
+ if ( mergedArgs . className ) wrapper . className = mergedArgs . className ;
261
+ if ( mergedArgs . height ) wrapper . style . height = mergedArgs . height ;
262
+ if ( mergedArgs . width ) wrapper . style . width = mergedArgs . width ;
202
263
}
203
264
}
204
265
0 commit comments