tiptap/packages/core/src/ExtensionManager.ts

161 lines
4.2 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'
2020-03-06 04:05:01 +08:00
2020-09-09 16:58:10 +08:00
type Extensions = (Extension | Node | Mark)[]
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-03-06 05:40:02 +08:00
this.extensions.forEach(extension => {
2020-09-09 16:58:10 +08:00
const simpleConfigs = ['name', 'defaults']
Object
.entries(extension.configs)
.sort(([name]) => simpleConfigs.includes(name) ? -1 : 1)
.forEach(([name, configs]) => {
extension.config[name] = configs.reduce((accumulator, { stategy, value: rawValue }) => {
const isSimpleConfig = simpleConfigs.includes(name)
const props = isSimpleConfig
? undefined
: {
editor,
options: deepmerge(extension.config.defaults, extension.usedOptions),
type: {},
name: '',
}
const value = typeof rawValue === 'function'
? rawValue(props)
: rawValue
if (accumulator === undefined) {
return value
}
if (stategy === 'overwrite') {
return value
}
if (stategy === 'extend') {
return deepmerge(accumulator, value)
}
return accumulator
}, undefined)
})
editor.on('schemaCreated', () => {
if (extension.config.commands) {
this.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-03-06 07:15:36 +08:00
get topNode() {
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-09-09 16:58:10 +08:00
const plugins = collect(this.extensions)
.flatMap(extension => extension.config.plugins)
.filter(plugin => plugin)
.toArray()
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 {
2020-09-09 16:58:10 +08:00
return collect(this.extensions)
.flatMap(extension => extension.config.inputRules)
.filter(plugin => plugin)
.toArray()
2020-04-02 14:53:59 +08:00
}
get pasteRules(): any {
2020-09-09 16:58:10 +08:00
return collect(this.extensions)
.flatMap(extension => extension.config.pasteRules)
.filter(plugin => plugin)
.toArray()
2020-04-02 14:53:59 +08:00
}
2020-04-01 04:17:54 +08:00
get keymaps() {
2020-09-09 16:58:10 +08:00
return collect(this.extensions)
.map(extension => extension.config.keys)
.filter(keys => keys)
.map(keys => keymap(keys))
.toArray()
2020-04-01 04:17:54 +08:00
}
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
}