fune/devtools/client/webconsole/new-console-output/components/console-output.js
Peter Elmers 6d74ac0231 Bug 1287958 - New console frontend: Add keys to messages. r=bgrins
--HG--
extra : rebase_source : c16e69da3e6c0a824316f3a97001d4b84efd1960
2016-07-19 15:41:39 -07:00

68 lines
2 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const {
createClass,
createFactory,
DOM: dom,
PropTypes
} = require("devtools/client/shared/vendor/react");
const ReactDOM = require("devtools/client/shared/vendor/react-dom");
const { connect } = require("devtools/client/shared/vendor/react-redux");
const { getAllMessages } = require("devtools/client/webconsole/new-console-output/selectors/messages");
const MessageContainer = createFactory(require("devtools/client/webconsole/new-console-output/components/message-container").MessageContainer);
const ConsoleOutput = createClass({
propTypes: {
jsterm: PropTypes.object.isRequired,
// This function is created in mergeProps
openVariablesView: PropTypes.func.isRequired,
messages: PropTypes.array.isRequired
},
displayName: "ConsoleOutput",
componentWillUpdate() {
let node = ReactDOM.findDOMNode(this);
if (node.lastChild) {
this.shouldScrollBottom = isScrolledToBottom(node.lastChild, node);
}
},
componentDidUpdate() {
if (this.shouldScrollBottom) {
let node = ReactDOM.findDOMNode(this);
node.scrollTop = node.scrollHeight;
}
},
render() {
let messageNodes = this.props.messages.map(function (message) {
return (
MessageContainer({ message, key: message.id })
);
});
return (
dom.div({className: "webconsole-output"}, messageNodes)
);
}
});
function isScrolledToBottom(outputNode, scrollNode) {
let lastNodeHeight = outputNode.lastChild ?
outputNode.lastChild.clientHeight : 0;
return scrollNode.scrollTop + scrollNode.clientHeight >=
scrollNode.scrollHeight - lastNodeHeight / 2;
}
function mapStateToProps(state) {
return {
messages: getAllMessages(state)
};
}
module.exports = connect(mapStateToProps)(ConsoleOutput);