({ original, modified, language, path, options }: DiffViewerProps)
| 125 | }; |
| 126 | |
| 127 | export 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 |
nothing calls this directly
no test coverage detected