feat: themes foundation + editors load improvements
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
const Model = require('objection').Model
|
||||
const autoload = require('auto-load')
|
||||
const fs = require('fs-extra')
|
||||
const path = require('path')
|
||||
const _ = require('lodash')
|
||||
const yaml = require('js-yaml')
|
||||
const commonHelper = require('../helpers/common')
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
@@ -14,42 +16,63 @@ module.exports = class Editor extends Model {
|
||||
static get jsonSchema () {
|
||||
return {
|
||||
type: 'object',
|
||||
required: ['key', 'title', 'isEnabled'],
|
||||
required: ['key', 'isEnabled'],
|
||||
|
||||
properties: {
|
||||
id: {type: 'integer'},
|
||||
key: {type: 'string'},
|
||||
title: {type: 'string'},
|
||||
isEnabled: {type: 'boolean'},
|
||||
config: {type: 'object'}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static async getEnabledEditors() {
|
||||
return WIKI.models.editors.query().where({ isEnabled: true })
|
||||
static async getEditors() {
|
||||
return WIKI.models.editors.query()
|
||||
}
|
||||
|
||||
static async refreshEditorsFromDisk() {
|
||||
try {
|
||||
const dbEditors = await WIKI.models.editors.query()
|
||||
const diskEditors = autoload(path.join(WIKI.SERVERPATH, 'modules/editor'))
|
||||
|
||||
// -> Fetch definitions from disk
|
||||
const editorDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/editors'))
|
||||
let diskEditors = []
|
||||
for (let dir of editorDirs) {
|
||||
const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/editors', dir, 'definition.yml'), 'utf8')
|
||||
diskEditors.push(yaml.safeLoad(def))
|
||||
}
|
||||
WIKI.data.editors = diskEditors.map(editor => ({
|
||||
...editor,
|
||||
props: commonHelper.parseModuleProps(editor.props)
|
||||
}))
|
||||
|
||||
// -> Insert new editors
|
||||
let newEditors = []
|
||||
_.forOwn(diskEditors, (strategy, strategyKey) => {
|
||||
if (!_.some(dbEditors, ['key', strategy.key])) {
|
||||
for (let editor of WIKI.data.editors) {
|
||||
if (!_.some(dbEditors, ['key', editor.key])) {
|
||||
newEditors.push({
|
||||
key: strategy.key,
|
||||
title: strategy.title,
|
||||
key: editor.key,
|
||||
isEnabled: false,
|
||||
config: _.reduce(strategy.props, (result, value, key) => {
|
||||
_.set(result, value, '')
|
||||
config: _.transform(editor.props, (result, value, key) => {
|
||||
_.set(result, key, value.default)
|
||||
return result
|
||||
}, {})
|
||||
})
|
||||
} else {
|
||||
const editorConfig = _.get(_.find(dbEditors, ['key', editor.key]), 'config', {})
|
||||
await WIKI.models.editors.query().patch({
|
||||
config: _.transform(editor.props, (result, value, key) => {
|
||||
if (!_.has(result, key)) {
|
||||
_.set(result, key, value.default)
|
||||
}
|
||||
return result
|
||||
}, editorConfig)
|
||||
}).where('key', editor.key)
|
||||
}
|
||||
})
|
||||
}
|
||||
if (newEditors.length > 0) {
|
||||
await WIKI.models.editors.query().insert(newEditors)
|
||||
await WIKI.models.storage.query().insert(newEditors)
|
||||
WIKI.logger.info(`Loaded ${newEditors.length} new editors: [ OK ]`)
|
||||
} else {
|
||||
WIKI.logger.info(`No new editors found: [ SKIPPED ]`)
|
||||
|
@@ -88,10 +88,12 @@ module.exports = class Page extends Model {
|
||||
}
|
||||
|
||||
static async createPage(opts) {
|
||||
await WIKI.models.pages.renderPage(opts)
|
||||
const page = await WIKI.models.pages.query().insertAndFetch({
|
||||
authorId: opts.authorId,
|
||||
content: opts.content,
|
||||
creatorId: opts.authorId,
|
||||
contentType: _.get(WIKI.data.editors, `${opts.editor}.contentType`, 'text'),
|
||||
description: opts.description,
|
||||
editorKey: opts.editor,
|
||||
isPrivate: opts.isPrivate,
|
||||
@@ -130,4 +132,11 @@ module.exports = class Page extends Model {
|
||||
})
|
||||
return page
|
||||
}
|
||||
|
||||
static async renderPage(opts) {
|
||||
WIKI.queue.job.renderPage.add(opts, {
|
||||
removeOnComplete: true,
|
||||
removeOnFail: true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user