Skip to content

Commit

Permalink
Merge pull request #17 from aimeex3/webpack
Browse files Browse the repository at this point in the history
fix(build): add storybook and update build to use webpack
  • Loading branch information
aimeex3 authored Dec 4, 2019
2 parents 052611e + 0728deb commit 0845a2c
Show file tree
Hide file tree
Showing 11 changed files with 3,600 additions and 404 deletions.
5 changes: 3 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"plugins": ["@babel/plugin-transform-react-jsx", "@babel/plugin-proposal-optional-chaining"],
"plugins": ["@babel/plugin-proposal-optional-chaining"],
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage",
"corejs": 3
}
]
],
"@babel/preset-react"
]
}
3 changes: 3 additions & 0 deletions .storybook/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { configure } from '@storybook/react';

configure(require.context('../src', true, /\.stories\.js$/), module);
18 changes: 18 additions & 0 deletions .storybook/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const path = require('path');

// Export a function. Accept the base config as the only param.
module.exports = async ({ config, mode }) => {
// `mode` has a value of 'DEVELOPMENT' or 'PRODUCTION'
// You can change the configuration based on that.
// 'PRODUCTION' is used when building the static version of storybook.

// Make whatever fine-grained changes you need
config.module.rules.push({
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader'],
include: path.resolve(__dirname, '../'),
});

// Return the altered config
return config;
};
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
"name": "@webex/message-composer",
"version": "1.0.1",
"description": "React message composer",
"main": "dist/components/MessageComposer/index.js",
"main": "dist/index.js",
"author": "Michael Wegman (mwegman@cisco.com)",
"license": "MIT",
"scripts": {
"start": "webpack-dev-server --config webpack/dev.config.js",
"build": "babel src -d dist --copy-files --presets minify",
"build": "webpack --config webpack/prod.config.js",
"prepublishOnly": "yarn build",
"semantic-release": "semantic-release",
"semantic-release:dry-run": "semantic-release --dry-run"
"semantic-release:dry-run": "semantic-release --dry-run",
"storybook": "start-storybook"
},
"dependencies": {
"classnames": "^2.2.6",
Expand Down Expand Up @@ -44,14 +45,17 @@
"@commitlint/config-conventional": "8.2.0",
"@semantic-release/changelog": "3.0.6",
"@semantic-release/git": "7.0.18",
"@storybook/react": "5.2.8",
"babel-loader": "^8.0.5",
"babel-preset-minify": "^0.5.1",
"clean-webpack-plugin": "^1.0.1",
"css-loader": "^2.1.0",
"dotenv-webpack": "^1.7.0",
"html-webpack-plugin": "^3.2.0",
"husky": "3.1.0",
"mini-css-extract-plugin": "0.8.0",
"node-sass": "^4.12.0",
"optimize-css-assets-webpack-plugin": "5.0.3",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"resolve-url-loader": "^3.0.0",
Expand All @@ -62,6 +66,7 @@
"slate-plain-serializer": "^0.6.34",
"source-map-loader": "^0.2.4",
"style-loader": "^0.23.1",
"terser-webpack-plugin": "2.2.1",
"webpack": "^4.29.0",
"webpack-cli": "^3.2.1",
"webpack-dev-server": "^3.1.14",
Expand Down
170 changes: 1 addition & 169 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,169 +1 @@
import React, {useMemo, useState, useRef} from 'react';
import ReactDOM from 'react-dom';

import MessageComposer from './components/MessageComposer';

import './styles.scss';

let ids = 1;
const users = [{
id: 'all',
displayName: 'All',
objectType: 'groupMention',
},{
displayName: 'Philip Fry',
},{
displayName: 'Turanga Leela',
},{
displayName: 'Hubert Farnsworth',
},{
displayName: 'Zapp Brannigan',
},{
displayName: 'John Zoidberg',
},{
displayName: 'Amy Wang',
},{
displayName: 'Bender Rodriguez',
},{
displayName: 'Hermes Conrad',
},{
displayName: 'Kif Kroker',
},{
displayName: 'Barbados Slim',
},{
displayName: 'Bill McNeal',
},];

for (const user of users) {
if (!user.id) {
user.id = ids++;
}
if (!user.objectType) {
user.objectType = 'person';
}
}

const mentions = {
filter: (query) =>
Promise.resolve(
(query === '') ? users : users.filter((user) => user.displayName.toLowerCase().startsWith(query.toLowerCase()))
),
renderSuggestion: (user, {active}) => {
const activeStyle = active ? {backgroundColor: 'lightblue'} : null;
const style = {
...activeStyle,
height: '30px',
width: '200px',
};
return {
key: user.id,
render: (<div style={style}>{user.displayName}</div>),
};
},
renderInsert: (user) => {
const style = {background: 'lightblue'};
const text = (user.objectType === 'person') ? user.displayName.split(' ')[0] : user.displayName;
return <b style={style}>{text}</b>;
},
getDisplay: (user) => {
return (user.objectType === 'person') ? user.displayName.split(' ')[0] : user.displayName;
},
}

const spaces = [];
const setValue = (v, num) => {
spaces[num] = v;
};


const Example = (props) => {
const [message, setMessage] = useState('');
const [number, setNumber] = useState(1);

const show = (num) => {
setMessage('');
setNumber(num);
};

const other = (number === 1) ? 2 : 1;

const draft = {
id: number,
value: spaces[number],
save: setValue,
};

const emitter = useRef();
const setEmitter = (e) => {
emitter.current = e;
};

const focus = (e) => {
e.preventDefault();
emitter.current.emit('FOCUS');
};

const insertText = (t) => (e) => {
e.preventDefault();
emitter.current.emit('INSERT_TEXT', t);
}

const send = (e) => {
e.preventDefault();
emitter.current.emit('SEND');
}

const notifyKeyDown = (event) => {
console.log('Key pressed', event);
}

const [disabled, setDisabled] = useState(false);
const toggleDisabled = () => {
setDisabled(!disabled);
}

const [isMarkdownDisabled, setMarkdownDisabled] = useState(false);
const toggleMarkdownDisabled = () => {
setMarkdownDisabled(!isMarkdownDisabled);
};

const [placeholder, setPlaceholder] = useState('Write your message in this space.');
const changePlaceholder = () => {
setPlaceholder('This is a new placeholder');
};

const markdown = useMemo(() => ({
disabled: isMarkdownDisabled,
}), [isMarkdownDisabled]);

return (
<div className='container'>
<div className='content' onClick={focus} />
<div className='mc'>
<MessageComposer
disabled={disabled}
draft={draft}
markdown={markdown}
mentions={mentions}
send={(message) => setMessage(message)}
notifyKeyDown={notifyKeyDown}
placeholder={placeholder}
setEmitter={setEmitter} />
<br/>
<div>Sending: {JSON.stringify(message)}</div>
<button onClick={() => show(other)}>Show Space {other}</button>
<button onClick={insertText('🎉')}>Insert Emoji</button>
<button onClick={insertText('@')}>@Mention</button>
<button onClick={toggleDisabled}>{(disabled) ? 'enable' : 'disable'}</button>
<button onClick={toggleMarkdownDisabled}>{(isMarkdownDisabled) ? 'enable markdown' : 'disable markdown'}</button>
<button onClick={send}>SEND</button>
<button onClick={changePlaceholder}>Change placeholder</button>
</div>
</div>
);
}

ReactDOM.render(
<Example />,
document.getElementById("root")
);
export {default} from './components/MessageComposer/index.js';
Loading

0 comments on commit 0845a2c

Please sign in to comment.