From 656edb07ea3746bb4aadebecc572208cf4ac5503 Mon Sep 17 00:00:00 2001 From: David Bolack Date: Mon, 8 Jul 2024 18:12:58 -0500 Subject: [PATCH] Rework detection of user brews to look up themeid in static themes list before assuming is a user brew. Ended up being a fairly straightforward change. A few ternaries got smooshed or inverted. Passes builtin and local tests. Need to compare on the test instance. --- client/homebrew/brewRenderer/brewRenderer.jsx | 25 ++++++++----------- .../editor/metadataEditor/metadataEditor.jsx | 3 +-- .../homebrew/editor/snippetbar/snippetbar.jsx | 2 +- server/homebrew.api.js | 8 +++--- server/homebrew.api.spec.js | 2 +- 5 files changed, 18 insertions(+), 22 deletions(-) diff --git a/client/homebrew/brewRenderer/brewRenderer.jsx b/client/homebrew/brewRenderer/brewRenderer.jsx index d87e720c1..918fdb5c9 100644 --- a/client/homebrew/brewRenderer/brewRenderer.jsx +++ b/client/homebrew/brewRenderer/brewRenderer.jsx @@ -188,27 +188,24 @@ const BrewRenderer = (props)=>{ document.dispatchEvent(new MouseEvent('click')); }; - let brewThemeRendererPath = props?.renderer ? props.renderer : 'Legacy'; - // Correct for casing vs theme.json - if(brewThemeRendererPath == 'legacy') { brewThemeRendererPath = 'Legacy'; } - if(props?.theme && (props?.theme[0] === '#')) { - brewThemeRendererPath = 'Brew'; - } + let brewThemeRendererPath = `${props?.renderer ? _.upperFirst(props.renderer) : 'V3'}`; + let themePath = props.theme ?? '5ePHB'; const Themes = { ...staticThemes, ...props.userThemes }; - const baseThemePath = (themePath && themePath[0] !== '#') ? Themes[brewThemeRendererPath][themePath].baseTheme : 'Brew'; + let staticOrUserParent; + let baseThemePath = null; - // Override static theme values if a Brew theme. - - if(themePath && themePath[0] === '#') { - themePath = themePath.slice(1); + if (!Themes[brewThemeRendererPath].hasOwnProperty(themePath)) { brewThemeRendererPath = ''; + staticOrUserParent = `/cssParent/${themePath}`; + baseThemePath = 'Brew'; } else { + baseThemePath = Themes[brewThemeRendererPath][themePath].baseTheme brewThemeRendererPath += '/'; + staticOrUserParent = `/css/${brewThemeRendererPath}${baseThemePath}`; + } - const staticOrUserParent = (props.theme && props?.theme[0] == '#') ? `/cssParent/${themePath}` : `/css/${brewThemeRendererPath}${baseThemePath}`; - return ( <> {/*render dummy page while iFrame is mounting.*/} @@ -238,7 +235,7 @@ const BrewRenderer = (props)=>{ tabIndex={-1} style={{ height: state.height }}> - + {baseThemePath && } diff --git a/client/homebrew/editor/metadataEditor/metadataEditor.jsx b/client/homebrew/editor/metadataEditor/metadataEditor.jsx index 14a61800b..8f236b833 100644 --- a/client/homebrew/editor/metadataEditor/metadataEditor.jsx +++ b/client/homebrew/editor/metadataEditor/metadataEditor.jsx @@ -213,8 +213,7 @@ const MetadataEditor = createClass({ }); }; - const currentThemePath = this.props.metadata?.theme && this.props.metadata.theme[0] === '#' ? 'Brew' : this.props.metadata.renderer; -// const currentTheme = mergedThemes[`${_.upperFirst(currentThemePath)}`][this.props.metadata.theme]; + const currentThemePath = this.props.metadata?.theme && Themes[_.upperFirst(this.props.metadata.renderer)].hasOwnProperty(this.props.metadata?.theme) ? this.props.metadata.renderer : 'Brew'; const currentTheme = mergedThemes[`${_.upperFirst(currentThemePath)}`].hasOwnProperty(this.props.metadata.theme) ? mergedThemes[`${_.upperFirst(currentThemePath)}`][this.props.metadata.theme] : { name: `!!! THEME MISSING !!! ID=${this.props.metadata.theme.slice(1)}`}; let dropdown; diff --git a/client/homebrew/editor/snippetbar/snippetbar.jsx b/client/homebrew/editor/snippetbar/snippetbar.jsx index 80f7fc7b2..445ca2748 100644 --- a/client/homebrew/editor/snippetbar/snippetbar.jsx +++ b/client/homebrew/editor/snippetbar/snippetbar.jsx @@ -84,7 +84,7 @@ const Snippetbar = createClass({ compileSnippets : function(rendererPath, themePath, snippets) { let compiledSnippets = snippets; - const baseSnippetsPath = themePath && (themePath[0] === '#') ? false : Themes[rendererPath][themePath].baseSnippets; + const baseSnippetsPath = themePath && (Themes[rendererPath].hasOwnProperty(themePath)) ? Themes[rendererPath][themePath].baseSnippets : false; const objB = _.keyBy(compiledSnippets, 'groupName'); diff --git a/server/homebrew.api.js b/server/homebrew.api.js index f7da4f898..1fdd707fb 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -89,13 +89,13 @@ const api = { if(brews) { for await (const brew of brews) { - userThemes.Brew[`#${brew.shareId}`] = { + userThemes.Brew[brew.shareId] = { name : brew.title, renderer : 'V3', baseTheme : '', baseSnippets : false, author : brew.authors[0], - path : `#${brew.shareId}`, + path : brew.shareId, thumbnail : brew.thumbnail.length > 0 ? brew.thumbnail : '/assets/naturalCritLogoWhite.svg' }; } @@ -285,7 +285,7 @@ const api = { const brew = req.brew; splitTextStyleAndMetadata(brew); res.setHeader('Content-Type', 'text/css'); - const themePath = req.brew.theme[0] != '#' ? `/css/${req.brew.renderer}/${req.brew.theme}` : `/css/${req.brew.theme.slice(1)}`; + const themePath = themes[_.upperFirst(req.brew.renderer)].hasOwnProperty(req.brew.theme) ? `/css/${req.brew.renderer}/${req.brew.theme}` : `/css/${req.brew.theme}`; // Drop Parent theme if it has already been loaded. // This assumes the continued use of the V3/5ePHB and V3/Blank themes for the app. const parentThemeImport = ((req.brew.theme != '5ePHB') && (req.brew.theme != 'Blank')) ? `@import url(\"${themePath}\");\n\n`:''; @@ -296,7 +296,7 @@ const api = { const brew = req.brew; splitTextStyleAndMetadata(brew); res.setHeader('Content-Type', 'text/css'); - const themePath = req.brew.theme[0] != '#' ? `/css/${req.brew.renderer}/${req.brew.theme}` : `/css/${req.brew.theme.slice(1)}`; + const themePath = themes[_.upperFirst(req.brew.renderer)].hasOwnProperty(req.brew.theme) ? `/css/${req.brew.renderer}/${req.brew.theme}` : `/css/${req.brew.theme}`; const parentThemeImport = `@import url(\"${themePath}\");\n\n`; const themeLocationComment = `/* From Brew: ${req.protocol}://${req.get('host')}/share/${req.brew.shareId} */\n\n`; return res.status(200).send(req.brew.renderer == 'legacy' ? '' : `${parentThemeImport}${themeLocationComment}`); diff --git a/server/homebrew.api.spec.js b/server/homebrew.api.spec.js index 945af7a04..5704d01fa 100644 --- a/server/homebrew.api.spec.js +++ b/server/homebrew.api.spec.js @@ -604,7 +604,7 @@ brew`); describe('getBrewThemeWithUserParent', ()=>{ it('should collect parent theme and brew style - returning as css with user-theme parent imported.', async ()=>{ const toBrewPromise = (brew)=>new Promise((res)=>res({ toObject: ()=>brew })); - model.get = jest.fn(()=>toBrewPromise({ title: 'test brew', renderer: 'V3', shareId: 'iAmAUserTheme', theme: '#IamATheme', style: 'I Have a style!' })); + model.get = jest.fn(()=>toBrewPromise({ title: 'test brew', renderer: 'V3', shareId: 'iAmAUserTheme', theme: 'IamATheme', style: 'I Have a style!' })); const fn = api.getBrew('share', true); const req = { brew: {}, get: ()=>{return 'localhost';}, protocol: 'https' }; const next = jest.fn();