resolve all configs

This commit is contained in:
Philipp Kühn 2020-09-09 18:07:17 +02:00
parent 9a1f99ba4c
commit 2ff134274e
5 changed files with 140 additions and 127 deletions

View File

@ -23,56 +23,18 @@ export default class ExtensionManager {
this.extensions = extensions
this.extensions.forEach(extension => {
const simpleConfigs = ['name', 'defaults']
Object
.entries(extension.configs)
.sort(([name]) => simpleConfigs.includes(name) ? -1 : 1)
.forEach(([name, configs]) => {
extension.config[name] = configs.reduce((accumulator, { stategy, value: rawValue }) => {
const isSimpleConfig = simpleConfigs.includes(name)
const props = isSimpleConfig
? undefined
: {
editor,
options: deepmerge(extension.config.defaults, extension.options),
// TODO: type is not available here
// get type() {
// console.log('called', editor.schema)
// if (!editor.schema) {
// return
// }
// if (extension.type === 'node') {
// return editor.schema.nodes[extension.config.name]
// }
// return editor.schema.marks[extension.config.name]
// },
name: 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)
})
this.resolveConfig(extension, 'name')
this.resolveConfig(extension, 'defaults')
this.resolveConfig(extension, 'topNode')
this.resolveConfig(extension, 'schema', ['name', 'options'])
editor.on('schemaCreated', () => {
this.resolveConfig(extension, 'commands', ['name', 'options', 'editor', 'type'])
this.resolveConfig(extension, 'inputRules', ['name', 'options', 'editor', 'type'])
this.resolveConfig(extension, 'pasteRules', ['name', 'options', 'editor', 'type'])
this.resolveConfig(extension, 'keys', ['name', 'options', 'editor', 'type'])
this.resolveConfig(extension, 'plugins', ['name', 'options', 'editor', 'type'])
if (extension.config.commands) {
this.editor.registerCommands(extension.config.commands)
}
@ -80,6 +42,57 @@ 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() {
const topNode = collect(this.extensions).firstWhere('config.topNode', true)

View File

@ -50,19 +50,19 @@ export default new Mark()
return markInputRule(regex, type)
})
})
// .pasteRules(({ type }) => {
// return ['**', '__'].map(character => {
// const regex = VerEx()
// .add('(?:^|\\s)')
// .beginCapture()
// .find(character)
// .beginCapture()
// .somethingBut(character)
// .endCapture()
// .find(character)
// .endCapture()
.pasteRules(({ type }) => {
return ['**', '__'].map(character => {
const regex = VerEx()
.add('(?:^|\\s)')
.beginCapture()
.find(character)
.beginCapture()
.somethingBut(character)
.endCapture()
.find(character)
.endCapture()
// return markPasteRule(regex, type)
// })
// })
return markPasteRule(regex, type)
})
})
.create()

View File

@ -25,31 +25,31 @@ export default new Mark()
.keys(({ editor }) => ({
'Mod-`': () => editor.code()
}))
// .inputRules(({ type }) => {
// const regex = VerEx()
// .add('(?:^|\\s)')
// .beginCapture()
// .find('`')
// .beginCapture()
// .somethingBut('`')
// .endCapture()
// .find('`')
// .endCapture()
// .endOfLine()
.inputRules(({ type }) => {
const regex = VerEx()
.add('(?:^|\\s)')
.beginCapture()
.find('`')
.beginCapture()
.somethingBut('`')
.endCapture()
.find('`')
.endCapture()
.endOfLine()
// return [markInputRule(regex, type)]
// })
// .pasteRules(({ type }) => {
// const regex = VerEx()
// .add('(?:^|\\s)')
// .beginCapture()
// .find('`')
// .beginCapture()
// .somethingBut('`')
// .endCapture()
// .find('`')
// .endCapture()
return [markInputRule(regex, type)]
})
.pasteRules(({ type }) => {
const regex = VerEx()
.add('(?:^|\\s)')
.beginCapture()
.find('`')
.beginCapture()
.somethingBut('`')
.endCapture()
.find('`')
.endCapture()
// return [markPasteRule(regex, type)]
// })
return [markPasteRule(regex, type)]
})
.create()

View File

@ -43,16 +43,16 @@ export default new Node<HeadingOptions>()
next()
},
}))
// .inputRules(({ options, type }) => {
// return options.levels.map((level: Level) => {
// const regex = VerEx()
// .startOfLine()
// .find('#')
// .repeatPrevious(level)
// .whitespace()
// .endOfLine()
.inputRules(({ options, type }) => {
return options.levels.map((level: Level) => {
const regex = VerEx()
.startOfLine()
.find('#')
.repeatPrevious(level)
.whitespace()
.endOfLine()
// return textblockTypeInputRule(regex, type, { level })
// })
// })
return textblockTypeInputRule(regex, type, { level })
})
})
.create()

View File

@ -26,35 +26,35 @@ export default new Mark()
.keys(({ editor }) => ({
'Mod-i': () => editor.italic()
}))
// .inputRules(({ type }) => {
// return ['*', '_'].map(character => {
// const regex = VerEx()
// .add('(?:^|\\s)')
// .beginCapture()
// .find(character)
// .beginCapture()
// .somethingBut(character)
// .endCapture()
// .find(character)
// .endCapture()
// .endOfLine()
.inputRules(({ type }) => {
return ['*', '_'].map(character => {
const regex = VerEx()
.add('(?:^|\\s)')
.beginCapture()
.find(character)
.beginCapture()
.somethingBut(character)
.endCapture()
.find(character)
.endCapture()
.endOfLine()
// return markInputRule(regex, type)
// })
// })
// .pasteRules(({ type }) => {
// return ['*', '_'].map(character => {
// const regex = VerEx()
// .add('(?:^|\\s)')
// .beginCapture()
// .find(character)
// .beginCapture()
// .somethingBut(character)
// .endCapture()
// .find(character)
// .endCapture()
return markInputRule(regex, type)
})
})
.pasteRules(({ type }) => {
return ['*', '_'].map(character => {
const regex = VerEx()
.add('(?:^|\\s)')
.beginCapture()
.find(character)
.beginCapture()
.somethingBut(character)
.endCapture()
.find(character)
.endCapture()
// return markPasteRule(regex, type)
// })
// })
return markPasteRule(regex, type)
})
})
.create()