From acc3b7369faf4d02862b49d44278e4bebb116973 Mon Sep 17 00:00:00 2001 From: Nicolas Giard Date: Tue, 1 Nov 2022 03:22:31 +0000 Subject: [PATCH] feat: page TOC + refactor PageDataDialog to composition API --- server/controllers/common.js | 74 +--- server/graph/resolvers/page.js | 5 +- server/graph/schemas/page.graphql | 3 +- server/models/pages.js | 1 + server/tasks/workers/render-page.js | 4 +- ux/src/components/PageDataDialog.vue | 107 +++--- ux/src/components/PageDataTemplateDialog.vue | 375 ++++++++++--------- ux/src/components/PagePropertiesDialog.vue | 5 +- ux/src/pages/Index.vue | 83 +--- ux/src/stores/page.js | 7 +- 10 files changed, 308 insertions(+), 356 deletions(-) diff --git a/server/controllers/common.js b/server/controllers/common.js index 21f4b21f..7c7312bb 100644 --- a/server/controllers/common.js +++ b/server/controllers/common.js @@ -153,6 +153,11 @@ router.get(['/d', '/d/*'], async (req, res, next) => { */ router.get(['/_edit', '/_edit/*'], async (req, res, next) => { const pageArgs = pageHelper.parsePath(req.path, { stripExt: true }) + const site = await WIKI.db.sites.getSiteByHostname({ hostname: req.hostname }) + + if (!site) { + throw new Error('INVALID_SITE') + } if (pageArgs.path === '') { return res.redirect(`/_edit/home`) @@ -175,10 +180,10 @@ router.get(['/_edit', '/_edit/*'], async (req, res, next) => { // -> Get page data from DB let page = await WIKI.db.pages.getPageFromDb({ + siteId: site.id, path: pageArgs.path, locale: pageArgs.locale, - userId: req.user.id, - isPrivate: false + userId: req.user.id }) pageArgs.tags = _.get(page, 'tags', []) @@ -415,6 +420,11 @@ router.get('/*', async (req, res, next) => { const stripExt = _.some(WIKI.data.pageExtensions, ext => _.endsWith(req.path, `.${ext}`)) const pageArgs = pageHelper.parsePath(req.path, { stripExt }) const isPage = (stripExt || pageArgs.path.indexOf('.') === -1) + const site = await WIKI.db.sites.getSiteByHostname({ hostname: req.hostname }) + + if (!site) { + throw new Error('INVALID_SITE') + } if (isPage) { // if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) { @@ -426,6 +436,7 @@ router.get('/*', async (req, res, next) => { try { // -> Get Page from cache const page = await WIKI.db.pages.getPage({ + siteId: site.id, path: pageArgs.path, locale: pageArgs.locale, userId: req.user.id @@ -470,67 +481,8 @@ router.get('/*', async (req, res, next) => { }) } - // -> Build sidebar navigation - let sdi = 1 - const sidebar = (await WIKI.db.navigation.getTree({ cache: true, locale: pageArgs.locale, groups: req.user.groups })).map(n => ({ - i: `sdi-${sdi++}`, - k: n.kind, - l: n.label, - c: n.icon, - y: n.targetType, - t: n.target - })) - - // -> Build theme code injection - const injectCode = { - css: '', // WIKI.config.theming.injectCSS, - head: '', // WIKI.config.theming.injectHead, - body: '' // WIKI.config.theming.injectBody - } - - // Handle missing extra field - page.extra = page.extra || { css: '', js: '' } - - if (!_.isEmpty(page.extra.css)) { - injectCode.css = `${injectCode.css}\n${page.extra.css}` - } - - if (!_.isEmpty(page.extra.js)) { - injectCode.body = `${injectCode.body}\n${page.extra.js}` - } - - // -> Convert page TOC - if (!_.isString(page.toc)) { - page.toc = JSON.stringify(page.toc) - } - - // -> Inject comments variables - const commentTmpl = { - codeTemplate: '', // WIKI.data.commentProvider.codeTemplate, - head: '', // WIKI.data.commentProvider.head, - body: '', // WIKI.data.commentProvider.body, - main: '' // WIKI.data.commentProvider.main - } - if (false && WIKI.config.features.featurePageComments && WIKI.data.commentProvider.codeTemplate) { - [ - { key: 'pageUrl', value: `${WIKI.config.host}/i/${page.id}` }, - { key: 'pageId', value: page.id } - ].forEach((cfg) => { - commentTmpl.head = _.replace(commentTmpl.head, new RegExp(`{{${cfg.key}}}`, 'g'), cfg.value) - commentTmpl.body = _.replace(commentTmpl.body, new RegExp(`{{${cfg.key}}}`, 'g'), cfg.value) - commentTmpl.main = _.replace(commentTmpl.main, new RegExp(`{{${cfg.key}}}`, 'g'), cfg.value) - }) - } - // -> Render view res.sendFile(path.join(WIKI.ROOTPATH, 'assets/index.html')) - // res.render('page', { - // page, - // sidebar, - // injectCode, - // comments: commentTmpl, - // effectivePermissions - // }) } else if (pageArgs.path === 'home') { res.redirect('/_welcome') } else { diff --git a/server/graph/resolvers/page.js b/server/graph/resolvers/page.js index 09710808..524e0898 100644 --- a/server/graph/resolvers/page.js +++ b/server/graph/resolvers/page.js @@ -166,7 +166,10 @@ module.exports = { */ async pageByPath (obj, args, context, info) { const pageArgs = pageHelper.parsePath(args.path) - let page = await WIKI.db.pages.getPageFromDb(pageArgs) + let page = await WIKI.db.pages.getPageFromDb({ + ...pageArgs, + siteId: args.siteId + }) if (page) { return { ...page, diff --git a/server/graph/schemas/page.graphql b/server/graph/schemas/page.graphql index 3d300cfe..29086992 100644 --- a/server/graph/schemas/page.graphql +++ b/server/graph/schemas/page.graphql @@ -35,6 +35,7 @@ extend type Query { ): Page pageByPath( + siteId: UUID! path: String! ): Page @@ -173,7 +174,7 @@ type Page { tags: [PageTag] content: String render: String - toc: String + toc: [JSON] contentType: String createdAt: Date updatedAt: Date diff --git a/server/models/pages.js b/server/models/pages.js index 3c374497..24964c1f 100644 --- a/server/models/pages.js +++ b/server/models/pages.js @@ -1010,6 +1010,7 @@ module.exports = class Page extends Model { .where(queryModeID ? { 'pages.id': opts } : { + 'pages.siteId': opts.siteId, 'pages.path': opts.path, 'pages.localeCode': opts.locale }) diff --git a/server/tasks/workers/render-page.js b/server/tasks/workers/render-page.js index 6c23cbf5..4a8e4278 100644 --- a/server/tasks/workers/render-page.js +++ b/server/tasks/workers/render-page.js @@ -62,8 +62,8 @@ module.exports = async ({ payload }) => { $('.toc-anchor', el).remove() _.get(toc, leafPath).push({ - title: _.trim($(el).text()), - anchor: leafSlug, + label: _.trim($(el).text()), + key: leafSlug.substring(1), children: [] }) }) diff --git a/ux/src/components/PageDataDialog.vue b/ux/src/components/PageDataDialog.vue index ee4b8777..f23a5139 100644 --- a/ux/src/components/PageDataDialog.vue +++ b/ux/src/components/PageDataDialog.vue @@ -1,7 +1,7 @@ - diff --git a/ux/src/components/PageDataTemplateDialog.vue b/ux/src/components/PageDataTemplateDialog.vue index c78d9774..1f43dba0 100644 --- a/ux/src/components/PageDataTemplateDialog.vue +++ b/ux/src/components/PageDataTemplateDialog.vue @@ -1,7 +1,7 @@ - diff --git a/ux/src/stores/page.js b/ux/src/stores/page.js index 5d2242fa..24a3b541 100644 --- a/ux/src/stores/page.js +++ b/ux/src/stores/page.js @@ -80,7 +80,8 @@ export const usePageStore = defineStore('page', { }, commentsCount: 0, content: '', - render: '' + render: '', + toc: [] }), getters: {}, actions: { @@ -93,9 +94,11 @@ export const usePageStore = defineStore('page', { const resp = await APOLLO_CLIENT.query({ query: gql` query loadPage ( + $siteId: UUID! $path: String! ) { pageByPath( + siteId: $siteId path: $path ) { id @@ -105,10 +108,12 @@ export const usePageStore = defineStore('page', { locale updatedAt render + toc } } `, variables: { + siteId: siteStore.id, path }, fetchPolicy: 'network-only'