fix extension manager
This commit is contained in:
@@ -12,6 +12,9 @@ import removeElement from './utils/removeElement'
|
||||
import getSchemaTypeByName from './utils/getSchemaTypeByName'
|
||||
import ExtensionManager from './ExtensionManager'
|
||||
import EventEmitter from './EventEmitter'
|
||||
import Extension from './Extension'
|
||||
import Node from './Node'
|
||||
import Mark from './Mark'
|
||||
import ComponentRenderer from './ComponentRenderer'
|
||||
import defaultPlugins from './plugins'
|
||||
import * as commands from './commands'
|
||||
@@ -27,7 +30,7 @@ type EditorContent = string | JSON | null
|
||||
interface EditorOptions {
|
||||
element: Element,
|
||||
content: EditorContent,
|
||||
extensions: Function[],
|
||||
extensions: (Extension | Node | Mark)[],
|
||||
injectCSS: boolean,
|
||||
autoFocus: 'start' | 'end' | number | boolean | null,
|
||||
editable: boolean,
|
||||
|
||||
@@ -78,9 +78,15 @@ export interface ExtensionExtends<Callback = ExtensionCallback> {
|
||||
plugins: (params: Callback) => Plugin[]
|
||||
}
|
||||
|
||||
export default class Extension<Options, Extends extends ExtensionExtends = ExtensionExtends> {
|
||||
export default class Extension<Options = {}, Extends extends ExtensionExtends = ExtensionExtends> {
|
||||
type = 'extension'
|
||||
configs: any = {}
|
||||
config: any = {}
|
||||
configs: {
|
||||
[key: string]: {
|
||||
stategy: 'extend' | 'overwrite'
|
||||
value: any
|
||||
}[]
|
||||
} = {}
|
||||
usedOptions: Partial<Options> = {}
|
||||
|
||||
protected storeConfig(key: string, value: any, stategy: 'extend' | 'overwrite') {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import deepmerge from 'deepmerge'
|
||||
import collect from 'collect.js'
|
||||
import { Plugin } from 'prosemirror-state'
|
||||
import { keymap } from 'prosemirror-keymap'
|
||||
@@ -10,8 +11,7 @@ import Node from './Node'
|
||||
import Mark from './Mark'
|
||||
import capitalize from './utils/capitalize'
|
||||
|
||||
// type Extensions = (Extension | Node | Mark)[]
|
||||
type Extensions = Function[]
|
||||
type Extensions = (Extension | Node | Mark)[]
|
||||
|
||||
export default class ExtensionManager {
|
||||
|
||||
@@ -22,77 +22,106 @@ export default class ExtensionManager {
|
||||
this.editor = editor
|
||||
this.extensions = extensions
|
||||
this.extensions.forEach(extension => {
|
||||
console.log({extension})
|
||||
// extension.bindEditor(editor)
|
||||
// editor.on('schemaCreated', () => {
|
||||
// this.editor.registerCommands(extension.commands())
|
||||
// extension.created()
|
||||
// })
|
||||
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.usedOptions),
|
||||
type: {},
|
||||
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)
|
||||
})
|
||||
|
||||
editor.on('schemaCreated', () => {
|
||||
if (extension.config.commands) {
|
||||
this.editor.registerCommands(extension.config.commands)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
get topNode() {
|
||||
const topNode = collect(this.extensions).firstWhere('topNode', true)
|
||||
const topNode = collect(this.extensions).firstWhere('config.topNode', true)
|
||||
|
||||
if (topNode) {
|
||||
return topNode.name
|
||||
return topNode.config.name
|
||||
}
|
||||
}
|
||||
|
||||
get nodes(): any {
|
||||
// return collect(this.extensions)
|
||||
// .where('extensionType', 'node')
|
||||
// .mapWithKeys((extension: Node) => [extension.name, extension.schema()])
|
||||
// .all()
|
||||
return []
|
||||
return collect(this.extensions)
|
||||
.where('type', 'node')
|
||||
.mapWithKeys((extension: Node) => [extension.config.name, extension.config.schema])
|
||||
.all()
|
||||
}
|
||||
|
||||
get marks(): any {
|
||||
// return collect(this.extensions)
|
||||
// .where('extensionType', 'mark')
|
||||
// .mapWithKeys((extension: Mark) => [extension.name, extension.schema()])
|
||||
// .all()
|
||||
return []
|
||||
return collect(this.extensions)
|
||||
.where('type', 'mark')
|
||||
.mapWithKeys((extension: Mark) => [extension.config.name, extension.config.schema])
|
||||
.all()
|
||||
}
|
||||
|
||||
get plugins(): Plugin[] {
|
||||
// const plugins = collect(this.extensions)
|
||||
// .flatMap(extension => extension.plugins())
|
||||
// .toArray()
|
||||
const plugins = collect(this.extensions)
|
||||
.flatMap(extension => extension.config.plugins)
|
||||
.filter(plugin => plugin)
|
||||
.toArray()
|
||||
|
||||
// return [
|
||||
// ...plugins,
|
||||
// ...this.keymaps,
|
||||
// ...this.pasteRules,
|
||||
// inputRules({ rules: this.inputRules }),
|
||||
// ]
|
||||
|
||||
return []
|
||||
return [
|
||||
...plugins,
|
||||
...this.keymaps,
|
||||
...this.pasteRules,
|
||||
inputRules({ rules: this.inputRules }),
|
||||
]
|
||||
}
|
||||
|
||||
get inputRules(): any {
|
||||
// return collect(this.extensions)
|
||||
// .flatMap(extension => extension.inputRules())
|
||||
// .toArray()
|
||||
|
||||
return {}
|
||||
return collect(this.extensions)
|
||||
.flatMap(extension => extension.config.inputRules)
|
||||
.filter(plugin => plugin)
|
||||
.toArray()
|
||||
}
|
||||
|
||||
get pasteRules(): any {
|
||||
// return collect(this.extensions)
|
||||
// .flatMap(extension => extension.pasteRules())
|
||||
// .toArray()
|
||||
return {}
|
||||
return collect(this.extensions)
|
||||
.flatMap(extension => extension.config.pasteRules)
|
||||
.filter(plugin => plugin)
|
||||
.toArray()
|
||||
}
|
||||
|
||||
get keymaps() {
|
||||
// return collect(this.extensions)
|
||||
// .map(extension => extension.keys())
|
||||
// .filter(keys => !!Object.keys(keys).length)
|
||||
// // @ts-ignore
|
||||
// .map(keys => keymap(keys))
|
||||
// .toArray()
|
||||
return []
|
||||
return collect(this.extensions)
|
||||
.map(extension => extension.config.keys)
|
||||
.filter(keys => keys)
|
||||
.map(keys => keymap(keys))
|
||||
.toArray()
|
||||
}
|
||||
|
||||
get nodeViews() {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user