From 0653712295e0ef0ce59c232b8fe995b73c1b3b15 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 24 Sep 2015 20:36:56 +0300 Subject: [PATCH] Initial commit --- .babelrc | 4 + .eslintignore | 2 + .eslintrc | 20 ++++ .gitignore | 5 + .npmignore | 5 + .travis.yml | 3 + CHANGELOG.md | 4 + CODE_OF_CONDUCT.md | 14 +++ LICENSE.md | 21 ++++ README.md | 10 ++ package.json | 61 +++++++++++ src/LogMonitor.js | 196 +++++++++++++++++++++++++++++++++++ src/LogMonitorButton.js | 85 +++++++++++++++ src/LogMonitorEntry.js | 82 +++++++++++++++ src/LogMonitorEntryAction.js | 44 ++++++++ src/index.js | 1 + test/index.spec.js | 0 17 files changed, 557 insertions(+) create mode 100644 .babelrc create mode 100644 .eslintignore create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 .travis.yml create mode 100644 CHANGELOG.md create mode 100644 CODE_OF_CONDUCT.md create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 package.json create mode 100644 src/LogMonitor.js create mode 100644 src/LogMonitorButton.js create mode 100644 src/LogMonitorEntry.js create mode 100644 src/LogMonitorEntryAction.js create mode 100644 src/index.js create mode 100644 test/index.spec.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..15d27ad --- /dev/null +++ b/.babelrc @@ -0,0 +1,4 @@ +{ + "stage": 0, + "loose": "all" +} diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..81b0eb7 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +lib +**/node_modules diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..47dc057 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,20 @@ +{ + "extends": "eslint-config-airbnb", + "env": { + "browser": true, + "mocha": true, + "node": true + }, + "rules": { + "react/jsx-uses-react": 2, + "react/jsx-uses-vars": 2, + "react/react-in-jsx-scope": 2, + "no-console": 0, + // Temporarily disabled due to babel-eslint issues: + "block-scoped-var": 0, + "padded-blocks": 0, + }, + "plugins": [ + "react" + ] +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c0f0a02 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +node_modules +*.log +.DS_Store +lib +coverage diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..0b80f42 --- /dev/null +++ b/.npmignore @@ -0,0 +1,5 @@ +.DS_Store +*.log +src +test +coverage diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..c42701f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: + - "iojs" diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..20aca58 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,4 @@ +# Change log + +This project adheres to [Semantic Versioning](http://semver.org/). +See all notable changes on [Releases](https://github.com/gaearon/redux-devtools-log-monitor/releases) page. diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..a0fd0d6 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,14 @@ +# Contributor Code of Conduct + +As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. + +We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion. + +Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team. + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. + +This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/) + diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..af2353d --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Dan Abramov + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..004473a --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +Redux DevTools Log Monitor +========================= + +The default monitor for [Redux DevTools](https://github.com/gaearon/redux-devtools). + +![](http://i.imgur.com/J4GeW0M.gif) + +### License + +MIT diff --git a/package.json b/package.json new file mode 100644 index 0000000..7b69543 --- /dev/null +++ b/package.json @@ -0,0 +1,61 @@ +{ + "name": "redux-devtools-log-monitor", + "version": "1.0.0-alpha", + "description": "The default tree view monitor for Redux DevTools", + "main": "lib/index.js", + "scripts": { + "clean": "rimraf lib", + "build": "babel src --out-dir lib", + "lint": "eslint src test", + "test": "NODE_ENV=test mocha --compilers js:babel/register --recursive", + "test:watch": "NODE_ENV=test mocha --compilers js:babel/register --recursive --watch", + "test:cov": "babel-node ./node_modules/.bin/isparta cover ./node_modules/.bin/_mocha -- --recursive", + "prepublish": "npm run lint && npm run test && npm run clean && npm run build" + }, + "repository": { + "type": "git", + "url": "https://github.com/gaearon/redux-devtools-log-monitor.git" + }, + "keywords": [ + "redux", + "devtools", + "flux", + "react", + "hot reloading", + "time travel", + "live edit" + ], + "author": "Dan Abramov (http://github.com/gaearon)", + "license": "MIT", + "bugs": { + "url": "https://github.com/gaearon/redux-devtools-log-monitor/issues" + }, + "homepage": "https://github.com/gaearon/redux-devtools-log-monitor", + "devDependencies": { + "babel": "^5.5.8", + "babel-core": "^5.6.18", + "babel-eslint": "^3.1.15", + "babel-loader": "^5.1.4", + "eslint": "^0.23", + "eslint-config-airbnb": "0.0.6", + "eslint-plugin-react": "^2.3.0", + "expect": "^1.6.0", + "isparta": "^3.0.3", + "jsdom": "^6.5.1", + "mocha": "^2.2.5", + "mocha-jsdom": "^1.0.0", + "react": "^0.14.0-rc1", + "react-addons-test-utils": "^0.14.0-rc1", + "react-dom": "^0.14.0-rc1", + "rimraf": "^2.3.4", + "webpack": "^1.11.0" + }, + "peerDependencies": { + "redux-devtools": "^3.0.0-alpha" + }, + "dependencies": { + "color": "^0.10.1", + "react-json-tree": "^0.1.9", + "redux-devtools-themes": "^1.0.0" + } +} diff --git a/src/LogMonitor.js b/src/LogMonitor.js new file mode 100644 index 0000000..75cfa53 --- /dev/null +++ b/src/LogMonitor.js @@ -0,0 +1,196 @@ +import React, { PropTypes, Component } from 'react'; +import { findDOMNode } from 'react-dom'; +import { connectMonitor } from 'redux-devtools'; +import LogMonitorEntry from './LogMonitorEntry'; +import LogMonitorButton from './LogMonitorButton'; +import * as themes from 'redux-devtools-themes'; + +const styles = { + container: { + fontFamily: 'monaco, Consolas, Lucida Console, monospace', + position: 'relative', + overflowY: 'hidden', + width: '100%', + height: '100%', + minWidth: 300 + }, + buttonBar: { + textAlign: 'center', + borderBottomWidth: 1, + borderBottomStyle: 'solid', + borderColor: 'transparent', + zIndex: 1, + display: 'flex', + flexDirection: 'row' + }, + elements: { + position: 'absolute', + left: 0, + right: 0, + top: 38, + bottom: 0, + overflowX: 'hidden', + overflowY: 'auto' + } +}; + +class LogMonitor extends Component { + constructor(props) { + super(props); + if (typeof window !== 'undefined') { + window.addEventListener('keydown', ::this.handleKeyPress); + } + } + + static propTypes = { + computedStates: PropTypes.array.isRequired, + currentStateIndex: PropTypes.number.isRequired, + monitorState: PropTypes.object.isRequired, + stagedActions: PropTypes.array.isRequired, + skippedActions: PropTypes.object.isRequired, + reset: PropTypes.func.isRequired, + commit: PropTypes.func.isRequired, + rollback: PropTypes.func.isRequired, + sweep: PropTypes.func.isRequired, + toggleAction: PropTypes.func.isRequired, + jumpToState: PropTypes.func.isRequired, + setMonitorState: PropTypes.func.isRequired, + select: PropTypes.func.isRequired, + visibleOnLoad: PropTypes.bool + }; + + static defaultProps = { + select: (state) => state, + monitorState: { isVisible: true }, + theme: 'nicinabox', + visibleOnLoad: true + }; + + componentWillReceiveProps(nextProps) { + const node = findDOMNode(this.refs.elements); + if (!node) { + this.scrollDown = true; + } else if ( + this.props.stagedActions.length < nextProps.stagedActions.length + ) { + const { scrollTop, offsetHeight, scrollHeight } = node; + + this.scrollDown = Math.abs( + scrollHeight - (scrollTop + offsetHeight) + ) < 20; + } else { + this.scrollDown = false; + } + } + + componentDidUpdate() { + const node = findDOMNode(this.refs.elements); + if (!node) { + return; + } + if (this.scrollDown) { + const { offsetHeight, scrollHeight } = node; + node.scrollTop = scrollHeight - offsetHeight; + this.scrollDown = false; + } + } + + componentWillMount() { + let visibleOnLoad = this.props.visibleOnLoad; + const { monitorState } = this.props; + this.props.setMonitorState({ + ...monitorState, + isVisible: visibleOnLoad + }); + } + + handleRollback() { + this.props.rollback(); + } + + handleSweep() { + this.props.sweep(); + } + + handleCommit() { + this.props.commit(); + } + + handleToggleAction(index) { + this.props.toggleAction(index); + } + + handleReset() { + this.props.reset(); + } + + handleKeyPress(event) { + const { monitorState } = this.props; + + if (event.ctrlKey && event.keyCode === 72) { // Ctrl+H + event.preventDefault(); + this.props.setMonitorState({ + ...monitorState, + isVisible: !monitorState.isVisible + }); + } + } + + render() { + const elements = []; + const { monitorState, skippedActions, stagedActions, computedStates, select } = this.props; + + let theme; + if (typeof this.props.theme === 'string') { + if (typeof themes[this.props.theme] !== 'undefined') { + theme = themes[this.props.theme]; + } else { + console.warn('DevTools theme ' + this.props.theme + ' not found, defaulting to nicinabox'); + theme = themes.nicinabox; + } + } else { + theme = this.props.theme; + } + + if (!monitorState.isVisible) { + return null; + } + + for (let i = 0; i < stagedActions.length; i++) { + const action = stagedActions[i]; + const { state, error } = computedStates[i]; + let previousState; + if (i > 0) { + previousState = computedStates[i - 1].state; + } + elements.push( + + ); + } + + return ( +
+
+ Reset + Revert + skippedActions[key])}>Sweep + 1}>Commit +
+
+ {elements} +
+
+ ); + } +} + +export default connectMonitor(LogMonitor); diff --git a/src/LogMonitorButton.js b/src/LogMonitorButton.js new file mode 100644 index 0000000..8b28dbe --- /dev/null +++ b/src/LogMonitorButton.js @@ -0,0 +1,85 @@ +import React from 'react'; +import color from 'color'; + +const styles = { + base: { + cursor: 'pointer', + fontWeight: 'bold', + borderRadius: 3, + padding: 4, + marginLeft: 3, + marginRight: 3, + marginTop: 5, + marginBottom: 5, + flexGrow: 1, + display: 'inline-block', + fontSize: '0.8em', + color: 'white', + textDecoration: 'none' + } +}; + +export default class LogMonitorButton extends React.Component { + constructor(props) { + super(props); + this.state = { + hovered: false, + active: false + }; + } + + handleMouseEnter() { + this.setState({ hovered: true }); + } + + handleMouseLeave() { + this.setState({ hovered: false }); + } + + handleMouseDown() { + this.setState({ active: true }); + } + + handleMouseUp() { + this.setState({ active: false }); + } + + onClick() { + if (!this.props.enabled) { + return; + } + if (this.props.onClick) { + this.props.onClick(); + } + } + + render() { + let style = { + ...styles.base, + backgroundColor: this.props.theme.base02 + }; + if (this.props.enabled && this.state.hovered) { + style = { + ...style, + backgroundColor: color(this.props.theme.base02).lighten(0.2).hexString() + }; + } + if (!this.props.enabled) { + style = { + ...style, + opacity: 0.2, + cursor: 'text', + backgroundColor: 'transparent' + }; + } + return ( + + {this.props.children} + + ); + } +} diff --git a/src/LogMonitorEntry.js b/src/LogMonitorEntry.js new file mode 100644 index 0000000..e9edfb9 --- /dev/null +++ b/src/LogMonitorEntry.js @@ -0,0 +1,82 @@ +import React, { PropTypes, Component } from 'react'; +import JSONTree from 'react-json-tree'; +import LogMonitorEntryAction from './LogMonitorEntryAction'; + +const styles = { + entry: { + display: 'block', + WebkitUserSelect: 'none' + }, + tree: { + paddingLeft: 0 + } +}; + +export default class LogMonitorEntry extends Component { + static propTypes = { + index: PropTypes.number.isRequired, + state: PropTypes.object.isRequired, + action: PropTypes.object.isRequired, + select: PropTypes.func.isRequired, + error: PropTypes.string, + onActionClick: PropTypes.func.isRequired, + collapsed: PropTypes.bool + }; + + printState(state, error) { + let errorText = error; + if (!errorText) { + try { + return ; + } catch (err) { + errorText = 'Error selecting state.'; + } + } + return ( +
+ {errorText} +
+ ); + } + + handleActionClick() { + const { index, onActionClick } = this.props; + if (index > 0) { + onActionClick(index); + } + } + + render() { + const { index, error, action, state, collapsed } = this.props; + const styleEntry = { + opacity: collapsed ? 0.5 : 1, + cursor: (index > 0) ? 'pointer' : 'default' + }; + return ( +
+ + {!collapsed && +
+ {this.printState(state, error)} +
+ } +
+ ); + } +} diff --git a/src/LogMonitorEntryAction.js b/src/LogMonitorEntryAction.js new file mode 100644 index 0000000..1468038 --- /dev/null +++ b/src/LogMonitorEntryAction.js @@ -0,0 +1,44 @@ +import React from 'react'; +import JSONTree from 'react-json-tree'; + +const styles = { + actionBar: { + paddingTop: 8, + paddingBottom: 7, + paddingLeft: 16 + }, + payload: { + margin: 0, + overflow: 'auto' + } +}; + +export default class LogMonitorAction extends React.Component { + renderPayload(payload) { + return ( +
+ { Object.keys(payload).length > 0 ? : '' } +
+ ); + } + + render() { + const { type, ...payload } = this.props.action; + return ( +
+
+ {type} +
+ {!this.props.collapsed ? this.renderPayload(payload) : ''} +
+ ); + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..0a3a289 --- /dev/null +++ b/src/index.js @@ -0,0 +1 @@ +export default from './LogMonitor'; diff --git a/test/index.spec.js b/test/index.spec.js new file mode 100644 index 0000000..e69de29