Skip to content

Commit b900c97

Browse files
committed
fix: input events duplicating ref: microsoft/monaco-editor#4370
1 parent adfb5a3 commit b900c97

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

src/common.ts

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,46 @@ export function setValueForModel(base64Url: string, base64Content: string) {
8787
}
8888
}
8989

90+
function createEditorRootDOM(): HTMLDivElement | undefined {
91+
// Remove existing DOM
92+
document.getElementById("monaco-editor-root")?.remove();
93+
// Create editor DOM in body after div id="overlay"
94+
const overlay = document.getElementById("overlay");
95+
if (!overlay) return;
96+
const editorRoot = document.createElement("div");
97+
editorRoot.id = "monaco-editor-root";
98+
editorRoot.style = "width: 100%; height: 100vh; overflow: hidden";
99+
overlay.insertAdjacentElement("afterend", editorRoot);
100+
return editorRoot;
101+
}
102+
103+
function disposeAllEditors() {
104+
if (CodeStorage.editor) {
105+
CodeStorage.editor.dispose();
106+
CodeStorage.editor = undefined;
107+
}
108+
if (CodeStorage.diffEditor) {
109+
CodeStorage.diffEditor.getModifiedEditor().dispose();
110+
CodeStorage.diffEditor.getOriginalEditor().dispose();
111+
CodeStorage.diffEditor.dispose();
112+
CodeStorage.diffEditor = undefined;
113+
}
114+
const editorRoot = document.getElementById("monaco-editor-root");
115+
if (editorRoot) {
116+
editorRoot.innerHTML = "";
117+
}
118+
}
119+
90120
export function switchToDiffView(
91121
base64OriginalText: string,
92122
base64ModifiedText: string,
93123
base64UrlOriginal: string,
94124
base64UrlModified: string
95125
) {
96126
if (!CodeStorage.diffEditor) {
97-
CodeStorage.editor?.dispose();
98-
CodeStorage.editor = undefined;
127+
disposeAllEditors();
99128
CodeStorage.diffEditor = monaco.editor.createDiffEditor(
100-
document.getElementById("monaco-editor-root")!,
129+
createEditorRootDOM()!,
101130
{
102131
enableSplitViewResizing: false,
103132
automaticLayout: true,
@@ -146,18 +175,14 @@ export function switchToDiffView(
146175
}
147176

148177
export function switchToNormalView() {
149-
CodeStorage.diffEditor?.dispose();
150-
CodeStorage.diffEditor = undefined;
151-
CodeStorage.editor = monaco.editor.create(
152-
document.getElementById("monaco-editor-root")!,
153-
{
154-
theme: "vs-dark",
155-
automaticLayout: true,
156-
unicodeHighlight: {
157-
ambiguousCharacters: false,
158-
},
159-
}
160-
);
178+
disposeAllEditors();
179+
CodeStorage.editor = monaco.editor.create(createEditorRootDOM()!, {
180+
theme: "vs-dark",
181+
automaticLayout: true,
182+
unicodeHighlight: {
183+
ambiguousCharacters: false,
184+
},
185+
});
161186
CodeStorage.editor.getModel()?.dispose();
162187
CodeStorage.editor
163188
.getContribution("editor.contrib.iPadShowKeyboard")

0 commit comments

Comments
 (0)