Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #305. #373

Closed
wants to merge 7 commits into from
Closed
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
129 changes: 81 additions & 48 deletions pages/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,69 @@ export default class Documentation extends Component {
}

loadStateFromURL = () => {
const { pathname } = window.location
const sectionURL = pathname.split('/')[2] // match section from URL
const { pathname } = window.location;
const sectionURL = pathname.split('/')[2];
const sectionIndex = sidebar.findIndex(
section => (section.slug || kebabCase(section.name)) === sectionURL
)
section => kebabCase(section.name) === sectionURL
);
if (sectionIndex === -1) {
sectionURL
? this.setState({ pageNotFound: true })
: this.onSectionSelect(0)
this.setState({ pageNotFound: true })
} else {
const fileURL = pathname.split('/')[3] // match file from URL
const sectionFiles = flatten(sidebar[sectionIndex].files)
const fileIndex = sectionFiles.findIndex(
file => kebabCase(file.slice(0, -3)) === fileURL
)
if (fileIndex === -1) {
fileURL
? this.setState({ pageNotFound: true })
: this.onSectionSelect(sectionIndex)
} else {
this.loadFile({
section: sectionIndex,
file: sectionFiles[fileIndex],
parseHeadings: true,
pageNotFound: false
})
const fileURL = pathname.split('/')[3]; // match file from URL
const section = sidebar[sectionIndex];
if(fileURL){
if(pathname.split('/')[4]){
let subsectionIndex = section.files.findIndex(
file => kebabCase(file.indexFile.slice(0, -3)) === fileURL
);
let fileIndex = section.files[subsectionIndex].files.findIndex(
file => kebabCase(file.indexFile.slice(0, -3)) === pathname.split('/')[4]
);
this.loadFile({
file: section.files[subsectionIndex].files[fileIndex],
section: sectionIndex,
parseHeadings: true,
})
}else{
let fileIndex = section.files.findIndex(
file => kebabCase(file.indexFile.slice(0, -3)) === fileURL
);
if (fileIndex === -1) {
let fileIndex2=-1, subsectionIndex=0;
section.files.map((subsection,index2)=>{
if (subsection.files.length>0){
fileIndex2 = subsection.files.findIndex(
file2 => {
if(kebabCase(file2.indexFile.slice(0, -3)) === fileURL){
subsectionIndex = index2;
}
return kebabCase(file2.indexFile.slice(0, -3)) === fileURL
}
);
}
});
if (fileIndex2 === -1){
fileURL ? this.setState({ pageNotFound: true }) : this.onSectionSelect(sectionIndex)
}else{
this.loadFile({
file: section.files[subsectionIndex].files[fileIndex2],
section: sectionIndex,
parseHeadings: true,
})
}
}else{
this.loadFile({
file: section.files[fileIndex],
section: sectionIndex,
parseHeadings: true,
})
}
}
}else{
this.loadFile({ file:section.hasOwnProperty('indexFile') ? section : section.files[0], section: sectionIndex, parseHeadings: false });
}
}
}
};

initDocsearch = () => {
docsearch({
Expand All @@ -92,41 +126,40 @@ export default class Documentation extends Component {
inputSelector: '#doc-search',
debug: false // Set debug to true if you want to inspect the dropdown
})
}
};

getLinkHref = (section, file) => {
const sectionSlug =
sidebar[section].slug || kebabCase(sidebar[section].name)
const fileSlug = file ? kebabCase(file.slice(0, -3)) : undefined
return `/doc/${compact([sectionSlug, fileSlug]).join('/')}`
}
getLinkHref = (section, file, subsection=null) => {
const sectionSlug = kebabCase(sidebar[section].name);
const fileSlug = file ? file.slice(0, -3) : undefined;
let subsectionSlug = subsection ? sidebar[section].files[subsection].indexFile.slice(0,-3) : '';
return `/doc/${compact([sectionSlug, subsectionSlug, fileSlug]).join('/')}`;
};

setCurrentPath = (section, file) => {
window.history.pushState(null, null, this.getLinkHref(section, file))
}
setCurrentPath = (section, file, subsection) => {
window.history.pushState(null, null, this.getLinkHref(section, file, subsection))
};

onSectionSelect = (section, e) => {
e && e.preventDefault()
const { indexFile, files } = sidebar[section]
const file = indexFile || flatten(files)[0]
e && this.setCurrentPath(section, indexFile ? undefined : file)
this.loadFile({ file, section, parseHeadings: false })
}

