tiptap/packages/core/src/ExtensionManager.ts

153 lines
4.1 KiB
TypeScript
Raw Normal View History

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'
import { Extensions, NodeViewRenderer } from './types'
2020-11-30 16:42:53 +08:00
import getSchema from './helpers/getSchema'
import getSchemaTypeByName from './helpers/getSchemaTypeByName'
import splitExtensions from './helpers/splitExtensions'
import getAttributesFromExtensions from './helpers/getAttributesFromExtensions'
import getRenderedAttributes from './helpers/getRenderedAttributes'
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,
2020-11-16 16:43:17 +08:00
type: getSchemaTypeByName(extension.config.name, this.schema),
2020-10-23 05:21:52 +08:00
}
2020-11-16 16:43:17 +08:00
const commands = extension.config.addCommands.bind(context)()
2020-10-23 05:21:52 +08:00
editor.registerCommands(commands)
2020-11-30 20:50:06 +08:00
if (typeof extension.config.onDestroy === 'function') {
this.editor.on('destroy', extension.config.onDestroy.bind(context))
}
2020-10-23 05:21:52 +08:00
})
}
2020-04-02 20:34:07 +08:00
get plugins() {
2020-10-23 05:21:52 +08:00
const plugins = this.extensions
.map(extension => {
const context = {
options: extension.options,
editor: this.editor,
2020-11-16 16:43:17 +08:00
type: getSchemaTypeByName(extension.config.name, this.schema),
2020-10-23 05:21:52 +08:00
}
2020-11-16 16:43:17 +08:00
return extension.config.addProseMirrorPlugins.bind(context)()
2020-10-23 05:21:52 +08:00
})
.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
}
get inputRules() {
if (!this.editor.options.enableInputRules) {
return []
}
2020-10-23 05:21:52 +08:00
return this.extensions
.map(extension => {
const context = {
options: extension.options,
editor: this.editor,
2020-11-16 16:43:17 +08:00
type: getSchemaTypeByName(extension.config.name, this.schema),
2020-10-23 05:21:52 +08:00
}
2020-11-16 16:43:17 +08:00
return extension.config.addInputRules.bind(context)()
2020-10-23 05:21:52 +08:00
})
.flat()
2020-04-02 14:53:59 +08:00
}
get pasteRules() {
if (!this.editor.options.enablePasteRules) {
return []
}
2020-10-23 05:21:52 +08:00
return this.extensions
.map(extension => {
const context = {
options: extension.options,
editor: this.editor,
2020-11-16 16:43:17 +08:00
type: getSchemaTypeByName(extension.config.name, this.schema),
2020-10-23 05:21:52 +08:00
}
2020-11-16 16:43:17 +08:00
return extension.config.addPasteRules.bind(context)()
2020-10-23 05:21:52 +08:00
})
.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-11-16 16:43:17 +08:00
type: getSchemaTypeByName(extension.config.name, this.schema),
2020-10-22 17:14:44 +08:00
}
2020-11-16 16:43:17 +08:00
return keymap(extension.config.addKeyboardShortcuts.bind(context)())
2020-10-22 17:14:44 +08:00
})
2020-04-01 04:17:54 +08:00
}
2020-04-24 15:32:37 +08:00
get nodeViews() {
2020-10-30 21:55:48 +08:00
const { editor } = this
2020-10-29 16:26:24 +08:00
const { nodeExtensions } = splitExtensions(this.extensions)
2020-10-30 21:55:48 +08:00
const allAttributes = getAttributesFromExtensions(this.extensions)
2020-10-29 16:26:24 +08:00
return Object.fromEntries(nodeExtensions
2020-11-16 16:43:17 +08:00
.filter(extension => !!extension.config.addNodeView)
2020-10-29 16:26:24 +08:00
.map(extension => {
2020-11-16 16:43:17 +08:00
const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.config.name)
2020-10-29 16:26:24 +08:00
const context = {
options: extension.options,
2020-10-30 21:55:48 +08:00
editor,
2020-11-16 16:43:17 +08:00
type: getSchemaTypeByName(extension.config.name, this.schema),
2020-10-29 16:26:24 +08:00
}
2020-10-31 00:43:59 +08:00
// @ts-ignore
2020-11-16 16:43:17 +08:00
const renderer = extension.config.addNodeView?.bind(context)?.() as NodeViewRenderer
2020-10-29 16:26:24 +08:00
const nodeview = (
node: ProsemirrorNode,
view: EditorView,
getPos: (() => number) | boolean,
decorations: Decoration[],
2020-10-30 21:55:48 +08:00
) => {
2020-11-13 23:07:20 +08:00
const HTMLAttributes = getRenderedAttributes(node, extensionAttributes)
2020-10-30 21:55:48 +08:00
return renderer({
editor,
node,
getPos,
decorations,
2020-11-13 23:07:20 +08:00
HTMLAttributes,
2020-11-19 05:50:07 +08:00
extension,
2020-10-30 21:55:48 +08:00
})
}
2020-10-29 16:26:24 +08:00
2020-11-16 16:43:17 +08:00
return [extension.config.name, nodeview]
2020-10-29 16:26:24 +08:00
}))
2020-04-24 15:32:37 +08:00
}
2020-03-06 04:05:01 +08:00
}