1
0
mirror of https://github.com/stolksdorf/homebrewery.git synced 2025-12-25 14:51:28 +00:00

Proof of concept working

This commit is contained in:
Scott Tolksdorf
2016-04-02 12:02:05 -04:00
parent e61e1a698d
commit 263257bfb8
16 changed files with 344 additions and 112 deletions

View File

@@ -1,14 +0,0 @@
var React = require('react');
var _ = require('lodash');
var cx = require('classnames');
var SheetEditor = React.createClass({
render : function(){
return <div className='sheetEditor'>
SheetEditor Ready!
</div>
}
});
module.exports = SheetEditor;

View File

@@ -16,10 +16,8 @@ var SheetEditor = React.createClass({
render : function(){
return <div className='sheetEditor'>
SheetEditor Ready!
<h2>Sheet Template</h2>
<textarea value={this.props.code} onChange={this.handleCodeChange} />
</div>
}
});

View File

@@ -1,8 +1,8 @@
.sheetEditor{
textarea{
margin-top: 50px;
height : 500px
height : 300px;
width : 80%;
}
}

View File

@@ -0,0 +1,50 @@
var React = require('react');
var _ = require('lodash');
var cx = require('classnames');
var Box = React.createClass({
getDefaultProps: function() {
return {
data : {},
onChange : function(){},
defaultValue : {},
id : 'box',
};
},
//Maybe remove
id : function(){
return _.snakeCase(this.props.label) || this.props.id;
},
data : function(){
return this.props.data[this.id()] || this.props.defaultValue;
},
handleChange : function(newData){
this.props.onChange({
[this.id()] : _.extend(this.data(), newData)
});
},
renderChildren : function(){
return React.Children.map(this.props.children, (child)=>{
if(!React.isValidElement(child)) return null;
return React.cloneElement(child, {
onChange : this.handleChange,
data : this.data()
})
})
},
render : function(){
return <div className={cx('box', this.props.className)}>
{this.renderChildren()}
</div>
}
});
module.exports = Box;

View File

@@ -0,0 +1,4 @@
module.exports = {
TextInput : require('./textInput/textInput.jsx'),
PlayerInfo : require('./playerInfo/playerInfo.jsx'),
}

View File

@@ -0,0 +1,67 @@
var React = require('react');
var _ = require('lodash');
var cx = require('classnames');
var TextInput = require('../textInput/textInput.jsx');
var Box = require('../box/box.jsx');
var PlayerInfo = React.createClass({
getDefaultProps: function() {
return {
data : {},
onChange : function(){},
id : 'playerInfo',
};
},
/*
id : function(){
return _.snakeCase(this.props.label) || this.props.id;
},
data : function(){
return this.props.data[this.id()] || this.props.defaultValue;
},
handleChange : function(newData){
this.props.onChange({
[this.id()] : _.extend(this.data(), newData)
});
},
renderChildren : function(){
return React.Children.map(this.props.children, (child)=>{
return React.cloneElement(child, {
onChange : this.handleChange,
data : this.data()
})
})
},
*/
render : function(){
return <Box className='playerInfo' {...this.props}>
<TextInput id='name' label="Name" />
<TextInput id='class' label="Class" />
<TextInput id='race' label="Race" />
{this.props.children}
</Box>
}
/*{this.props.children}*/
/*
render : function(){
return <div className='playerInfo'>
<TextInput id='name' label="Name" onChange={this.handleChange} data={this.data()} />
<TextInput id='class' label="Class" onChange={this.handleChange} data={this.data()} />
<TextInput id='race' label="Race" onChange={this.handleChange} data={this.data()} />
{this.renderChildren()}
</div>
}
*/
});
module.exports = PlayerInfo;

View File

@@ -0,0 +1,3 @@
.playerInfo{
margin-bottom: 20px;
}

View File

@@ -0,0 +1,45 @@
var React = require('react');
var _ = require('lodash');
var cx = require('classnames');
var TextInput = React.createClass({
getDefaultProps: function() {
return {
data : {},
defaultValue : '',
onChange : function(){},
id : 'textInput',
label : '',
};
},
id : function(){
return _.snakeCase(this.props.label) || this.props.id;
},
data : function(){
return this.props.data[this.id()] || this.props.defaultValue;
},
handleChange : function(e){
this.props.onChange({
[this.id()] : e.target.value
});
},
renderLabel : function(){
if(!this.props.label) return;
return <label htmlFor={this.id()}>{this.props.label}</label>
},
render : function(){
return <div className='textInput'>
{this.renderLabel()}
<input id={this.id()} type='text' onChange={this.handleChange} value={this.data()} />
</div>
}
});
module.exports = TextInput;

