2018-05-28 18:46:55 +00:00
|
|
|
const Model = require('objection').Model
|
2018-08-09 04:07:15 +00:00
|
|
|
const fs = require('fs-extra')
|
2018-05-28 18:46:55 +00:00
|
|
|
const path = require('path')
|
|
|
|
const _ = require('lodash')
|
2018-08-09 04:07:15 +00:00
|
|
|
const yaml = require('js-yaml')
|
|
|
|
const commonHelper = require('../helpers/common')
|
2018-05-28 18:46:55 +00:00
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Editor model
|
|
|
|
*/
|
|
|
|
module.exports = class Editor extends Model {
|
|
|
|
static get tableName() { return 'editors' }
|
2018-09-08 19:49:36 +00:00
|
|
|
static get idColumn() { return 'key' }
|
2018-05-28 18:46:55 +00:00
|
|
|
|
|
|
|
static get jsonSchema () {
|
|
|
|
return {
|
|
|
|
type: 'object',
|
2018-08-09 04:07:15 +00:00
|
|
|
required: ['key', 'isEnabled'],
|
2018-05-28 18:46:55 +00:00
|
|
|
|
|
|
|
properties: {
|
|
|
|
key: {type: 'string'},
|
2019-02-10 00:10:34 +00:00
|
|
|
isEnabled: {type: 'boolean'}
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 00:10:34 +00:00
|
|
|
static get jsonAttributes() {
|
|
|
|
return ['config']
|
|
|
|
}
|
|
|
|
|
2018-08-09 04:07:15 +00:00
|
|
|
static async getEditors() {
|
|
|
|
return WIKI.models.editors.query()
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static async refreshEditorsFromDisk() {
|
2018-09-29 19:39:09 +00:00
|
|
|
let trx
|
2018-05-28 18:46:55 +00:00
|
|
|
try {
|
2018-07-30 02:23:33 +00:00
|
|
|
const dbEditors = await WIKI.models.editors.query()
|
2018-08-09 04:07:15 +00:00
|
|
|
|
|
|
|
// -> Fetch definitions from disk
|
2018-08-11 01:00:20 +00:00
|
|
|
const editorDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/editor'))
|
2018-08-09 04:07:15 +00:00
|
|
|
let diskEditors = []
|
|
|
|
for (let dir of editorDirs) {
|
2018-08-11 01:00:20 +00:00
|
|
|
const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/editor', dir, 'definition.yml'), 'utf8')
|
2018-08-09 04:07:15 +00:00
|
|
|
diskEditors.push(yaml.safeLoad(def))
|
|
|
|
}
|
|
|
|
WIKI.data.editors = diskEditors.map(editor => ({
|
|
|
|
...editor,
|
|
|
|
props: commonHelper.parseModuleProps(editor.props)
|
|
|
|
}))
|
|
|
|
|
|
|
|
// -> Insert new editors
|
2018-05-28 18:46:55 +00:00
|
|
|
let newEditors = []
|
2018-08-09 04:07:15 +00:00
|
|
|
for (let editor of WIKI.data.editors) {
|
|
|
|
if (!_.some(dbEditors, ['key', editor.key])) {
|
2018-05-28 18:46:55 +00:00
|
|
|
newEditors.push({
|
2018-08-09 04:07:15 +00:00
|
|
|
key: editor.key,
|
2018-05-28 18:46:55 +00:00
|
|
|
isEnabled: false,
|
2018-08-09 04:07:15 +00:00
|
|
|
config: _.transform(editor.props, (result, value, key) => {
|
|
|
|
_.set(result, key, value.default)
|
2018-05-28 18:46:55 +00:00
|
|
|
return result
|
|
|
|
}, {})
|
|
|
|
})
|
2018-08-09 04:07:15 +00:00
|
|
|
} 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)
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
2018-08-09 04:07:15 +00:00
|
|
|
}
|
2018-05-28 18:46:55 +00:00
|
|
|
if (newEditors.length > 0) {
|
2018-09-29 19:39:09 +00:00
|
|
|
trx = await WIKI.models.Objection.transaction.start(WIKI.models.knex)
|
|
|
|
for (let editor of newEditors) {
|
|
|
|
await WIKI.models.editors.query(trx).insert(editor)
|
|
|
|
}
|
|
|
|
await trx.commit()
|
2018-05-28 18:46:55 +00:00
|
|
|
WIKI.logger.info(`Loaded ${newEditors.length} new editors: [ OK ]`)
|
|
|
|
} else {
|
|
|
|
WIKI.logger.info(`No new editors found: [ SKIPPED ]`)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.error(`Failed to scan or load new editors: [ FAILED ]`)
|
|
|
|
WIKI.logger.error(err)
|
2019-05-11 05:23:32 +00:00
|
|
|
if (trx) {
|
|
|
|
trx.rollback()
|
|
|
|
}
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-25 04:48:28 +00:00
|
|
|
|
|
|
|
static async getDefaultEditor(contentType) {
|
|
|
|
// TODO - hardcoded for now
|
|
|
|
switch (contentType) {
|
|
|
|
case 'markdown':
|
|
|
|
return 'markdown'
|
2019-10-15 03:44:37 +00:00
|
|
|
case 'html':
|
|
|
|
return 'ckeditor'
|
2022-12-24 01:19:16 +00:00
|
|
|
case 'asciidoc':
|
|
|
|
return 'asciidoc'
|
2019-02-25 04:48:28 +00:00
|
|
|
default:
|
|
|
|
return 'code'
|
|
|
|
}
|
|
|
|
}
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|