Skip to content

Commit 9a83c1e

Browse files
committed
bugfix: attach content listener when model is already created in xml
1 parent 20b74bf commit 9a83c1e

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/components/editor/xml-validation.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ import { XMLValidator } from 'fast-xml-parser'
33

44
export function setupXMLValidation(monaco: typeof MonacoTypes) {
55
const markerId = 'xml-validation'
6-
const contentChangeListeners = new Map<MonacoTypes.editor.ITextModel, MonacoTypes.IDisposable>()
7-
8-
monaco.editor.onWillDisposeModel(model => {
9-
contentChangeListeners.delete(model)
10-
})
116

127
function validate(model: MonacoTypes.editor.ITextModel) {
138
const markers: MonacoTypes.editor.IMarkerData[] = []
@@ -33,7 +28,8 @@ export function setupXMLValidation(monaco: typeof MonacoTypes) {
3328
monaco.editor.setModelMarkers(model, markerId, markers)
3429
}
3530

36-
monaco.editor.onDidChangeModelLanguage(({ model }) => {
31+
const contentChangeListeners = new Map<MonacoTypes.editor.ITextModel, MonacoTypes.IDisposable>()
32+
function manageContentChangeListener(model: MonacoTypes.editor.ITextModel) {
3733
const isXml = model.getModeId() === 'xml'
3834
const listener = contentChangeListeners.get(model)
3935

@@ -48,5 +44,15 @@ export function setupXMLValidation(monaco: typeof MonacoTypes) {
4844
contentChangeListeners.delete(model)
4945
monaco.editor.setModelMarkers(model, markerId, [])
5046
}
47+
}
48+
49+
monaco.editor.onWillDisposeModel(model => {
50+
contentChangeListeners.delete(model)
51+
})
52+
monaco.editor.onDidChangeModelLanguage(({ model }) => {
53+
manageContentChangeListener(model)
54+
})
55+
monaco.editor.onDidCreateModel(model => {
56+
manageContentChangeListener(model)
5157
})
5258
}

0 commit comments

Comments
 (0)