0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-26 09:32:51 +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}
showEditButtons={this.props.showEditButtons}
renderer={this.props.renderer}
theme={this.props.brew.theme}
undo={this.undo}
redo={this.redo}
historySize={this.historySize()} />

View File

@@ -6,12 +6,12 @@ const cx = require('classnames');
//Import all themes
const ThemesSettings = require('themes/themes.json');
const Themes = require('themes/themes.json');
const Themes = {};
Themes['Legacy_5ePHB'] = require('themes/Legacy/5ePHB/snippets.js');
Themes['V3_5ePHB'] = require('themes/V3/5ePHB/snippets.js');
Themes['V3_5eDMG'] = require('themes/V3/5eDMG/snippets.js');
const ThemeSnippets = {};
ThemeSnippets['Legacy_5ePHB'] = require('themes/Legacy/5ePHB/snippets.js');
ThemeSnippets['V3_5ePHB'] = require('themes/V3/5ePHB/snippets.js');
ThemeSnippets['V3_5eDMG'] = require('themes/V3/5eDMG/snippets.js');
const execute = function(val, brew){
if(_.isFunction(val)) return val(brew);
@@ -45,7 +45,7 @@ const Snippetbar = createClass({
componentDidMount : async function() {
const rendererPath = this.props.renderer == 'V3' ? 'V3' : 'Legacy';
const themePath = this.props.theme ?? '5ePHB';
let snippets = Themes[`${rendererPath}_${themePath}`];
let snippets = ThemeSnippets[`${rendererPath}_${themePath}`];
snippets = this.compileSnippets(rendererPath, themePath, snippets);
this.setState({
snippets : snippets
@@ -53,10 +53,11 @@ const Snippetbar = createClass({
},
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 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);
this.setState({
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) {
let compiledSnippets = snippets;
console.log(ThemesSettings);
console.log("rendererpath:");
console.log(rendererPath);
console.log("themepath");
console.log(themePath);
const baseThemePath = ThemesSettings[rendererPath][themePath].baseTheme;
const baseThemePath = Themes[rendererPath][themePath].baseTheme;
console.log({baseSnippets:ThemeSnippets[`${rendererPath}_${baseThemePath}`]});
console.log({themeSnippets:compiledSnippets});
if(baseThemePath) {
compiledSnippets = _.merge(compiledSnippets, Themes[`${rendererPath}_${baseThemePath}`]);
this.compileSnippets(rendererPath, themePath, compiledSnippets);
compiledSnippets = _.mergeWith([], ThemeSnippets[`${rendererPath}_${baseThemePath}`], compiledSnippets, this.mergeCustomizer);
console.log({compiledSnippets:compiledSnippets});
//this.compileSnippets(rendererPath, themePath, compiledSnippets); (for nested baseThemes)
}
return compiledSnippets;
},

View File

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