View File

@@ -0,0 +1,6 @@
.textInput{
label{
display: inline-block;
width : 50px;
}
}

View File

@@ -2,7 +2,10 @@ var React = require('react');
var _ = require('lodash');
var cx = require('classnames');
var babel = require('babel-core')
var jsx2json = require('jsx-parser');
var Parts = require('./parts');
var SheetRenderer = React.createClass({
getDefaultProps: function() {
@@ -13,29 +16,57 @@ var SheetRenderer = React.createClass({
};
},
componentWillReceiveProps: function(nextProps) {
/*
augmentProps : function(props, key){
return _.extend({}, props, {
key : key,
data : this.props.characterData,
onChange :
})
},
*/
renderElement : function(node, key){
return React.createElement(
(Parts[node.tag] ? Parts[node.tag] : node.tag),
{key : key, ...node.props},
//this.augmentProps(node.props, key),
...this.renderChildren(node.children))
},
renderChildren : function(nodes){
return _.map(nodes, (node, index)=>{
if(_.isString(node)) return node;
return this.renderElement(node, index);
})
},
renderSheet : function(){
// var render = jsx.transform(this.props.code);
try{
var nodes = jsx2json(this.props.code);
nodes = _.map(nodes, (node)=>{
node.props.data = this.props.characterData;
node.props.onChange = (newData)=>{
this.props.onChange(_.extend(this.props.characterData, newData));
}
return node
})
// return eval(render);
return this.renderChildren(nodes);
}catch(e){
return <div>Error bruh {e.toString()}</div>
}
},
render : function(){
return <div className='sheetRenderer'>
console.log(babel);
return <div className='SheetRenderer'>
<h2>Character Sheet</h2>
<div className='sheetContainer' ref='sheetContainer'>
{this.renderSheet()}
@@ -46,4 +77,15 @@ var SheetRenderer = React.createClass({
}
});
module.exports = SheetRenderer;
/*
<Temp text="cool">yo test <a href="google.com">link</a> </Temp>
*/

View File

@@ -1,3 +1,11 @@
.COM{
.sheetRenderer{
padding-right : 10px;
.sheetContainer{
background-color: white;
padding : 20px;
}
}

View File

@@ -6,34 +6,64 @@ var StatusBar = require('./statusBar/statusBar.jsx');
var SheetEditor = require('./sheetEditor/sheetEditor.jsx');
var SheetRenderer = require('./sheetRenderer/sheetRenderer.jsx');
const SPLATSHEET_TEMPLATE = 'splatsheet_template';
const SPLATSHEET_CHARACTER = 'splatsheet_character';
var SplatSheet = React.createClass({
getInitialState: function() {
return {
sheetCode: '<div>yo test</div>',
characterData : {}
sheetCode: '',
characterData : {
playerInfo : {
name : 'scott',
race : 'human',
class : 'coder'
}
}
};
},
componentDidMount: function() {
this.setState({
sheetCode : localStorage.getItem(SPLATSHEET_TEMPLATE),
characterData : JSON.parse(localStorage.getItem(SPLATSHEET_CHARACTER)) || this.state.characterData
})
},
handleCodeChange : function(text){
this.setState({
sheetCode : text,
});
localStorage.setItem(SPLATSHEET_TEMPLATE, text);
},
handeCharacterChange : function(data){
this.setState({
characterData : data,
});
localStorage.setItem(SPLATSHEET_CHARACTER, JSON.stringify(data));
},
clearCharacterData : function(){
this.handeCharacterChange({});
},
render : function(){
return <div className='splatSheet'>
return <div className='splatsheet'>
<StatusBar />
<div className='paneSplit'>
<div className='leftPane'>
<SheetEditor code={this.state.sheetCode} onChange={this.handleCodeChange} />
<h2>
Character Data
<i className='fa fa-times' style={{color : 'red'}} onClick={this.clearCharacterData} />
</h2>
<pre><code>{JSON.stringify(this.state.characterData, null, ' ')}</code></pre>
</div>
<div className='rightPane'>
<SheetRenderer

View File

@@ -25,12 +25,34 @@ html,body, #reactContainer{
min-height : 100%;
}
.leftPane{
width : 40%;
width : 50%;
}
.rightPane{
overflow-y : scroll;
height : 100%;
width : 60%;
width : 50%;
}
}
}
h2{
color : white;
margin-top: 20px;
text-transform: uppercase;
font-weight: 800;
}
pre{
background-color: black;
padding : 10px;
display: inline-block;
min-width: 200px;
code{
font-size: 0.8em;
font-family: monospace;
color : @teal;
}
}

View File

@@ -55,3 +55,6 @@ gulp.task('phb', function(){
.pipe(gulp.dest('./'));
})
//Maybe remove later?

View File

@@ -1,20 +1,10 @@
var WHITESPACE = /(\s|\t|\n)/g;
var WHITESPACE = /(\s|\t|\n|\r)/g;
var NUMBERS = /[0-9]/;
var LETTERS = /[a-zA-Z_]/;
var tokenMap = {
//'<' : 'brace',
//'>' : 'brace',
//'/' : 'close',
'=' : 'equals',
}
var tokenizer = function(input){
var tokens = [];
var current = 0;
var inTag = false;
while(current < input.length){
@@ -35,7 +25,6 @@ var tokenizer = function(input){
tokens.push({
type : 'closeTag'
})
}
else if(char == '/' && input[current+1] == '>'){
inTag = false;
@@ -57,15 +46,14 @@ var tokenizer = function(input){
type : 'number',
value : getToken(NUMBERS)*1
});
continue;
current--;
}
else if(LETTERS.test(char)){
tokens.push({
type : 'word',
value : getToken(LETTERS)
});
continue;
current--;
}
else if(char == "'"){
char = input[++current]
@@ -73,7 +61,6 @@ var tokenizer = function(input){
type : 'text',
value : getToken(/[^\']/)
});
}
else if(char == '"'){
char = input[++current]
@@ -83,10 +70,8 @@ var tokenizer = function(input){
});
}
}
//Not in a tag def
//Not tokenizing a tag definition
else{
//End tag
if(char == '<' && input[current+1] == '/'){
char = input[++current]
@@ -95,7 +80,6 @@ var tokenizer = function(input){
type : 'endTag',
value : getToken(LETTERS)
})
//current++;
}
else if(char == '<'){
inTag = true;
@@ -104,7 +88,6 @@ var tokenizer = function(input){
type : 'openTag',
value : getToken(LETTERS)
})
console.log(char);
current--;
}
else{
@@ -123,62 +106,49 @@ var tokenizer = function(input){
}
current--;
}
}
current++;
continue;
}
return tokens;
}
var parser = function(tokens){
var nodes = [];
var current = 0;
var token = tokens[current];
var getProps = function(){
var parseProps = function(){
var props = {};
var key = null;
var temp = null;
var last = null;
while(token.type != 'endTag' && token.type != 'closeTag' && current < tokens.length){
if(temp && token.type == 'equals'){
key = temp;
temp = null;
token = tokens[++current];
continue;
}
if(key){
while(current < tokens.length && token.type != 'endTag' && token.type != 'closeTag'){
if(!key && token.type == 'word'){
last = token.value;
}else if(last && token.type == 'equals'){
key = last;
last = null;
}else if(key && (token.type == 'number' || token.type == 'text')){
props[key] = token.value;
key = null;
temp = null;
last = null;
token = tokens[++current];
continue;
}else if(last && token.type == 'word'){
props[last] = true;
}else{
throw "Invalid property value: " + key + '=' + token.value;
}
if(temp){
props[temp] = true;
}
temp = token.value;
token = tokens[++current];
}
return props;
}
var genNode = function(tagType){
token = tokens[++current];
var node = {
tag : tagType,
props : getProps(),
props : parseProps(),
children : getChildren(tagType)
}
return node
@@ -186,7 +156,14 @@ var parser = function(tokens){
var getChildren = function(tagType){
var children = [];
while(current < tokens.length && token.type != 'endTag' && token.value != tagType){
while(current < tokens.length){
if(token.type == 'endTag'){
if(token.value && token.value != tagType){
throw "Invalid closing tag: " + token.value + ". Expected closing tag of type: " + tagType
}else{
break;
}
}
if(token.type == 'openTag'){
children.push(genNode(token.value));
}else if(token.type == 'text'){
@@ -196,43 +173,34 @@ var parser = function(tokens){
}
return children;
}
return getChildren();
}
/*
var test1 = `
why you so cray
<div test="here there 'champ'" more_cool size=0>
<span>Hey there!<a>so fucking cool</a></span>
let's go party!@
we be cray
<div test="hey there champ" more_cool=shoobydo size=0>
<span>
Hey there!
<a>so fucking cool </span> </a>
</span>
let's go party
<a href='neato' />
</div>
<a href='neato' />
`
var test2 = "<div>Hey there!</div>"
var tokens = tokenizer(test1);
console.log(tokens);
console.log(test1, '\n---\n', tokens, '---\n', JSON.stringify(parser(tokens), null, ' '));
*/
console.log(test1, JSON.stringify(parser(tokens), null, ' '));
module.exports = tokenizer;
module.exports = function(input){
return parser(tokenizer(input));
}