tiptap/packages/core/src/ExtensionManager.ts

160 lines
4.6 KiB
TypeScript
Raw Normal View History

2020-09-09 16:58:10 +08:00
import deepmerge from 'deepmerge'
2020-03-06 04:35:30 +08:00
import collect from 'collect.js'
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'
import { inputRules } from 'prosemirror-inputrules'
2020-04-26 04:40:38 +08:00
import { EditorView, Decoration } from 'prosemirror-view'
import { Node as ProsemirrorNode } from 'prosemirror-model'
2020-08-22 06:12:34 +08:00
import { Editor } from './Editor'
2020-03-08 06:37:58 +08:00
import Extension from './Extension'
import Node from './Node'
2020-08-22 06:05:00 +08:00
import Mark from './Mark'
2020-04-26 04:40:38 +08:00
import capitalize from './utils/capitalize'
import { Extensions } from './types'
2020-09-04 18:11:18 +08:00
import getTopNodeFromExtensions from './utils/getTopNodeFromExtensions'
import getNodesFromExtensions from './utils/getNodesFromExtensions'
import getMarksFromExtensions from './utils/getMarksFromExtensions'
2020-09-10 03:06:57 +08:00
import resolveExtensionConfig from './utils/resolveExtensionConfig'
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-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-09-09 17:23:24 +08:00
2020-03-06 05:40:02 +08:00
this.extensions.forEach(extension => {
2020-09-10 03:06:57 +08:00
resolveExtensionConfig(extension, 'name')
resolveExtensionConfig(extension, 'defaults')
resolveExtensionConfig(extension, 'topNode')
const name = extension.config.name
const options = deepmerge(extension.config.defaults, extension.options)
resolveExtensionConfig(extension, 'schema', { name, options })
2020-09-09 16:58:10 +08:00
2020-03-30 18:40:25 +08:00
editor.on('schemaCreated', () => {
2020-09-10 03:06:57 +08:00
const type = extension.type === 'node'
? this.editor.schema.nodes[extension.config.name]
: this.editor.schema.marks[extension.config.name]
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 })
2020-09-10 00:07:17 +08:00
2020-09-09 16:58:10 +08:00
if (extension.config.commands) {
this.editor.registerCommands(extension.config.commands)
}
2020-03-30 18:40:25 +08:00
})
2020-03-06 05:40:02 +08:00
})
2020-03-06 04:05:01 +08:00
}
2020-04-02 20:34:07 +08:00
// get topNode() {
// return getTopNodeFromExtensions(this.extensions)
// }
// get nodes(): any {
// return getNodesFromExtensions(this.extensions)
// }
// get marks(): any {
// return getMarksFromExtensions(this.extensions)
// }
get topNode(): any {
2020-09-09 16:58:10 +08:00
const topNode = collect(this.extensions).firstWhere('config.topNode', true)
2020-03-06 07:15:36 +08:00
if (topNode) {
2020-09-09 16:58:10 +08:00
return topNode.config.name
2020-03-06 07:15:36 +08:00
}
}
2020-03-06 04:35:30 +08:00
get nodes(): any {
2020-09-09 16:58:10 +08:00
return collect(this.extensions)
.where('type', 'node')
.mapWithKeys((extension: Node) => [extension.config.name, extension.config.schema])
.all()
2020-03-06 04:05:01 +08:00
}
2020-03-06 04:35:30 +08:00
get marks(): any {
2020-09-09 16:58:10 +08:00
return collect(this.extensions)
.where('type', 'mark')
.mapWithKeys((extension: Mark) => [extension.config.name, extension.config.schema])
.all()
2020-03-06 04:05:01 +08:00
}
2020-08-22 06:12:34 +08:00
get plugins(): Plugin[] {
2020-04-02 15:42:26 +08:00
const plugins = collect(this.extensions)
2020-09-09 16:58:10 +08:00
.flatMap(extension => extension.config.plugins)
.filter(plugin => plugin)
2020-03-06 05:15:17 +08:00
.toArray()
2020-04-02 15:42:26 +08:00
return [
...plugins,
...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 {
return collect(this.extensions)
2020-09-09 16:58:10 +08:00
.flatMap(extension => extension.config.inputRules)
.filter(plugin => plugin)
2020-04-02 14:53:59 +08:00
.toArray()
}
get pasteRules(): any {
return collect(this.extensions)
2020-09-09 16:58:10 +08:00
.flatMap(extension => extension.config.pasteRules)
.filter(plugin => plugin)
2020-04-02 14:53:59 +08:00
.toArray()
}
2020-04-01 04:17:54 +08:00
get keymaps() {
return collect(this.extensions)
2020-09-09 16:58:10 +08:00
.map(extension => extension.config.keys)
.filter(keys => keys)
2020-04-01 04:17:54 +08:00
.map(keys => keymap(keys))
.toArray()
}
2020-04-24 15:32:37 +08:00
get nodeViews() {
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
}