Skip to content

Commit 513271c

Browse files
authored
Merge pull request #2291 from Sefaria/bug/sc-25530/current-place-in-toc-not-displaying-properly
fix: [sc-25530] Current place in TOC not displaying properly in the resource panel TOC. Force merged due to flaky playwright tests
2 parents b6bd215 + 1227d42 commit 513271c

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

static/js/BookPage.jsx

+27-5
Original file line numberDiff line numberDiff line change
@@ -557,10 +557,31 @@ class SchemaNode extends Component {
557557
constructor(props) {
558558
super(props);
559559
this.state = {
560-
// Collapse nodes below top level, and those that aren't default or makred includedSections
561-
collapsed: "nodes" in props.schema && !(props.topLevel || props.disableSubCollapse) ? props.schema.nodes.map(node => !(node.default || node.includeSections)) : []
560+
collapsed: this.getCollapsedState(this.props.schema, this.props.topLevel, this.props.disableSubCollapse)
562561
};
563562
}
563+
componentDidUpdate(prevProps) {
564+
if (this.props.currentlyVisibleRef !== prevProps.currentlyVisibleRef) {
565+
this.setState({collapsed: this.getCollapsedState(this.props.schema, this.props.topLevel, this.props.disableSubCollapse)});
566+
}
567+
}
568+
shouldNodeCollapse(node) {
569+
// Determine 'collapsed' state for node.
570+
// A node should be collapsed if it is not the currently visible node and if it is not a default node.
571+
const fullNodeRef = this.props.refPath + (node.default ? "" : `, ${node.title}`);
572+
const currentlyVisible = Sefaria.refContains(fullNodeRef, this.props.currentlyVisibleRef);
573+
return !currentlyVisible && !node.default && !node.includeSections;
574+
}
575+
getCollapsedState(schema, topLevel, disableSubCollapse) {
576+
// Determine 'collapsed' state for each node in schema.
577+
// If no children or topLevel, return [] which is equivalent to no nodes collapsed.
578+
// Otherwise, return array of booleans based on call to `shouldNodeCollapse` for each node.
579+
if (topLevel || disableSubCollapse || !("nodes" in schema)) {
580+
return [];
581+
} else {
582+
return schema.nodes.map(this.shouldNodeCollapse);
583+
}
584+
}
564585
toggleCollapse(i) {
565586
if(this.props.disableSubCollapse) return;
566587

@@ -598,7 +619,7 @@ class SchemaNode extends Component {
598619
} else { //we do have subcontent
599620
let content = this.props.schema.nodes.map(function(node, i) {
600621
let path;
601-
if (node.nodeType == "ArrayMapNode") {
622+
if (node.nodeType === "ArrayMapNode") {
602623
//ArrayMapNode content
603624
path = this.props.refPath + ", " + node.title;
604625
return <ArrayMapNode schema={node} currentlyVisibleRef={this.props.currentlyVisibleRef} currentlyVisibleSectionRef={this.props.currentlyVisibleSectionRef} key={path}/>;
@@ -700,15 +721,16 @@ class JaggedArrayNode extends Component {
700721
render() {
701722
const offset = this.props.schema?.index_offsets_by_depth?.['1'] || 0;
702723
if ("toc_zoom" in this.props.schema) {
703-
let zoom = this.props.schema.toc_zoom - 1;
724+
const zoom = this.props.schema.toc_zoom - 1;
725+
const currentZoomedOutRef = this.props.currentlyVisibleSectionRef && Sefaria.zoomOutRef(this.props.currentlyVisibleSectionRef, zoom);
704726
return (<JaggedArrayNodeSection
705727
depth={this.props.schema.depth - zoom}
706728
sectionNames={this.props.schema.sectionNames.slice(0, -zoom)}
707729
addressTypes={this.props.schema.addressTypes.slice(0, -zoom)}
708730
contentCounts={this.props.schema.content_counts}
709731
refPath={this.props.refPath}
710732
currentlyVisibleRef={this.props.currentlyVisibleRef}
711-
currentlyVisibleSectionRef={this.props.currentlyVisibleSectionRef}
733+
currentlyVisibleSectionRef={currentZoomedOutRef}
712734
offset={offset}
713735
/>);
714736
}

static/js/sefaria/sefaria.js

+9
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,15 @@ Sefaria = extend(Sefaria, {
292292
return oref ? oref.sectionRef : null;
293293

294294
},
295+
zoomOutRef: function(ref, zoom=1) {
296+
// go up `zoom` levels in the ref's sections and toSections
297+
// for example, if ref == "Ramban on Genesis, Introduction 2" and zoom == 1, this returns "Ramabn on Genesis, Introduction"
298+
// if ref == "Ramban on Genesis, Introduction 2" and zoom == 2, this returns "Ramban on Genesis, Introduction" because we're only zooming out on the ref's sections/toSections
299+
const pRef = Sefaria.util.clone(Sefaria.parseRef(ref));
300+
pRef.sections = pRef.sections.slice(0, -zoom);
301+
pRef.toSections = pRef.toSections.slice(0, -zoom);
302+
return Sefaria.makeRef(pRef);
303+
},
295304
splitSpanningRefNaive: function(ref){
296305
if (ref.indexOf("-") == -1) { return ref; }
297306
return ref.split("-");

0 commit comments

Comments
 (0)