|
var App = React.createClass({ |
|
mixins: [ReactMeteor.Mixin], |
|
|
|
startMeteorSubscriptions: function() { |
|
Meteor.subscribe('documents'); |
|
}, |
|
|
|
getMeteorState: function() { |
|
var doc = Documents.findOne({ |
|
title: 'Initial document' |
|
}); |
|
return { |
|
document: doc, |
|
documentContent: doc ? doc.content: '', |
|
users: Meteor.users.find({}) |
|
}; |
|
}, |
|
|
|
handleChange: function(event) { |
|
var text = event.target.value; |
|
this.setState({ |
|
documentContent: text, |
|
cursor: event.target.selectionStart, |
|
nbChars: event.target.value.length |
|
}); |
|
Meteor.call('updateDocument', |
|
this.state.document._id, |
|
text); |
|
}, |
|
|
|
componentDidUpdate: function() { |
|
var domNode = this.refs.textarea.getDOMNode(), |
|
newNbChars = domNode.value.length, |
|
diffNbChars = newNbChars - this.state.nbChars, |
|
newCursor = this.state.cursor + diffNbChars; |
|
domNode.setSelectionRange( |
|
newCursor, newCursor |
|
); |
|
}, |
|
|
|
render: function() { |
|
var users = this.state.users.map(function(user) { |
|
return (<span class="username">{user.username}</span>); |
|
}); |
|
return ( |
|
<div> |
|
<p>{users}</p> |
|
<textarea ref="textarea" onChange={this.handleChange} |
|
defaultValue={'I am empty'} |
|
value={this.state.documentContent}> |
|
</textarea> |
|
</div> |
|
); |
|
} |
|
}); |
|
|
|
if (Meteor.isClient) { |
|
Meteor.startup(function() { |
|
React.render( |
|
<App />, |
|
document.getElementById('react-root') |
|
); |
|
}); |
|
|
|
Accounts.ui.config({ |
|
passwordSignupFields: 'USERNAME_ONLY' |
|
}); |
|
} |