@@ -3,7 +3,16 @@ import type * as Monaco from 'modern-monaco/editor-core'
33import { isDark } from ' @vitejs/devtools-ui/composables/dark'
44import { Pane , Splitpanes } from ' splitpanes'
55import { computed , nextTick , onBeforeUnmount , onMounted , useTemplateRef , watch } from ' vue'
6- import { applyMonacoTheme , getMonaco , guessMonacoLanguage , setupMonacoScrollSync , syncMonacoEditorScrolls } from ' ~/composables/monaco'
6+ import {
7+ applyMonacoTheme ,
8+ createReadOnlyMonacoEditor ,
9+ getMonaco ,
10+ getMonacoWordWrap ,
11+ guessMonacoLanguage ,
12+ setModelLanguageIfNeeded ,
13+ setupMonacoScrollSync ,
14+ syncMonacoEditorScrolls ,
15+ } from ' ~/composables/monaco'
716import { settings } from ' ~/state/settings'
817import { calculateDiffWithWorker } from ' ~/worker/diff'
918
@@ -27,23 +36,6 @@ let toDecorations: Monaco.editor.IEditorDecorationsCollection | null = null
2736let disposeScrollSync: (() => void ) | null = null
2837let diffVersion = 0
2938
30- function createReadOnlyEditor(container : HTMLElement ) {
31- if (! monaco )
32- throw new Error (' Monaco is not initialized' )
33-
34- return monaco .editor .create (container , {
35- automaticLayout: true ,
36- fontFamily: ' \' Input Mono\' , \' FiraCode\' , monospace' ,
37- fontSize: 13 ,
38- lineNumbers: ' on' ,
39- minimap: { enabled: false },
40- readOnly: true ,
41- renderLineHighlight: ' none' ,
42- scrollBeyondLastLine: false ,
43- wordWrap: settings .value .codeviewerLineWrap ? ' on' : ' off' ,
44- })
45- }
46-
4739function setModelValue(model : Monaco .editor .ITextModel , value : string ) {
4840 if (model .getValue () !== value )
4941 model .setValue (value )
@@ -133,8 +125,12 @@ onMounted(async () => {
133125
134126 monaco = await getMonaco ()
135127
136- fromEditor = createReadOnlyEditor (fromEl .value )
137- toEditor = createReadOnlyEditor (toEl .value )
128+ fromEditor = createReadOnlyMonacoEditor (monaco , fromEl .value , {
129+ wordWrap: getMonacoWordWrap (settings .value .codeviewerLineWrap ),
130+ })
131+ toEditor = createReadOnlyMonacoEditor (monaco , toEl .value , {
132+ wordWrap: getMonacoWordWrap (settings .value .codeviewerLineWrap ),
133+ })
138134
139135 fromModel = monaco .editor .createModel (props .from , guessMonacoLanguage (props .from ))
140136 toModel = monaco .editor .createModel (props .to , guessMonacoLanguage (props .to ))
@@ -151,12 +147,14 @@ onMounted(async () => {
151147
152148 if (! props .oneColumn )
153149 syncMonacoEditorScrolls (toEditor , fromEditor )
150+
151+ await updateDiffDecorations (props .from , props .to , props .diff )
154152})
155153
156154watch (
157155 () => settings .value .codeviewerLineWrap ,
158156 (enabled ) => {
159- const wordWrap = enabled ? ' on ' : ' off '
157+ const wordWrap = getMonacoWordWrap ( enabled )
160158 fromEditor ?.updateOptions ({ wordWrap })
161159 toEditor ?.updateOptions ({ wordWrap })
162160 },
@@ -187,33 +185,36 @@ watch(
187185 { immediate: true },
188186)
189187
190- watch (
191- () => [props .from , props .to , props .diff ] as const ,
192- async ([from , to , diffEnabled ]) => {
193- if (! monaco || ! fromModel || ! toModel || ! fromDecorations || ! toDecorations )
194- return
188+ async function updateDiffDecorations(from : string , to : string , diffEnabled : boolean ) {
189+ if (! monaco || ! fromModel || ! toModel || ! fromDecorations || ! toDecorations )
190+ return
195191
196- const currentVersion = ++ diffVersion
192+ const currentVersion = ++ diffVersion
197193
198- setModelValue (fromModel , from )
199- setModelValue (toModel , to )
194+ setModelValue (fromModel , from )
195+ setModelValue (toModel , to )
200196
201- monaco . editor . setModelLanguage ( fromModel , guessMonacoLanguage (from ))
202- monaco . editor . setModelLanguage ( toModel , guessMonacoLanguage (to ))
197+ setModelLanguageIfNeeded ( monaco , fromModel , guessMonacoLanguage (from ))
198+ setModelLanguageIfNeeded ( monaco , toModel , guessMonacoLanguage (to ))
203199
204- fromDecorations .set ([])
205- toDecorations .set ([])
200+ fromDecorations .set ([])
201+ toDecorations .set ([])
206202
207- if (! diffEnabled || ! from )
208- return
203+ if (! diffEnabled || ! from || from === to )
204+ return
209205
210- const changes = await calculateDiffWithWorker (from , to )
211- if (currentVersion !== diffVersion )
212- return
206+ const changes = await calculateDiffWithWorker (from , to )
207+ if (currentVersion !== diffVersion )
208+ return
213209
214- applyDiffDecorations (changes )
210+ applyDiffDecorations (changes )
211+ }
212+
213+ watch (
214+ () => [props .from , props .to , props .diff ] as const ,
215+ ([from , to , diffEnabled ]) => {
216+ updateDiffDecorations (from , to , diffEnabled )
215217 },
216- { immediate: true },
217218)
218219
219220const leftPanelSize = computed (() => {
0 commit comments