Skip to content

Commit

Permalink
feat: added page container wrapper, pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jun 16, 2020
1 parent 52d9c8f commit 1aeeef1
Show file tree
Hide file tree
Showing 44 changed files with 339 additions and 50 deletions.
4 changes: 4 additions & 0 deletions core/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"deepmerge": "^4.2.2",
"escape-html": "^1.0.3",
"faker": "^4.1.0",
"react": "^16.13.1",
"typescript": "^3.8.3"
},
"devDependencies": {
Expand All @@ -43,6 +44,9 @@
"jest": "^24.9.0",
"webpack": "^4.43.0"
},
"peerDependencies": {
"react": "^16.13.1"
},
"publishConfig": {
"access": "public"
},
Expand Down
6 changes: 6 additions & 0 deletions core/core/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Configuration as WebpackConfiguration } from 'webpack';
import { ComponentType } from 'react';
import { StoryRenderFn } from './utility';

export type PageType = 'story' | 'blog' | 'page' | 'tags' | 'author';
Expand Down Expand Up @@ -128,6 +129,11 @@ export interface RunOnlyConfiguration {
* story sorting function
*/
storySort?: (a: string, b: string) => number;

/**
* page container react component
*/
container?: ComponentType;
}

export type RunConfiguration = RunOnlyConfiguration &
Expand Down
17 changes: 13 additions & 4 deletions core/core/src/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,14 @@ export interface StoriesDoc {
sidebars?: boolean;

/**
* optional date the document was created
* optional date the document was created. If not assigned, the instrumentation process will use birthtime
*/
date?: string;
date?: Date;

/**
* optional date the document was last modified. If not assigned, the instrumentation process will use mtime
*/
dateModified?: Date;

/**
* comma-separated list of document tags, used for search
Expand All @@ -276,10 +281,14 @@ export interface StoriesDoc {
* document author
*/
author?: string;

[name: string]: any;
}

