Skip to content

Commit 7ab7c71

Browse files
tabs: fix tab with code snippets regression (#1494)
1 parent d616b90 commit 7ab7c71

7 files changed

Lines changed: 68 additions & 37 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Fix: Tab properly displays selected content (instead of previously selected content)
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class JsClass {
2-
constructor() {
3-
usefulAction()
4-
}
2+
constructor() {
3+
usefulAction();
4+
}
55
}
66

7-
export default JsClass
7+
export default JsClass;

znai-reactjs/src/doc-elements/code-snippets/SimpleCodeSnippet.jsx

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,12 @@ class SimpleCodeSnippet extends React.Component {
3838
constructor(props) {
3939
super(props);
4040

41-
this.processProps(props);
4241
this.state = {
4342
clickedReadMore: false,
4443
hasHighlightedText: false,
4544
};
4645
}
4746

48-
// handles changes during preview
49-
componentDidUpdate(prevProps) {
50-
// Only process props if they actually changed
51-
if (
52-
prevProps.tokens !== this.props.tokens ||
53-
prevProps.linesOfCode !== this.props.linesOfCode ||
54-
prevProps.highlight !== this.props.highlight
55-
) {
56-
this.processProps(this.props);
57-
// If you need to update state based on props changes, you can do it here
58-
// but be careful to avoid infinite loops
59-
}
60-
}
61-
62-
processProps({ tokens, linesOfCode, highlight }) {
63-
this.linesOfTokens = !linesOfCode ? splitTokensIntoLines(tokens) : linesOfCode;
64-
65-
// highlight is either a single line index/substring or a collection of line indexes and substrings
66-
this.highlight = convertToList(highlight);
67-
}
68-
6947
componentDidMount() {
7048
addHighlightedTextListener(this);
7149
}
@@ -99,14 +77,16 @@ class SimpleCodeSnippet extends React.Component {
9977
render() {
10078
this.hiddenLinesContainerRef = React.createRef();
10179
const { clickedReadMore } = this.state;
102-
const { wrap, isPresentation, slideIdx, references } = this.props;
80+
const { wrap, isPresentation, slideIdx, references, tokens, linesOfCode, highlight } = this.props;
10381

10482
// slideIdx === 0 means no highlights, 1 - first highlight, etc
10583
const highlightIsVisible = !isPresentation || slideIdx > 0;
10684

107-
const linesOfTokens = this.linesOfTokens;
85+
const linesOfTokens = !linesOfCode ? splitTokensIntoLines(tokens) : linesOfCode;
86+
const highlightList = convertToList(highlight);
87+
10888
const visibleLines =
109-
this.limitLines(this.props) && !clickedReadMore && !isPresentation
89+
this.limitLines(linesOfTokens, this.props) && !clickedReadMore && !isPresentation
11090
? linesOfTokens.slice(0, this.readMoreVisibleLines(this.props))
11191
: linesOfTokens;
11292

@@ -115,7 +95,7 @@ class SimpleCodeSnippet extends React.Component {
11595
const linesToRender = this.processLinesToRender(visibleLines);
11696

11797
const mergedReferences = mergeWithGlobalDocReferences(references);
118-
const isHighlightedByIdx = highlightIsVisible ? linesToRender.map((_, lineIdx) => this.isHighlighted(lineIdx)) : [];
98+
const isHighlightedByIdx = highlightIsVisible ? linesToRender.map((_, lineIdx) => this.isHighlighted(highlightList, lineIdx)) : [];
11999

120100
return (
121101
<pre>
@@ -157,9 +137,11 @@ class SimpleCodeSnippet extends React.Component {
157137

158138
renderReadMore() {
159139
const { clickedReadMore } = this.state;
160-
const { isPresentation } = this.props;
140+
const { isPresentation, tokens, linesOfCode } = this.props;
141+
142+
const linesOfTokens = !linesOfCode ? splitTokensIntoLines(tokens) : linesOfCode;
161143

162-
if (isPresentation || !this.limitLines(this.props)) {
144+
if (isPresentation || !this.limitLines(linesOfTokens, this.props)) {
163145
return null;
164146
}
165147

@@ -208,20 +190,20 @@ class SimpleCodeSnippet extends React.Component {
208190
);
209191
};
210192

211-
limitLines(props) {
212-
return this.linesOfTokens.length >= this.readMoreVisibleLines(props) && props.readMore;
193+
limitLines(linesOfTokens, props) {
194+
return linesOfTokens.length >= this.readMoreVisibleLines(props) && props.readMore;
213195
}
214196

215197
readMoreVisibleLines(props) {
216198
return props.readMoreVisibleLines || 8;
217199
}
218200

219-
isHighlighted(lineIdx) {
201+
isHighlighted(highlightList, lineIdx) {
220202
const { meta, isPresentation, slideIdx, revealLineStop } = this.props;
221203

222-
const highlightSliceIdx = calcHighlightSliceIdx(this.highlight);
204+
const highlightSliceIdx = calcHighlightSliceIdx(highlightList);
223205

224-
const highlight = !isPresentation ? this.highlight : this.highlight.slice(0, highlightSliceIdx);
206+
const highlight = !isPresentation ? highlightList : highlightList.slice(0, highlightSliceIdx);
225207

226208
if (!highlight) {
227209
return false;

znai-tests/src/test/groovy/pages/DocContent.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class DocContent {
2626
def mainPanelScrollTop = mainPanel.scrollTop
2727
def mermaidClickableNodes = $(".znai-mermaid .clickable")
2828

29+
def tabNames = $(".tabs-area .tab-name")
30+
def activeTabName = $(".tabs-area .tab-name.active")
31+
def tabsSnippet = $(".tabs-area .snippet")
32+
2933
void clickMermaidNode() {
3034
// GeckoDriver can't click elements inside SVG (even HTML inside foreignObject),
3135
// use DOM .click() which creates a trusted event
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Tabs Switching
2+
3+
Tabs with code snippets for e2e testing of tab switching.
4+
5+
````tabs
6+
Java:
7+
```java
8+
System.out.println("hello from java");
9+
```
10+
11+
Python:
12+
```python
13+
print("hello from python")
14+
```
15+
16+
Ruby:
17+
```ruby
18+
puts "hello from ruby"
19+
```
20+
````

znai-tests/src/test/groovy/sampledoc/toc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ outside-chapter-file.md
22
chapter-one
33
links
44
target
5+
tabs
56
chapter-two
67
doxygen-from-zip
78
chapter-three

znai-tests/src/test/groovy/scenarios/sampleDoc.groovy

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,29 @@ scenario("check redirect page") {
4545
docContent.title.waitToBe == "Links"
4646
}
4747

48+
scenario("switch tabs and validate snippet content") {
49+
def snippets = [
50+
Java : 'System.out.println("hello from java");',
51+
Python: 'print("hello from python")',
52+
Ruby : 'puts "hello from ruby"',
53+
]
54+
55+
def assertActiveTab = { String name ->
56+
docContent.activeTabName.waitTo == name
57+
docContent.tabsSnippet.waitTo == snippets[name]
58+
}
59+
60+
standardView.tocItems.get("Tabs").click()
61+
docContent.title.waitTo == "Tabs"
62+
63+
assertActiveTab("Java")
64+
65+
["Python", "Ruby", "Java"].each { name ->
66+
docContent.tabNames.get(name).click()
67+
assertActiveTab(name)
68+
}
69+
}
70+
4871
scenario("validate uploads files") {
4972
def baseUrl = "http://localhost:$port/preview"
5073

0 commit comments

Comments
 (0)