onFileSelect = (file, section, e) => {
e && e.preventDefault()
this.setCurrentPath(section, file)
this.loadFile({ file, section, parseHeadings: true })
}
e && e.preventDefault();
const file = sidebar[section].hasOwnProperty('indexFile') ? sidebar[section] : sidebar[section].files[0];
e && this.setCurrentPath(section);
this.loadFile({ file, section, parseHeadings: false });
};

onFileSelect = (file, section, e, subsection) => {
e && e.preventDefault();
this.setCurrentPath(section, file.indexFile, subsection);
this.loadFile({ file, section, parseHeadings: true });
};

loadFile = ({ file, section, parseHeadings }) => {
fetch(`${sidebar[section].folder}/${file}`)
fetch(`${file.folder}/${file.indexFile}`)
.then(res => {
res.text().then(text => {
this.setState(
{
currentSection: section,
currentFile: file,
currentFile: file.indexFile,
markdown: text,
headings: [],
pageNotFound: false,
Expand Down
64 changes: 24 additions & 40 deletions src/Documentation/SidebarMenu/SidebarMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import React, { Fragment } from 'react'
import $ from 'jquery'
// components
import DownloadButton from '../../DownloadButton'
// utils
import startCase from 'lodash.startcase'
import includes from 'lodash.includes'
// styles
import styled from 'styled-components'
import { media, OnlyDesktop } from '../../styles'
Expand Down Expand Up @@ -32,7 +29,16 @@ export default class SidebarMenu extends React.Component {
}
}
render() {
let self = this
let self = this;
function includes(array, value) {
let flag = false;
array.map(elem=>{
if(elem.indexFile===value){
flag = true;
}
});
return flag;
}
const {
sidebar,
currentSection,
Expand All @@ -48,15 +54,12 @@ export default class SidebarMenu extends React.Component {
<SectionLinks>
{sidebar.map(
({ name, files = [], labels = {}, indexFile }, index) => {
const isSectionActive = currentSection === index
const isSectionActive = currentSection === index;
return (
<div key={index}>
<SectionLink
level={1}
href={getLinkHref(
index,
indexFile ? undefined : files[0]
)}
href={getLinkHref(index, files[0].indexFile)}
onClick={e => onSectionSelect(index, e)}
className={isSectionActive ? 'docSearch-lvl0' : ''}
isActive={isSectionActive}
Expand All @@ -66,54 +69,35 @@ export default class SidebarMenu extends React.Component {

{/* Section Files */}
<Collapse data-open={isSectionActive ? 'true' : 'false'}>
{files &&
files.map((fileOrGroup, fileIndex) => {
const file = Array.isArray(fileOrGroup)
? fileOrGroup[0]
: fileOrGroup
const subgroup = Array.isArray(fileOrGroup)
? fileOrGroup.slice(1)
: null
const isFileActive = currentFile === file
{files && files.map((file, fileIndex) => {
const subgroup = file.files.length>0 ? file.files : null;
const isFileActive = currentFile === file.indexFile;
return (
<Fragment key={`file-${fileIndex}`}>
<div>
<SectionLink
level={2}
href={getLinkHref(index, file)}
href={getLinkHref(index, file.indexFile)}
onClick={e => onFileSelect(file, index, e)}
isActive={isFileActive}
>
{labels[file] || startCase(file.slice(0, -3))}
{file.name}
</SectionLink>
</div>

{/* Subgroup files */}
{subgroup && (
<Collapse
data-flag={'first'}
data-open={
Array.isArray(fileOrGroup) &&
includes(fileOrGroup, currentFile)
? 'true'
: 'false'
}
>
{subgroup.map((sub, subIndex) => {
<Collapse data-flag={'first'} data-open={(isFileActive || includes(subgroup, currentFile))? 'true' : 'false'}>
{subgroup.map((file, subIndex) => {
return (
<div
key={`file-${fileIndex}-${subIndex}`}
>
<div key={`file-${fileIndex}-${subIndex}`}>
<SectionLink
level={3}
href={getLinkHref(index, sub)}
onClick={e =>
onFileSelect(sub, index, e)
}
isActive={currentFile === sub}
href={getLinkHref(index, file.indexFile)}
onClick={e => onFileSelect(file, index, e, fileIndex)}
isActive={currentFile === file.indexFile}
>
{labels[sub] ||
startCase(sub.slice(0, -3))}
{file.name}
</SectionLink>
</div>
)
Expand Down
Loading