Skip to content

Commit b5c1513

Browse files
javier-godoypaodb
authored andcommitted
feat: add highlightOnClick method
1 parent 2c153a5 commit b5c1513

4 files changed

Lines changed: 14 additions & 4 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Commonly used boilerplate code from source snippets is automatically hidden:
113113
- `@com.flowingcode.vaadin.addons.demo.DemoSource`
114114
- `@java.lang.SupressWarning`
115115
- `@org.junit.Ignore`
116-
- Calls to `SourceCodeViewer.highlightOnHover` and `SourceCodeViewer.highlight`
116+
- Calls to `SourceCodeViewer.highlight`, `SourceCodeViewer.highlightOnHover` and `SourceCodeViewer.highlightOnClick`
117117

118118
This feature cannot be disabled.
119119

@@ -167,6 +167,7 @@ This feature supports highlighting a source code fragment in order to emphasize
167167
The highlighted fragment is automatically scrolled into view.
168168

169169
A fragment is highlighted either by calling `SourceCodeViewer.highlight(name)` or when hovering a component that has been configured with `SourceCodeViewer.highlightOnHover(component,name)`, where `name` is the name of the fragment. `SourceCodeViewer.highlight(null)` turn off the highlighting.
170+
`SourceCodeViewer.highlightOnClick` allows configuring a click listener that turns highlight on.
170171

171172
In the source code, a fragment is delimited by `// begin-block name` and `// end-block` comments. Nested fragments are not supported.
172173
The `// begin-block` and `// end-block` comments are removed after post-processing.

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
package com.flowingcode.vaadin.addons.demo;
2121

2222
import com.flowingcode.vaadin.addons.DevSourceRequestHandler;
23+
import com.vaadin.flow.component.ClickNotifier;
2324
import com.vaadin.flow.component.Component;
25+
import com.vaadin.flow.component.HasElement;
2426
import com.vaadin.flow.component.HasSize;
2527
import com.vaadin.flow.component.UI;
2628
import com.vaadin.flow.component.dependency.JsModule;
@@ -89,6 +91,12 @@ public static void highlightOnHover(Component c, String id) {
8991
});
9092
}
9193

94+
public static <T extends HasElement & ClickNotifier<?>> void highlightOnClick(T c, String id) {
95+
c.addClickListener(ev -> {
96+
c.getElement().executeJs("Vaadin.Flow.fcCodeViewerConnector.highlight($0)", id);
97+
});
98+
}
99+
92100
public static void highlight(String id) {
93101
UI.getCurrent().getElement().executeJs("Vaadin.Flow.fcCodeViewerConnector.highlight($0)", id);
94102
}

src/main/resources/META-INF/resources/frontend/code-viewer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ pre[class*="language-"] {
334334
&& !line.startsWith('@Ignore')
335335
&& !line.startsWith('package ')
336336
&& !line.trim().startsWith('SourceCodeViewer.highlightOnHover(')
337+
&& !line.trim().startsWith('SourceCodeViewer.highlightOnClick(')
337338
&& !line.trim().startsWith('SourceCodeViewer.highlight(')
338339
&& line != 'import com.vaadin.flow.router.PageTitle;'
339340
&& line != 'import com.vaadin.flow.router.Route;'

src/test/java/com/flowingcode/vaadin/addons/demo/SampleDemoHighlight.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public SampleDemoHighlight() {
5454
HorizontalLayout hl = new HorizontalLayout();
5555

5656
// begin-block button
57-
hl.add(new Button("Click me", ev -> {
58-
SourceCodeViewer.highlight("button");
59-
}));
57+
Button button = new Button("Click me");
58+
SourceCodeViewer.highlightOnClick(button, "button");
59+
add(button);
6060
// end-block
6161

6262
hl.add(new Button("Highlight Off", ev -> {

0 commit comments

Comments
 (0)