feat: tags UI (wip) + save tags from page

This commit is contained in:
Nick
2019-09-01 18:33:36 -04:00
parent 8e80b7471d
commit 5a7fd2d73e
22 changed files with 326 additions and 4 deletions

View File

@@ -165,6 +165,14 @@ router.get(['/s', '/s/*'], async (req, res, next) => {
}
})
/**
* Tags
*/
router.get(['/t', '/t/*'], (req, res, next) => {
_.set(res.locals, 'pageMeta.title', 'Tags')
res.render('tags')
})
/**
* View document / asset
*/

View File

@@ -76,6 +76,9 @@ module.exports = {
} else {
throw new WIKI.Error.PageNotFound()
}
},
async tags (obj, args, context, info) {
return WIKI.models.tags.query().orderBy('tag', 'asc')
}
},
PageMutation: {

View File

@@ -36,6 +36,8 @@ type PageQuery {
single(
id: Int!
): Page @auth(requires: ["manage:pages", "delete:pages", "manage:system"])
tags: [PageTag]! @auth(requires: ["manage:system", "read:pages"])
}
# -----------------------------------------------
@@ -109,6 +111,7 @@ type Page {
privateNS: String
publishStartDate: Date!
publishEndDate: String!
tags: [PageTag]!
content: String!
render: String
toc: String
@@ -125,6 +128,14 @@ type Page {
creatorEmail: String!
}
type PageTag {
id: Int!
tag: String!
title: String
createdAt: Date!
updatedAt: Date!
}
type PageHistory {
versionId: Int!
authorId: Int!

View File

@@ -210,6 +210,11 @@ module.exports = class Page extends Model {
isPrivate: opts.isPrivate
})
// -> Save Tags
if (opts.tags.length > 0) {
await WIKI.models.tags.associateTags({ tags: opts.tags, page })
}
// -> Render page to HTML
await WIKI.models.pages.renderPage(page)
@@ -260,6 +265,9 @@ module.exports = class Page extends Model {
isPrivate: ogPage.isPrivate
})
// -> Save Tags
await WIKI.models.tags.associateTags({ tags: opts.tags, page })
// -> Render page to HTML
await WIKI.models.pages.renderPage(page)

View File

@@ -1,4 +1,7 @@
const Model = require('objection').Model
const _ = require('lodash')
/* global WIKI */
/**
* Tags model
@@ -46,4 +49,51 @@ module.exports = class Tag extends Model {
this.createdAt = new Date().toISOString()
this.updatedAt = new Date().toISOString()
}
static async associateTags ({ tags, page }) {
let existingTags = await WIKI.models.tags.query().column('id', 'tag')
// Create missing tags
const newTags = _.filter(tags, t => !_.some(existingTags, ['tag', t])).map(t => ({
tag: t,
title: t
}))
if (newTags.length > 0) {
if (WIKI.config.db.type === 'postgres') {
const createdTags = await WIKI.models.tags.query().insert(newTags)
existingTags = _.concat(existingTags, createdTags)
} else {
for (const newTag of newTags) {
const createdTag = await WIKI.models.tags.query().insert(newTag)
existingTags.push(createdTag)
}
}
}
// Fetch current page tags
const targetTags = _.filter(existingTags, t => _.includes(tags, t.tag))
const currentTags = await page.$relatedQuery('tags')
// Tags to relate
const tagsToRelate = _.differenceBy(targetTags, currentTags, 'id')
if (tagsToRelate.length > 0) {
if (WIKI.config.db.type === 'postgres') {
await page.$relatedQuery('tags').relate(tagsToRelate)
} else {
for (const tag of tagsToRelate) {
await page.$relatedQuery('tags').relate(tag)
}
}
}
// Tags to unrelate
const tagsToUnrelate = _.differenceBy(currentTags, targetTags, 'id')
if (tagsToUnrelate.length > 0) {
await page.$relatedQuery('tags').unrelate().whereIn('tags.id', _.map(tagsToUnrelate, 'id'))
}
}
}

5
server/views/tags.pug Normal file
View File

@@ -0,0 +1,5 @@
extends master.pug
block body
#root
tags