From 3da2d094a0de9b35187421b7fc0ca22b129cb434 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Thu, 4 Aug 2022 22:20:29 -0400 Subject: [PATCH] Fix merging snippets with base snippets. --- .../homebrew/editor/snippetbar/snippetbar.jsx | 25 ++++---- themes/V3/5ePHB/snippets.js | 60 +------------------ themes/themes.json | 7 +++ 3 files changed, 22 insertions(+), 70 deletions(-) diff --git a/client/homebrew/editor/snippetbar/snippetbar.jsx b/client/homebrew/editor/snippetbar/snippetbar.jsx index 03b6a6f5f..92c083383 100644 --- a/client/homebrew/editor/snippetbar/snippetbar.jsx +++ b/client/homebrew/editor/snippetbar/snippetbar.jsx @@ -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; }, diff --git a/themes/V3/5ePHB/snippets.js b/themes/V3/5ePHB/snippets.js index cd0911c1b..5f4070fdf 100644 --- a/themes/V3/5ePHB/snippets.js +++ b/themes/V3/5ePHB/snippets.js @@ -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 : '' - }, + } ] }, { @@ -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', diff --git a/themes/themes.json b/themes/themes.json index 4d5fdadb6..0d28c7394 100644 --- a/themes/themes.json +++ b/themes/themes.json @@ -22,6 +22,13 @@ "baseSnippets": false, "path": "5ePHB" }, + "Blank": { + "name": "Blank", + "renderer": "V3", + "baseTheme": false, + "baseSnippets": false, + "path": "Blank" + }, "Journal": { "name": "Journal", "renderer": "V3",