@@ -9,27 +9,8 @@ import {
99 parseMultiFilePatchGithub ,
1010} from "./github.svelte" ;
1111import { type StructuredPatch } from "diff" ;
12- import {
13- ConciseDiffViewCachedState ,
14- DEFAULT_THEME_DARK ,
15- DEFAULT_THEME_LIGHT ,
16- isNoNewlineAtEofLine ,
17- parseSinglePatch ,
18- patchHeaderDiffOnly ,
19- } from "$lib/components/diff/concise-diff-view.svelte" ;
20- import type { BundledTheme } from "shiki" ;
21- import { browser } from "$app/environment" ;
22- import { getEffectiveGlobalTheme } from "$lib/theme.svelte" ;
23- import {
24- countOccurrences ,
25- type FileTreeNodeData ,
26- makeFileTree ,
27- type LazyPromise ,
28- lazyPromise ,
29- watchLocalStorage ,
30- animationFramePromise ,
31- yieldToBrowser ,
32- } from "$lib/util" ;
12+ import { ConciseDiffViewCachedState , isNoNewlineAtEofLine , parseSinglePatch , patchHeaderDiffOnly } from "$lib/components/diff/concise-diff-view.svelte" ;
13+ import { countOccurrences , type FileTreeNodeData , makeFileTree , type LazyPromise , lazyPromise , animationFramePromise , yieldToBrowser } from "$lib/util" ;
3314import { onDestroy , tick } from "svelte" ;
3415import { type TreeNode , TreeState } from "$lib/components/tree/index.svelte" ;
3516import { VList } from "virtua/svelte" ;
@@ -40,126 +21,6 @@ import { ProgressBarState } from "$lib/components/progress-bar/index.svelte";
4021export const GITHUB_URL_PARAM = "github_url" ;
4122export const PATCH_URL_PARAM = "patch_url" ;
4223
43- export type SidebarLocation = "left" | "right" ;
44-
45- export class GlobalOptions {
46- static readonly key = "diff-viewer-global-options" ;
47- private static readonly context = new Context < GlobalOptions > ( GlobalOptions . key ) ;
48-
49- static init ( cookie ?: string ) {
50- const opts = new GlobalOptions ( ) ;
51- if ( ! browser ) {
52- GlobalOptions . context . set ( opts ) ;
53- if ( cookie ) {
54- opts . deserialize ( cookie ) ;
55- }
56- return opts ;
57- }
58- const serialized = localStorage . getItem ( GlobalOptions . key ) ;
59- if ( serialized !== null ) {
60- opts . deserialize ( serialized ) ;
61- }
62- GlobalOptions . context . set ( opts ) ;
63- return opts ;
64- }
65-
66- static get ( ) {
67- return GlobalOptions . context . get ( ) ;
68- }
69-
70- syntaxHighlighting = $state ( true ) ;
71- syntaxHighlightingThemeLight : BundledTheme = $state ( DEFAULT_THEME_LIGHT ) ;
72- syntaxHighlightingThemeDark : BundledTheme = $state ( DEFAULT_THEME_DARK ) ;
73- wordDiffs = $state ( true ) ;
74- lineWrap = $state ( true ) ;
75- omitPatchHeaderOnlyHunks = $state ( true ) ;
76- sidebarLocation : SidebarLocation = $state ( "left" ) ;
77-
78- private constructor ( ) {
79- $effect ( ( ) => {
80- this . save ( ) ;
81- } ) ;
82-
83- watchLocalStorage ( GlobalOptions . key , ( newValue ) => {
84- if ( newValue ) {
85- this . deserialize ( newValue ) ;
86- }
87- } ) ;
88- }
89-
90- get syntaxHighlightingTheme ( ) {
91- switch ( getEffectiveGlobalTheme ( ) ) {
92- case "dark" :
93- return this . syntaxHighlightingThemeDark ;
94- case "light" :
95- return this . syntaxHighlightingThemeLight ;
96- }
97- }
98-
99- private save ( ) {
100- if ( ! browser ) {
101- return ;
102- }
103- localStorage . setItem ( GlobalOptions . key , this . serialize ( ) ) ;
104- document . cookie = `${ GlobalOptions . key } =${ encodeURIComponent ( this . serializeCookie ( ) ) } ; path=/; max-age=31536000; SameSite=Lax` ;
105- }
106-
107- private serialize ( ) {
108- // eslint-disable-next-line @typescript-eslint/no-explicit-any
109- const cereal : any = {
110- syntaxHighlighting : this . syntaxHighlighting ,
111- omitPatchHeaderOnlyHunks : this . omitPatchHeaderOnlyHunks ,
112- wordDiff : this . wordDiffs ,
113- lineWrap : this . lineWrap ,
114- sidebarLocation : this . sidebarLocation ,
115- } ;
116- if ( this . syntaxHighlightingThemeLight !== DEFAULT_THEME_LIGHT ) {
117- cereal . syntaxHighlightingThemeLight = this . syntaxHighlightingThemeLight ;
118- }
119- if ( this . syntaxHighlightingThemeDark !== DEFAULT_THEME_DARK ) {
120- cereal . syntaxHighlightingThemeDark = this . syntaxHighlightingThemeDark ;
121- }
122- return JSON . stringify ( cereal ) ;
123- }
124-
125- private serializeCookie ( ) {
126- // eslint-disable-next-line @typescript-eslint/no-explicit-any
127- const cereal : any = {
128- sidebarLocation : this . sidebarLocation ,
129- } ;
130- return JSON . stringify ( cereal ) ;
131- }
132-
133- private deserialize ( serialized : string ) {
134- const jsonObject = JSON . parse ( serialized ) ;
135- if ( jsonObject . syntaxHighlighting !== undefined ) {
136- this . syntaxHighlighting = jsonObject . syntaxHighlighting ;
137- }
138- if ( jsonObject . syntaxHighlightingThemeLight !== undefined ) {
139- this . syntaxHighlightingThemeLight = jsonObject . syntaxHighlightingThemeLight as BundledTheme ;
140- } else {
141- this . syntaxHighlightingThemeLight = DEFAULT_THEME_LIGHT ;
142- }
143- if ( jsonObject . syntaxHighlightingThemeDark !== undefined ) {
144- this . syntaxHighlightingThemeDark = jsonObject . syntaxHighlightingThemeDark as BundledTheme ;
145- } else {
146- this . syntaxHighlightingThemeDark = DEFAULT_THEME_DARK ;
147- }
148- if ( jsonObject . omitPatchHeaderOnlyHunks !== undefined ) {
149- this . omitPatchHeaderOnlyHunks = jsonObject . omitPatchHeaderOnlyHunks ;
150- }
151- if ( jsonObject . wordDiff !== undefined ) {
152- this . wordDiffs = jsonObject . wordDiff ;
153- }
154- if ( jsonObject . lineWrap !== undefined ) {
155- this . lineWrap = jsonObject . lineWrap ;
156- }
157- if ( jsonObject . sidebarLocation !== undefined ) {
158- this . sidebarLocation = jsonObject . sidebarLocation ;
159- }
160- }
161- }
162-
16324export const staticSidebar = new MediaQuery ( "(width >= 64rem)" ) ;
16425
16526export type AddOrRemove = "add" | "remove" ;
0 commit comments