Skip to content

Commit

Permalink
Announce document title changes handled by apps
Browse files Browse the repository at this point in the history
... and not the runtime
  • Loading branch information
dschmidt committed Oct 13, 2022
1 parent dc86cff commit 669ac3f
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 24 deletions.
1 change: 1 addition & 0 deletions changelog/unreleased/change-drive-aliases-in-urls
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ BREAKING CHANGE for developers: the appDefaults composables from web-pkg now wor

https://github.com/owncloud/web/issues/6648
https://github.com/owncloud/web/pull/7430
https://github.com/owncloud/web/pull/7791
2 changes: 1 addition & 1 deletion packages/web-app-files/src/views/spaces/GenericSpace.vue
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export default defineComponent({
return segments
})
useDocumentTitle({ document, titleSegments })
useDocumentTitle({ titleSegments })
const { $gettext } = useTranslations()
const route = useRoute()
Expand Down
2 changes: 1 addition & 1 deletion packages/web-app-files/src/views/spaces/GenericTrash.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export default defineComponent({
}
return segments
})
useDocumentTitle({ document, titleSegments })
useDocumentTitle({ titleSegments })
return {
...useResourcesViewDefaults<Resource, any, any[]>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export function useAppDocumentTitle({
})

useDocumentTitle({
document,
titleSegments
})
}
17 changes: 12 additions & 5 deletions packages/web-pkg/src/composables/appDefaults/useDocumentTitle.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import { watch, Ref, unref } from '@vue/composition-api'
import { useStore } from '../store'
import { Store } from 'vuex'
import { useEventBus } from '../eventBus'
import { EventBus } from '../../services'

interface DocumentTitleOptions {
document: Document
titleSegments: Ref<string[]>
store?: Store<any>
eventBus?: EventBus
}

export function useDocumentTitle({ document, titleSegments, store }: DocumentTitleOptions): void {
export function useDocumentTitle({ titleSegments, store, eventBus }: DocumentTitleOptions): void {
store = store || useStore()
eventBus = eventBus || useEventBus()

watch(
titleSegments,
(newTitleSegments) => {
const titleSegments = unref(newTitleSegments)

// TODO: announce navigation
const glue = ' - '
const generalName = store.getters['configuration'].currentTheme.general.name
const payload = {
shortDocumentTitle: titleSegments.join(glue),
fullDocumentTitle: [...titleSegments, generalName].join(glue)
}

titleSegments.push(store.getters['configuration'].currentTheme.general.name)
document.title = titleSegments.join(' - ')
eventBus.publish('runtime.documentTitle.changed', payload)
},
{ immediate: true }
)
Expand Down
1 change: 1 addition & 0 deletions packages/web-pkg/src/composables/eventBus/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useEventBus'
5 changes: 5 additions & 0 deletions packages/web-pkg/src/composables/eventBus/useEventBus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { eventBus } from '../../services'

export const useEventBus = () => {
return eventBus
}
39 changes: 23 additions & 16 deletions packages/web-runtime/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { getBackendVersion, getWebVersion } from './container/versions'
import { defineComponent } from '@vue/composition-api'
import { isPublicLinkContext, isUserContext, isAuthenticationRequired } from './router'
import { additionalTranslations } from './helpers/additionalTranslations' // eslint-disable-line
import { eventBus } from 'web-pkg/src/services'
export default defineComponent({
components: {
Expand Down Expand Up @@ -100,12 +101,13 @@ export default defineComponent({
$route: {
immediate: true,
handler: function (to) {
const title = this.extractPageTitleFromRoute(to)
if (!title) {
const extracted = this.extractPageTitleFromRoute(to)
if (!extracted) {
return
}
this.announceRouteChange(to)
document.title = title
const { shortDocumentTitle, fullDocumentTitle } = extracted
this.announceRouteChange(shortDocumentTitle)
document.title = fullDocumentTitle
}
},
capabilities: {
Expand Down Expand Up @@ -145,7 +147,15 @@ export default defineComponent({
}
}
},
mounted() {
eventBus.subscribe(
'runtime.documentTitle.changed',
({ shortDocumentTitle, fullDocumentTitle }) => {
document.title = fullDocumentTitle
this.announceRouteChange(shortDocumentTitle)
}
)
},
destroyed() {
if (this.$_notificationsInterval) {
clearInterval(this.$_notificationsInterval)
Expand Down Expand Up @@ -183,26 +193,23 @@ export default defineComponent({
})
},
announceRouteChange(route) {
const pageTitle = this.extractPageTitleFromRoute(route, false)
announceRouteChange(pageTitle) {
const translated = this.$gettext('Navigated to %{ pageTitle }')
this.announcement = this.$gettextInterpolate(translated, { pageTitle })
},
extractPageTitleFromRoute(route, includeGeneralName = true) {
const titleSegments = []
extractPageTitleFromRoute(route) {
const routeTitle = route.meta.title ? this.$gettext(route.meta.title) : undefined
if (!routeTitle) {
return
}
titleSegments.push(routeTitle)
if (includeGeneralName) {
titleSegments.push(this.configuration.currentTheme.general.name)
const glue = ' - '
const titleSegments = [routeTitle]
const generalName = this.configuration.currentTheme.general.name
return {
shortDocumentTitle: titleSegments.join(glue),
fullDocumentTitle: [...titleSegments, generalName].join(glue)
}
return titleSegments.join(' - ')
}
}
})
Expand Down

0 comments on commit 669ac3f

Please sign in to comment.