@@ -29,16 +29,19 @@ import { MediaQuery } from "svelte/reactivity";
2929export type SidebarLocation = "left" | "right" ;
3030
3131export class GlobalOptions {
32- private static readonly localStorageKey = "diff-viewer-global-options" ;
33- private static readonly context = new Context < GlobalOptions > ( GlobalOptions . localStorageKey ) ;
32+ static readonly key = "diff-viewer-global-options" ;
33+ private static readonly context = new Context < GlobalOptions > ( GlobalOptions . key ) ;
3434
35- static init ( ) {
35+ static init ( cookie ?: string ) {
3636 const opts = new GlobalOptions ( ) ;
3737 if ( ! browser ) {
3838 GlobalOptions . context . set ( opts ) ;
39+ if ( cookie ) {
40+ opts . deserialize ( cookie ) ;
41+ }
3942 return opts ;
4043 }
41- const serialized = localStorage . getItem ( GlobalOptions . localStorageKey ) ;
44+ const serialized = localStorage . getItem ( GlobalOptions . key ) ;
4245 if ( serialized !== null ) {
4346 opts . deserialize ( serialized ) ;
4447 }
@@ -56,15 +59,14 @@ export class GlobalOptions {
5659 wordDiffs = $state ( true ) ;
5760 lineWrap = $state ( true ) ;
5861 omitPatchHeaderOnlyHunks = $state ( true ) ;
59- // TODO: send to server (use cookie?) to that the initial position is correct
6062 sidebarLocation : SidebarLocation = $state ( "left" ) ;
6163
6264 private constructor ( ) {
6365 $effect ( ( ) => {
6466 this . save ( ) ;
6567 } ) ;
6668
67- watchLocalStorage ( GlobalOptions . localStorageKey , ( newValue ) => {
69+ watchLocalStorage ( GlobalOptions . key , ( newValue ) => {
6870 if ( newValue ) {
6971 this . deserialize ( newValue ) ;
7072 }
@@ -84,7 +86,8 @@ export class GlobalOptions {
8486 if ( ! browser ) {
8587 return ;
8688 }
87- localStorage . setItem ( GlobalOptions . localStorageKey , this . serialize ( ) ) ;
89+ localStorage . setItem ( GlobalOptions . key , this . serialize ( ) ) ;
90+ document . cookie = `${ GlobalOptions . key } =${ encodeURIComponent ( this . serializeCookie ( ) ) } ; path=/; max-age=31536000; SameSite=Lax` ;
8891 }
8992
9093 private serialize ( ) {
@@ -105,6 +108,14 @@ export class GlobalOptions {
105108 return JSON . stringify ( cereal ) ;
106109 }
107110
111+ private serializeCookie ( ) {
112+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
113+ const cereal : any = {
114+ sidebarLocation : this . sidebarLocation ,
115+ } ;
116+ return JSON . stringify ( cereal ) ;
117+ }
118+
108119 private deserialize ( serialized : string ) {
109120 const jsonObject = JSON . parse ( serialized ) ;
110121 if ( jsonObject . syntaxHighlighting !== undefined ) {
0 commit comments