require('./snippetbar.less'); const React = require('react'); const createClass = require('create-react-class'); const _ = require('lodash'); const cx = require('classnames'); //Import all themes const Themes = {}; Themes['Legacy_5ePHB'] = require('themes/Legacy/5ePHB/snippets.js'); Themes['V3_5ePHB'] = require('themes/V3/5ePHB/snippets.js'); const execute = function(val, brew){ if(_.isFunction(val)) return val(brew); return val; }; const Snippetbar = createClass({ displayName : 'SnippetBar', getDefaultProps : function() { return { brew : {}, view : 'text', onViewChange : ()=>{}, onInject : ()=>{}, onToggle : ()=>{}, showEditButtons : true, renderer : 'legacy', undo : ()=>{}, redo : ()=>{}, historySize : ()=>{} }; }, getInitialState : function() { return { renderer : this.props.renderer, snippets : [] }; }, componentDidMount : async function() { const rendererPath = this.props.renderer == 'V3' ? 'V3' : 'Legacy'; const themePath = this.props.theme ?? '5ePHB'; const snippets = Themes[`${rendererPath}_${themePath}`]; this.setState({ snippets : snippets }); }, componentDidUpdate : async function(prevProps) { if(prevProps.renderer != this.props.renderer) { const rendererPath = this.props.renderer == 'V3' ? 'V3' : 'Legacy'; const themePath = this.props.theme ?? '5ePHB'; const snippets = Themes[`${rendererPath}_${themePath}`]; this.setState({ snippets : snippets }); } }, handleSnippetClick : function(injectedText){ this.props.onInject(injectedText); }, renderSnippetGroups : function(){ let snippets = []; snippets = this.state.snippets.filter((snippetGroup)=>snippetGroup.view === this.props.view); return _.map(snippets, (snippetGroup)=>{ return ; }); }, renderEditorButtons : function(){ if(!this.props.showEditButtons) return; return
this.props.onViewChange('text')}>
this.props.onViewChange('style')}>
this.props.onViewChange('meta')}>
; }, render : function(){ return
{this.renderSnippetGroups()} {this.renderEditorButtons()}
; } }); module.exports = Snippetbar; const SnippetGroup = createClass({ displayName : 'SnippetGroup', getDefaultProps : function() { return { brew : {}, groupName : '', icon : 'fas 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
this.handleSnippetClick(snippet)}> {snippet.name}
; }); }, render : function(){ return
{this.props.groupName}
{this.renderSnippets()}
; }, });