You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.0 KiB

  1. const Model = require('objection').Model
  2. const fs = require('fs-extra')
  3. const path = require('path')
  4. const _ = require('lodash')
  5. const yaml = require('js-yaml')
  6. const commonHelper = require('../helpers/common')
  7. /* global WIKI */
  8. /**
  9. * Editor model
  10. */
  11. module.exports = class Editor extends Model {
  12. static get tableName() { return 'editors' }
  13. static get idColumn() { return 'key' }
  14. static get jsonSchema () {
  15. return {
  16. type: 'object',
  17. required: ['key', 'isEnabled'],
  18. properties: {
  19. key: {type: 'string'},
  20. isEnabled: {type: 'boolean'}
  21. }
  22. }
  23. }
  24. static get jsonAttributes() {
  25. return ['config']
  26. }
  27. static async getEditors() {
  28. return WIKI.models.editors.query()
  29. }
  30. static async refreshEditorsFromDisk() {
  31. let trx
  32. try {
  33. const dbEditors = await WIKI.models.editors.query()
  34. // -> Fetch definitions from disk
  35. const editorDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/editor'))
  36. let diskEditors = []
  37. for (let dir of editorDirs) {
  38. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/editor', dir, 'definition.yml'), 'utf8')
  39. diskEditors.push(yaml.safeLoad(def))
  40. }
  41. WIKI.data.editors = diskEditors.map(editor => ({
  42. ...editor,
  43. props: commonHelper.parseModuleProps(editor.props)
  44. }))
  45. // -> Insert new editors
  46. let newEditors = []
  47. for (let editor of WIKI.data.editors) {
  48. if (!_.some(dbEditors, ['key', editor.key])) {
  49. newEditors.push({
  50. key: editor.key,
  51. isEnabled: false,
  52. config: _.transform(editor.props, (result, value, key) => {
  53. _.set(result, key, value.default)
  54. return result
  55. }, {})
  56. })
  57. } else {
  58. const editorConfig = _.get(_.find(dbEditors, ['key', editor.key]), 'config', {})
  59. await WIKI.models.editors.query().patch({
  60. config: _.transform(editor.props, (result, value, key) => {
  61. if (!_.has(result, key)) {
  62. _.set(result, key, value.default)
  63. }
  64. return result
  65. }, editorConfig)
  66. }).where('key', editor.key)
  67. }
  68. }
  69. if (newEditors.length > 0) {
  70. trx = await WIKI.models.Objection.transaction.start(WIKI.models.knex)
  71. for (let editor of newEditors) {
  72. await WIKI.models.editors.query(trx).insert(editor)
  73. }
  74. await trx.commit()
  75. WIKI.logger.info(`Loaded ${newEditors.length} new editors: [ OK ]`)
  76. } else {
  77. WIKI.logger.info(`No new editors found: [ SKIPPED ]`)
  78. }
  79. } catch (err) {
  80. WIKI.logger.error(`Failed to scan or load new editors: [ FAILED ]`)
  81. WIKI.logger.error(err)
  82. if (trx) {
  83. trx.rollback()
  84. }
  85. }
  86. }
  87. static async getDefaultEditor(contentType) {
  88. // TODO - hardcoded for now
  89. switch (contentType) {
  90. case 'markdown':
  91. return 'markdown'
  92. case 'html':
  93. return 'ckeditor'
  94. default:
  95. return 'code'
  96. }
  97. }
  98. }