Skip to content

Commit d5d77b7

Browse files
javier-godoymlopezFC
authored andcommitted
fix: replace gist-it with polymer-code-highlighter
Close #6
1 parent c9c0b5d commit d5d77b7

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

src/main/java/com/flowingcode/vaadin/addons/demo/SourceCodeView.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,49 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.demo;
2121

22-
import com.vaadin.flow.component.Composite;
23-
import com.vaadin.flow.component.html.IFrame;
22+
import com.vaadin.flow.component.AttachEvent;
23+
import com.vaadin.flow.component.Component;
24+
import com.vaadin.flow.component.HasSize;
25+
import com.vaadin.flow.component.Tag;
26+
import com.vaadin.flow.component.dependency.JsModule;
27+
import com.vaadin.flow.component.dependency.NpmPackage;
28+
import com.vaadin.flow.component.html.Div;
2429

2530
@SuppressWarnings("serial")
26-
class SourceCodeView extends Composite<IFrame> {
31+
@NpmPackage(value = "polymer-code-highlighter", version = "1.0.5")
32+
@JsModule("polymer-code-highlighter/code-highlighter.js")
33+
class SourceCodeView extends Div implements HasSize {
34+
35+
private String sourceUrl;
2736

2837
public SourceCodeView(String sourceUrl) {
29-
getContent().getElement().setAttribute("frameborder", "0");
30-
getContent().setMinHeight("0");
31-
getContent().setMinWidth("0");
32-
getContent().getElement().setAttribute("srcdoc", getSrcdoc(sourceUrl));
33-
getContent().setSizeFull();
38+
this.sourceUrl = translateSource(sourceUrl);
3439
}
3540

36-
private String getSrcdoc(String sourceUrl) {
37-
return "<html style=\"overflow-y:hidden; height:100%;\"><body style=\"overflow-y: scroll; height:100%;\"><script src=\"https://gist-it.appspot.com/"
38-
+ sourceUrl
39-
+ "\"></script></body></html>";
41+
@Override
42+
protected void onAttach(AttachEvent attachEvent) {
43+
super.onAttach(attachEvent);
44+
45+
getElement().executeJs(
46+
"var self=this;"
47+
+ "var xhr = new XMLHttpRequest();"
48+
+ "xhr.onreadystatechange = function() {"
49+
+ "if (this.readyState == 4 && this.status == 200) {"
50+
+ " var elem = document.createElement('code-highlighter');"
51+
+ " elem.setAttribute('lang','java');"
52+
+ " elem.innerHTML = this.responseText;"
53+
+ " self.appendChild(elem);"
54+
+ "}};"
55+
+ "xhr.open('GET', $0, true);"
56+
+ "xhr.send();", sourceUrl);
4057
}
4158

42-
public void setSizeFull() {
43-
getContent().setSizeFull();
59+
private static String translateSource(String url) {
60+
if (url.startsWith("https://github.com")) {
61+
url = url.replaceFirst("github.com", "raw.githubusercontent.com");
62+
url = url.replaceFirst("/blob", "");
63+
}
64+
return url;
4465
}
66+
4567
}

src/main/resources/META-INF/resources/frontend/styles/commons-demo/shared-styles.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,12 @@
2020
vaadin-checkbox.smallcheckbox {
2121
font-size: small;
2222
}
23+
24+
code-highlighter pre.vrPre {
25+
background: unset;
26+
overflow-x: unset;
27+
}
28+
29+
code-highlighter code {
30+
background: unset;
31+
}

0 commit comments

Comments
 (0)