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

TS Migration: addon-notes #4758

Merged
merged 30 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
aaf78da
First poc approach with addon-notes
kroeder Nov 9, 2018
c51d7c8
Tsconfig configuration update
kroeder Nov 10, 2018
6dc5f05
Added public_api entry point
kroeder Nov 10, 2018
8891dd4
Removed addon-react since it's not used anymore
kroeder Nov 11, 2018
dca0f7d
Fixed import for addon-notes test file
kroeder Nov 11, 2018
3bf2c8a
Added @types/node for allowing the use of 'module'
kroeder Nov 11, 2018
558da37
fixed jsnext:main path
kroeder Nov 11, 2018
6823d78
removed prop-types, they are not needed anymore
kroeder Nov 11, 2018
b51f8d1
Moved addon tsconfig configuration to root tsconfig
kroeder Nov 11, 2018
d37fcf6
Added default export, might fix CI
kroeder Nov 11, 2018
5e84f3b
Added marked types and updated marked implementation
kroeder Nov 11, 2018
852973f
Removed unnecessary function; updated marked dependencies
kroeder Dec 6, 2018
f25a05b
Changed addon.ts back to index.ts
kroeder Dec 6, 2018
94e6e12
Updated @emotion/styled because the newest version has definition fil…
kroeder Dec 8, 2018
206b304
Added .tsx to the allowed file extensions for jsx
kroeder Dec 8, 2018
49b467d
Re-enabled noImplicitAny again
kroeder Dec 8, 2018
149f471
Resolved a couple of any to their actual types
kroeder Dec 8, 2018
e943ff3
Updated NotesApi; refactored "styled" usage
kroeder Dec 8, 2018
14afa9c
Refactoring
kroeder Dec 8, 2018
88d1f70
Re-added .ts to import/resolver in eslint
kroeder Dec 8, 2018
a11cd70
Added todo for refactoring purpose
kroeder Dec 8, 2018
2d00ee6
Force eslint to ignore *.ts files
igor-dv Dec 8, 2018
73f4715
Merge remote-tracking branch 'origin/next' into typescript-poc
igor-dv Dec 8, 2018
79306bd
Set @emotion/styled back to ^0.10.6
kroeder Dec 9, 2018
876488c
Added 'jsx-double' rule for tsx files
kroeder Dec 9, 2018
018f83a
Created @emotion/styled module to satisfy noImplicitAny rule
kroeder Dec 9, 2018
d7184fe
Fixed build issue
kroeder Dec 9, 2018
c1669d8
Reverted @emotion/styled change
kroeder Dec 9, 2018
5312cd4
Refactoring
kroeder Dec 9, 2018
1a4e510
Added prop-types + typings for prop-types. Restored the old prop-type…
kroeder Dec 9, 2018
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 .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ built-storybooks
lib/cli/test
*.bundle.js
*.js.map
*.ts

