refactoring
This commit is contained in:
@@ -10,6 +10,7 @@ import Extension from './Extension'
|
|||||||
import Node from './Node'
|
import Node from './Node'
|
||||||
import Mark from './Mark'
|
import Mark from './Mark'
|
||||||
import capitalize from './utils/capitalize'
|
import capitalize from './utils/capitalize'
|
||||||
|
import resolveExtensionConfig from './utils/resolveExtensionConfig'
|
||||||
|
|
||||||
type Extensions = (Extension | Node | Mark)[]
|
type Extensions = (Extension | Node | Mark)[]
|
||||||
|
|
||||||
@@ -23,17 +24,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)
|
||||||
@@ -42,57 +51,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() {
|
||||||
const topNode = collect(this.extensions).firstWhere('config.topNode', true)
|
const topNode = collect(this.extensions).firstWhere('config.topNode', true)
|
||||||
|
|
||||||
|
|||||||
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