Skip to content

Commit 1413c09

Browse files
authored
Merge pull request #81 from webdoc-labs/feature/tables+explorer-expand
Feature: Summary tables + Explorer expand & auto-scroll
2 parents 1a4f7f0 + e28cb6a commit 1413c09

File tree

22 files changed

+470
-20
lines changed

22 files changed

+470
-20
lines changed

packages/webdoc-default-template/helper/renderer-plugins/category-filter.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ exports.categoryFilterPlugin = (doc /*: Doc */, constraints /*: CategoryConstrai
1515
return false;
1616
}
1717
}
18+
if (child.inherited && !child.overrides) {
19+
return;
20+
}
1821

1922
if (child.type === "MethodDoc" && child.name === "constructor") {
2023
return false;// Filter constructors always!

packages/webdoc-default-template/helper/renderer-plugins/signature.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {Doc} from "@webdoc/types";
55
const {linker} = require("../linker");
66

77
exports.signaturePlugin = {
8-
generateSignature(doc /*: Doc */) {
8+
generateSignature(doc /*: Doc */, options = {} /*: { noTail: boolean } */) {
99
const mapDocTypeToKeyboard = {
1010
ClassDoc: "class",
1111
NSDoc: "namespace",
@@ -48,7 +48,7 @@ exports.signaturePlugin = {
4848
.join(", ")
4949
})`;
5050
}
51-
if (doc.returns) {
51+
if (!options.noTail && doc.returns) {
5252
signature += ` → {${
5353
(doc.returns || [])
5454
.map((returns) => (returns.dataType ?
@@ -59,7 +59,7 @@ exports.signaturePlugin = {
5959
}
6060
break;
6161
case "PropertyDoc":
62-
if (doc.dataType) {
62+
if (!options.noTail && doc.dataType) {
6363
signature += ": " + linker.linkTo(doc.dataType, undefined, {htmlSafe: false});
6464
}
6565
break;

packages/webdoc-default-template/publish.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const path = require("path");
66
const {traverse} = require("@webdoc/model");
77
const {
88
FlushToFile,
9+
RelationsPlugin,
910
TemplateRenderer,
1011
TemplatePipeline,
1112
TemplateTagsResolver,
@@ -62,11 +63,14 @@ exports.publish = (options /*: PublishOptions */) => {
6263
.installPlugin("linker", linker)
6364
.installPlugin("generateIndex", indexSorterPlugin)
6465
.installPlugin("signature", signaturePlugin)
65-
.installPlugin("categoryFilter", categoryFilterPlugin);
66+
.installPlugin("categoryFilter", categoryFilterPlugin)
67+
.installPlugin("relations", RelationsPlugin);
6668
const pipeline = new TemplatePipeline(renderer)
6769
.pipe(new TemplateTagsResolver())
6870
.pipe(new FlushToFile({skipNullFile: false}));
6971

72+
renderer.getPlugin("relations").buildRelations();
73+
7074
idToDoc = new Map();
7175

7276
traverse(docTree, (doc) => {

packages/webdoc-default-template/src/app/components/Explorer/ExplorerItem.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import {useExplorerCategoryStyles, useExplorerStyles} from "./useExplorerStyles";
1+
import {
2+
useExplorerCategoryStyles,
3+
useExplorerPrimaryItemStyles,
4+
useExplorerStyles,
5+
} from "./useExplorerStyles";
26
import ExplorerCategoryItem from "./ExplorerCategoryItem";
37
import Link from "@material-ui/core/Link";
48
import React from "react";
59
import TreeItem from "@material-ui/lab/TreeItem";
10+
import {isSamePage} from "./helpers";
611

712
export default function ExplorerItem(props) {
813
if (!props.data.$nodeId) {
@@ -11,6 +16,7 @@ export default function ExplorerItem(props) {
1116

1217
const classesItem = useExplorerStyles();
1318
const classesCategory = useExplorerCategoryStyles();
19+
const classesPrimaryItem = useExplorerPrimaryItemStyles();
1420
const targetChildren = [];
1521

1622
let i = 0;
@@ -24,6 +30,11 @@ export default function ExplorerItem(props) {
2430

2531
const classes = i > 0 ? classesCategory : classesItem;
2632
const nodeId = props.data.$nodeId;
33+
const primary = props.data.page && isSamePage(props.data);
34+
35+
if (primary) {
36+
console.log(props.data);
37+
}
2738

2839
const toggle = React.useCallback(
2940
() => props.toggle(nodeId),
@@ -36,6 +47,7 @@ export default function ExplorerItem(props) {
3647

3748
return (
3849
<TreeItem
50+
id={props.data.$nodeId}
3951
className="explorer-tree__target"
4052
classes={{
4153
label: classes.label,
@@ -47,7 +59,7 @@ export default function ExplorerItem(props) {
4759
label={
4860
props.data.page ?
4961
(
50-
<Link classes={{root: classes.labelLinks}}
62+
<Link classes={{root: primary ? classesPrimaryItem.labelLinks : classes.labelLinks}}
5163
href={props.data.page}
5264
underline="hover"
5365
>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function isSamePage(data) {
2+
const path = window.location.pathname.includes(".html") ?
3+
window.location.pathname :
4+
`${window.location.pathname}.html`;
5+
6+
if (data.page.startsWith("/") && data.page === path) {
7+
return true;
8+
} else if (!data.page.startsWith("/") && path.includes(`/${data.page}`)) {
9+
return true;
10+
}
11+
12+
return false;
13+
}

packages/webdoc-default-template/src/app/components/Explorer/index.js

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,36 @@ import React from "react";
88
import TreeView from "@material-ui/lab/TreeView";
99
import {connect} from "react-redux";
1010
import cuid from "cuid";
11+
import {isSamePage} from "./helpers";
1112
import store from "../../store";
1213
import {useExplorerStyles} from "./useExplorerStyles";
1314

1415
let fetched = false;
1516

16-
function makeIds(data) {
17+
function makeIds(data, collector) {
1718
data.$nodeId = cuid();
1819

20+
let shouldBeExpanded = false;
21+
22+
if (data.page) {
23+
shouldBeExpanded = isSamePage(data);
24+
}
25+
1926
if (data.children) {
2027
for (const [, value] of Object.entries(data.children)) {
21-
makeIds(value);
28+
const childExpanded = makeIds(value, collector);
29+
30+
if (childExpanded) {
31+
collector.add(data.$nodeId);
32+
shouldBeExpanded = true;
33+
}
2234
}
2335
}
36+
if (shouldBeExpanded) {
37+
collector.add(data.$nodeId);
38+
}
39+
40+
return shouldBeExpanded;
2441
}
2542

2643
export default connect(({
@@ -35,6 +52,14 @@ export default connect(({
3552
value: isOpen,
3653
}),
3754
expandedItems: Array.from(expandedItems),
55+
setAutoScrollTo: (value) => store.dispatch({
56+
type: "setAutoScrollTo",
57+
value,
58+
}),
59+
setExpandedItems: (value) => store.dispatch({
60+
type: "setExpandedItems",
61+
value,
62+
}),
3863
toggleItem: (nodeId, optValue) => store.dispatch({
3964
type: "toggleItem",
4065
nodeId,
@@ -44,9 +69,12 @@ export default connect(({
4469
isOpen,
4570
setOpen,
4671
expandedItems,
72+
setExpandedItems,
4773
toggleItem,
74+
rootRef,
4875
}) {
4976
const [data, setData] = React.useState(null);
77+
const [autoScrollTo, setAutoScrollTo] = React.useState(null);
5078
const {root} = useExplorerStyles();
5179
const toggleOpen = React.useCallback(() => setOpen(!isOpen), [isOpen]);
5280
const children = [];
@@ -57,8 +85,12 @@ export default connect(({
5785
.then((response) => {
5886
if (response.ok) {
5987
response.json().then((idata) => {
60-
makeIds(idata);
88+
const defaultExpanded = new Set();
89+
90+
makeIds(idata, defaultExpanded);
6191
setData(idata || {});
92+
setAutoScrollTo(defaultExpanded.values().next().value);
93+
setExpandedItems(defaultExpanded);
6294
});
6395
} else {
6496
throw new Error("Can't fetch reference.json");
@@ -87,6 +119,29 @@ export default connect(({
87119
children.push(<span key={++i}>Loading...</span>);
88120
}
89121

122+
React.useEffect(
123+
() => {
124+
setTimeout(
125+
() => {
126+
const scrollEl = rootRef.current.querySelector(".MuiTreeView-root");
127+
const toEl = document.getElementById(autoScrollTo);
128+
129+
if (scrollEl && toEl) {
130+
const rect = toEl.getBoundingClientRect();
131+
132+
scrollEl.scrollTo({
133+
left: rect.left,
134+
top: rect.top - 124,
135+
behavior: "smooth",
136+
});
137+
}
138+
},
139+
400,
140+
);
141+
},
142+
[autoScrollTo],
143+
);
144+
90145
return (
91146
<div className="explorer" style={{
92147
transition: "margin-left 200ms, width 200ms",

packages/webdoc-default-template/src/app/components/Explorer/useExplorerStyles.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ export const useExplorerCategoryStyles = makeStyles({
3434
fontWeight: "bold !important",
3535
},
3636
});
37+
38+
export const useExplorerPrimaryItemStyles = makeStyles({
39+
labelLinks: {
40+
...itemStyle.labelLinks,
41+
color: "#0066CD !important",
42+
},
43+
});

packages/webdoc-default-template/src/app/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import ReactDOM from "react-dom";
66
import store from "./store";
77

88
window.onload = function() {
9+
wakeAccordions();
10+
911
const appBarRoot = document.getElementById("header-mount-point");
1012
const explorerRoot = document.getElementById("explorer-mount-point");
1113
const footerRoot = document.getElementById("footer-mount-point");
@@ -22,7 +24,7 @@ window.onload = function() {
2224
ReactDOM.render(
2325
(
2426
<Provider store={store}>
25-
<Explorer />
27+
<Explorer rootRef={{current: explorerRoot}} />
2628
</Provider>
2729
),
2830
explorerRoot,
@@ -35,3 +37,15 @@ window.onload = function() {
3537
footerRoot,
3638
);
3739
};
40+
41+
function wakeAccordions() {
42+
document.querySelectorAll(".accordion").forEach(
43+
(accordion) => {
44+
const btn = accordion.querySelector(".accordion__toggle");
45+
46+
btn.onclick = () => {
47+
accordion.classList.toggle("accordion-active");
48+
};
49+
},
50+
);
51+
}

packages/webdoc-default-template/src/app/store.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ function globalReducer(state = {}, action) {
2626
query: action.value,
2727
};
2828
}
29+
case "setExpandedItems":
30+
return {
31+
...state,
32+
expandedItems: new Set(action.value),
33+
};
2934
case "toggleItem": {
3035
const expandedItems = new Set(state.expandedItems);
3136

packages/webdoc-default-template/src/styles/members.scss

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@
1010
padding: 8px;
1111
}
1212

13+
&__subcategory {
14+
color: #333;
15+
font-size: 1.125em;
16+
font-weight: normal;
17+
line-height: 1.4em;
18+
margin: 14px 8px 14px 0;
19+
}
20+
1321
> hr {
14-
border: 2px solid $colorSheetSecondary
22+
border: 1px solid $colorSheetSecondary;
23+
margin-bottom: 10px;
1524
}
1625

1726
> * {
@@ -50,3 +59,57 @@
5059
margin-bottom: 8px;
5160
}
5261
}
62+
63+
.accordion {
64+
&__toggle {
65+
align-items: center;
66+
border-radius: 8px;
67+
display: flex;
68+
transition-duration: 200ms;
69+
70+
button {
71+
background: transparent;
72+
border: none;
73+
height: 40px;
74+
padding: 0;
75+
width: 40px;
76+
77+
.accordion__bg-active {
78+
display: none;
79+
}
80+
81+
&:focus {
82+
outline: none;
83+
}
84+
}
85+
86+
&:hover {
87+
background-color: rgba(0, 0, 0, 0.04);
88+
}
89+
}
90+
91+
&__content {
92+
max-height: 0;
93+
overflow: hidden;
94+
visibility: hidden;
95+
}
96+
}
97+
98+
.accordion-active {
99+
> .accordion__content {
100+
max-height: max-content;
101+
visibility: visible;
102+
}
103+
104+
.accordion__toggle {
105+
button {
106+
.accordion__bg-inactive {
107+
display: none;
108+
}
109+
110+
.accordion__bg-active {
111+
display: inline;
112+
}
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)