@@ -25,6 +25,7 @@ const store = localforage.createInstance({
25
25
version : 1 ,
26
26
} ) ;
27
27
28
+ const defaultEditorSize = '40%' ;
28
29
const defaultPosition = 'bottom' ;
29
30
30
31
export type EditorPosition = 'bottom' | 'right' | 'undocked' ;
@@ -36,6 +37,24 @@ const applyColorScheme = (colorScheme: Exclude<ColorScheme, 'system'>) => {
36
37
] ( 'data-playroom-dark' , '' ) ;
37
38
} ;
38
39
40
+ function convertAndStoreSizeAsPercentage (
41
+ mode : 'height' | 'width' ,
42
+ size : number
43
+ ) : string {
44
+ const viewportSize =
45
+ mode === 'height' ? window . innerHeight : window . innerWidth ;
46
+
47
+ const sizePercentage = ( size / viewportSize ) * 100 ;
48
+ const roundedSizePercentage = `${ Math . round ( sizePercentage ) } %` ;
49
+
50
+ store . setItem (
51
+ `${ mode === 'height' ? 'editorHeight' : 'editorWidth' } ` ,
52
+ roundedSizePercentage
53
+ ) ;
54
+
55
+ return `${ sizePercentage } %` ;
56
+ }
57
+
39
58
interface DebounceUpdateUrl {
40
59
code ?: string ;
41
60
themes ?: string [ ] ;
@@ -65,8 +84,8 @@ interface State {
65
84
cursorPosition : CursorPosition ;
66
85
editorHidden : boolean ;
67
86
editorPosition : EditorPosition ;
68
- editorHeight : number ;
69
- editorWidth : number ;
87
+ editorHeight : string ;
88
+ editorWidth : string ;
70
89
statusMessage ?: StatusMessage ;
71
90
visibleThemes ?: string [ ] ;
72
91
visibleWidths ?: number [ ] ;
@@ -328,21 +347,28 @@ const createReducer =
328
347
329
348
case 'updateEditorHeight' : {
330
349
const { size } = action . payload ;
331
- store . setItem ( 'editorHeight' , size ) ;
350
+
351
+ const updatedHeightPercentage = convertAndStoreSizeAsPercentage (
352
+ 'height' ,
353
+ size
354
+ ) ;
332
355
333
356
return {
334
357
...state ,
335
- editorHeight : size ,
358
+ editorHeight : updatedHeightPercentage ,
336
359
} ;
337
360
}
338
361
339
362
case 'updateEditorWidth' : {
340
363
const { size } = action . payload ;
341
- store . setItem ( 'editorWidth' , size ) ;
364
+ const updatedWidthPercentage = convertAndStoreSizeAsPercentage (
365
+ 'width' ,
366
+ size
367
+ ) ;
342
368
343
369
return {
344
370
...state ,
345
- editorWidth : size ,
371
+ editorWidth : updatedWidthPercentage ,
346
372
} ;
347
373
}
348
374
@@ -408,8 +434,8 @@ const initialState: State = {
408
434
cursorPosition : { line : 0 , ch : 0 } ,
409
435
editorHidden : false ,
410
436
editorPosition : defaultPosition ,
411
- editorHeight : 300 ,
412
- editorWidth : 360 ,
437
+ editorHeight : defaultEditorSize ,
438
+ editorWidth : defaultEditorSize ,
413
439
ready : false ,
414
440
colorScheme : 'light' ,
415
441
} ;
@@ -473,8 +499,8 @@ export const StoreProvider = ({
473
499
Promise . all ( [
474
500
store . getItem < string > ( 'code' ) ,
475
501
store . getItem < EditorPosition > ( 'editorPosition' ) ,
476
- store . getItem < number > ( 'editorHeight' ) ,
477
- store . getItem < number > ( 'editorWidth' ) ,
502
+ store . getItem < string | number > ( 'editorHeight' ) , // Number type deprecated
503
+ store . getItem < string | number > ( 'editorWidth' ) , // Number type deprecated
478
504
store . getItem < number [ ] > ( 'visibleWidths' ) ,
479
505
store . getItem < string [ ] > ( 'visibleThemes' ) ,
480
506
store . getItem < ColorScheme > ( 'colorScheme' ) ,
@@ -490,17 +516,28 @@ export const StoreProvider = ({
490
516
] ) => {
491
517
const code = codeFromQuery || storedCode || exampleCode ;
492
518
const editorPosition = storedPosition ;
493
- const editorHeight = storedHeight ;
494
- const editorWidth = storedWidth ;
519
+
520
+ const editorHeight =
521
+ ( typeof storedHeight === 'number'
522
+ ? convertAndStoreSizeAsPercentage ( 'height' , storedHeight )
523
+ : storedHeight ) || defaultEditorSize ;
524
+
525
+ const editorWidth =
526
+ ( typeof storedWidth === 'number'
527
+ ? convertAndStoreSizeAsPercentage ( 'width' , storedWidth )
528
+ : storedWidth ) || defaultEditorSize ;
529
+
495
530
const visibleWidths =
496
531
widthsFromQuery ||
497
532
storedVisibleWidths ||
498
533
playroomConfig ?. defaultVisibleWidths ;
534
+
499
535
const visibleThemes =
500
536
hasThemesConfigured &&
501
537
( themesFromQuery ||
502
538
storedVisibleThemes ||
503
539
playroomConfig ?. defaultVisibleThemes ) ;
540
+
504
541
const colorScheme = storedColorScheme ;
505
542
506
543
dispatch ( {
0 commit comments