Skip to content

Commit c34b111

Browse files
committed
ASTDiff: Fix SingleMonacoContent
1 parent 7e16ef5 commit c34b111

File tree

2 files changed

+93
-20
lines changed

2 files changed

+93
-20
lines changed

src/main/java/gui/webdiff/viewers/monaco/SingleMonacoContent.java

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package gui.webdiff.viewers.monaco;
22

3+
import gui.webdiff.WebDiff;
4+
import org.rendersnake.DocType;
35
import org.rendersnake.HtmlCanvas;
46
import org.rendersnake.Renderable;
57

68
import java.io.IOException;
79

10+
import static org.rendersnake.HtmlAttributesFactory.*;
11+
812
public class SingleMonacoContent implements Renderable {
913

1014
private final boolean isAdded;
@@ -18,28 +22,70 @@ public SingleMonacoContent(boolean isAdded, String path, String escapedContent)
1822
}
1923

2024
@Override
21-
public void renderOn(HtmlCanvas htmlCanvas) throws IOException {
22-
htmlCanvas.write(singleMonaco(isAdded, path, escapedContent));
25+
public void renderOn(HtmlCanvas html) throws IOException {
26+
String boxColor = isAdded ? "#d4edda" : "#f8d7da";
27+
String textColor = isAdded ? "#155724" : "#721c24";
28+
String borderColor = isAdded ? "#c3e6cb" : "#f5c6cb";
29+
String editorId = "monaco-editor-" + Math.abs(path.hashCode());
30+
31+
html
32+
.render(DocType.HTML5)
33+
.html(lang("en").class_("h-100"))
34+
.render(new SingleMonacoHeader(path, isAdded))
35+
.div(style("flex: 1 1 auto; width: 100%;"))
36+
.div(id(editorId).style("width:100%; height:100%;"))
37+
._div()
38+
._div()
39+
.div(style("height: 10px; flex-shrink: 0; background:#eee;"))
40+
._div()
41+
.script().content(
42+
"loadSingleMonacoEditor({ id: '" + editorId + "', value: `" + escapedContent + "`, language: 'java' });"
43+
)
44+
._body()
45+
._html();
2346
}
2447

25-
private static String singleMonaco(boolean isAdded, String path, String escapedContent) {
26-
String boxColor = isAdded ? "#d4edda" : "#f8d7da"; // Green or red
27-
String textColor = isAdded ? "#155724" : "#721c24"; // Dark green or dark red
28-
String borderColor = isAdded ? "#c3e6cb" : "#f5c6cb";
48+
private static class SingleMonacoHeader implements Renderable {
49+
private final String path;
50+
private final boolean isAdded;
2951

30-
String headerHtml = "<div style=\"background-color:" + boxColor + ";" +
31-
"color:" + textColor + ";" +
32-
"border: 1px solid " + borderColor + ";" +
33-
"padding: 10px; border-radius: 5px; font-weight: bold;\">" +
34-
path +
35-
"</div>";
36-
37-
return "<div style=\"padding:10px\">" +
38-
headerHtml +
39-
"<pre style=\"white-space: pre-wrap; background:#f8f8f8; padding:10px; border-radius:5px; margin-top:10px;\">" +
40-
escapedContent +
41-
"</pre>" +
42-
"</div>";
52+
public SingleMonacoHeader(String path, boolean isAdded) {
53+
this.path = path;
54+
this.isAdded = isAdded;
55+
}
56+
57+
@Override
58+
public void renderOn(HtmlCanvas html) throws IOException {
59+
String boxColor = isAdded ? "#d4edda" : "#f8d7da";
60+
String textColor = isAdded ? "#155724" : "#721c24";
61+
String borderColor = isAdded ? "#c3e6cb" : "#f5c6cb";
62+
63+
html
64+
.head().macros().javascript("/dist/single-monaco.js")
65+
.meta(charset("utf8"))
66+
.meta(name("viewport").content("width=device-width, initial-scale=1.0"))
67+
.title().content("RefactoringMiner")
68+
.macros().stylesheet(WebDiff.BOOTSTRAP_CSS_URL)
69+
.macros().stylesheet("/dist/monaco.css")
70+
.macros().javascript(WebDiff.JQUERY_JS_URL)
71+
.macros().javascript("https://code.jquery.com/ui/1.12.1/jquery-ui.min.js")
72+
.macros().javascript(WebDiff.BOOTSTRAP_JS_URL)
73+
.macros().javascript("/monaco/min/vs/loader.js")
74+
.style().content(
75+
"html, body { margin: 0; padding: 0; height: 100%; overflow: hidden; }"
76+
)
77+
._head()
78+
79+
// Top bar with back button and file path
80+
.body(style("margin:0; height:100%; display:flex; flex-direction:column;"))
81+
.div(style("flex: 0 0 auto; padding: 10px; display: flex; align-items: center; gap: 10px;" +
82+
"background-color:" + boxColor + ";" +
83+
"color:" + textColor + ";" +
84+
"border-bottom: 1px solid " + borderColor + ";"))
85+
.a(href("javascript:history.back()").class_("btn btn-secondary btn-sm"))
86+
.content("← Back")
87+
.div(style("font-weight: bold;")).content(path)
88+
._div();
89+
}
4390
}
4491
}
45-
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function loadSingleMonacoEditor({ id, value, language = 'java' }) {
2+
if (!window.monaco) {
3+
require.config({
4+
paths: { vs: 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.39.0/min/vs' }
5+
});
6+
require(['vs/editor/editor.main'], function () {
7+
_createEditor(id, value, language);
8+
});
9+
} else {
10+
_createEditor(id, value, language);
11+
}
12+
}
13+
14+
function _createEditor(id, value, language) {
15+
const el = document.getElementById(id);
16+
if (el) {
17+
monaco.editor.create(el, {
18+
value: value,
19+
language: language,
20+
readOnly: true,
21+
automaticLayout: true,
22+
theme: 'vs',
23+
scrollBeyondLastLine: false,
24+
minimap: { enabled: false },
25+
});
26+
}
27+
}

0 commit comments

Comments
 (0)