MCPcopy Index your code
hub / github.com/wavetermdev/waveterm / MonacoDiffViewer

Function MonacoDiffViewer

frontend/app/monaco/monaco-react.tsx:127–198  ·  view source on GitHub ↗
({ original, modified, language, path, options }: DiffViewerProps)

Source from the content-addressed store, hash-verified

125};
126
127export function MonacoDiffViewer({ original, modified, language, path, options }: DiffViewerProps) {
128 const divRef = useRef<HTMLDivElement>(null);
129 const diffRef = useRef<MonacoTypes.editor.IStandaloneDiffEditor | null>(null);
130
131 // Create once
132 useEffect(() => {
133 loadMonaco();
134
135 const el = divRef.current;
136 if (!el) return;
137
138 const origUri = monaco.Uri.parse(`wave://diff/${encodeURIComponent(path)}.orig`);
139 const modUri = monaco.Uri.parse(`wave://diff/${encodeURIComponent(path)}.mod`);
140
141 const originalModel = monaco.editor.createModel(original, language, origUri);
142 const modifiedModel = monaco.editor.createModel(modified, language, modUri);
143
144 const diff = monaco.editor.createDiffEditor(el, options);
145 diffRef.current = diff;
146
147 diff.setModel({ original: originalModel, modified: modifiedModel });
148
149 return () => {
150 diff.setModel(null);
151 diff.dispose();
152 originalModel.dispose();
153 modifiedModel.dispose();
154 diffRef.current = null;
155 };
156 }, []);
157
158 useEffect(() => {
159 const diff = diffRef.current;
160 const el = divRef.current;
161 if (!diff || !el) return;
162
163 const debouncedLayout = debounce(100, () => {
164 diff.layout();
165 });
166 const resizeObserver = new ResizeObserver(debouncedLayout);
167 resizeObserver.observe(el);
168
169 return () => {
170 resizeObserver.disconnect();
171 debouncedLayout.cancel();
172 };
173 }, []);
174
175 // Update models on prop change
176 useEffect(() => {
177 const diff = diffRef.current;
178 if (!diff) return;
179 const model = diff.getModel();
180 if (!model) return;
181
182 if (model.original.getValue() !== original) model.original.setValue(original);
183 if (model.modified.getValue() !== modified) model.modified.setValue(modified);
184

Callers

nothing calls this directly

Calls 3

loadMonacoFunction · 0.90
setModelMethod · 0.80
disposeMethod · 0.45

Tested by

no test coverage detected