0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 16:22:44 +00:00

Fix merging snippets with base snippets.

This commit is contained in:
Trevor Buckner
2022-08-04 22:20:29 -04:00
parent 993bcc2719
commit 3da2d094a0
3 changed files with 22 additions and 70 deletions

View File

@@ -13,6 +13,7 @@ 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');
ThemeSnippets['V3_Journal'] = require('themes/V3/Journal/snippets.js');
ThemeSnippets['V3_Blank'] = require('themes/V3/Blank/snippets.js');
const execute = function(val, brew){
if(_.isFunction(val)) return val(brew);
@@ -46,7 +47,7 @@ const Snippetbar = createClass({
componentDidMount : async function() {
const rendererPath = this.props.renderer == 'V3' ? 'V3' : 'Legacy';
const themePath = this.props.theme ?? '5ePHB';
let snippets = ThemeSnippets[`${rendererPath}_${themePath}`];
let snippets = _.cloneDeep(ThemeSnippets[`${rendererPath}_${themePath}`]);
snippets = this.compileSnippets(rendererPath, themePath, snippets);
this.setState({
snippets : snippets
@@ -57,8 +58,7 @@ const Snippetbar = createClass({
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';
console.log({ ThemeSnippets: ThemeSnippets });
let snippets = ThemeSnippets[`${rendererPath}_${themePath}`];
let snippets = _.cloneDeep(ThemeSnippets[`${rendererPath}_${themePath}`]);
snippets = this.compileSnippets(rendererPath, themePath, snippets);
this.setState({
snippets : snippets
@@ -66,9 +66,9 @@ const Snippetbar = createClass({
}
},
mergeCustomizer : function(objValue, srcValue) {
if(_.isArray(objValue)) {
const result = _.unionBy(srcValue, objValue, 'name'); // Join snippets together, with preference for the current theme over the base theme
mergeCustomizer : function(valueA, valueB, key) {
if(key == "snippets") {
const result = _.unionBy(valueB, valueA, '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.
}
},
@@ -76,13 +76,16 @@ const Snippetbar = createClass({
compileSnippets : function(rendererPath, themePath, snippets) {
let compiledSnippets = snippets;
const baseSnippetsPath = Themes[rendererPath][themePath].baseSnippets;
//console.log({ baseSnippets: ThemeSnippets[`${rendererPath}_${baseSnippetsPath}`] });
//console.log({ themeSnippets: compiledSnippets });
let objB = _.keyBy(compiledSnippets, 'groupName');
if(baseSnippetsPath) {
compiledSnippets = _.mergeWith([], ThemeSnippets[`${rendererPath}_${baseSnippetsPath}`], compiledSnippets, this.mergeCustomizer);
console.log({ compiledSnippets: compiledSnippets });
//this.compileSnippets(rendererPath, themePath, compiledSnippets); (for nested baseSnippets)
let objA = _.keyBy(_.cloneDeep(ThemeSnippets[`${rendererPath}_${baseSnippetsPath}`], 'groupName'));
compiledSnippets = _.values(_.mergeWith(objA, objB, this.mergeCustomizer));
}
else {
let objA = _.keyBy(_.cloneDeep(ThemeSnippets[`${rendererPath}_Blank`], 'groupName'));
compiledSnippets = _.values(_.mergeWith(objA, objB, this.mergeCustomizer));
}
return compiledSnippets;
},

View File

@@ -6,7 +6,6 @@ const MonsterBlockGen = require('./snippets/monsterblock.gen.js');
const ClassFeatureGen = require('./snippets/classfeature.gen.js');
const CoverPageGen = require('./snippets/coverpage.gen.js');
const TableOfContentsGen = require('./snippets/tableOfContents.gen.js');
const WatercolorGen = require('./snippets/watercolor.gen.js');
const dedent = require('dedent-tabs').default;
@@ -18,48 +17,6 @@ module.exports = [
icon : 'fas fa-pencil-alt',
view : 'text',
snippets : [
{
name : 'Column Break',
icon : 'fas fa-columns',
gen : '\n\\column\n'
},
{
name : 'New Page',
icon : 'fas fa-file-alt',
gen : '\n\\page\n'
},
{
name : 'Vertical Spacing',
icon : 'fas fa-arrows-alt-v',
gen : '\n::::\n'
},
{
name : 'Horizontal Spacing',
icon : 'fas fa-arrows-alt-h',
gen : ' {{width:100px}} '
},
{
name : 'Wide Block',
icon : 'fas fa-window-maximize',
gen : dedent`\n
{{wide
Everything in here will be extra wide. Tables, text, everything!
Beware though, CSS columns can behave a bit weird sometimes. You may
have to manually place column breaks with \`\column\` to make the
surrounding text flow with this wide block the way you want.
}}
\n`
},
{
name : 'QR Code',
icon : 'fas fa-qrcode',
gen : (brew)=>{
return `![]` +
`(https://api.qrserver.com/v1/create-qr-code/?data=` +
`https://homebrewery.naturalcrit.com${brew.shareId ? `/share/${brew.shareId}` : ''}` +
`&size=100x100) {width:100px;mix-blend-mode:multiply}`;
}
},
{
name : 'Page Number',
icon : 'fas fa-bookmark',
@@ -70,21 +27,11 @@ module.exports = [
icon : 'fas fa-sort-numeric-down',
gen : '{{pageNumber,auto}}\n{{footnote PART 1 | SECTION NAME}}\n\n'
},
{
name : 'Link to page',
icon : 'fas fa-link',
gen : '[Click here](#p3) to go to page 3\n'
},
{
name : 'Table of Contents',
icon : 'fas fa-book',
gen : TableOfContentsGen
},
{
name : 'Add Comment',
icon : 'fas fa-code',
gen : '<!-- This is a comment that will not be rendered into your brew. Hotkey (Ctrl/Cmd + /). -->'
},
}
]
},
{
@@ -151,11 +98,6 @@ module.exports = [
[naturalcrit](https://homebrew.naturalcrit.com)
}}`
},
{
name : 'Watercolor Splatter',
icon : 'fas fa-fill-drip',
gen : WatercolorGen,
},
{
name : 'Watermark',
icon : 'fas fa-id-card',

View File

@@ -22,6 +22,13 @@
"baseSnippets": false,
"path": "5ePHB"
},
"Blank": {
"name": "Blank",
"renderer": "V3",
"baseTheme": false,
"baseSnippets": false,
"path": "Blank"
},
"Journal": {
"name": "Journal",
"renderer": "V3",