tiptap/packages/core/src/ExtensionManager.ts

149 lines
3.9 KiB
TypeScript
Raw Normal View History

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'
2020-09-24 15:35:18 +08:00
import { Schema } from 'prosemirror-model'
// import { Schema, Node as ProsemirrorNode } from 'prosemirror-model'
2020-04-02 15:42:26 +08:00
import { inputRules } from 'prosemirror-inputrules'
2020-09-24 15:35:18 +08:00
// import { EditorView, Decoration } from 'prosemirror-view'
2020-09-24 06:29:05 +08:00
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'
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-09-10 06:09:05 +08:00
import getSchema from './utils/getSchema'
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-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-10 06:09:05 +08:00
}
2020-09-09 17:23:24 +08:00
2020-09-10 06:09:05 +08:00
resolveConfigs() {
2020-03-06 05:40:02 +08:00
this.extensions.forEach(extension => {
2020-09-10 06:09:05 +08:00
const { editor } = this
2020-09-24 06:29:05 +08:00
const { name } = extension.config
2020-09-25 04:47:32 +08:00
const options = {
...extension.config.defaults,
...extension.options,
}
2020-09-10 06:09:05 +08:00
const type = extension.type === 'node'
? editor.schema.nodes[name]
: editor.schema.marks[name]
2020-09-24 06:29:05 +08:00
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 06:09:05 +08:00
if (extension.config.commands) {
editor.registerCommands(extension.config.commands)
}
2020-03-06 05:40:02 +08:00
})
2020-03-06 04:05:01 +08:00
}
2020-04-02 20:34:07 +08:00
2020-09-10 06:09:05 +08:00
get schema(): Schema {
return getSchema(this.extensions)
}
get topNode(): any {
2020-09-10 03:12:25 +08:00
return getTopNodeFromExtensions(this.extensions)
2020-03-06 07:15:36 +08:00
}
2020-03-06 04:35:30 +08:00
get nodes(): any {
2020-09-10 03:12:25 +08:00
return getNodesFromExtensions(this.extensions)
2020-03-06 04:05:01 +08:00
}
2020-09-24 06:29:05 +08:00
2020-03-06 04:35:30 +08:00
get marks(): any {
2020-09-10 03:12:25 +08:00
return getMarksFromExtensions(this.extensions)
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-09-23 02:23:54 +08:00
.map(keys => keymap(keys))
2020-04-01 04:17:54 +08:00
.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
}