export const dateToLocalString = (date?: Date): string =>
date
? new Date(date).toLocaleDateString('en-US', {
timeZone: 'UTC',
})
: '';
/**
* list of components used in stories
*/
Expand Down
4 changes: 4 additions & 0 deletions core/instrument/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as parser from '@babel/parser';
import * as fs from 'fs';
import mdx from '@mdx-js/mdx';
import matter from 'gray-matter';
import { File } from '@babel/types';
Expand Down Expand Up @@ -102,6 +103,9 @@ const parseSource = async (
store.packages[storyPackage.fileHash] = storyPackage;
doc.package = storyPackage.fileHash;
}
const stats = fs.statSync(filePath);
doc.dateModified = doc.dateModified || stats.mtime;
doc.date = doc.date || stats.birthtime;
}
for (const key of Object.keys(store.stories)) {
const story: Story = store.stories[key];
Expand Down
7 changes: 1 addition & 6 deletions core/loader/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';
import * as chalk from 'chalk';
import { getOptions } from 'loader-utils';
import { loader } from 'webpack';

import {
InstrumentOptions,
parseStories,
Expand All @@ -12,7 +11,6 @@ import { addStoriesDoc } from './store';

module.exports.pitch = async function() {
const options: InstrumentOptions = getOptions(this) || {};
const context = this as loader.LoaderContext;
const filePath = this.resource;
const source = fs.readFileSync(filePath, 'utf8');
const { transformed, ...store } = await parseStories(
Expand All @@ -21,8 +19,6 @@ module.exports.pitch = async function() {
options,
);
if (store) {
const relPath = path.relative(context.rootContext, filePath);
const moduleId = relPath.startsWith('.') ? relPath : `./${relPath}`;
if (store.doc) {
console.log(chalk.bgRgb(244, 147, 66)('@loaded: '), filePath);
addStoriesDoc(filePath, {
Expand All @@ -32,7 +28,6 @@ module.exports.pitch = async function() {
doc: {
...store.doc,
fileName: filePath,
moduleId: moduleId,
},
});
}
Expand Down
1 change: 1 addition & 0 deletions core/loader/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Store implements LoadingStore {
getUniquesByField = (field: string) => {
return this.stores.reduce((acc: { [key: string]: number }, store) => {
if (store?.doc) {
//@ts-ignore
const value = store.doc[field];
const values = Array.isArray(value) ? value : [value];
values.forEach(v => {
Expand Down
28 changes: 28 additions & 0 deletions core/store/src/Store/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,40 @@ export class Store implements StoryStore {
}
return [];
};
getPrevPage = (
type: PageType | undefined,
docId: string,
): StoriesDoc | undefined => {
if (docId) {
const pages = this.getPageList(type);
const index = pages.findIndex(p => p.title === docId);
if (index > 0) {
return pages[index - 1];
}
}
return undefined;
};
getNextPage = (
type: PageType | undefined,
docId: string,
): StoriesDoc | undefined => {
if (docId) {
const pages = this.getPageList(type);
const index = pages.findIndex(p => p.title === docId);
if (index >= 0 && index < pages.length - 1) {
return pages[index + 1];
}
}
return undefined;
};

getPagesByCategory = (category: string, value?: any): Pages => {
if (this.loadedStore?.docs) {
const docs = this.loadedStore?.docs;
return Object.keys(docs)
.filter(key => {
const doc = docs[key];
//@ts-ignore
const cateValue = doc[category];
if (value === undefined) {
return cateValue !== undefined;
Expand All @@ -221,6 +248,7 @@ export class Store implements StoryStore {
this._categoryItems[category] = Object.keys(docs).reduce(
(acc: { [key: string]: number }, key) => {
const doc = docs[key];
//@ts-ignore
const value = doc[category];
const values = Array.isArray(value) ? value : [value];
values.forEach(v => {
Expand Down
1 change: 0 additions & 1 deletion core/store/src/serialization/load-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export const loadStoryStore = (
source,
component,
fileName,
repository,
components,
excludeStories,
includeStories,
Expand Down
8 changes: 8 additions & 0 deletions core/store/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ export interface StoryStore {
getDocs: () => Pages;
getBlogs: () => Pages;
getPageList: (type: PageType) => Pages;
getPrevPage: (
type: PageType | undefined,
docId: string,
) => StoriesDoc | undefined;
getNextPage: (
type: PageType | undefined,
docId: string,
) => StoriesDoc | undefined;
getPagesByCategory: (category: string, value?: any) => Pages;
getUniquesByCategory: (category: string) => { [key: string]: number };
config: RunConfiguration | undefined;
Expand Down
2 changes: 2 additions & 0 deletions examples/gatsby/.config/runtime.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Container } from "@component-controls/app";
const categories = ['Introduction', 'Application','Controls','Blocks', 'Editors', 'Components', 'Plugins']

module.exports = {
container: Container,
siteTitle: `Component controls`,
siteTitleAlt: `Component controls - https://github.com/ccontrols/component-controls`,
siteHeadline: `Component controls gatsby`,
Expand Down
2 changes: 1 addition & 1 deletion examples/stories/src/blogs/custom-docs-pages.mdx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Custom documentation pages for storybookjs
type: blog
date: 2019-11-07
date: 2020-04-27
author: atanasster
description: Storybookjs plugin that overcomes some "docs page" storybook architecture flaws to display multiple documentation pages.
tags:
Expand Down
8 changes: 5 additions & 3 deletions ui/app/src/App/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/** @jsx jsx */
import { FC, Fragment } from 'react';
import { FC, Fragment, useContext } from 'react';
import { jsx, Box } from 'theme-ui';
import { SkipLinks, SkiLinksItemProps } from '@component-controls/components';
import { useStoryContext } from '@component-controls/blocks';
import { BlockContext } from '@component-controls/blocks';
import { SEO } from '../SEO';
import { Header } from '../Header';
import { Footer } from '../Footer';
Expand All @@ -19,7 +19,8 @@ export interface AppProps {
*
*/
export const App: FC<AppProps> = ({ title, children }) => {
const { doc } = useStoryContext({ id: '.' });
const { storeProvider, docId } = useContext(BlockContext);
const doc = docId ? storeProvider.getStoryDoc(docId) : undefined;
const items: SkiLinksItemProps[] = [
{
target: 'content',
Expand All @@ -43,6 +44,7 @@ export const App: FC<AppProps> = ({ title, children }) => {
<SkipLinks items={items} />
<Box variant="app">
<Header />

{children}
<Footer />
</Box>
Expand Down
3 changes: 2 additions & 1 deletion ui/app/src/CategoryList/CategoryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { FC, useContext } from 'react';
import { jsx, Box } from 'theme-ui';
import { PageType } from '@component-controls/core';
import { Title } from '@component-controls/components';
import { PageContainer, BlockContext } from '@component-controls/blocks';
import { BlockContext } from '@component-controls/blocks';
import { PageContainer } from '../PageContainer';
import { CategoryListItem } from './CategoryListItem';

export interface CategoryListProps {
Expand Down
3 changes: 2 additions & 1 deletion ui/app/src/CategoryPage/CategoryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { FC, useContext } from 'react';
import { jsx, Box } from 'theme-ui';
import { PageType } from '@component-controls/core';
import { Title, Link } from '@component-controls/components';
import { PageContainer, BlockContext } from '@component-controls/blocks';
import { BlockContext } from '@component-controls/blocks';
import { PageContainer } from '../PageContainer';
import { DocumentsList } from '../DocumentsList';

export interface CategoryPageProps {
Expand Down
14 changes: 14 additions & 0 deletions ui/app/src/Container/Container.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import { MockContext } from '@component-controls/blocks';
import { Container } from './Container';

export default {
title: 'Application/Container',
component: Container,
};

export const overview = () => (
<MockContext storyId="id-of-story">
<Container />
</MockContext>
);
20 changes: 20 additions & 0 deletions ui/app/src/Container/Container.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/** @jsx jsx */
import { jsx, Box } from 'theme-ui';
import { FC } from 'react';
import {
Pagination,
Container as BlocksContainer,
} from '@component-controls/blocks';

export const Container: FC = ({ children }) => {
return (
<Box variant="container.container">
<BlocksContainer>
{children}
<Box variant="container.pagination">
<Pagination />
</Box>
</BlocksContainer>
</Box>
);
};
1 change: 1 addition & 0 deletions ui/app/src/Container/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Container';
3 changes: 2 additions & 1 deletion ui/app/src/DocPage/DocPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/** @jsx jsx */
import { FC, useContext } from 'react';
import { jsx } from 'theme-ui';
import { PageContainer, BlockContext } from '@component-controls/blocks';
import { BlockContext } from '@component-controls/blocks';
import { PageContainer } from '../PageContainer';
import { SidebarsPage, DocPageProps, PageConfig } from '../SidebarsPage';
import { PageType } from '@component-controls/core';

Expand Down
7 changes: 4 additions & 3 deletions ui/app/src/DocumentsList/DocumentsListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getDocPath,
defPageType,
RunConfiguration,
dateToLocalString,
} from '@component-controls/core';
import { Subtitle, Markdown, Tag, Link } from '@component-controls/components';
import { TagsList } from '../TagsList';
Expand Down Expand Up @@ -60,9 +61,9 @@ export const DocumentsListItem: FC<PageListItemProps> = ({
<Box variant="documentlistitem.info.container">
<Box variant="documentlistitem.info.inner">
{date ? (
<Box variant="documentlistitem.info.date">
{new Date(date).toDateString()}
</Box>
<Box variant="documentlistitem.info.date">{`created: ${dateToLocalString(
date,
)}`}</Box>
) : (
''
)}
Expand Down
16 changes: 16 additions & 0 deletions ui/app/src/PageContainer/PageContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React, { FC, useContext, forwardRef } from 'react';
import {
BlockContext,
PageContainer as BlockPageContainer,
PageContainerProps,
} from '@component-controls/blocks';
import { Container as DefaultContainer } from '../Container';

export const PageContainer: FC<Omit<
PageContainerProps,
'wrapper'
>> = forwardRef((props, ref: React.Ref<HTMLDivElement>) => {
const { storeProvider } = useContext(BlockContext);
const { container = DefaultContainer } = storeProvider.config || {};
return <BlockPageContainer ref={ref} wrapper={container} {...props} />;
});
1 change: 1 addition & 0 deletions ui/app/src/PageContainer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './PageContainer';
3 changes: 2 additions & 1 deletion ui/app/src/PageList/PageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { FC, useContext } from 'react';
import { jsx } from 'theme-ui';
import { PageType } from '@component-controls/core';
import { Title } from '@component-controls/components';
import { PageContainer, BlockContext } from '@component-controls/blocks';
import { BlockContext } from '@component-controls/blocks';
import { PageContainer } from '../PageContainer';
import { DocumentsList } from '../DocumentsList';

export interface PageListProps {
Expand Down
4 changes: 1 addition & 3 deletions ui/app/src/Sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ export const Sidebar: FC<SidebarProps> = ({
const menuItems = docs.reduce((acc: MenuItems, doc: StoriesDoc) => {
const { title } = doc;
const levels = title.split('/');
if (doc.menu !== false) {
createMenuItem(storeProvider, doc, type, levels, acc);
}
createMenuItem(storeProvider, doc, type, levels, acc);
return acc;
}, []);
return menuItems;
Expand Down
3 changes: 1 addition & 2 deletions ui/app/src/SidebarsPage/SidebarsMDXPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import { FC } from 'react';
import { jsx, Box } from 'theme-ui';
import { PageType } from '@component-controls/core';
import { PageContainer } from '@component-controls/blocks';

import { PageContainer } from '../PageContainer';
import { Sidebar } from '../Sidebar';

export interface SidebarsMDXPageProps {
Expand Down
3 changes: 1 addition & 2 deletions ui/app/src/SidebarsPage/SidebarsStoryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { jsx, Box } from 'theme-ui';
import * as qs from 'qs';
import { PageType } from '@component-controls/core';
import { Tabs, Tab, TabList, TabPanel } from '@component-controls/components';
import { PageContainer } from '@component-controls/blocks';

import { PageContainer } from '../PageContainer';
import { SideContext } from '../SideContext';
import { Sidebar } from '../Sidebar';

Expand Down
Loading

0 comments on commit 1aeeef1

Please sign in to comment.