Skip to content

Commit

Permalink
Migrated to vue unhead.
Browse files Browse the repository at this point in the history
Refs #156
  • Loading branch information
The4thLaw committed Mar 27, 2024
1 parent dc8f9d2 commit 6035aa6
Show file tree
Hide file tree
Showing 52 changed files with 318 additions and 152 deletions.
80 changes: 80 additions & 0 deletions source/demyo-vue-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions source/demyo-vue-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"dependencies": {
"@mdi/font": "^5.9.55",
"@unhead/vue": "^1.9.1",
"autoprefixer": "^10.4.18",
"axios": "^1.3.1",
"currency-symbol-map": "^5.0.1",
Expand Down
8 changes: 3 additions & 5 deletions source/demyo-vue-frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import { contextRoot } from '@/myenv'
export default {
name: 'DemyoApp',
metaInfo() {
const self = this
head() {
return {
title: null,
titleTemplate: '%s — Demyo',
titleTemplate: (title) => !title ? 'Demyo' : `${title} – Demyo`,
link: [
{
rel: 'manifest',
href: `${contextRoot}manifest.json?lang=${self.$i18n.locale.replace(/-/, '_')}`
href: `${contextRoot}manifest.json?lang=${this.$i18n.locale.replace(/-/, '_')}`
}
]
}
Expand Down
1 change: 0 additions & 1 deletion source/demyo-vue-frontend/src/components/SectionCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ export default {
padding-bottom: 0;
> :last-child {
padding-bottom: 0;
margin-bottom: 0;
}
}
Expand Down
9 changes: 8 additions & 1 deletion source/demyo-vue-frontend/src/composables/model-index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { useUiStore } from '@/stores/ui'
import { useHead } from '@unhead/vue'
import { useI18n } from 'vue-i18n'

/**
* Composable for simple model indexes loading data from the service.
* @param {AbstractModelService} serviceInstance The service to use to fetch the data.
* @param {string} titleKey the page title key
*/
export function useSimpleIndex(serviceInstance) {
export function useSimpleIndex(serviceInstance, titleKey) {
useHead({
title: useI18n().t(titleKey)
})

const uiStore = useUiStore()

const modelList = ref([])
Expand Down
3 changes: 2 additions & 1 deletion source/demyo-vue-frontend/src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ const i18n = createI18n({
locale: simpleLocale,
fallbackLocale: fallbackLanguage,
messages: loadLocaleMessages(),
datetimeFormats
datetimeFormats,
legacy: false
})
setHtmlLang(selectedLocale)
console.log(`Initialized i18n with '${selectedLocale}' as default language and '${fallbackLanguage}' as fallback`)
Expand Down
28 changes: 18 additions & 10 deletions source/demyo-vue-frontend/src/layouts/DefaultLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,12 @@ export default {
mixins: [quicksearch],
metaInfo() {
const self = this
return {
changed(newInfo, addedTags, removedTags) {
self.pageTitle = newInfo.titleChunk
}
}
},
data() {
return {
uiStore: useUiStore(),
pageTitle: 'Demyo',
pageTitleObserver: null,
demyoCodename: demyoCodename,
mainMenu: false,
Expand Down Expand Up @@ -189,6 +180,23 @@ export default {
}
},
mounted() {
// Monitor the page title for changes. unhead doesn't seem to have an event for this
this.pageTitleObserver = new MutationObserver((mutations) => {
const pageTitle = mutations[0].target.text.replace(/ – Demyo$/, '')
this.pageTitle = pageTitle
})
this.pageTitleObserver.observe(
document.querySelector('title'),
{ subtree: true, characterData: true, childList: true }
)
},
unmounted() {
this.pageTitleObserver.disconnect()
this.pageTitleObserver = null
},
methods: {
enterSearch() {
this.$refs.menuSearch.blur()
Expand Down
4 changes: 4 additions & 0 deletions source/demyo-vue-frontend/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import '@mdi/font/css/materialdesignicons.css'
import 'roboto-fontface/css/roboto/roboto-fontface.css'
import { createApp } from 'vue'
// From https://gitlab.com/totol.toolsuite/vue-3-fullscreen-image
import { VueHeadMixin, createHead } from '@unhead/vue'
import { fullscreenImagePlugin } from 'vue-3-fullscreen-image-directive-plugin'
import 'vue-3-fullscreen-image-directive-plugin/style.css'
import App from './App.vue'
Expand Down Expand Up @@ -29,6 +30,9 @@ app.use(i18n)
app.use(vuetify)
app.use(fullscreenImagePlugin)

app.use(createHead())
app.mixin(VueHeadMixin)

// Add the ID to the body element to ease style overrides
document.getElementsByTagName('body')[0].id = 'demyo'

Expand Down
21 changes: 10 additions & 11 deletions source/demyo-vue-frontend/src/mixins/model-edit.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { useUiStore } from '@/stores/ui'

export default {
metaInfo() {
return {
title: this.initialized
? (this.parsedId
? this.$t(this.mixinConfig.modelEdit.titleKeys.edit)
: this.$t(this.mixinConfig.modelEdit.titleKeys.add))
: ''
}
},

data() {
return {
initialized: false,
parsedId: undefined
}
},

computed: {
pageTitle() {
return this.initialized
? (this.parsedId
? this.$t(this.mixinConfig.modelEdit.titleKeys.edit)
: this.$t(this.mixinConfig.modelEdit.titleKeys.add))
: null
}
},

watch: {
$route: 'fetchDataInternal'
},
Expand Down Expand Up @@ -106,6 +106,5 @@ export default {
this.fetchDataInternal()
}
}

}
}
7 changes: 6 additions & 1 deletion source/demyo-vue-frontend/src/pages/AboutDemyo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,15 @@
</template>

<script setup lang="ts">
// TODO: Vue 3: Give a two-word component name
import libs from '@/assets/about-libs.json'
import { demyoCodename, demyoVersion } from '@/myenv'
import aboutService from '@/services/about-service'
import { useHead } from '@unhead/vue'
import { useI18n } from 'vue-i18n'
useHead({
title: useI18n().t('title.about')
})
const { userAgent } = navigator
Expand Down
8 changes: 4 additions & 4 deletions source/demyo-vue-frontend/src/pages/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ export default {
mixins: [quicksearch],
metaInfo() {
data() {
return {
title: this.$t('title.home')
uiStore: useUiStore()
}
},
data() {
head() {
return {
uiStore: useUiStore()
title: this.$t('title.home')
}
},
Expand Down
6 changes: 6 additions & 0 deletions source/demyo-vue-frontend/src/pages/albums/AlbumIndex.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
import { retrieveFilter } from '@/helpers/filter'
import albumService from '@/services/album-service'
import { useUiStore } from '@/stores/ui'
import { useHead } from '@unhead/vue'
import { useI18n } from 'vue-i18n'
import { useRoute } from 'vue-router'
useHead({
title: useI18n().t('title.index.album')
})
const uiStore = useUiStore()
const route = useRoute()
Expand Down
12 changes: 6 additions & 6 deletions source/demyo-vue-frontend/src/pages/albums/AlbumView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,6 @@ export default {
mixins: [i18nMixin, modelViewMixin],
metaInfo() {
return {
title: this.album.title
}
},
data() {
return {
uiStore: useUiStore(),
Expand All @@ -336,6 +330,12 @@ export default {
}
},
head() {
return {
title: this.album.title
}
},
computed: {
hasAuthors() {
return this.album.writers?.length
Expand Down
4 changes: 4 additions & 0 deletions source/demyo-vue-frontend/src/pages/authors/AuthorEdit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export default {
}
},
head() {
return { title: this.pageTitle }
},
methods: {
async fetchData() {
if (this.parsedId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
import { useSimpleIndex } from '@/composables/model-index'
import authorService from '@/services/author-service'
const { modelList } = useSimpleIndex(authorService)
const { modelList } = useSimpleIndex(authorService, 'title.index.author')
</script>

0 comments on commit 6035aa6

Please sign in to comment.