Merge branch 'main' into feature/generate-html-from-json-document
# Conflicts: # packages/core/src/ExtensionManager.ts
This commit is contained in:
@@ -14,6 +14,7 @@ import { Extensions } from './types'
|
|||||||
import getTopNodeFromExtensions from './utils/getTopNodeFromExtensions'
|
import getTopNodeFromExtensions from './utils/getTopNodeFromExtensions'
|
||||||
import getNodesFromExtensions from './utils/getNodesFromExtensions'
|
import getNodesFromExtensions from './utils/getNodesFromExtensions'
|
||||||
import getMarksFromExtensions from './utils/getMarksFromExtensions'
|
import getMarksFromExtensions from './utils/getMarksFromExtensions'
|
||||||
|
import resolveExtensionConfig from './utils/resolveExtensionConfig'
|
||||||
|
|
||||||
export default class ExtensionManager {
|
export default class ExtensionManager {
|
||||||
|
|
||||||
@@ -25,17 +26,25 @@ export default class ExtensionManager {
|
|||||||
this.extensions = extensions
|
this.extensions = extensions
|
||||||
|
|
||||||
this.extensions.forEach(extension => {
|
this.extensions.forEach(extension => {
|
||||||
this.resolveConfig(extension, 'name')
|
resolveExtensionConfig(extension, 'name')
|
||||||
this.resolveConfig(extension, 'defaults')
|
resolveExtensionConfig(extension, 'defaults')
|
||||||
this.resolveConfig(extension, 'topNode')
|
resolveExtensionConfig(extension, 'topNode')
|
||||||
this.resolveConfig(extension, 'schema', ['name', 'options'])
|
|
||||||
|
const name = extension.config.name
|
||||||
|
const options = deepmerge(extension.config.defaults, extension.options)
|
||||||
|
|
||||||
|
resolveExtensionConfig(extension, 'schema', { name, options })
|
||||||
|
|
||||||
editor.on('schemaCreated', () => {
|
editor.on('schemaCreated', () => {
|
||||||
this.resolveConfig(extension, 'commands', ['name', 'options', 'editor', 'type'])
|
const type = extension.type === 'node'
|
||||||
this.resolveConfig(extension, 'inputRules', ['name', 'options', 'editor', 'type'])
|
? this.editor.schema.nodes[extension.config.name]
|
||||||
this.resolveConfig(extension, 'pasteRules', ['name', 'options', 'editor', 'type'])
|
: this.editor.schema.marks[extension.config.name]
|
||||||
this.resolveConfig(extension, 'keys', ['name', 'options', 'editor', 'type'])
|
|
||||||
this.resolveConfig(extension, 'plugins', ['name', 'options', 'editor', 'type'])
|
resolveExtensionConfig(extension, 'commands', { name, options, editor, type })
|
||||||
|
resolveExtensionConfig(extension, 'inputRules', { name, options, editor, type })
|
||||||
|
resolveExtensionConfig(extension, 'pasteRules', { name, options, editor, type })
|
||||||
|
resolveExtensionConfig(extension, 'keys', { name, options, editor, type })
|
||||||
|
resolveExtensionConfig(extension, 'plugins', { name, options, editor, type })
|
||||||
|
|
||||||
if (extension.config.commands) {
|
if (extension.config.commands) {
|
||||||
this.editor.registerCommands(extension.config.commands)
|
this.editor.registerCommands(extension.config.commands)
|
||||||
@@ -44,57 +53,6 @@ export default class ExtensionManager {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
resolveConfig(
|
|
||||||
extension: Extension | Node | Mark,
|
|
||||||
name: string,
|
|
||||||
propValues: ('name' | 'options' | 'editor' | 'type')[] = []
|
|
||||||
) {
|
|
||||||
if (!extension.configs[name]) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
extension.config[name] = extension.configs[name]
|
|
||||||
.reduce((accumulator, { stategy, value: rawValue }) => {
|
|
||||||
const props: any = {}
|
|
||||||
|
|
||||||
if (propValues.includes('name')) {
|
|
||||||
props.name = extension.config.name
|
|
||||||
}
|
|
||||||
|
|
||||||
if (propValues.includes('options')) {
|
|
||||||
props.options = deepmerge(extension.config.defaults, extension.options)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (propValues.includes('editor')) {
|
|
||||||
props.editor = this.editor
|
|
||||||
}
|
|
||||||
|
|
||||||
if (propValues.includes('type')) {
|
|
||||||
props.type = extension.type === 'node'
|
|
||||||
? this.editor.schema.nodes[extension.config.name]
|
|
||||||
: this.editor.schema.marks[extension.config.name]
|
|
||||||
}
|
|
||||||
|
|
||||||
const value = typeof rawValue === 'function'
|
|
||||||
? rawValue(props)
|
|
||||||
: rawValue
|
|
||||||
|
|
||||||
if (accumulator === undefined) {
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stategy === 'overwrite') {
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stategy === 'extend') {
|
|
||||||
return deepmerge(accumulator, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
return accumulator
|
|
||||||
}, undefined)
|
|
||||||
}
|
|
||||||
|
|
||||||
// get topNode() {
|
// get topNode() {
|
||||||
// return getTopNodeFromExtensions(this.extensions)
|
// return getTopNodeFromExtensions(this.extensions)
|
||||||
// }
|
// }
|
||||||
|
|||||||
35
packages/core/src/utils/resolveExtensionConfig.ts
Normal file
35
packages/core/src/utils/resolveExtensionConfig.ts
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import deepmerge from 'deepmerge'
|
||||||
|
import Extension from '../Extension'
|
||||||
|
import Node from '../Node'
|
||||||
|
import Mark from '../Mark'
|
||||||
|
|
||||||
|
export default function resolveExtensionConfig(
|
||||||
|
extension: Extension | Node | Mark,
|
||||||
|
name: string,
|
||||||
|
props = {},
|
||||||
|
): void {
|
||||||
|
if (!extension.configs[name]) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
extension.config[name] = extension.configs[name]
|
||||||
|
.reduce((accumulator, { stategy, value: rawValue }) => {
|
||||||
|
const value = typeof rawValue === 'function'
|
||||||
|
? rawValue(props)
|
||||||
|
: rawValue
|
||||||
|
|
||||||
|
if (accumulator === undefined) {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stategy === 'overwrite') {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stategy === 'extend') {
|
||||||
|
return deepmerge(accumulator, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return accumulator
|
||||||
|
}, undefined)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user