forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonaco-diff-editor.js
More file actions
162 lines (144 loc) · 4.38 KB
/
monaco-diff-editor.js
File metadata and controls
162 lines (144 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// monaco-diff-editor.js - Monaco Editor diff view core functionality
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js';
// Editor state
let diffEditor = null;
let originalModel = null;
let modifiedModel = null;
let resizeObserver = null;
/**
* Initialize the Monaco diff editor
* @param {string} originalContent - Content for the original side
* @param {string} modifiedContent - Content for the modified side
* @param {Object} options - Optional configuration for the diff editor
* @returns {Object} The diff editor instance
*/
function initDiffEditor(originalContent, modifiedContent, options = {}) {
try {
// Default options
const editorOptions = {
renderSideBySide: false,
readOnly: true,
// Enable automatic layout adjustments
automaticLayout: true,
...options
};
// Create the diff editor if it doesn't exist yet
if (!diffEditor) {
diffEditor = monaco.editor.createDiffEditor(
document.getElementById("container"),
editorOptions
);
// Add resize handling
setupResizeHandling();
} else {
// Apply any new options
diffEditor.updateOptions(editorOptions);
}
// Create and set models
updateModels(originalContent, modifiedContent);
return diffEditor;
} catch (error) {
console.error("Error initializing diff editor:", error);
return null;
}
}
/**
* Setup proper resize handling for the editor
*/
function setupResizeHandling() {
window.addEventListener('resize', () => {
if (diffEditor) {
diffEditor.layout();
}
});
if (window.ResizeObserver && !resizeObserver) {
const container = document.getElementById('container');
resizeObserver = new ResizeObserver(() => {
if (diffEditor) {
diffEditor.layout()
}
});
if (container) {
resizeObserver.observe(container);
}
}
}
/**
* Create or update the models for the diff editor
* @param {string} originalContent - Content for the original side
* @param {string} modifiedContent - Content for the modified side
*/
function updateModels(originalContent, modifiedContent) {
try {
// Clean up existing models if they exist
if (originalModel) {
originalModel.dispose();
}
if (modifiedModel) {
modifiedModel.dispose();
}
// Create new models with the content
originalModel = monaco.editor.createModel(originalContent || "", "plaintext");
modifiedModel = monaco.editor.createModel(modifiedContent || "", "plaintext");
// Set the models to show the diff
if (diffEditor) {
diffEditor.setModel({
original: originalModel,
modified: modifiedModel,
});
}
} catch (error) {
console.error("Error updating models:", error);
}
}
/**
* Update the diff view with new content
* @param {string} originalContent - Content for the original side
* @param {string} modifiedContent - Content for the modified side
*/
function updateDiffContent(originalContent, modifiedContent) {
// If editor exists, update it
if (diffEditor && diffEditor.getModel()) {
const model = diffEditor.getModel();
// Update model values
model.original.setValue(originalContent || "");
model.modified.setValue(modifiedContent || "");
} else {
// Initialize if not already done
initDiffEditor(originalContent, modifiedContent);
}
}
/**
* Get the current diff editor instance
* @returns {Object|null} The diff editor instance or null
*/
function getEditor() {
return diffEditor;
}
/**
* Dispose of the editor and models to clean up resources
*/
function dispose() {
if (resizeObserver) {
resizeObserver.disconnect();
resizeObserver = null;
}
if (originalModel) {
originalModel.dispose();
originalModel = null;
}
if (modifiedModel) {
modifiedModel.dispose();
modifiedModel = null;
}
if (diffEditor) {
diffEditor.dispose();
diffEditor = null;
}
}
export {
initDiffEditor,
updateDiffContent,
getEditor,
dispose
};