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

feat: add option sideNavLayout #2633

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/components/SideMenu/MenuItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class MenuItems extends React.Component<MenuItemsProps> {
className={className}
style={this.props.style}
$expanded={expanded}
root={root || false}
{...(root ? { role: 'menu' } : {})}
>
{items.map((item, idx) => (
Expand Down
8 changes: 6 additions & 2 deletions src/components/SideMenu/SideMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ export class SideMenu extends React.Component<{ menu: MenuStore; className?: str
}

activate = (item: IMenuItem) => {
if (item && item.active && this.context.menuToggle) {
return item.expanded ? item.collapse() : item.expand();
if (item && this.context.menuToggle) {
if (item.expanded) {
item.collapse();
return;
}
if (item.active) item.expand();
}
this.props.menu.activateAndScroll(item, true);
setTimeout(() => {
Expand Down
4 changes: 3 additions & 1 deletion src/components/SideMenu/styled.elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function menuItemActive(
}
}

export const MenuItemUl = styled.ul<{ $expanded: boolean }>`
export const MenuItemUl = styled.ul<{ $expanded: boolean; root: boolean }>`
margin: 0;
padding: 0;

Expand All @@ -97,6 +97,8 @@ export const MenuItemUl = styled.ul<{ $expanded: boolean }>`
}

${props => (props.$expanded ? '' : 'display: none;')};
${props => (props.root ? '' : 'margin-left: 8px;')};
${props => (props.root ? '' : 'border-left: 1px solid lightblue;')}
`;

export const MenuItemLi = styled.li<{ depth: number }>`
Expand Down
41 changes: 40 additions & 1 deletion src/services/MenuBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import type { ContentItemModel, TagGroup, TagInfo, TagsInfoMap } from './types';

export const GROUP_DEPTH = 0;

function appendChildren(parent: ContentItemModel, children: ContentItemModel[], prefix: string) {
for (const child of MenuBuilder.factorByPrefix(children)) {
if (child.sidebarLabel.startsWith(prefix)) {
child.sidebarLabel = '…' + child.sidebarLabel.slice(prefix.length - 1);
}
parent.items.push(child);
}
}

export class MenuBuilder {
/**
* Builds page content structure based on tags
Expand All @@ -28,7 +37,37 @@ export class MenuBuilder {
} else {
items.push(...MenuBuilder.getTagsItems(parser, tagsMap, undefined, undefined, options));
}
return items;

if (options.sideNavLayout !== 'factored') return items;

return MenuBuilder.factorByPrefix(items);
}

static factorByPrefix(items: ContentItemModel[]): ContentItemModel[] {
const newItems: ContentItemModel[] = [];
let newChildren: ContentItemModel[] = [];
let parent: GroupModel | null = null;
let prefix = '';
for (const item of items) {
if (parent && item.name.startsWith(prefix)) {
item.parent = parent;
item.depth = parent.depth + 1;
newChildren.push(item);
} else {
if (newChildren.length > 0) {
appendChildren(parent!, newChildren, prefix);
newChildren = [];
}

newItems.push(item);
if (item instanceof GroupModel) {
parent = item;
prefix = item.name + '/';
} else parent = null;
}
}
if (newChildren.length > 0) appendChildren(parent!, newChildren, prefix);
return newItems;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/services/RedocNormalizedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface RedocRawOptions {
onlyRequiredInSamples?: boolean | string;
showExtensions?: boolean | string | string[];
sideNavStyle?: SideNavStyleEnum;
sideNavLayout?: 'default' | 'factored';
hideSingleRequestSampleTab?: boolean | string;
hideRequestPayloadSample?: boolean;
menuToggle?: boolean | string;
Expand Down Expand Up @@ -180,6 +181,10 @@ export class RedocNormalizedOptions {
}
}

static normalizeSideNavLayout(value: RedocRawOptions['sideNavLayout']): 'default' | 'factored' {
return value === 'factored' ? value : 'default';
}

static normalizePayloadSampleIdx(value: RedocRawOptions['payloadSampleIdx']): number {
if (typeof value === 'number') {
return Math.max(0, value); // always greater or equal than 0
Expand Down Expand Up @@ -231,6 +236,7 @@ export class RedocNormalizedOptions {
onlyRequiredInSamples: boolean;
showExtensions: boolean | string[];
sideNavStyle: SideNavStyleEnum;
sideNavLayout: 'default' | 'factored';
hideSingleRequestSampleTab: boolean;
hideRequestPayloadSample: boolean;
menuToggle: boolean;
Expand Down Expand Up @@ -303,6 +309,7 @@ export class RedocNormalizedOptions {
this.onlyRequiredInSamples = argValueToBoolean(raw.onlyRequiredInSamples);
this.showExtensions = RedocNormalizedOptions.normalizeShowExtensions(raw.showExtensions);
this.sideNavStyle = RedocNormalizedOptions.normalizeSideNavStyle(raw.sideNavStyle);
this.sideNavLayout = RedocNormalizedOptions.normalizeSideNavLayout(raw.sideNavLayout);
this.hideSingleRequestSampleTab = argValueToBoolean(raw.hideSingleRequestSampleTab);
this.hideRequestPayloadSample = argValueToBoolean(raw.hideRequestPayloadSample);
this.menuToggle = argValueToBoolean(raw.menuToggle, true);
Expand Down