Commit faa1f389 authored by Nicolas Giard's avatar Nicolas Giard Committed by Nick

feat: theme code injection

parent ded1cf62
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
outline outline
background-color='grey lighten-2' background-color='grey lighten-2'
prepend-icon='palette' prepend-icon='palette'
v-model='selectedTheme' v-model='config.theme'
label='Site Theme' label='Site Theme'
persistent-hint persistent-hint
hint='Themes affect how content pages are displayed. Other site sections (such as the editor or admin area) are not affected.' hint='Themes affect how content pages are displayed. Other site sections (such as the editor or admin area) are not affected.'
...@@ -47,41 +47,36 @@ ...@@ -47,41 +47,36 @@
v-toolbar(color='primary', dark, dense, flat) v-toolbar(color='primary', dark, dense, flat)
v-toolbar-title v-toolbar-title
.subheading Code Injection .subheading Code Injection
v-spacer
v-chip(label, color='white', small).primary--text coming soon
v-card-text v-card-text
v-textarea( v-textarea(
v-model='injectCSS' v-model='config.injectCSS'
label='CSS Override' label='CSS Override'
outline outline
background-color='grey lighten-1' background-color='grey lighten-1'
color='primary' color='primary'
persistent-hint persistent-hint
hint='CSS code to inject after system default CSS' hint='CSS code to inject after system default CSS. Consider using custom themes if you have a large amount of css code. Injecting too much CSS code will result in poor page load performance! CSS will automatically be minified.'
auto-grow auto-grow
disabled
) )
v-textarea.mt-2( v-textarea.mt-2(
v-model='injectHeader' v-model='config.injectHead'
label='Head HTML Injection' label='Head HTML Injection'
outline outline
background-color='grey lighten-1' background-color='grey lighten-1'
color='primary' color='primary'
persistent-hint persistent-hint
hint='HTML code to be injected just before the closing head tag' hint='HTML code to be injected just before the closing head tag. Usually for script tags.'
auto-grow auto-grow
disabled
) )
v-textarea.mt-2( v-textarea.mt-2(
v-model='injectFooter' v-model='config.injectBody'
label='Body HTML Injection' label='Body HTML Injection'
outline outline
background-color='grey lighten-1' background-color='grey lighten-1'
color='primary' color='primary'
persistent-hint persistent-hint
hint='HTML code to be injected just before the closing body tag' hint='HTML code to be injected just before the closing body tag.'
auto-grow auto-grow
disabled
) )
v-flex(lg6 xs12) v-flex(lg6 xs12)
v-card v-card
...@@ -97,6 +92,7 @@ ...@@ -97,6 +92,7 @@
import _ from 'lodash' import _ from 'lodash'
import { sync } from 'vuex-pathify' import { sync } from 'vuex-pathify'
import themeConfigQuery from 'gql/admin/theme/theme-query-config.gql'
import themeSaveMutation from 'gql/admin/theme/theme-mutation-save.gql' import themeSaveMutation from 'gql/admin/theme/theme-mutation-save.gql'
export default { export default {
...@@ -106,11 +102,14 @@ export default { ...@@ -106,11 +102,14 @@ export default {
themes: [ themes: [
{ text: 'Default', author: 'requarks.io', value: 'default' } { text: 'Default', author: 'requarks.io', value: 'default' }
], ],
selectedTheme: 'default', config: {
darkModeInitial: false, theme: 'default',
darkMode: false,
injectCSS: '', injectCSS: '',
injectHeader: '', injectHead: '',
injectFooter: '' injectBody: ''
},
darkModeInitial: false
} }
}, },
computed: { computed: {
...@@ -130,8 +129,11 @@ export default { ...@@ -130,8 +129,11 @@ export default {
const respRaw = await this.$apollo.mutate({ const respRaw = await this.$apollo.mutate({
mutation: themeSaveMutation, mutation: themeSaveMutation,
variables: { variables: {
theme: this.selectedTheme, theme: this.config.theme,
darkMode: this.darkMode darkMode: this.darkMode,
injectCSS: this.config.injectCSS,
injectHead: this.config.injectHead,
injectBody: this.config.injectBody
} }
}) })
const resp = _.get(respRaw, 'data.theming.setConfig.responseResult', {}) const resp = _.get(respRaw, 'data.theming.setConfig.responseResult', {})
...@@ -151,6 +153,16 @@ export default { ...@@ -151,6 +153,16 @@ export default {
this.$store.commit(`loadingStop`, 'admin-theme-save') this.$store.commit(`loadingStop`, 'admin-theme-save')
this.loading = false this.loading = false
} }
},
apollo: {
config: {
query: themeConfigQuery,
fetchPolicy: 'network-only',
update: (data) => data.theming.config,
watchLoading (isLoading) {
this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-theme-refresh')
}
}
} }
} }
</script> </script>
......
mutation($theme: String!, $darkMode: Boolean!) { mutation($theme: String!, $darkMode: Boolean!, $injectCSS: String, $injectHead: String, $injectBody: String) {
theming { theming {
setConfig(theme: $theme, darkMode: $darkMode) { setConfig(theme: $theme, darkMode: $darkMode, injectCSS: $injectCSS, injectHead: $injectHead, injectBody: $injectBody) {
responseResult { responseResult {
succeeded succeeded
errorCode errorCode
......
query {
theming {
config {
theme
darkMode
injectCSS
injectHead
injectBody
}
}
}
...@@ -54,6 +54,7 @@ ...@@ -54,6 +54,7 @@
"cheerio": "1.0.0-rc.2", "cheerio": "1.0.0-rc.2",
"child-process-promise": "2.2.1", "child-process-promise": "2.2.1",
"chokidar": "2.0.4", "chokidar": "2.0.4",
"clean-css": "4.2.1",
"compression": "1.7.3", "compression": "1.7.3",
"connect-redis": "3.4.0", "connect-redis": "3.4.0",
"cookie-parser": "1.4.3", "cookie-parser": "1.4.3",
...@@ -179,7 +180,6 @@ ...@@ -179,7 +180,6 @@
"@babel/polyfill": "^7.0.0", "@babel/polyfill": "^7.0.0",
"@babel/preset-env": "^7.1.6", "@babel/preset-env": "^7.1.6",
"@panter/vue-i18next": "0.13.0", "@panter/vue-i18next": "0.13.0",
"@vue/cli": "3.1.3",
"animated-number-vue": "0.1.3", "animated-number-vue": "0.1.3",
"apollo-cache-inmemory": "1.3.10", "apollo-cache-inmemory": "1.3.10",
"apollo-client": "2.4.6", "apollo-client": "2.4.6",
......
...@@ -149,7 +149,12 @@ router.get('/*', async (req, res, next) => { ...@@ -149,7 +149,12 @@ router.get('/*', async (req, res, next) => {
_.set(res.locals, 'pageMeta.title', page.title) _.set(res.locals, 'pageMeta.title', page.title)
_.set(res.locals, 'pageMeta.description', page.description) _.set(res.locals, 'pageMeta.description', page.description)
const sidebar = await WIKI.models.navigation.getTree({ cache: true }) const sidebar = await WIKI.models.navigation.getTree({ cache: true })
res.render('page', { page, sidebar }) const injectCode = {
css: WIKI.config.theming.injectCSS,
head: WIKI.config.theming.injectHead,
body: WIKI.config.theming.injectBody
}
res.render('page', { page, sidebar, injectCode })
} else if (pageArgs.path === 'home') { } else if (pageArgs.path === 'home') {
_.set(res.locals, 'pageMeta.title', 'Welcome') _.set(res.locals, 'pageMeta.title', 'Welcome')
res.render('welcome') res.render('welcome')
......
const graphHelper = require('../../helpers/graph') const graphHelper = require('../../helpers/graph')
const _ = require('lodash')
const CleanCSS = require('clean-css')
/* global WIKI */ /* global WIKI */
...@@ -18,17 +20,27 @@ module.exports = { ...@@ -18,17 +20,27 @@ module.exports = {
}] }]
}, },
async config(obj, args, context, info) { async config(obj, args, context, info) {
return { return _.pick(WIKI.config.theming, ['theme', 'darkMode', 'injectCSS', 'injectHead', 'injectBody'])
theme: WIKI.config.theming.theme,
darkMode: WIKI.config.theming.darkMode
}
} }
}, },
ThemingMutation: { ThemingMutation: {
async setConfig(obj, args, context, info) { async setConfig(obj, args, context, info) {
try { try {
WIKI.config.theming.theme = args.theme if (!_.isEmpty(args.injectCSS)) {
WIKI.config.theming.darkMode = args.darkMode args.injectCSS = new CleanCSS({
inline: false
}).minify(args.injectCSS).styles
}
WIKI.config.theming = {
...WIKI.config.theming,
theme: args.theme,
darkMode: args.darkMode,
injectCSS: args.injectCSS || '',
injectHead: args.injectHead || '',
injectBody: args.injectBody || ''
}
await WIKI.configSvc.saveToDb(['theming']) await WIKI.configSvc.saveToDb(['theming'])
return { return {
......
...@@ -27,6 +27,9 @@ type ThemingMutation { ...@@ -27,6 +27,9 @@ type ThemingMutation {
setConfig( setConfig(
theme: String! theme: String!
darkMode: Boolean! darkMode: Boolean!
injectCSS: String
injectHead: String
injectBody: String
): DefaultResponse @auth(requires: ["manage:theme", "manage:system"]) ): DefaultResponse @auth(requires: ["manage:theme", "manage:system"])
} }
...@@ -35,8 +38,11 @@ type ThemingMutation { ...@@ -35,8 +38,11 @@ type ThemingMutation {
# ----------------------------------------------- # -----------------------------------------------
type ThemingConfig { type ThemingConfig {
theme: String theme: String!
darkMode: Boolean darkMode: Boolean!
injectCSS: String
injectHead: String
injectBody: String
} }
type ThemingTheme { type ThemingTheme {
......
extends master.pug extends master.pug
block head block head
if injectCode.css
style(type='text/css')!= injectCode.css
if injectCode.head
!= injectCode.head
block body block body
#root #root
...@@ -31,3 +35,5 @@ block body ...@@ -31,3 +35,5 @@ block body
else if navItem.kind === 'header' else if navItem.kind === 'header'
v-subheader.pl-4= navItem.label v-subheader.pl-4= navItem.label
template(slot='contents')!= page.render template(slot='contents')!= page.render
if injectCode.body
!= injectCode.body
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment