0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-10 02:42:43 +00:00

Merge snippets between theme and basetheme

- Current theme has preference over the base theme
- Snippets with the same name are overwritten by the current theme.
- If the overwritten snippet has no `gen` property (or `gen` is falsy), it will remove the base Theme snippet.
- Snippets not overwritten will carry over from base Theme
- New snippets will be added on to base Theme
This commit is contained in:
Trevor Buckner
2022-05-16 01:40:40 -04:00
parent 4167026ca7
commit 4f6555b522
3 changed files with 25 additions and 17 deletions

View File

@@ -248,6 +248,7 @@ const Editor = createClass({
onInject={this.handleInject} onInject={this.handleInject}
showEditButtons={this.props.showEditButtons} showEditButtons={this.props.showEditButtons}
renderer={this.props.renderer} renderer={this.props.renderer}
theme={this.props.brew.theme}
undo={this.undo} undo={this.undo}
redo={this.redo} redo={this.redo}
historySize={this.historySize()} /> historySize={this.historySize()} />

View File

@@ -6,12 +6,12 @@ const cx = require('classnames');
//Import all themes //Import all themes
const ThemesSettings = require('themes/themes.json'); const Themes = require('themes/themes.json');
const Themes = {}; const ThemeSnippets = {};
Themes['Legacy_5ePHB'] = require('themes/Legacy/5ePHB/snippets.js'); ThemeSnippets['Legacy_5ePHB'] = require('themes/Legacy/5ePHB/snippets.js');
Themes['V3_5ePHB'] = require('themes/V3/5ePHB/snippets.js'); ThemeSnippets['V3_5ePHB'] = require('themes/V3/5ePHB/snippets.js');
Themes['V3_5eDMG'] = require('themes/V3/5eDMG/snippets.js'); ThemeSnippets['V3_5eDMG'] = require('themes/V3/5eDMG/snippets.js');
const execute = function(val, brew){ const execute = function(val, brew){
if(_.isFunction(val)) return val(brew); if(_.isFunction(val)) return val(brew);
@@ -45,7 +45,7 @@ const Snippetbar = createClass({
componentDidMount : async function() { componentDidMount : async function() {
const rendererPath = this.props.renderer == 'V3' ? 'V3' : 'Legacy'; const rendererPath = this.props.renderer == 'V3' ? 'V3' : 'Legacy';
const themePath = this.props.theme ?? '5ePHB'; const themePath = this.props.theme ?? '5ePHB';
let snippets = Themes[`${rendererPath}_${themePath}`]; let snippets = ThemeSnippets[`${rendererPath}_${themePath}`];
snippets = this.compileSnippets(rendererPath, themePath, snippets); snippets = this.compileSnippets(rendererPath, themePath, snippets);
this.setState({ this.setState({
snippets : snippets snippets : snippets
@@ -53,10 +53,11 @@ const Snippetbar = createClass({
}, },
componentDidUpdate : async function(prevProps) { componentDidUpdate : async function(prevProps) {
if(prevProps.renderer != this.props.renderer) { if(prevProps.renderer != this.props.renderer || prevProps.theme != this.props.theme) {
const rendererPath = this.props.renderer == 'V3' ? 'V3' : 'Legacy'; const rendererPath = this.props.renderer == 'V3' ? 'V3' : 'Legacy';
const themePath = this.props.theme ?? '5ePHB'; const themePath = this.props.theme ?? '5ePHB';
let snippets = Themes[`${rendererPath}_${themePath}`]; console.log({ThemeSnippets:ThemeSnippets});
let snippets = ThemeSnippets[`${rendererPath}_${themePath}`];
snippets = this.compileSnippets(rendererPath, themePath, snippets); snippets = this.compileSnippets(rendererPath, themePath, snippets);
this.setState({ this.setState({
snippets : snippets snippets : snippets
@@ -64,17 +65,23 @@ const Snippetbar = createClass({
} }
}, },
mergeCustomizer : function(objValue, srcValue) {
  if (_.isArray(objValue)) {
let result = _.unionBy(srcValue, objValue, 'name'); // Join snippets together, with preference for the current theme over the base theme
return _.filter(result, 'gen'); //Only keep snippets with a 'gen' property.
  }
},
compileSnippets : function(rendererPath, themePath, snippets) { compileSnippets : function(rendererPath, themePath, snippets) {
let compiledSnippets = snippets; let compiledSnippets = snippets;
console.log(ThemesSettings); const baseThemePath = Themes[rendererPath][themePath].baseTheme;
console.log("rendererpath:"); console.log({baseSnippets:ThemeSnippets[`${rendererPath}_${baseThemePath}`]});
console.log(rendererPath); console.log({themeSnippets:compiledSnippets});
console.log("themepath");
console.log(themePath);
const baseThemePath = ThemesSettings[rendererPath][themePath].baseTheme;
if(baseThemePath) { if(baseThemePath) {
compiledSnippets = _.merge(compiledSnippets, Themes[`${rendererPath}_${baseThemePath}`]); compiledSnippets = _.mergeWith([], ThemeSnippets[`${rendererPath}_${baseThemePath}`], compiledSnippets, this.mergeCustomizer);
this.compileSnippets(rendererPath, themePath, compiledSnippets); console.log({compiledSnippets:compiledSnippets});
//this.compileSnippets(rendererPath, themePath, compiledSnippets); (for nested baseThemes)
} }
return compiledSnippets; return compiledSnippets;
}, },

View File

@@ -21,7 +21,7 @@ module.exports = [
{ {
name : 'Column Break', name : 'Column Break',
icon : 'fas fa-columns', icon : 'fas fa-columns',
gen : '\n\\column\n' gen : false
}, },
{ {
name : 'New Page', name : 'New Page',