!.remarkrc.js
!.babelrc.js
Expand Down
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ module.exports = {
'react/jsx-filename-extension': [
warn,
{
extensions: ['.js', '.jsx'],
extensions: ['.js', '.jsx', '.tsx'],
},
],
'react/jsx-no-bind': [
Expand Down
11 changes: 8 additions & 3 deletions addons/notes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@
"url": "https://github.com/storybooks/storybook.git"
},
"license": "MIT",
"main": "dist/index.js",
"jsnext:main": "src/index.js",
"main": "dist/public_api.js",
"types": "dist/public_api.d.ts",
"jsnext:main": "src/public_api.ts",
"scripts": {
"prepare": "node ../../scripts/prepare.js"
},
"dependencies": {
"@emotion/styled": "^0.10.6",
"@storybook/addons": "4.1.0-alpha.11",
"marked": "^0.5.1",
"marked": "^0.5.2",
"prop-types": "^15.6.2"
},
"devDependencies": {
"@types/marked": "^0.5.0",
"@types/prop-types": "^15.5.7"
},
kroeder marked this conversation as resolved.
Show resolved Hide resolved
"peerDependencies": {
"react": "*"
},
Expand Down
19 changes: 11 additions & 8 deletions addons/notes/src/index.js → addons/notes/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import addons, { makeDecorator } from '@storybook/addons';
import marked from 'marked';

function renderMarkdown(text, options) {
return marked(text, { ...marked.defaults, ...options });
}
import { parse as renderMarkdown } from 'marked';

// todo resolve any after @storybook/addons and @storybook/channels are migrated to TypeScript
export const withNotes = makeDecorator({
name: 'withNotes',
parameterName: 'notes',
skipIfNoParametersOrOptions: true,
allowDeprecatedUsage: true,
wrapper: (getStory, context, { options, parameters }) => {
wrapper: (getStory: (context: any) => any, context: any, { options, parameters }: any) => {
const channel = addons.getChannel();

const storyOptions = parameters || options;

const { text, markdown, markdownOptions } =
typeof storyOptions === 'string' ? { text: storyOptions } : storyOptions;
typeof storyOptions === 'string'
? {
text: storyOptions,
markdown: undefined,
markdownOptions: undefined,
}
: storyOptions;
kroeder marked this conversation as resolved.
Show resolved Hide resolved

if (!text && !markdown) {
throw new Error('You must set of one of `text` or `markdown` on the `notes` parameter');
Expand All @@ -28,7 +31,7 @@ export const withNotes = makeDecorator({
},
});

export const withMarkdownNotes = (text, options) =>
export const withMarkdownNotes = (text: string, options: any) =>
kroeder marked this conversation as resolved.
Show resolved Hide resolved
withNotes({
markdown: text,
markdownOptions: options,
Expand Down
1 change: 1 addition & 0 deletions addons/notes/src/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '.';
23 changes: 0 additions & 23 deletions addons/notes/src/react.js

This file was deleted.

86 changes: 0 additions & 86 deletions addons/notes/src/register.js

This file was deleted.

114 changes: 114 additions & 0 deletions addons/notes/src/register.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import * as React from 'react';
import addons from '@storybook/addons';
import * as PropTypes from 'prop-types';

import styled from '@emotion/styled';

// todo this is going to be refactored after the migration of @storybook/channel to TypeScript
interface NotesChannel {
kroeder marked this conversation as resolved.
Show resolved Hide resolved
emit: any;
on(listener: string, callback: (text: string) => void): any;
removeListener(listener: string, callback: (text: string) => void): void;
}

interface NotesApi {
setQueryParams: any; // todo check correct definition
getQueryParam(queryParamName: string): any;

onStory(callback: () => void): () => void;
}

interface NotesProps {
active: boolean;
channel: NotesChannel;
api: NotesApi;
}

interface NotesState {
text: string;
}

const Panel = styled.div({
padding: 10,
boxSizing: 'border-box',
width: '100%',
});

export class Notes extends React.Component<NotesProps, NotesState> {

static propTypes = {
active: PropTypes.bool.isRequired,
channel: PropTypes.shape({
on: PropTypes.func,
emit: PropTypes.func,
removeListener: PropTypes.func,
}).isRequired,
api: PropTypes.shape({
onStory: PropTypes.func,
getQueryParam: PropTypes.func,
setQueryParams: PropTypes.func,
}).isRequired,
};

stopListeningOnStory: () => void;
isUnmounted = false;

readonly state: NotesState = { text: '' };

constructor(props: NotesProps) {
super(props);
this.onAddNotes = this.onAddNotes.bind(this);
}

componentDidMount() {
const { channel, api } = this.props;
// Listen to the notes and render it.
channel.on('storybook/notes/add_notes', this.onAddNotes);

// Clear the current notes on every story change.
this.stopListeningOnStory = api.onStory(() => {
this.onAddNotes('');
});
}

// This is some cleanup tasks when the Notes panel is unmounting.
componentWillUnmount() {
if (this.stopListeningOnStory) {
this.stopListeningOnStory();
}

this.isUnmounted = true;
const { channel } = this.props;
channel.removeListener('storybook/notes/add_notes', this.onAddNotes);
}

onAddNotes(text: string) {
this.setState({ text });
}

render() {
const { active } = this.props;
const { text } = this.state;
const textAfterFormatted = text
? text
.trim()
.replace(/(<\S+.*>)\n/g, '$1')
.replace(/\n/g, '<br />')
: '';

return active ? (
<Panel
className="addon-notes-container"
dangerouslySetInnerHTML={{ __html: textAfterFormatted }}
/>
) : null;
}
}

addons.register('storybook/notes', (api: any) => {
const channel = addons.getChannel();
addons.addPanel('storybook/notes/panel', {
title: 'Notes',
render: ({ active }: { active: boolean }) => <Notes channel={channel} api={api} active={active}/>,
});
});
5 changes: 5 additions & 0 deletions addons/notes/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Fixes 'noImplicitAny' lint error because @storybook/addons isn't migrated to typescript yet
declare module '@storybook/addons';

// Only necessary for 0.x.x. Version 10.x.x has definition files included
declare module '@emotion/styled';
13 changes: 13 additions & 0 deletions addons/notes/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src"
},
"include": [
"src/**/*.ts",
"src/**/*.tsx"
],
"exclude": [
"src/__tests__/**/*"
]
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"@emotion/snapshot-serializer": "^0.8.2",
"@types/jest": "^23.3.9",
"@types/node": "^10.12.5",
"@types/react": "^16.7.3",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^23.6.0",
Expand Down
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": ".",
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"noImplicitAny": true,
"jsx": "react",
"module": "commonjs",
"target": "es5",
"types": [
"node",
"jest"
],
"lib": [
"es2016",
"dom"
Expand Down
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
"prefer-const": true,
"quotemark": [
true,
"single"
"single",
"jsx-double"
],
"radix": true,
"semicolon": [
Expand Down
Loading