tiptap/packages/core/src/ExtensionManager.ts

123 lines
3.0 KiB
TypeScript
Raw Normal View History

2020-03-06 04:35:30 +08:00
import collect from 'collect.js'
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-04-02 20:34:07 +08:00
import { Editor, CommandSpec } from './Editor'
2020-03-08 06:37:58 +08:00
import Extension from './Extension'
import Node from './Node'
2020-04-26 04:40:38 +08:00
import capitalize from './utils/capitalize'
2020-03-06 04:05:01 +08:00
export default class ExtensionManager {
2020-04-01 04:17:54 +08:00
editor: Editor
2020-03-08 06:37:58 +08:00
extensions: (Extension | Node)[]
2020-03-06 04:05:01 +08:00
2020-03-08 06:37:58 +08:00
constructor(extensions: (Extension | Node)[], 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-03-06 05:40:02 +08:00
this.extensions.forEach(extension => {
extension.bindEditor(editor)
2020-03-30 18:40:25 +08:00
editor.on('schemaCreated', () => {
2020-04-02 20:34:07 +08:00
this.registerCommands(extension.commands())
2020-03-30 18:40:25 +08:00
extension.created()
})
2020-03-06 05:40:02 +08:00
})
2020-03-06 04:05:01 +08:00
}
2020-04-02 20:34:07 +08:00
registerCommands(commands: CommandSpec) {
Object
.entries(commands)
.forEach(([name, command]) => {
this.editor.registerCommand(name, command)
})
}
2020-03-06 07:15:36 +08:00
get topNode() {
2020-03-08 06:37:58 +08:00
const topNode = collect(this.extensions).firstWhere('topNode', true)
2020-03-06 07:15:36 +08:00
if (topNode) {
return topNode.name
}
}
2020-03-06 04:35:30 +08:00
get nodes(): any {
return collect(this.extensions)
2020-04-11 04:07:27 +08:00
.where('extensionType', 'node')
2020-03-29 06:54:49 +08:00
.mapWithKeys((extension: any) => [extension.name, extension.schema()])
2020-03-06 04:35:30 +08:00
.all()
2020-03-06 04:05:01 +08:00
}
2020-03-06 04:35:30 +08:00
get marks(): any {
return collect(this.extensions)
2020-04-11 04:07:27 +08:00
.where('extensionType', 'mark')
2020-03-29 06:54:49 +08:00
.mapWithKeys((extension: any) => [extension.name, extension.schema()])
2020-03-06 04:35:30 +08:00
.all()
2020-03-06 04:05:01 +08:00
}
2020-03-06 07:02:48 +08:00
get plugins(): any {
2020-04-02 15:42:26 +08:00
const plugins = collect(this.extensions)
2020-03-29 06:58:48 +08:00
.flatMap(extension => extension.plugins())
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)
.flatMap(extension => extension.inputRules())
.toArray()
}
get pasteRules(): any {
return collect(this.extensions)
.flatMap(extension => extension.pasteRules())
.toArray()
}
2020-04-01 04:17:54 +08:00
get keymaps() {
return collect(this.extensions)
2020-04-13 18:27:29 +08:00
.map(extension => extension.keys())
2020-04-01 04:17:54 +08:00
.filter(keys => !!Object.keys(keys).length)
.map(keys => keymap(keys))
.toArray()
}
2020-04-24 15:32:37 +08:00
get nodeViews() {
2020-04-26 04:40:38 +08:00
const { renderer: Renderer } = this.editor.options
if (!Renderer || !Renderer.type) {
return {}
}
const prop = `to${capitalize(Renderer.type)}`
2020-04-27 05:22:10 +08:00
return collect(this.extensions)
.where('extensionType', 'node')
.filter((extension: any) => extension.schema()[prop])
.map((extension: any) => {
2020-04-26 04:40:38 +08:00
return (
node: ProsemirrorNode,
view: EditorView,
getPos: (() => number) | boolean,
decorations: Decoration[],
) => {
2020-04-27 05:22:10 +08:00
return new Renderer(extension.schema()[prop], {
extension,
editor: this.editor,
2020-04-24 15:32:37 +08:00
node,
getPos,
decorations,
})
}
})
.all()
}
2020-03-06 04:05:01 +08:00
}