Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<vaadin.version>14.8.9</vaadin.version>
<vaadin.version>14.10.3</vaadin.version>
<jetty.version>9.4.36.v20210114</jetty.version>
</properties>

Expand Down Expand Up @@ -155,6 +155,32 @@
</plugin>
</plugins>
</pluginManagement>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<!-- Generated file that shouldn't be included in add-ons -->
<excludes>
<exclude>META-INF/VAADIN/config/flow-build-info.json</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-maven-plugin</artifactId>
<version>${vaadin.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-frontend</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,28 @@
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.server.VaadinService;
import elemental.json.Json;
import elemental.json.JsonObject;
import java.util.Map;

@SuppressWarnings("serial")
@JsModule("./code-viewer.ts")
@NpmPackage(value = "lit", version = "2.5.0")
class SourceCodeView extends Div implements HasSize {

private final Element codeViewer;

public SourceCodeView(String sourceUrl) {
this(sourceUrl, null);
}

public SourceCodeView(String sourceUrl, Map<String, String> properties) {
String url = translateSource(sourceUrl);
Element codeViewer = new Element("code-viewer");
codeViewer = new Element("code-viewer");
getElement().appendChild(codeViewer);
getElement().getStyle().set("display", "flex");
codeViewer.getStyle().set("flex-grow", "1");
setProperties(properties);
addAttachListener(
ev -> {
codeViewer.executeJs("this.fetchContents($0,$1)", url, "java");
Expand All @@ -58,4 +68,16 @@ private static String translateSource(String url) {
}
return url;
}

private void setProperties(Map<String, String> properties) {
if (properties != null) {
JsonObject env = Json.createObject();
properties.forEach((k, v) -> {
if (v != null) {
env.put(k, Json.create(v));
}
});
codeViewer.setPropertyJson("env", env);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import com.vaadin.flow.component.Composite;
import com.vaadin.flow.component.splitlayout.SplitLayout;
import com.vaadin.flow.component.splitlayout.SplitLayout.Orientation;
import com.vaadin.flow.server.Version;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("serial")
class SplitLayoutDemo extends Composite<SplitLayout> {
Expand All @@ -31,8 +34,12 @@ class SplitLayoutDemo extends Composite<SplitLayout> {

public SplitLayoutDemo(Component demo, String sourceUrl) {
getContent().setOrientation(Orientation.HORIZONTAL);
code = new SourceCodeView(sourceUrl);

Map<String, String> properties = new HashMap<>();
properties.put("vaadin", VaadinVersion.getVaadinVersion());
properties.put("flow", Version.getFullVersion());

code = new SourceCodeView(sourceUrl, properties);
getContent().addToPrimary(demo);
getContent().addToSecondary(code);
getContent().setSizeFull();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.flowingcode.vaadin.addons.demo;

import com.vaadin.flow.server.Version;
import java.util.Objects;
import java.util.Properties;
import org.slf4j.LoggerFactory;

public class VaadinVersion {

private VaadinVersion() {
throw new UnsupportedOperationException();
}

public static String getVaadinVersion() {
try {
Properties pom = new Properties();
pom.load(VaadinVersion.class
.getResourceAsStream("/META-INF/maven/com.vaadin/vaadin-core/pom.properties"));
return Objects.requireNonNull((String) pom.get("version"));
} catch (Exception e) {
LoggerFactory.getLogger(Version.class.getName())
.warn("Unable to determine Vaadin version number", e);
return null;
}
}

}
95 changes: 94 additions & 1 deletion src/main/resources/META-INF/resources/frontend/code-viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export class CodeViewer extends LitElement {

private __license : Element[] = [];

env: any = {};

createRenderRoot() {
return this;
}
Expand Down Expand Up @@ -244,7 +246,57 @@ pre[class*="language-"] {
}

cleanupCode(text: string) : string {
return text.split('\n').filter(line=>
let lines : (string|null)[] = text.split('\n');
let guards : (string|undefined)[] = [];
let stack : string[] = [];

let __elif = (guard:(string|undefined),value:(string|undefined)) => {
return guard=='isfalse' ? value : 'wastrue';
};

let transition = (top:string, next:string) => {
let result = stack.pop()==top;
stack.push(result?next:'error');
return result;
};

for (let i=0;i<lines.length;i++) {
let m = lines[i]!.match("^\\s*//\\s*#(?<directive>\\w+)\\s*(?<line>.*)");
if (m && m.groups) {
let line = m.groups.line;
switch (m.groups.directive) {
case 'if':
stack.push('if');
guards.push(this.__eval(line));
lines[i]=null;
break;
case 'else':
if (!transition('if', 'else')) break;
guards.push(__elif(guards.pop(), 'istrue'));
lines[i]=null;
break;
case 'elif':
if (!transition('if', 'if')) break;
guards.push(__elif(guards.pop(), this.__eval(line)));
lines[i]=null;
break;
case 'endif':
stack.pop();
guards.pop();
lines[i]=null;
}
}

if (!guards.every(x=>x=='istrue')) {
lines[i] = null;
}
}

return lines.filter(line=>line!==null)
.map(line=>{
let m= line!.match("^(?<spaces>\\s*)//\\s*show-source\\s(?<line>.*)");
return m?m.groups!.spaces+m.groups!.line : line!;
}).filter(line=>
!line.match("//\\s*hide-source(\\s|$)")
&& !line.startsWith('@Route')
&& !line.startsWith('@PageTitle')
Expand All @@ -265,4 +317,45 @@ pre[class*="language-"] {
.replace(/'/g, "&#039;");
}

__eval(line: string) : string|undefined {
let expr = line.split(' ');
if (expr.length==3) {
const value = this.env[expr[0]];
if (value==undefined) {
return 'isfalse';
}

let op = (a:string,b:string) => {
switch (expr[1]) {
case 'lt': return this.__compare(a,b)<0;
case 'le': return this.__compare(a,b)<=0;
case 'eq': return this.__compare(a,b)==0;
case 'ge': return this.__compare(a,b)>=0;
case 'gt': return this.__compare(a,b)>0;
case 'ne': return this.__compare(a,b)!=0;;
default: return undefined;
}};

switch (op(value, expr[2])) {
case true: return 'istrue';
case false: return 'isfalse';
}
}
return undefined;
}

__compare(a: string, b:string) : number {
let aa = a.split('.');
let bb = b.split('.');
for (let i=0; i<Math.min(aa.length,bb.length); i++) {
let ai = parseInt(aa[i]);
let bi = parseInt(bb[i]);
if (ai<bi) return -1;
if (ai>bi) return +1;
}
if (aa.length<bb.length) return -1;
if (aa.length<bb.length) return +1;
return 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public class SampleDemoDefault extends Div {

public SampleDemoDefault() {
add(new Span("Demo component with defaulted @DemoSource annotation"));
// show-source System.out.println("this line will be displayed in the code snippet");
this.getClass(); // hide-source (this line will not be displayed in the code snippet)
// #if vaadin ge 23
// show-source System.out.println("conditional code for Vaadin 23+");
// #elif vaadin ge 14
// show-source System.out.println("conditional code for Vaadin 14-22");
// #endif
}
}