mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-02 02:02:43 +00:00
When adding the Class Feature snippet, all curent text in the document is inserted in place of the randomly generated class name, creating several duplicates of the document and generally creating a mess. See #413 which was closed but the issue was never fixed. Most of the snippet code generators do not have any input arguments in the `module.exports` function, so they don't have this issue. However, Table of Contents needs to parse the text in the brew, so it is passed a copy of the brew document. Unfortunately, Class Feature (classfeature.gen.js) also has an input parameter for when it is used as part of the Full Class snippet. Thus, the unintended consequence occurs in snippetbar.jsx in the `execute` function. This fix simply adds a check to the execute function and only passes in the brew as an argument if the clicked snipped actually is the Table of Contents. Some restructuring might later reveal a cleaner way to do this rather than an awkward special case check like this.
97 lines
2.1 KiB
JavaScript
97 lines
2.1 KiB
JavaScript
const React = require('react');
|
|
const createClass = require('create-react-class');
|
|
const _ = require('lodash');
|
|
const cx = require('classnames');
|
|
|
|
|
|
const Snippets = require('./snippets/snippets.js');
|
|
|
|
const execute = function(val, brew){
|
|
if(snippet.name == 'Table of Contents')
|
|
return val(brew);
|
|
else if(_.isFunction(val)) return val();
|
|
return val;
|
|
};
|
|
|
|
|
|
|
|
const Snippetbar = createClass({
|
|
getDefaultProps : function() {
|
|
return {
|
|
brew : '',
|
|
onInject : ()=>{},
|
|
onToggle : ()=>{},
|
|
showmeta : false
|
|
};
|
|
},
|
|
|
|
handleSnippetClick : function(injectedText){
|
|
this.props.onInject(injectedText);
|
|
},
|
|
|
|
renderSnippetGroups : function(){
|
|
return _.map(Snippets, (snippetGroup)=>{
|
|
return <SnippetGroup
|
|
brew={this.props.brew}
|
|
groupName={snippetGroup.groupName}
|
|
icon={snippetGroup.icon}
|
|
snippets={snippetGroup.snippets}
|
|
key={snippetGroup.groupName}
|
|
onSnippetClick={this.handleSnippetClick}
|
|
/>;
|
|
});
|
|
},
|
|
|
|
render : function(){
|
|
return <div className='snippetBar'>
|
|
{this.renderSnippetGroups()}
|
|
<div className={cx('toggleMeta', { selected: this.props.showmeta })}
|
|
onClick={this.props.onToggle}>
|
|
<i className='fa fa-bars' />
|
|
</div>
|
|
</div>;
|
|
}
|
|
});
|
|
|
|
module.exports = Snippetbar;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const SnippetGroup = createClass({
|
|
getDefaultProps : function() {
|
|
return {
|
|
brew : '',
|
|
groupName : '',
|
|
icon : 'fa-rocket',
|
|
snippets : [],
|
|
onSnippetClick : function(){},
|
|
};
|
|
},
|
|
handleSnippetClick : function(snippet){
|
|
this.props.onSnippetClick(execute(snippet.gen, this.props.brew));
|
|
},
|
|
renderSnippets : function(){
|
|
return _.map(this.props.snippets, (snippet)=>{
|
|
return <div className='snippet' key={snippet.name} onClick={()=>this.handleSnippetClick(snippet)}>
|
|
<i className={`fa fa-fw ${snippet.icon}`} />
|
|
{snippet.name}
|
|
</div>;
|
|
});
|
|
},
|
|
|
|
render : function(){
|
|
return <div className='snippetGroup'>
|
|
<div className='text'>
|
|
<i className={`fa fa-fw ${this.props.icon}`} />
|
|
<span className='groupName'>{this.props.groupName}</span>
|
|
</div>
|
|
<div className='dropdown'>
|
|
{this.renderSnippets()}
|
|
</div>
|
|
</div>;
|
|
},
|
|
|
|
}); |