2020-08-22 06:12:34 +08:00
|
|
|
import { Plugin } from 'prosemirror-state'
|
2020-04-02 15:42:26 +08:00
|
|
|
import { keymap } from 'prosemirror-keymap'
|
2020-10-29 16:26:24 +08:00
|
|
|
import { Schema, Node as ProsemirrorNode } from 'prosemirror-model'
|
2020-04-02 15:42:26 +08:00
|
|
|
import { inputRules } from 'prosemirror-inputrules'
|
2020-10-29 16:26:24 +08:00
|
|
|
import { EditorView, Decoration } from 'prosemirror-view'
|
2020-08-22 06:12:34 +08:00
|
|
|
import { Editor } from './Editor'
|
2020-09-24 15:35:18 +08:00
|
|
|
// import capitalize from './utils/capitalize'
|
2020-09-04 18:10:51 +08:00
|
|
|
import { Extensions } from './types'
|
2020-09-10 06:09:05 +08:00
|
|
|
import getSchema from './utils/getSchema'
|
2020-10-23 05:21:52 +08:00
|
|
|
import getSchemaTypeByName from './utils/getSchemaTypeByName'
|
2020-10-29 16:26:24 +08:00
|
|
|
import splitExtensions from './utils/splitExtensions'
|
2020-08-22 06:05:00 +08:00
|
|
|
|
2020-03-06 04:05:01 +08:00
|
|
|
export default class ExtensionManager {
|
|
|
|
|
2020-04-01 04:17:54 +08:00
|
|
|
editor: Editor
|
2020-09-24 06:29:05 +08:00
|
|
|
|
2020-10-23 15:20:26 +08:00
|
|
|
schema: Schema
|
|
|
|
|
2020-08-22 06:05:00 +08:00
|
|
|
extensions: Extensions
|
2020-03-06 04:05:01 +08:00
|
|
|
|
2020-08-22 06:05:00 +08:00
|
|
|
constructor(extensions: Extensions, editor: Editor) {
|
2020-04-01 04:17:54 +08:00
|
|
|
this.editor = editor
|
2020-03-06 04:05:01 +08:00
|
|
|
this.extensions = extensions
|
2020-10-23 15:20:26 +08:00
|
|
|
this.schema = getSchema(this.extensions)
|
2020-09-09 17:23:24 +08:00
|
|
|
|
2020-10-23 05:21:52 +08:00
|
|
|
this.extensions.forEach(extension => {
|
|
|
|
const context = {
|
|
|
|
options: extension.options,
|
|
|
|
editor: this.editor,
|
|
|
|
type: getSchemaTypeByName(extension.name, this.schema),
|
|
|
|
}
|
|
|
|
|
|
|
|
const commands = extension.addCommands.bind(context)()
|
|
|
|
|
|
|
|
editor.registerCommands(commands)
|
|
|
|
})
|
|
|
|
}
|
2020-04-02 20:34:07 +08:00
|
|
|
|
2020-08-22 06:12:34 +08:00
|
|
|
get plugins(): Plugin[] {
|
2020-10-23 05:21:52 +08:00
|
|
|
const plugins = this.extensions
|
|
|
|
.map(extension => {
|
|
|
|
const context = {
|
|
|
|
options: extension.options,
|
|
|
|
editor: this.editor,
|
|
|
|
type: getSchemaTypeByName(extension.name, this.schema),
|
|
|
|
}
|
|
|
|
|
|
|
|
return extension.addProseMirrorPlugins.bind(context)()
|
|
|
|
})
|
|
|
|
.flat()
|
2020-04-02 15:42:26 +08:00
|
|
|
|
|
|
|
return [
|
2020-10-23 05:21:52 +08:00
|
|
|
...plugins,
|
2020-04-02 15:42:26 +08:00
|
|
|
...this.keymaps,
|
|
|
|
...this.pasteRules,
|
|
|
|
inputRules({ rules: this.inputRules }),
|
|
|
|
]
|
2020-03-06 05:15:17 +08:00
|
|
|
}
|
|
|
|
|
2020-04-02 14:53:59 +08:00
|
|
|
get inputRules(): any {
|
2020-10-23 05:21:52 +08:00
|
|
|
return this.extensions
|
|
|
|
.map(extension => {
|
|
|
|
const context = {
|
|
|
|
options: extension.options,
|
|
|
|
editor: this.editor,
|
|
|
|
type: getSchemaTypeByName(extension.name, this.schema),
|
|
|
|
}
|
|
|
|
|
|
|
|
return extension.addInputRules.bind(context)()
|
|
|
|
})
|
|
|
|
.flat()
|
2020-04-02 14:53:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
get pasteRules(): any {
|
2020-10-23 05:21:52 +08:00
|
|
|
return this.extensions
|
|
|
|
.map(extension => {
|
|
|
|
const context = {
|
|
|
|
options: extension.options,
|
|
|
|
editor: this.editor,
|
|
|
|
type: getSchemaTypeByName(extension.name, this.schema),
|
|
|
|
}
|
|
|
|
|
|
|
|
return extension.addPasteRules.bind(context)()
|
|
|
|
})
|
|
|
|
.flat()
|
2020-04-02 14:53:59 +08:00
|
|
|
}
|
|
|
|
|
2020-04-01 04:17:54 +08:00
|
|
|
get keymaps() {
|
2020-10-22 17:14:44 +08:00
|
|
|
return this.extensions.map(extension => {
|
|
|
|
const context = {
|
|
|
|
options: extension.options,
|
|
|
|
editor: this.editor,
|
2020-10-23 05:21:52 +08:00
|
|
|
type: getSchemaTypeByName(extension.name, this.schema),
|
2020-10-22 17:14:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return keymap(extension.addKeyboardShortcuts.bind(context)())
|
|
|
|
})
|
2020-04-01 04:17:54 +08:00
|
|
|
}
|
|
|
|
|
2020-04-24 15:32:37 +08:00
|
|
|
get nodeViews() {
|
2020-10-29 16:26:24 +08:00
|
|
|
const { nodeExtensions } = splitExtensions(this.extensions)
|
|
|
|
|
|
|
|
return Object.fromEntries(nodeExtensions
|
|
|
|
.filter(extension => !!extension.addNodeView)
|
|
|
|
.map(extension => {
|
|
|
|
const context = {
|
|
|
|
options: extension.options,
|
|
|
|
editor: this.editor,
|
|
|
|
type: getSchemaTypeByName(extension.name, this.schema),
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderer = extension.addNodeView?.bind(context)?.()
|
|
|
|
|
|
|
|
const nodeview = (
|
|
|
|
node: ProsemirrorNode,
|
|
|
|
view: EditorView,
|
|
|
|
getPos: (() => number) | boolean,
|
|
|
|
decorations: Decoration[],
|
|
|
|
) => {
|
|
|
|
// @ts-ignore
|
|
|
|
return new renderer({
|
|
|
|
editor: this.editor,
|
|
|
|
view,
|
|
|
|
node,
|
|
|
|
getPos,
|
|
|
|
decorations,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return [extension.name, nodeview]
|
|
|
|
}))
|
|
|
|
|
2020-09-09 05:49:58 +08:00
|
|
|
// const { renderer: Renderer } = this.editor
|
|
|
|
|
|
|
|
// if (!Renderer || !Renderer.type) {
|
|
|
|
// return {}
|
|
|
|
// }
|
|
|
|
|
|
|
|
// const prop = `to${capitalize(Renderer.type)}`
|
|
|
|
|
|
|
|
// return collect(this.extensions)
|
|
|
|
// .where('extensionType', 'node')
|
|
|
|
// .filter((extension: any) => extension.schema()[prop])
|
|
|
|
// .map((extension: any) => {
|
|
|
|
// return (
|
|
|
|
// node: ProsemirrorNode,
|
|
|
|
// view: EditorView,
|
|
|
|
// getPos: (() => number) | boolean,
|
|
|
|
// decorations: Decoration[],
|
|
|
|
// ) => {
|
|
|
|
// return new Renderer(extension.schema()[prop], {
|
|
|
|
// extension,
|
|
|
|
// editor: this.editor,
|
|
|
|
// node,
|
|
|
|
// getPos,
|
|
|
|
// decorations,
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// .all()
|
|
|
|
|
|
|
|
return {}
|
2020-04-24 15:32:37 +08:00
|
|
|
}
|
|
|
|
|
2020-03-06 04:05:01 +08:00
|
|
|
}
|